aboutsummaryrefslogtreecommitdiff
path: root/led.c
blob: 0d92563c52d77540222429cedc176c5914800fba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
Copyright 2021 Robin Haberkorn <robin.haberkorn@googlemail.com>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdint.h>

#include <avr/io.h>

#include "debug.h"
#include "keyclick.h"
#include "led.h"
#include "pwm.h"

void led_set(uint8_t usb_led)
{
    dprintf("Set keyboard LEDs: 0x%02X\n", usb_led);

    /*
     * All LEDs and the buzzer are output pins obviously, even for PWM operation
     */
    DDRD |= 0b00001111;
    DDRB |= 0b11110000;

    /* LED right next to the Capslock key (C99) */
    if (usb_led & (1 << USB_LED_CAPS_LOCK))
        PORTD &= ~(1 << PD3);
    else
        PORTD |= (1 << PD3);

    /* 1st LED on first row (G00). */
    pwm_pb5_set_led(usb_led & (1 << USB_LED_NUM_LOCK) ? 255 : 0);

    /* 2nd LED on first row (G01): Highlight keyclick mode. */
    pwm_pd1_set_led(keyclick_mode*255/(KEYCLICK_MAX-1));

    /* 3rd LED on the first row (G02) */
    pwm_pb7_set_led(usb_led & (1 << USB_LED_COMPOSE) ? 255 : 0);

    /*
     * 4th LED (G03) are currently not triggerable via USB.
     * Could be triggered as the "backlight".
     */
    pwm_pb4_set_led(0);

    /* 5th LED on the first row (G04) */
    pwm_pb6_set_led(usb_led & (1 << USB_LED_SCROLL_LOCK) ? 255 : 0);

    /*
     * 6th LED on the first row (G53).
     *
     * Triggering this LED (PD2) will also enable the buzzer (PD0),
     * so we have got a way to beep from userspace (see ./k7637-beep.sh).
     *
     * The original firmware also had the error display on G53
     * (cf. Betriebsdokumentation).
     */
    if (usb_led & (1 << USB_LED_KANA)) {
        PORTD &= ~(1 << PD2);
        pwm_pd0_set_tone(2200);
    } else {
        PORTD |= (1 << PD2);
        pwm_pd0_set_tone(0);
    }
}