/* Copyright 2017 Jason Williams (Wilba)
*
* 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/>.
*/
#if RGB_BACKLIGHT_ENABLED
#if defined (RGB_BACKLIGHT_ZEAL60) || defined (RGB_BACKLIGHT_ZEAL65) || defined (RGB_BACKLIGHT_M60_A)
#else
#error None of the following was defined: RGB_BACKLIGHT_ZEAL60, RGB_BACKLIGHT_ZEAL65, RGB_BACKLIGHT_M60_A
#endif
#include "zeal60.h"
#include "rgb_backlight.h"
#include "rgb_backlight_api.h"
#include "rgb_backlight_keycodes.h"
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include "progmem.h"
#include "quantum/color.h"
#include "drivers/avr/i2c_master.h"
#include "drivers/issi/is31fl3731.h"
#define BACKLIGHT_EFFECT_MAX 10
backlight_config g_config = {
.use_split_backspace = RGB_BACKLIGHT_USE_SPLIT_BACKSPACE,
.use_split_left_shift = RGB_BACKLIGHT_USE_SPLIT_LEFT_SHIFT,
.use_split_right_shift = RGB_BACKLIGHT_USE_SPLIT_RIGHT_SHIFT,
.use_7u_spacebar = RGB_BACKLIGHT_USE_7U_SPACEBAR,
.use_iso_enter = RGB_BACKLIGHT_USE_ISO_ENTER,
.disable_hhkb_blocker_leds = RGB_BACKLIGHT_DISABLE_HHKB_BLOCKER_LEDS,
.disable_when_usb_suspended = RGB_BACKLIGHT_DISABLE_WHEN_USB_SUSPENDED,
.disable_after_timeout = RGB_BACKLIGHT_DISABLE_AFTER_TIMEOUT,
.brightness = 255,
.effect = RGB_BACKLIGHT_EFFECT,
.effect_speed = 0,
.color_1 = { .h = 0, .s = 255, .v = 255 },
.color_2 = { .h = 127, .s = 255, .v = 255 },
.caps_lock_indicator = { .color = { .h = 0, .s = 0, .v = 255 }, .index = 255 },
.layer_1_indicator = { .color = { .h = 0, .s = 0, .v = 255 }, .index = 255 },
.layer_2_indicator = { .color = { .h = 0, .s = 0, .v = 255 }, .index = 255 },
.layer_3_indicator = { .color = { .h = 0, .s = 0, .v = 255 }, .index = 255 },
.alphas_mods = {
RGB_BACKLIGHT_ALPHAS_MODS_ROW_0,
RGB_BACKLIGHT_ALPHAS_MODS_ROW_1,
RGB_BACKLIGHT_ALPHAS_MODS_ROW_2,
RGB_BACKLIGHT_ALPHAS_MODS_ROW_3,
RGB_BACKLIGHT_ALPHAS_MODS_ROW_4 }
};
bool g_suspend_state = false;
uint8_t g_indicator_state = 0;
// Global tick at 20 Hz
uint32_t g_tick = 0;
// Ticks since this key was last hit.
uint8_t g_key_hit[72];
// Ticks since any key was last hit.
uint32_t g_any_key_hit = 0;
// This is a 7-bit address, that gets left-shifted and bit 0
// set to 0 for write, 1 for read (as per I2C protocol)
#define ISSI_ADDR_1 0x74
#define ISSI_ADDR_2 0x76
const is31_led g_is31_leds[DRIVER_LED_TOTAL] = {
/* Refer to IS31 manual for these locations
* driver
* | R location
* | | G location
* | | | B location
* | | | | */
{0, C2_1, C3_1, C4_1}, // LA0
{0, C1_1, C3_2, C4_2}, // LA1
{0, C1_2, C2_2, C4_3}, // LA2
{0, C1_3, C2_3, C3_3}, // LA3
{0, C1_4, C2_4, C3_4}, // LA4
{0, C1_5, C2_5, C3_5}, // LA5
{0, C1_6, C2_6, C3_6}, // LA6
{0, C1_7, C2_7, C3_7}, // LA7
{0, C1_8, C2_8, C3_8}, // LA8
{0, C9_1, C8_1, C7_1}, // LA9
{0, C9_2, C8_2, C7_2}, // LA10
{0, C9_3, C8_3, C7_3}, // LA11
{0, C9_4, C8_4, C7_4}, // LA12
{0, C9_5, C8_5, C7_5}, // LA13
{0, C9_6, C8_6, C7_6}, // LA14
{0, C9_7, C8_7, C6_6}, // LA15
{0, C9_8, C7_7, C6_7}, // LA16
{0, C8_8, C7_8, C6_8}, // LA17
{0, C2_9, C3_9, C4_9}, // LB0
{0, C1_9, C3_10, C4_10}, // LB1
{0, C1_10, C2_10, C4_11}, // LB2
{0, C1_11, C2_11, C3_11}, // LB3
{0, C1_12, C2_12, C3_12}, // LB4
{0, C1_13, C2_13, C3_13}, // LB5
{0, C1_14, C2_14, C3_14}, // LB6
{0, C1_15, C2_15, C3_15}, // LB7
{0, C1_16, C2_16, C3_16}, // LB8
{0, C9_9, C8_9, C7_9}, // LB9
{0, C9_10, C8_10, C7_10}, // LB10
{0, C9_11, C8_11, C7_11}, // LB11
{0, C9_12, C8_12, C7_12}, // LB12
{0, C9_13, C8_13, C7_13}, // LB13
{0, C9_14, C8_14, C7_14}, // LB14
{0, C9_15, C8_15, C6_14}, // LB15
{0, C9_16, C7_15, C6_15}, // LB16
{0, C8_16, C7_16, C6_16}, // LB17
{1, C2_1, C3_1, C4_1}, // LC0
{1, C1_1, C3_2, C4_2}, // LC1
{1, C1_2, C2_2, C4_3}, // LC2
{1, C1_3, C2_3, C3_3}, // LC3
{1, C1_4, C2_4, C3_4}, // LC4
{1, C1_5, C2_5, C3_5}, // LC5
{1, C1_6, C2_6, C3_6}, // LC6
{1, C1_7, C2_7, C3_7}, // LC7
{1, C1_8, C2_8, C3_8}, // LC8
{1, C9_1, C8_1, C7_1}, // LC9
{1, C9_2, C8_2, C7_2}, // LC10
{1, C9_3, C8_3, C7_3}, // LC11
{1, C9_4, C8_4, C7_4}, // LC12
{1, C9_5, C8_5, C7_5}, // LC13
{1, C9_6, C8_6, C7_6}, // LC14
{1, C9_7, C8_7, C6_6}, // LC15
{1, C9_8, C7_7, C6_7}, // LC16
{1, C8_8, C7_8, C6_8}, // LC17
{1, C2_9, C3_9, C4_9}, // LD0
{1, C1_9, C3_10, C4_10}, // LD1
{1, C1_10, C2_10, C4_11}, // LD2
{1, C1_11, C2_11, C3_11}, // LD3
{1, C1_12, C2_12, C3_12}, // LD4
{1, C1_13, C2_13, C3_13}, // LD5
{1, C1_14, C2_14, C3_14}, // LD6
{1, C1_15, C2_15, C3_15}, // LD7
{1, C1_16, C2_16, C3_16}, // LD8
{1, C9_9, C8_9, C7_9}, // LD9
{1, C9_10, C8_10, C7_10}, // LD10
{1, C9_11, C8_11, C7_11}, // LD11
{1, C9_12, C8_12, C7_12}, // LD12
{1, C9_13, C8_13, C7_13}, // LD13
{1, C9_14, C8_14, C7_14}, // LD14
{1, C9_15, C8_15, C6_14}, // LD15
{1, C9_16, C7_15, C6_15}, // LD16
{1, C8_16, C7_16, C6_16}, // LD17
};
typedef struct Point {
uint8_t x;
uint8_t y;
} Point;
// index in range 0..71 (LA0..LA17, LB0..LB17, LC0..LC17, LD0..LD17)
// point values in range x=0..224 y=0..64
// origin is center of top-left key (i.e Esc)
#if defined (RGB_BACKLIGHT_ZEAL65)
const Point g_map_led_to_point[72] PROGMEM = {
// LA0..LA17
{120,16}, {104,16}, {88,16}, {72,16}, {56,16}, {40,16}, {24,16}, {4,16}, {4,32},
{128,0}, {112,0}, {96,0}, {80,0}, {64,0}, {48,0}, {32,0}, {16,0}, {0,0},
// LB0..LB17
{144,0}, {160,0}, {176,0}, {192,0}, {216,0}, {224,0}, {240,0}, {240,16}, {240,32},
{136,16}, {152,16}, {168,16}, {184,16}, {200,16}, {220,16}, {240,48}, {240,64}, {224,64},
// LC0..LC17
{96,64}, {100,48}, {84,48}, {68,48}, {52,48}, {36,48}, {255,255}, {48,60}, {28,64},
{108,32}, {92,32}, {76,32}, {60,32}, {44,32}, {28,32}, {20,44}, {10,48}, {4,64},
// LD0..LD17
{124,32}, {140,32}, {156,32}, {172,32}, {188,32}, {214,32}, {180,48}, {202,48}, {224,48},
{116,48}, {132,48}, {148,48}, {164,48}, {255,255}, {144,60}, {164,64}, {188,64}, {208,64}
};
const Point g_map_led_to_point_polar[72] PROGMEM = {
// LA0..LA17
{64,128}, {75,132}, {84,145}, {91,164}, {97,187}, {102,213}, {105,242}, {109,255}, {128,247},
{61,255}, {67,255}, {72,255}, {77,255}, {82,255}, {86,255}, {90,255}, {93,255}, {96,255},
// LB0..LB17
{56,255}, {51,255}, {46,255}, {42,255}, {37,255}, {35,255}, {32,255}, {19,255}, {0,255},
{53,132}, {44,145}, {37,164}, {31,187}, {26,213}, {22,249}, {237,255}, {224,255}, {221,255},
// LC0..LC17
{184,255}, {179,135}, {170,149}, {163,169}, {157,193}, {153,220}, {255,255}, {167,255}, {165,255},
{128,26}, {128,60}, {128,94}, {128,128}, {128,162}, {128,196}, {145,233}, {148,255}, {161,255},
// LD0..LD17
{0,9}, {0,43}, {0,77}, {0,111}, {0,145}, {255,201}, {224,181}, {230,217}, {235,255},
{189,128}, {200,131}, {210,141}, {218,159}, {201,228}, {201,228}, {206,255}, {213,255}, {218,255}
};
#elif defined (RGB_BACKLIGHT_ZEAL60) || defined (RGB_BACKLIGHT_M60_A)
const Point g_map_led_to_point[72] PROGMEM = {
// LA0..LA17
{120,16}, {104,16}, {88,16}, {72,16}, {56,16}, {40,16}, {24,16}, {4,16}, {4,32},
{128,0}, {112,0}, {96,0}, {80,0}, {64,0}, {48,0}, {32,0}, {16,0}, {0,0},
// LB0..LB17
{144,0}, {160,0}, {176,0}, {192,0}, {216,0}, {224,0}, {255,255}, {255,255}, {255,255},
{136,16}, {152,16}, {168,16}, {184,16}, {200,16}, {220,16}, {255,255}, {255,255}, {255,255},
// LC0..LC17
{102,64}, {100,48}, {84,48}, {68,48}, {52,48}, {36,48}, {60,64}, {43,64}, {23,64},
{108,32}, {92,32}, {76,32}, {60,32}, {44,32}, {28,32}, {20,48}, {2,48}, {3,64},
// LD0..LD17
{124,32}, {140,32}, {156,32}, {172,32}, {188,32}, {214,32}, {180,48}, {210,48}, {224,48},
{116,48}
|