diff options
Diffstat (limited to 'quantum/quantum.c')
-rw-r--r-- | quantum/quantum.c | 509 |
1 files changed, 0 insertions, 509 deletions
diff --git a/quantum/quantum.c b/quantum/quantum.c index 16922dd01..f4999456e 100644 --- a/quantum/quantum.c +++ b/quantum/quantum.c | |||
@@ -24,10 +24,6 @@ | |||
24 | # include "outputselect.h" | 24 | # include "outputselect.h" |
25 | #endif | 25 | #endif |
26 | 26 | ||
27 | #ifndef BREATHING_PERIOD | ||
28 | # define BREATHING_PERIOD 6 | ||
29 | #endif | ||
30 | |||
31 | #include "backlight.h" | 27 | #include "backlight.h" |
32 | extern backlight_config_t backlight_config; | 28 | extern backlight_config_t backlight_config; |
33 | 29 | ||
@@ -1019,511 +1015,6 @@ void matrix_scan_quantum() { | |||
1019 | 1015 | ||
1020 | matrix_scan_kb(); | 1016 | matrix_scan_kb(); |
1021 | } | 1017 | } |
1022 | #if defined(BACKLIGHT_ENABLE) && (defined(BACKLIGHT_PIN) || defined(BACKLIGHT_PINS)) | ||
1023 | |||
1024 | // This logic is a bit complex, we support 3 setups: | ||
1025 | // | ||
1026 | // 1. Hardware PWM when backlight is wired to a PWM pin. | ||
1027 | // Depending on this pin, we use a different output compare unit. | ||
1028 | // 2. Software PWM with hardware timers, but the used timer | ||
1029 | // depends on the Audio setup (Audio wins over Backlight). | ||
1030 | // 3. Full software PWM, driven by the matrix scan, if both timers are used by Audio. | ||
1031 | |||
1032 | # if (defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB647__) || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) || defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__)) && (BACKLIGHT_PIN == B5 || BACKLIGHT_PIN == B6 || BACKLIGHT_PIN == B7) | ||
1033 | # define HARDWARE_PWM | ||
1034 | # define ICRx ICR1 | ||
1035 | # define TCCRxA TCCR1A | ||
1036 | # define TCCRxB TCCR1B | ||
1037 | # define TIMERx_OVF_vect TIMER1_OVF_vect | ||
1038 | # define TIMSKx TIMSK1 | ||
1039 | # define TOIEx TOIE1 | ||
1040 | |||
1041 | # if BACKLIGHT_PIN == B5 | ||
1042 | # define COMxx1 COM1A1 | ||
1043 | # define OCRxx OCR1A | ||
1044 | # elif BACKLIGHT_PIN == B6 | ||
1045 | # define COMxx1 COM1B1 | ||
1046 | # define OCRxx OCR1B | ||
1047 | # elif BACKLIGHT_PIN == B7 | ||
1048 | # define COMxx1 COM1C1 | ||
1049 | # define OCRxx OCR1C | ||
1050 | # endif | ||
1051 | # elif (defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB647__) || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) || defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__)) && (BACKLIGHT_PIN == C4 || BACKLIGHT_PIN == C5 || BACKLIGHT_PIN == C6) | ||
1052 | # define HARDWARE_PWM | ||
1053 | # define ICRx ICR3 | ||
1054 | # define TCCRxA TCCR3A | ||
1055 | # define TCCRxB TCCR3B | ||
1056 | # define TIMERx_OVF_vect TIMER3_OVF_vect | ||
1057 | # define TIMSKx TIMSK3 | ||
1058 | # define TOIEx TOIE3 | ||
1059 | |||
1060 | # if BACKLIGHT_PIN == C4 | ||
1061 | # if (defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__)) | ||
1062 | # error This MCU has no C4 pin! | ||
1063 | # else | ||
1064 | # define COMxx1 COM3C1 | ||
1065 | # define OCRxx OCR3C | ||
1066 | # endif | ||
1067 | # elif BACKLIGHT_PIN == C5 | ||
1068 | # if (defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__)) | ||
1069 | # error This MCU has no C5 pin! | ||
1070 | # else | ||
1071 | # define COMxx1 COM3B1 | ||
1072 | # define OCRxx OCR3B | ||
1073 | # endif | ||
1074 | # elif BACKLIGHT_PIN == C6 | ||
1075 | # define COMxx1 COM3A1 | ||
1076 | # define OCRxx OCR3A | ||
1077 | # endif | ||
1078 | # elif (defined(__AVR_ATmega16U2__) || defined(__AVR_ATmega32U2__)) && (BACKLIGHT_PIN == B7 || BACKLIGHT_PIN == C5 || BACKLIGHT_PIN == C6) | ||
1079 | # define HARDWARE_PWM | ||
1080 | # define ICRx ICR1 | ||
1081 | # define TCCRxA TCCR1A | ||
1082 | # define TCCRxB TCCR1B | ||
1083 | # define TIMERx_OVF_vect TIMER1_OVF_vect | ||
1084 | # define TIMSKx TIMSK1 | ||
1085 | # define TOIEx TOIE1 | ||
1086 | |||
1087 | # if BACKLIGHT_PIN == B7 | ||
1088 | # define COMxx1 COM1C1 | ||
1089 | # define OCRxx OCR1C | ||
1090 | # elif BACKLIGHT_PIN == C5 | ||
1091 | # define COMxx1 COM1B1 | ||
1092 | # define OCRxx OCR1B | ||
1093 | # elif BACKLIGHT_PIN == C6 | ||
1094 | # define COMxx1 COM1A1 | ||
1095 | # define OCRxx OCR1A | ||
1096 | # endif | ||
1097 | # elif defined(__AVR_ATmega32A__) && (BACKLIGHT_PIN == D4 || BACKLIGHT_PIN == D5) | ||
1098 | # define HARDWARE_PWM | ||
1099 | # define ICRx ICR1 | ||
1100 | # define TCCRxA TCCR1A | ||
1101 | # define TCCRxB TCCR1B | ||
1102 | # define TIMERx_OVF_vect TIMER1_OVF_vect | ||
1103 | # define TIMSKx TIMSK | ||
1104 | # define TOIEx TOIE1 | ||
1105 | |||
1106 | # if BACKLIGHT_PIN == D4 | ||
1107 | # define COMxx1 COM1B1 | ||
1108 | # define OCRxx OCR1B | ||
1109 | # elif BACKLIGHT_PIN == D5 | ||
1110 | # define COMxx1 COM1A1 | ||
1111 | # define OCRxx OCR1A | ||
1112 | # endif | ||
1113 | # elif defined(__AVR_ATmega328P__) && (BACKLIGHT_PIN == B1 || BACKLIGHT_PIN == B2) | ||
1114 | # define HARDWARE_PWM | ||
1115 | # define ICRx ICR1 | ||
1116 | # define TCCRxA TCCR1A | ||
1117 | # define TCCRxB TCCR1B | ||
1118 | # define TIMERx_OVF_vect TIMER1_OVF_vect | ||
1119 | # define TIMSKx TIMSK1 | ||
1120 | # define TOIEx TOIE1 | ||
1121 | |||
1122 | # if BACKLIGHT_PIN == B1 | ||
1123 | # define COMxx1 COM1A1 | ||
1124 | # define OCRxx OCR1A | ||
1125 | # elif BACKLIGHT_PIN == B2 | ||
1126 | # define COMxx1 COM1B1 | ||
1127 | # define OCRxx OCR1B | ||
1128 | # endif | ||
1129 | # else | ||
1130 | # if !defined(BACKLIGHT_CUSTOM_DRIVER) | ||
1131 | # if !defined(B5_AUDIO) && !defined(B6_AUDIO) && !defined(B7_AUDIO) | ||
1132 | // Timer 1 is not in use by Audio feature, Backlight can use it | ||
1133 | # pragma message "Using hardware timer 1 with software PWM" | ||
1134 | # define HARDWARE_PWM | ||
1135 | # define BACKLIGHT_PWM_TIMER | ||
1136 | # define ICRx ICR1 | ||
1137 | # define TCCRxA TCCR1A | ||
1138 | # define TCCRxB TCCR1B | ||
1139 | # define TIMERx_COMPA_vect TIMER1_COMPA_vect | ||
1140 | # define TIMERx_OVF_vect TIMER1_OVF_vect | ||
1141 | # if defined(__AVR_ATmega32A__) // This MCU has only one TIMSK register | ||
1142 | # define TIMSKx TIMSK | ||
1143 | # else | ||
1144 | # define TIMSKx TIMSK1 | ||
1145 | # endif | ||
1146 | # define TOIEx TOIE1 | ||
1147 | |||
1148 | # define OCIExA OCIE1A | ||
1149 | # define OCRxx OCR1A | ||
1150 | # elif !defined(C6_AUDIO) && !defined(C5_AUDIO) && !defined(C4_AUDIO) | ||
1151 | # pragma message "Using hardware timer 3 with software PWM" | ||
1152 | // Timer 3 is not in use by Audio feature, Backlight can use it | ||
1153 | # define HARDWARE_PWM | ||
1154 | # define BACKLIGHT_PWM_TIMER | ||
1155 | # define ICRx ICR1 | ||
1156 | # define TCCRxA TCCR3A | ||
1157 | # define TCCRxB TCCR3B | ||
1158 | # define TIMERx_COMPA_vect TIMER3_COMPA_vect | ||
1159 | # define TIMERx_OVF_vect TIMER3_OVF_vect | ||
1160 | # define TIMSKx TIMSK3 | ||
1161 | # define TOIEx TOIE3 | ||
1162 | |||
1163 | # define OCIExA OCIE3A | ||
1164 | # define OCRxx OCR3A | ||
1165 | # else | ||
1166 | # pragma message "Audio in use - using pure software PWM" | ||
1167 | # define NO_HARDWARE_PWM | ||
1168 | # endif | ||
1169 | # else | ||
1170 | # pragma message "Custom driver defined - using pure software PWM" | ||
1171 | # define NO_HARDWARE_PWM | ||
1172 | # endif | ||
1173 | # endif | ||
1174 | |||
1175 | # ifndef BACKLIGHT_ON_STATE | ||
1176 | # define BACKLIGHT_ON_STATE 0 | ||
1177 | # endif | ||
1178 | |||
1179 | void backlight_on(uint8_t backlight_pin) { | ||
1180 | # if BACKLIGHT_ON_STATE == 0 | ||
1181 | writePinLow(backlight_pin); | ||
1182 | # else | ||
1183 | writePinHigh(backlight_pin); | ||
1184 | # endif | ||
1185 | } | ||
1186 | |||
1187 | void backlight_off(uint8_t backlight_pin) { | ||
1188 | # if BACKLIGHT_ON_STATE == 0 | ||
1189 | writePinHigh(backlight_pin); | ||
1190 | # else | ||
1191 | writePinLow(backlight_pin); | ||
1192 | # endif | ||
1193 | } | ||
1194 | |||
1195 | # if defined(NO_HARDWARE_PWM) || defined(BACKLIGHT_PWM_TIMER) // pwm through software | ||
1196 | |||
1197 | // we support multiple backlight pins | ||
1198 | # ifndef BACKLIGHT_LED_COUNT | ||
1199 | # define BACKLIGHT_LED_COUNT 1 | ||
1200 | # endif | ||
1201 | |||
1202 | # if BACKLIGHT_LED_COUNT == 1 | ||
1203 | # define BACKLIGHT_PIN_INIT \ | ||
1204 | { BACKLIGHT_PIN } | ||
1205 | # else | ||
1206 | # define BACKLIGHT_PIN_INIT BACKLIGHT_PINS | ||
1207 | # endif | ||
1208 | |||
1209 | # define FOR_EACH_LED(x) \ | ||
1210 | for (uint8_t i = 0; i < BACKLIGHT_LED_COUNT; i++) { \ | ||
1211 | uint8_t backlight_pin = backlight_pins[i]; \ | ||
1212 | { x } \ | ||
1213 | } | ||
1214 | |||
1215 | static const uint8_t backlight_pins[BACKLIGHT_LED_COUNT] = BACKLIGHT_PIN_INIT; | ||
1216 | |||
1217 | # else // full hardware PWM | ||
1218 | |||
1219 | // we support only one backlight pin | ||
1220 | static const uint8_t backlight_pin = BACKLIGHT_PIN; | ||
1221 | # define FOR_EACH_LED(x) x | ||
1222 | |||
1223 | # endif | ||
1224 | |||
1225 | # ifdef NO_HARDWARE_PWM | ||
1226 | __attribute__((weak)) void backlight_init_ports(void) { | ||
1227 | // Setup backlight pin as output and output to on state. | ||
1228 | FOR_EACH_LED(setPinOutput(backlight_pin); backlight_on(backlight_pin);) | ||
1229 | |||
1230 | # ifdef BACKLIGHT_BREATHING | ||
1231 | if (is_backlight_breathing()) { | ||
1232 | breathing_enable(); | ||
1233 | } | ||
1234 | # endif | ||
1235 | } | ||
1236 | |||
1237 | __attribute__((weak)) void backlight_set(uint8_t level) {} | ||
1238 | |||
1239 | uint8_t backlight_tick = 0; | ||
1240 | |||
1241 | # ifndef BACKLIGHT_CUSTOM_DRIVER | ||
1242 | void backlight_task(void) { | ||
1243 | if ((0xFFFF >> ((BACKLIGHT_LEVELS - get_backlight_level()) * ((BACKLIGHT_LEVELS + 1) / 2))) & (1 << backlight_tick)) { | ||
1244 | FOR_EACH_LED(backlight_on(backlight_pin);) | ||
1245 | } else { | ||
1246 | FOR_EACH_LED(backlight_off(backlight_pin);) | ||
1247 | } | ||
1248 | backlight_tick = (backlight_tick + 1) % 16; | ||
1249 | } | ||
1250 | # endif | ||
1251 | |||
1252 | # ifdef BACKLIGHT_BREATHING | ||
1253 | # ifndef BACKLIGHT_CUSTOM_DRIVER | ||
1254 | # error "Backlight breathing only available with hardware PWM. Please disable." | ||
1255 | # endif | ||
1256 | # endif | ||
1257 | |||
1258 | # else // hardware pwm through timer | ||
1259 | |||
1260 | # ifdef BACKLIGHT_PWM_TIMER | ||
1261 | |||
1262 | // The idea of software PWM assisted by hardware timers is the following | ||
1263 | // we use the hardware timer in fast PWM mode like for hardware PWM, but | ||
1264 | // instead of letting the Output Match Comparator control the led pin | ||
1265 | // (which is not possible since the backlight is not wired to PWM pins on the | ||
1266 | // CPU), we do the LED on/off by oursleves. | ||
1267 | // The timer is setup to count up to 0xFFFF, and we set the Output Compare | ||
1268 | // register to the current 16bits backlight level (after CIE correction). | ||
1269 | // This means the CPU will trigger a compare match interrupt when the counter | ||
1270 | // reaches the backlight level, where we turn off the LEDs, | ||
1271 | // but also an overflow interrupt when the counter rolls back to 0, | ||
1272 | // in which we're going to turn on the LEDs. | ||
1273 | // The LED will then be on for OCRxx/0xFFFF time, adjusted every 244Hz. | ||
1274 | |||
1275 | // Triggered when the counter reaches the OCRx value | ||
1276 | ISR(TIMERx_COMPA_vect) { FOR_EACH_LED(backlight_off(backlight_pin);) } | ||
1277 | |||
1278 | // Triggered when the counter reaches the TOP value | ||
1279 | // this one triggers at F_CPU/65536 =~ 244 Hz | ||
1280 | ISR(TIMERx_OVF_vect) { | ||
1281 | # ifdef BACKLIGHT_BREATHING | ||
1282 | if (is_breathing()) { | ||
1283 | breathing_task(); | ||
1284 | } | ||
1285 | # endif | ||
1286 | // for very small values of OCRxx (or backlight level) | ||
1287 | // we can't guarantee this whole code won't execute | ||
1288 | // at the same time as the compare match interrupt | ||
1289 | // which means that we might turn on the leds while | ||
1290 | // trying to turn them off, leading to flickering | ||
1291 | // artifacts (especially while breathing, because breathing_task | ||
1292 | // takes many computation cycles). | ||
1293 | // so better not turn them on while the counter TOP is very low. | ||
1294 | if (OCRxx > 256) { | ||
1295 | FOR_EACH_LED(backlight_on(backlight_pin);) | ||
1296 | } | ||
1297 | } | ||
1298 | |||
1299 | # endif | ||
1300 | |||
1301 | # define TIMER_TOP 0xFFFFU | ||
1302 | |||
1303 | // See http://jared.geek.nz/2013/feb/linear-led-pwm | ||
1304 | static uint16_t cie_lightness(uint16_t v) { | ||
1305 | if (v <= 5243) // if below 8% of max | ||
1306 | return v / 9; // same as dividing by 900% | ||
1307 | else { | ||
1308 | uint32_t y = (((uint32_t)v + 10486) << 8) / (10486 + 0xFFFFUL); // add 16% of max and compare | ||
1309 | // to get a useful result with integer division, we shift left in the expression above | ||
1310 | // and revert what we've done again after squaring. | ||
1311 | y = y * y * y >> 8; | ||
1312 | if (y > 0xFFFFUL) // prevent overflow | ||
1313 | return 0xFFFFU; | ||
1314 | else | ||
1315 | return (uint16_t)y; | ||
1316 | } | ||
1317 | } | ||
1318 | |||
1319 | // range for val is [0..TIMER_TOP]. PWM pin is high while the timer count is below val. | ||
1320 | static inline void set_pwm(uint16_t val) { OCRxx = val; } | ||
1321 | |||
1322 | # ifndef BACKLIGHT_CUSTOM_DRIVER | ||
1323 | __attribute__((weak)) void backlight_set(uint8_t level) { | ||
1324 | if (level > BACKLIGHT_LEVELS) level = BACKLIGHT_LEVELS; | ||
1325 | |||
1326 | if (level == 0) { | ||
1327 | # ifdef BACKLIGHT_PWM_TIMER | ||
1328 | if (OCRxx) { | ||
1329 | TIMSKx &= ~(_BV(OCIExA)); | ||
1330 | TIMSKx &= ~(_BV(TOIEx)); | ||
1331 | FOR_EACH_LED(backlight_off(backlight_pin);) | ||
1332 | } | ||
1333 | # else | ||
1334 | // Turn off PWM control on backlight pin | ||
1335 | TCCRxA &= ~(_BV(COMxx1)); | ||
1336 | # endif | ||
1337 | } else { | ||
1338 | # ifdef BACKLIGHT_PWM_TIMER | ||
1339 | if (!OCRxx) { | ||
1340 | TIMSKx |= _BV(OCIExA); | ||
1341 | TIMSKx |= _BV(TOIEx); | ||
1342 | } | ||
1343 | # else | ||
1344 | // Turn on PWM control of backlight pin | ||
1345 | TCCRxA |= _BV(COMxx1); | ||
1346 | # endif | ||
1347 | } | ||
1348 | // Set the brightness | ||
1349 | set_pwm(cie_lightness(TIMER_TOP * (uint32_t)level / BACKLIGHT_LEVELS)); | ||
1350 | } | ||
1351 | |||
1352 | void backlight_task(void) {} | ||
1353 | # endif // BACKLIGHT_CUSTOM_DRIVER | ||
1354 | |||
1355 | # ifdef BACKLIGHT_BREATHING | ||
1356 | |||
1357 | # define BREATHING_NO_HALT 0 | ||
1358 | # define BREATHING_HALT_OFF 1 | ||
1359 | # define BREATHING_HALT_ON 2 | ||
1360 | # define BREATHING_STEPS 128 | ||
1361 | |||
1362 | static uint8_t breathing_period = BREATHING_PERIOD; | ||
1363 | static uint8_t breathing_halt = BREATHING_NO_HALT; | ||
1364 | static uint16_t breathing_counter = 0; | ||
1365 | |||
1366 | # ifdef BACKLIGHT_PWM_TIMER | ||
1367 | static bool breathing = false; | ||
1368 | |||
1369 | bool is_breathing(void) { return breathing; } | ||
1370 | |||
1371 | # define breathing_interrupt_enable() \ | ||
1372 | do { \ | ||
1373 | breathing = true; \ | ||
1374 | } while (0) | ||
1375 | # define breathing_interrupt_disable() \ | ||
1376 | do { \ | ||
1377 | breathing = false; \ | ||
1378 | } while (0) | ||
1379 | # else | ||
1380 | |||
1381 | bool is_breathing(void) { return !!(TIMSKx & _BV(TOIEx)); } | ||
1382 | |||
1383 | # define breathing_interrupt_enable() \ | ||
1384 | do { \ | ||
1385 | TIMSKx |= _BV(TOIEx); \ | ||
1386 | } while (0) | ||
1387 | # define breathing_interrupt_disable() \ | ||
1388 | do { \ | ||
1389 | TIMSKx &= ~_BV(TOIEx); \ | ||
1390 | } while (0) | ||
1391 | # endif | ||
1392 | |||
1393 | # define breathing_min() \ | ||
1394 | do { \ | ||
1395 | breathing_counter = 0; \ | ||
1396 | } while (0) | ||
1397 | # define breathing_max() \ | ||
1398 | do { \ | ||
1399 | breathing_counter = breathing_period * 244 / 2; \ | ||
1400 | } while (0) | ||
1401 | |||
1402 | void breathing_enable(void) { | ||
1403 | breathing_counter = 0; | ||
1404 | breathing_halt = BREATHING_NO_HALT; | ||
1405 | breathing_interrupt_enable(); | ||
1406 | } | ||
1407 | |||
1408 | void breathing_pulse(void) { | ||
1409 | if (get_backlight_level() == 0) | ||
1410 | breathing_min(); | ||
1411 | else | ||
1412 | breathing_max(); | ||
1413 | breathing_halt = BREATHING_HALT_ON; | ||
1414 | breathing_interrupt_enable(); | ||
1415 | } | ||
1416 | |||
1417 | void breathing_disable(void) { | ||
1418 | breathing_interrupt_disable(); | ||
1419 | // Restore backlight level | ||
1420 | backlight_set(get_backlight_level()); | ||
1421 | } | ||
1422 | |||
1423 | void breathing_self_disable(void) { | ||
1424 | if (get_backlight_level() == 0) | ||
1425 | breathing_halt = BREATHING_HALT_OFF; | ||
1426 | else | ||
1427 | breathing_halt = BREATHING_HALT_ON; | ||
1428 | } | ||
1429 | |||
1430 | void breathing_toggle(void) { | ||
1431 | if (is_breathing()) | ||
1432 | breathing_disable(); | ||
1433 | else | ||
1434 | breathing_enable(); | ||
1435 | } | ||
1436 | |||
1437 | void breathing_period_set(uint8_t value) { | ||
1438 | if (!value) value = 1; | ||
1439 | breathing_period = value; | ||
1440 | } | ||
1441 | |||
1442 | void breathing_period_default(void) { breathing_period_set(BREATHING_PERIOD); } | ||
1443 | |||
1444 | void breathing_period_inc(void) { breathing_period_set(breathing_period + 1); } | ||
1445 | |||
1446 | void breathing_period_dec(void) { breathing_period_set(breathing_period - 1); } | ||
1447 | |||
1448 | /* To generate breathing curve in python: | ||
1449 | * from math import sin, pi; [int(sin(x/128.0*pi)**4*255) for x in range(128)] | ||
1450 | */ | ||
1451 | static const uint8_t breathing_table[BREATHING_STEPS] PROGMEM = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 3, 4, 5, 6, 8, 10, 12, 15, 17, 20, 24, 28, 32, 36, 41, 46, 51, 57, 63, 70, 76, 83, 91, 98, 106, 113, 121, 129, 138, 146, 154, 162, 170, 178, 185, 193, 200, 207, 213, 220, 225, 231, 235, 240, 244, 247, 250, 252, 253, 254, 255, 254, 253, 252, 250, 247, 244, 240, 235, 231, 225, 220, 213, 207, 200, 193, 185, 178, 170, 162, 154, 146, 138, 129, 121, 113, 106, 98, 91, 83, 76, 70, 63, 57, 51, 46, 41, 36, 32, 28, 24, 20, 17, 15, 12, 10, 8, 6, 5, 4, 3, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; | ||
1452 | |||
1453 | // Use this before the cie_lightness function. | ||
1454 | static inline uint16_t scale_backlight(uint16_t v) { return v / BACKLIGHT_LEVELS * get_backlight_level(); } | ||
1455 | |||
1456 | # ifdef BACKLIGHT_PWM_TIMER | ||
1457 | void breathing_task(void) | ||
1458 | # else | ||
1459 | /* Assuming a 16MHz CPU clock and a timer that resets at 64k (ICR1), the following interrupt handler will run | ||
1460 | * about 244 times per second. | ||
1461 | */ | ||
1462 | ISR(TIMERx_OVF_vect) | ||
1463 | # endif | ||
1464 | { | ||
1465 | uint16_t interval = (uint16_t)breathing_period * 244 / BREATHING_STEPS; | ||
1466 | // resetting after one period to prevent ugly reset at overflow. | ||
1467 | breathing_counter = (breathing_counter + 1) % (breathing_period * 244); | ||
1468 | uint8_t index = breathing_counter / interval % BREATHING_STEPS; | ||
1469 | |||
1470 | if (((breathing_halt == BREATHING_HALT_ON) && (index == BREATHING_STEPS / 2)) || ((breathing_halt == BREATHING_HALT_OFF) && (index == BREATHING_STEPS - 1))) { | ||
1471 | breathing_interrupt_disable(); | ||
1472 | } | ||
1473 | |||
1474 | set_pwm(cie_lightness(scale_backlight((uint16_t)pgm_read_byte(&breathing_table[index]) * 0x0101U))); | ||
1475 | } | ||
1476 | |||
1477 | # endif // BACKLIGHT_BREATHING | ||
1478 | |||
1479 | __attribute__((weak)) void backlight_init_ports(void) { | ||
1480 | // Setup backlight pin as output and output to on state. | ||
1481 | FOR_EACH_LED(setPinOutput(backlight_pin); backlight_on(backlight_pin);) | ||
1482 | |||
1483 | // I could write a wall of text here to explain... but TL;DW | ||
1484 | // Go read the ATmega32u4 datasheet. | ||
1485 | // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on | ||
1486 | |||
1487 | # ifdef BACKLIGHT_PWM_TIMER | ||
1488 | // TimerX setup, Fast PWM mode count to TOP set in ICRx | ||
1489 | TCCRxA = _BV(WGM11); // = 0b00000010; | ||
1490 | // clock select clk/1 | ||
1491 | TCCRxB = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001; | ||
1492 | # else // hardware PWM | ||
1493 | // Pin PB7 = OCR1C (Timer 1, Channel C) | ||
1494 | // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0 | ||
1495 | // (i.e. start high, go low when counter matches.) | ||
1496 | // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0 | ||
1497 | // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1 | ||
1498 | |||
1499 | /* | ||
1500 | 14.8.3: | ||
1501 | "In fast PWM mode, the compare units allow generation of PWM waveforms on the OCnx pins. Setting the COMnx1:0 bits to two will produce a non-inverted PWM [..]." | ||
1502 | "In fast PWM mode the counter is incremented until the counter value matches either one of the fixed values 0x00FF, 0x01FF, or 0x03FF (WGMn3:0 = 5, 6, or 7), the value in ICRn (WGMn3:0 = 14), or the value in OCRnA (WGMn3:0 = 15)." | ||
1503 | */ | ||
1504 | TCCRxA = _BV(COMxx1) | _BV(WGM11); // = 0b00001010; | ||
1505 | TCCRxB = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001; | ||
1506 | # endif | ||
1507 | // Use full 16-bit resolution. Counter counts to ICR1 before reset to 0. | ||
1508 | ICRx = TIMER_TOP; | ||
1509 | |||
1510 | backlight_init(); | ||
1511 | # ifdef BACKLIGHT_BREATHING | ||
1512 | if (is_backlight_breathing()) { | ||
1513 | breathing_enable(); | ||
1514 | } | ||
1515 | # endif | ||
1516 | } | ||
1517 | |||
1518 | # endif // hardware backlight | ||
1519 | |||
1520 | #else // no backlight | ||
1521 | |||
1522 | __attribute__((weak)) void backlight_init_ports(void) {} | ||
1523 | |||
1524 | __attribute__((weak)) void backlight_set(uint8_t level) {} | ||
1525 | |||
1526 | #endif // backlight | ||
1527 | 1018 | ||
1528 | #ifdef HD44780_ENABLED | 1019 | #ifdef HD44780_ENABLED |
1529 | # include "hd44780.h" | 1020 | # include "hd44780.h" |