backlight.c (1409B)
1 /* 2 MNT Reform 2.0 Keyboard Firmware 3 See keyboard.c for Copyright 4 SPDX-License-Identifier: MIT 5 */ 6 7 #include <avr/io.h> 8 #include "backlight.h" 9 10 #define KBD_MIN_PWMVAL 0 11 #define KBD_MAX_PWMVAL 6 12 #define KBD_PWM_STEP 1 13 int16_t pwmval = KBD_MAX_PWMVAL; 14 15 void kbd_brightness_init(void) { 16 // Set initial brightness 17 OCR0A = pwmval; 18 19 // Set 'Waveform Generation Mode' to: 20 // - Mode 1: 21 // * Phase correct PWM 22 // * TOP = 0xFF 23 // * Update OCRx at TOP 24 // * TOV flag set on BOTTOM (0x00) 25 TCCR0A |= (1 << WGM00); 26 TCCR0A &= ~(1 << WGM01); 27 28 // Set 'Compare Output Mode' to: 29 // - Clear OC0A on Compare Match when up-counting. 30 // - Set OC0A on Compare Match when down-counting. 31 TCCR0A &= ~(1 << COM0A0); 32 TCCR0A |= (1 << COM0A1); 33 34 // Set 'Clock Select' to: 35 // - prescale clk_io / 8 36 TCCR0B &= ~(1 << CS00); 37 TCCR0B |= (1 << CS01); 38 TCCR0B &= ~(1 << CS02); 39 } 40 41 void kbd_brightness_inc(void) { 42 pwmval += KBD_PWM_STEP; 43 if (pwmval >= KBD_MAX_PWMVAL) pwmval = KBD_MAX_PWMVAL; 44 OCR0A = pwmval; 45 } 46 47 void kbd_brightness_dec(void) { 48 pwmval -= KBD_PWM_STEP; 49 if (pwmval < KBD_MIN_PWMVAL) pwmval = KBD_MIN_PWMVAL; 50 OCR0A = pwmval; 51 } 52 53 void kbd_brightness_set(int brite) { 54 pwmval = brite; 55 if (pwmval < KBD_MIN_PWMVAL) pwmval = KBD_MIN_PWMVAL; 56 if (pwmval >= KBD_MAX_PWMVAL) pwmval = KBD_MAX_PWMVAL; 57 OCR0A = pwmval; 58 } 59 60 int16_t kbd_brightness_get(void) { 61 return pwmval; 62 }