summaryrefslogtreecommitdiffstats
path: root/keyboards/jj40/breathing_custom.h
diff options
context:
space:
mode:
authorKenneth Aloysius <krusli@users.noreply.github.com>2018-01-18 22:02:32 +0100
committerJack Humbert <jack.humb@gmail.com>2018-01-18 22:02:32 +0100
commit365b86357825ebed7012377a5684269760326fd2 (patch)
treee54f479c245e1509fa6511114dea037b3999b3a5 /keyboards/jj40/breathing_custom.h
parent5b22ddf5262af8860373ce7a3847f912d5947071 (diff)
downloadqmk_firmware-365b86357825ebed7012377a5684269760326fd2.tar.gz
qmk_firmware-365b86357825ebed7012377a5684269760326fd2.tar.xz
Backlighting for JJ40 and underglow initialisation code (#2260)
* Cleanup Mechmini keymap. Once the custom RGB function is defined, there is no need to manually handle RGB code. * Change default to KEYMAP_MIT, not KEYMAP_OFFSET * Add custom RGB code for JJ40 * Reset Mechmini advertised power draw to 500. Will have to test actual maximum power draw later. * RGB working on JJ40. * Fix: saturation increase/decrease flipped * Add new directory for my custom keymap with RGB keycodes * Swap LAlt and LGUI * Update JJ40 max power draw with measured value * Update: fun40 rules.mk to enable underglow; earlier failed Travis CI * Fix: init RGB LEDs on boot. Also added HHKB-like keymap for XD60. * Super rudimentary backlight test, init RGB LEDs on boot * Backlighting works - stays on for now * Toggling working * Now can override backlight.c functions. Problem was functions in backlight.c weren't called before due to a lack of matrix_scan_quantum() in matrix.c * Timers not working * Delete global.h * Cleanup * Compiles * Good sign: LEDs stop working again * Handle timer1 overflow * Progress: fix: forgot to init * Backlighting fully working now except breathing. * Revert keymap to original keycodes * Update XD60 keymap README * Update JJ40 keymap with backlight toggles * Breathing working just fine. * Update references * Add backlight_set() call * Cleanup code to disable backlight * Fix: does not compile * Fix: missing call to rgblight_task. * Testing with BACKLIGHT_BREATHING * Cleanup * Cleanup comments * More commenting cleanup. * Do not enable BACKLIGHT_BREATHING by default
Diffstat (limited to 'keyboards/jj40/breathing_custom.h')
-rw-r--r--keyboards/jj40/breathing_custom.h140
1 files changed, 140 insertions, 0 deletions
diff --git a/keyboards/jj40/breathing_custom.h b/keyboards/jj40/breathing_custom.h
new file mode 100644
index 000000000..71416b1b4
--- /dev/null
+++ b/keyboards/jj40/breathing_custom.h
@@ -0,0 +1,140 @@
+/**
+ * Breathing effect code for PS2AVRGB boards (ATMEGA32A)
+ * Works in conjunction with `backlight.c`.
+ *
+ * Code adapted from `quantum.c` to register with the existing TIMER1 overflow
+ * handler in `backlight.c` instead of setting up its own timer.
+ * Kenneth A. (github.com/krusli | krusli.me)
+ */
+
+#ifdef BACKLIGHT_ENABLE
+#ifdef BACKLIGHT_BREATHING
+
+#include "backlight_custom.h"
+
+#ifndef BREATHING_PERIOD
+#define BREATHING_PERIOD 6
+#endif
+
+#define breathing_min() do {breathing_counter = 0;} while (0)
+#define breathing_max() do {breathing_counter = breathing_period * 244 / 2;} while (0)
+
+// TODO make this share code with quantum.c
+
+#define BREATHING_NO_HALT 0
+#define BREATHING_HALT_OFF 1
+#define BREATHING_HALT_ON 2
+#define BREATHING_STEPS 128
+
+static uint8_t breathing_period = BREATHING_PERIOD;
+static uint8_t breathing_halt = BREATHING_NO_HALT;
+static uint16_t breathing_counter = 0;
+
+static bool breathing = false;
+
+bool is_breathing(void) {
+ return breathing;
+}
+
+// See http://jared.geek.nz/2013/feb/linear-led-pwm
+static uint16_t cie_lightness(uint16_t v) {
+ if (v <= 5243) // if below 8% of max
+ return v / 9; // same as dividing by 900%
+ else {
+ uint32_t y = (((uint32_t) v + 10486) << 8) / (10486 + 0xFFFFUL); // add 16% of max and compare
+ // to get a useful result with integer division, we shift left in the expression above
+ // and revert what we've done again after squaring.
+ y = y * y * y >> 8;
+ if (y > 0xFFFFUL) // prevent overflow
+ return 0xFFFFU;
+ else
+ return (uint16_t) y;
+ }
+}
+
+void breathing_enable(void) {
+ breathing = true;
+ breathing_counter = 0;
+ breathing_halt = BREATHING_NO_HALT;
+ // interrupt already registered
+}
+
+void breathing_pulse(void) {
+ if (get_backlight_level() == 0)
+ breathing_min();
+ else
+ breathing_max();
+ breathing_halt = BREATHING_HALT_ON;
+ // breathing_interrupt_enable();
+ breathing = true;
+}
+
+void breathing_disable(void) {
+ breathing = false;
+ // backlight_set(get_backlight_level());
+ b_led_set(get_backlight_level()); // custom implementation of backlight_set()
+}
+
+void breathing_self_disable(void)
+{
+ if (get_backlight_level() == 0)
+ breathing_halt = BREATHING_HALT_OFF;
+ else
+ breathing_halt = BREATHING_HALT_ON;
+}
+
+void breathing_toggle(void) {
+ if (is_breathing())
+ breathing_disable();
+ else
+ breathing_enable();
+}
+
+void breathing_period_set(uint8_t value)
+{
+ if (!value)
+ value = 1;
+ breathing_period = value;
+}
+
+void breathing_period_default(void) {
+ breathing_period_set(BREATHING_PERIOD);
+}
+
+void breathing_period_inc(void)
+{
+ breathing_period_set(breathing_period+1);
+}
+
+void breathing_period_dec(void)
+{
+ breathing_period_set(breathing_period-1);
+}
+
+/* To generate breathing curve in python:
+ * from math import sin, pi; [int(sin(x/128.0*pi)**4*255) for x in range(128)]
+ */
+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};
+
+// Use this before the cie_lightness function.
+static inline uint16_t scale_backlight(uint16_t v) {
+ return v / BACKLIGHT_LEVELS * get_backlight_level();
+}
+
+void custom_breathing_handler(void) {
+ uint16_t interval = (uint16_t) breathing_period * 244 / BREATHING_STEPS;
+ // resetting after one period to prevent ugly reset at overflow.
+ breathing_counter = (breathing_counter + 1) % (breathing_period * 244);
+ uint8_t index = breathing_counter / interval % BREATHING_STEPS;
+
+ if (((breathing_halt == BREATHING_HALT_ON) && (index == BREATHING_STEPS / 2)) ||
+ ((breathing_halt == BREATHING_HALT_OFF) && (index == BREATHING_STEPS - 1)))
+ {
+ // breathing_interrupt_disable();
+ }
+
+ setPWM(cie_lightness(scale_backlight((uint16_t) pgm_read_byte(&breathing_table[index]) * 0x0101U)));
+}
+
+#endif // BACKLIGHT_BREATHING
+#endif // BACKLIGHT_ENABLE