diff options
| author | Andrew Kannan <andrew.kannan@klaviyo.com> | 2019-01-28 19:40:02 -0500 |
|---|---|---|
| committer | Drashna Jaelre <drashna@live.com> | 2019-01-28 16:40:02 -0800 |
| commit | 0f507f01696eae0e8fe808d17a19db3f6d9e2ce4 (patch) | |
| tree | 986c78fbb09b6bf5e33df6c28158e6877af64960 | |
| parent | d9120412d3d81fc688a7e5346711cf716518dc34 (diff) | |
| download | qmk_firmware-0f507f01696eae0e8fe808d17a19db3f6d9e2ce4.tar.gz qmk_firmware-0f507f01696eae0e8fe808d17a19db3f6d9e2ce4.zip | |
Practice60 RGB and PWM Backlight (#4929)
* Update Practice60 to enable RGB via SPI DMA and use PWM backlight breathing
* Correct stm32f103c8t6 flash size in eeprom definition
* Remove unused files and improve ifdef checks
* Update quantum/rgblight.c
Co-Authored-By: awkannan <andrew.kannan@klaviyo.com>
* Update quantum/rgblight.c
Co-Authored-By: awkannan <andrew.kannan@klaviyo.com>
* EEPROM implementation fix and updated p60 code
* Update define
* Remove dead code
* Update keymap to remove test key
* Update keymap again
| -rw-r--r-- | drivers/avr/ws2812.c | 4 | ||||
| -rw-r--r-- | keyboards/handwired/practice60/bootloader_defs.h | 2 | ||||
| -rw-r--r-- | keyboards/handwired/practice60/config.h | 13 | ||||
| -rw-r--r-- | keyboards/handwired/practice60/halconf.h | 2 | ||||
| -rw-r--r-- | keyboards/handwired/practice60/hsv2rgb.c | 80 | ||||
| -rw-r--r-- | keyboards/handwired/practice60/hsv2rgb.h | 23 | ||||
| -rw-r--r-- | keyboards/handwired/practice60/keymaps/default/keymap.c | 6 | ||||
| -rw-r--r-- | keyboards/handwired/practice60/led.c | 220 | ||||
| -rw-r--r-- | keyboards/handwired/practice60/led_custom.h | 6 | ||||
| -rw-r--r-- | keyboards/handwired/practice60/mcuconf.h | 2 | ||||
| -rw-r--r-- | keyboards/handwired/practice60/practice60.c | 15 | ||||
| -rw-r--r-- | keyboards/handwired/practice60/rules.mk | 4 | ||||
| -rw-r--r-- | keyboards/handwired/practice60/underglow.h | 10 | ||||
| -rw-r--r-- | keyboards/handwired/practice60/ws2812.c (renamed from keyboards/handwired/practice60/underglow.c) | 113 | ||||
| -rw-r--r-- | keyboards/handwired/practice60/ws2812.h | 20 | ||||
| -rw-r--r-- | quantum/rgblight.c | 11 | ||||
| -rwxr-xr-x | tmk_core/common/chibios/eeprom_stm32.c | 17 |
17 files changed, 328 insertions, 220 deletions
diff --git a/drivers/avr/ws2812.c b/drivers/avr/ws2812.c index 59e032bf7..5bd837ec7 100644 --- a/drivers/avr/ws2812.c +++ b/drivers/avr/ws2812.c | |||
| @@ -128,11 +128,11 @@ unsigned char I2C_Write(unsigned char c) | |||
| 128 | c <<= 1; | 128 | c <<= 1; |
| 129 | } | 129 | } |
| 130 | 130 | ||
| 131 | 131 | ||
| 132 | I2C_WriteBit(0); | 132 | I2C_WriteBit(0); |
| 133 | _delay_us(I2C_DELAY); | 133 | _delay_us(I2C_DELAY); |
| 134 | _delay_us(I2C_DELAY); | 134 | _delay_us(I2C_DELAY); |
| 135 | 135 | ||
| 136 | // _delay_us(I2C_DELAY); | 136 | // _delay_us(I2C_DELAY); |
| 137 | //return I2C_ReadBit(); | 137 | //return I2C_ReadBit(); |
| 138 | return 0; | 138 | return 0; |
diff --git a/keyboards/handwired/practice60/bootloader_defs.h b/keyboards/handwired/practice60/bootloader_defs.h index 0f45203cb..6b8fa9f72 100644 --- a/keyboards/handwired/practice60/bootloader_defs.h +++ b/keyboards/handwired/practice60/bootloader_defs.h | |||
| @@ -7,4 +7,4 @@ | |||
| 7 | 7 | ||
| 8 | // STM32F103* does NOT have an USB bootloader in ROM (only serial), | 8 | // STM32F103* does NOT have an USB bootloader in ROM (only serial), |
| 9 | // so setting anything here does not make much sense | 9 | // so setting anything here does not make much sense |
| 10 | // #define STM32_BOOTLOADER_ADDRESS 0x1FFFC800 | 10 | #define STM32_BOOTLOADER_ADDRESS 0x80000000 |
diff --git a/keyboards/handwired/practice60/config.h b/keyboards/handwired/practice60/config.h index 648dd62ed..c015bb746 100644 --- a/keyboards/handwired/practice60/config.h +++ b/keyboards/handwired/practice60/config.h | |||
| @@ -35,7 +35,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
| 35 | #define MATRIX_ROW_PINS { B3, B4, B5, B6, B7 } | 35 | #define MATRIX_ROW_PINS { B3, B4, B5, B6, B7 } |
| 36 | #define DIODE_DIRECTION COL2ROW | 36 | #define DIODE_DIRECTION COL2ROW |
| 37 | 37 | ||
| 38 | #define BACKLIGHT_LEVELS 1 | 38 | #define BACKLIGHT_LEVELS 6 |
| 39 | #define BACKLIGHT_BREATHING | ||
| 40 | #define BREATHING_PERIOD 6 | ||
| 39 | 41 | ||
| 40 | /* define if matrix has ghost */ | 42 | /* define if matrix has ghost */ |
| 41 | //#define MATRIX_HAS_GHOST | 43 | //#define MATRIX_HAS_GHOST |
| @@ -48,6 +50,15 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
| 48 | /* Locking resynchronize hack */ | 50 | /* Locking resynchronize hack */ |
| 49 | #define LOCKING_RESYNC_ENABLE | 51 | #define LOCKING_RESYNC_ENABLE |
| 50 | 52 | ||
| 53 | #define RGBLIGHT_ANIMATIONS | ||
| 54 | |||
| 55 | #define WS2812_LED_N 9 | ||
| 56 | #define RGBLED_NUM WS2812_LED_N | ||
| 57 | #define PORT_WS2812 GPIOB | ||
| 58 | #define PIN_WS2812 15 | ||
| 59 | #define WS2812_SPI SPID2 | ||
| 60 | |||
| 61 | |||
| 51 | /* | 62 | /* |
| 52 | * Feature disable options | 63 | * Feature disable options |
| 53 | * These options are also useful to firmware size reduction. | 64 | * These options are also useful to firmware size reduction. |
diff --git a/keyboards/handwired/practice60/halconf.h b/keyboards/handwired/practice60/halconf.h index 5fba285a3..72879a575 100644 --- a/keyboards/handwired/practice60/halconf.h +++ b/keyboards/handwired/practice60/halconf.h | |||
| @@ -111,7 +111,7 @@ | |||
| 111 | * @brief Enables the PWM subsystem. | 111 | * @brief Enables the PWM subsystem. |
| 112 | */ | 112 | */ |
| 113 | #if !defined(HAL_USE_PWM) || defined(__DOXYGEN__) | 113 | #if !defined(HAL_USE_PWM) || defined(__DOXYGEN__) |
| 114 | #define HAL_USE_PWM FALSE | 114 | #define HAL_USE_PWM TRUE |
| 115 | #endif | 115 | #endif |
| 116 | 116 | ||
| 117 | /** | 117 | /** |
diff --git a/keyboards/handwired/practice60/hsv2rgb.c b/keyboards/handwired/practice60/hsv2rgb.c deleted file mode 100644 index adb7af8fa..000000000 --- a/keyboards/handwired/practice60/hsv2rgb.c +++ /dev/null | |||
| @@ -1,80 +0,0 @@ | |||
| 1 | /* hsv2rgb.c | ||
| 2 | * Integer only conversion functions between HSV and RGB | ||
| 3 | */ | ||
| 4 | |||
| 5 | #include "hsv2rgb.h" | ||
| 6 | |||
| 7 | // TODO fix these buggy macros | ||
| 8 | #define max(x,y) ((x>y) ? x:y) | ||
| 9 | #define min(x,y) ((x>y) ? y:x) | ||
| 10 | #define min3(x,y,z) (min(min(x,y),z)) | ||
| 11 | #define max3(x,y,z) (max(max(x,y),z)) | ||
| 12 | |||
| 13 | |||
| 14 | rgb_color hsv2rgb(hsv_color hsv) | ||
| 15 | { | ||
| 16 | // From : http://qscribble.blogspot.fr/2008/06/integer-conversion-from-hsl-to-rgb.html | ||
| 17 | int h = hsv.h; | ||
| 18 | int s = hsv.s; | ||
| 19 | int v = hsv.v; | ||
| 20 | rgb_color rgb = {0, 0, 0}; | ||
| 21 | |||
| 22 | if (v == 0) | ||
| 23 | return rgb; | ||
| 24 | |||
| 25 | // sextant = 0 .. 5 | ||
| 26 | int sextant = (h*6)/256; | ||
| 27 | // f = 0 .. 42 | ||
| 28 | int f = h - (sextant*256)/6; | ||
| 29 | |||
| 30 | int p = (v * (256 - s))/256; | ||
| 31 | int q = (v * (256*43 - s*f))/(256*43); | ||
| 32 | int t = (v * (256*43 - s*(43-f)))/(256*43); | ||
| 33 | |||
| 34 | // Corrige les erreurs dues aux arrondis | ||
| 35 | p = max(min(p, 255), 0); | ||
| 36 | q = max(min(q, 255), 0); | ||
| 37 | t = max(min(t, 255), 0); | ||
| 38 | |||
| 39 | switch(sextant){ | ||
| 40 | case 0: rgb.r = v; rgb.g = t; rgb.b = p; break; | ||
| 41 | case 1: rgb.r = q; rgb.g = v; rgb.b = p; break; | ||
| 42 | case 2: rgb.r = p; rgb.g = v; rgb.b = t; break; | ||
| 43 | case 3: rgb.r = p; rgb.g = q; rgb.b = v; break; | ||
| 44 | case 4: rgb.r = t; rgb.g = p; rgb.b = v; break; | ||
| 45 | default:rgb.r = v; rgb.g = p; rgb.b = q; break; | ||
| 46 | } | ||
| 47 | return rgb; | ||
| 48 | } | ||
| 49 | |||
| 50 | |||
| 51 | hsv_color rgb2hsv(rgb_color rgb) | ||
| 52 | { | ||
| 53 | // From : http://www.ruinelli.ch/rgb-to-hsv | ||
| 54 | hsv_color hsv = {0, 0, 0}; | ||
| 55 | int min, max, delta; | ||
| 56 | |||
| 57 | min = min3(rgb.r, rgb.g, rgb.b); | ||
| 58 | max = max3(rgb.r, rgb.g, rgb.b); | ||
| 59 | |||
| 60 | if(max==0) { | ||
| 61 | hsv.h = 0; | ||
| 62 | hsv.s = 0; | ||
| 63 | hsv.v = 0; | ||
| 64 | return hsv; | ||
| 65 | } | ||
| 66 | |||
| 67 | hsv.v = max; | ||
| 68 | delta = max - min; | ||
| 69 | |||
| 70 | hsv.s = (delta)*255 / max; | ||
| 71 | |||
| 72 | if(rgb.r == max) | ||
| 73 | hsv.h = (rgb.g - rgb.b)*42/delta; // between yellow & magenta | ||
| 74 | else if(rgb.g == max) | ||
| 75 | hsv.h = 120 + (rgb.b - rgb.r)*42/delta; // between cyan & yellow | ||
| 76 | else | ||
| 77 | hsv.h = 240 + (rgb.r - rgb.g)*42/delta; // between magenta & cyan | ||
| 78 | |||
| 79 | return hsv; | ||
| 80 | } \ No newline at end of file | ||
diff --git a/keyboards/handwired/practice60/hsv2rgb.h b/keyboards/handwired/practice60/hsv2rgb.h deleted file mode 100644 index 99566c32c..000000000 --- a/keyboards/handwired/practice60/hsv2rgb.h +++ /dev/null | |||
| @@ -1,23 +0,0 @@ | |||
| 1 | /* hsv2rgb.h | ||
| 2 | * Convert Hue Saturation Value to Red Green Blue | ||
| 3 | * | ||
| 4 | * Programme de convertion d'une information HSV en RGB | ||
| 5 | */ | ||
| 6 | #ifndef HSV2RGB_H | ||
| 7 | #define HSV2RGB_H | ||
| 8 | |||
| 9 | typedef struct { | ||
| 10 | unsigned char h; | ||
| 11 | unsigned char s; | ||
| 12 | unsigned char v; | ||
| 13 | } hsv_color; | ||
| 14 | |||
| 15 | typedef struct { | ||
| 16 | unsigned char r; | ||
| 17 | unsigned char g; | ||
| 18 | unsigned char b; | ||
| 19 | } rgb_color; | ||
| 20 | |||
| 21 | rgb_color hsv2rgb(hsv_color hsv); | ||
| 22 | |||
| 23 | #endif \ No newline at end of file | ||
diff --git a/keyboards/handwired/practice60/keymaps/default/keymap.c b/keyboards/handwired/practice60/keymaps/default/keymap.c index ac6e75509..66ebe0646 100644 --- a/keyboards/handwired/practice60/keymaps/default/keymap.c +++ b/keyboards/handwired/practice60/keymaps/default/keymap.c | |||
| @@ -32,16 +32,16 @@ enum custom_keycodes { | |||
| 32 | const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { | 32 | const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { |
| 33 | [_BASE] = LAYOUT_60_ansi( | 33 | [_BASE] = LAYOUT_60_ansi( |
| 34 | KC_GESC, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_MINS, KC_EQL, KC_BSPC, \ | 34 | KC_GESC, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_MINS, KC_EQL, KC_BSPC, \ |
| 35 | KC_TAB, KC_Q, KC_W, KC_E, KC_R, KC_T, MT(MOD_LSFT, KC_Y), KC_U, KC_I, KC_O, KC_P, KC_LBRC, KC_RBRC, KC_BSLS, \ | 35 | KC_TAB, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P, KC_LBRC, KC_RBRC, KC_BSLS, \ |
| 36 | KC_CAPS, KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN, KC_QUOT, KC_ENT, \ | 36 | KC_CAPS, KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN, KC_QUOT, KC_ENT, \ |
| 37 | KC_LSFT, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, KC_RSFT, \ | 37 | KC_LSFT, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, KC_RSFT, \ |
| 38 | KC_LCTL, KC_LGUI, KC_LALT, KC_SPC, KC_RALT, KC_RGUI, MO(_FN1), KC_RCTL | 38 | KC_LCTL, KC_LGUI, KC_LALT, KC_SPC, KC_RALT, KC_RGUI, MO(_FN1), KC_RCTL |
| 39 | ), | 39 | ), |
| 40 | 40 | ||
| 41 | [_FN1] = LAYOUT_60_ansi( | 41 | [_FN1] = LAYOUT_60_ansi( |
| 42 | KC_GESC, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, KC_BSPC, \ | 42 | KC_GESC, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, KC_DEL, \ |
| 43 | RGB_TOG, RGB_MOD, KC_UP, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, \ | 43 | RGB_TOG, RGB_MOD, KC_UP, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, \ |
| 44 | _______, KC_LEFT, KC_DOWN, KC_RGHT, _______, _______, _______, _______, _______, _______, _______, _______, _______, \ | 44 | BL_BRTG, KC_LEFT, KC_DOWN, KC_RGHT, _______, _______, _______, _______, _______, _______, _______, _______, _______, \ |
| 45 | BL_INC, BL_DEC, BL_TOGG, _______, _______, _______, _______, _______, _______, _______, _______, _______, \ | 45 | BL_INC, BL_DEC, BL_TOGG, _______, _______, _______, _______, _______, _______, _______, _______, _______, \ |
| 46 | KC_GRV, _______, _______, _______, _______, _______, _______, _______ | 46 | KC_GRV, _______, _______, _______, _______, _______, _______, _______ |
| 47 | ) | 47 | ) |
diff --git a/keyboards/handwired/practice60/led.c b/keyboards/handwired/practice60/led.c index f15baed16..a9ede5bba 100644 --- a/keyboards/handwired/practice60/led.c +++ b/keyboards/handwired/practice60/led.c | |||
| @@ -18,34 +18,240 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
| 18 | #include "hal.h" | 18 | #include "hal.h" |
| 19 | #include "backlight.h" | 19 | #include "backlight.h" |
| 20 | #include "led.h" | 20 | #include "led.h" |
| 21 | #include "led_custom.h" | ||
| 21 | #include "printf.h" | 22 | #include "printf.h" |
| 22 | 23 | ||
| 24 | static void breathing_callback(PWMDriver *pwmp); | ||
| 25 | |||
| 26 | static PWMConfig pwmCFG = { | ||
| 27 | 0xFFFF, /* PWM clock frequency */ | ||
| 28 | 256, /* PWM period (in ticks) 1S (1/10kHz=0.1mS 0.1ms*10000 ticks=1S) */ | ||
| 29 | NULL, /* No Callback */ | ||
| 30 | { | ||
| 31 | {PWM_OUTPUT_ACTIVE_HIGH, NULL}, /* Enable Channel 0 */ | ||
| 32 | {PWM_OUTPUT_DISABLED, NULL}, | ||
| 33 | {PWM_OUTPUT_DISABLED, NULL}, | ||
| 34 | {PWM_OUTPUT_DISABLED, NULL} | ||
| 35 | }, | ||
| 36 | 0, /* HW dependent part.*/ | ||
| 37 | 0 | ||
| 38 | }; | ||
| 39 | |||
| 40 | static PWMConfig pwmCFG_breathing = { | ||
| 41 | 0xFFFF, /* 10kHz PWM clock frequency */ | ||
| 42 | 256, /* PWM period (in ticks) 1S (1/10kHz=0.1mS 0.1ms*10000 ticks=1S) */ | ||
| 43 | breathing_callback, /* Breathing Callback */ | ||
| 44 | { | ||
| 45 | {PWM_OUTPUT_ACTIVE_HIGH, NULL}, /* Enable Channel 0 */ | ||
| 46 | {PWM_OUTPUT_DISABLED, NULL}, | ||
| 47 | {PWM_OUTPUT_DISABLED, NULL}, | ||
| 48 | {PWM_OUTPUT_DISABLED, NULL} | ||
| 49 | }, | ||
| 50 | 0, /* HW dependent part.*/ | ||
| 51 | 0 | ||
| 52 | }; | ||
| 53 | |||
| 54 | // See http://jared.geek.nz/2013/feb/linear-led-pwm | ||
| 55 | static uint16_t cie_lightness(uint16_t v) { | ||
| 56 | if (v <= 5243) // if below 8% of max | ||
| 57 | return v / 9; // same as dividing by 900% | ||
| 58 | else { | ||
| 59 | uint32_t y = (((uint32_t) v + 10486) << 8) / (10486 + 0xFFFFUL); // add 16% of max and compare | ||
| 60 | // to get a useful result with integer division, we shift left in the expression above | ||
| 61 | // and revert what we've done again after squaring. | ||
| 62 | y = y * y * y >> 8; | ||
| 63 | if (y > 0xFFFFUL) // prevent overflow | ||
| 64 | return 0xFFFFU; | ||
| 65 | else | ||
| 66 | return (uint16_t) y; | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | |||
| 23 | void backlight_init_ports(void) { | 71 | void backlight_init_ports(void) { |
| 24 | printf("backlight_init_ports()\n"); | 72 | printf("backlight_init_ports()\n"); |
| 25 | #ifdef BACKLIGHT_ENABLE | 73 | #ifdef BACKLIGHT_ENABLE |
| 26 | palSetPadMode(GPIOA, 8, PAL_MODE_OUTPUT_PUSHPULL); | 74 | |
| 27 | palSetPad(GPIOA, 8); | 75 | palSetPadMode(GPIOA, 8, PAL_MODE_STM32_ALTERNATE_PUSHPULL); |
| 76 | pwmStart(&PWMD1, &pwmCFG); | ||
| 77 | pwmEnableChannel(&PWMD1, 0, PWM_FRACTION_TO_WIDTH(&PWMD1, 0xFFFF,cie_lightness(0xFFFF))); | ||
| 28 | #endif | 78 | #endif |
| 29 | } | 79 | } |
| 30 | 80 | ||
| 31 | void backlight_set(uint8_t level) { | 81 | void backlight_set(uint8_t level) { |
| 32 | printf("backlight_set(%d)\n", level); | 82 | printf("backlight_set(%d)\n", level); |
| 33 | #ifdef BACKLIGHT_ENABLE | 83 | #ifdef BACKLIGHT_ENABLE |
| 84 | uint32_t duty = (uint32_t)(cie_lightness(0xFFFF * (uint32_t) level / BACKLIGHT_LEVELS)); | ||
| 85 | printf("duty: (%d)\n", duty); | ||
| 34 | if (level == 0) { | 86 | if (level == 0) { |
| 35 | // Turn backlight off | 87 | // Turn backlight off |
| 36 | palSetPad(GPIOA, 8); | 88 | pwmDisableChannel(&PWMD1, 0); |
| 37 | } else { | 89 | } else { |
| 38 | // Turn backlight on | 90 | // Turn backlight on |
| 39 | palClearPad(GPIOA, 8); | 91 | if(!is_breathing()){ |
| 92 | pwmEnableChannel(&PWMD1, 0, PWM_FRACTION_TO_WIDTH(&PWMD1,0xFFFF,duty)); | ||
| 93 | } | ||
| 40 | } | 94 | } |
| 41 | #endif | 95 | #endif |
| 42 | } | 96 | } |
| 43 | 97 | ||
| 98 | |||
| 99 | uint8_t backlight_tick = 0; | ||
| 100 | |||
| 101 | void backlight_task(void) { | ||
| 102 | } | ||
| 103 | |||
| 104 | #define BREATHING_NO_HALT 0 | ||
| 105 | #define BREATHING_HALT_OFF 1 | ||
| 106 | #define BREATHING_HALT_ON 2 | ||
| 107 | #define BREATHING_STEPS 128 | ||
| 108 | |||
| 109 | static uint8_t breathing_period = BREATHING_PERIOD; | ||
| 110 | static uint8_t breathing_halt = BREATHING_NO_HALT; | ||
| 111 | static uint16_t breathing_counter = 0; | ||
| 112 | |||
| 113 | bool is_breathing(void) { | ||
| 114 | return PWMD1.config == &pwmCFG_breathing; | ||
| 115 | } | ||
| 116 | |||
| 117 | #define breathing_min() do {breathing_counter = 0;} while (0) | ||
| 118 | #define breathing_max() do {breathing_counter = breathing_period * 256 / 2;} while (0) | ||
| 119 | |||
| 120 | |||
| 121 | void breathing_interrupt_enable(void){ | ||
| 122 | pwmStop(&PWMD1); | ||
| 123 | printf("starting with callback\n"); | ||
| 124 | pwmStart(&PWMD1, &pwmCFG_breathing); | ||
| 125 | chSysLockFromISR(); | ||
| 126 | pwmEnablePeriodicNotification(&PWMD1); | ||
| 127 | pwmEnableChannelI( | ||
| 128 | &PWMD1, | ||
| 129 | 0, | ||
| 130 | PWM_FRACTION_TO_WIDTH( | ||
| 131 | &PWMD1, | ||
| 132 | 0xFFFF, | ||
| 133 | 0xFFFF | ||
| 134 | ) | ||
| 135 | ); | ||
| 136 | chSysUnlockFromISR(); | ||
| 137 | } | ||
| 138 | |||
| 139 | void breathing_interrupt_disable(void){ | ||
| 140 | pwmStop(&PWMD1); | ||
| 141 | printf("starting without callback\n"); | ||
| 142 | pwmStart(&PWMD1, &pwmCFG); | ||
| 143 | } | ||
| 144 | |||
| 145 | void breathing_enable(void) | ||
| 146 | { | ||
| 147 | printf("breathing_enable()\n"); | ||
| 148 | breathing_counter = 0; | ||
| 149 | breathing_halt = BREATHING_NO_HALT; | ||
| 150 | breathing_interrupt_enable(); | ||
| 151 | } | ||
| 152 | |||
| 153 | void breathing_pulse(void) | ||
| 154 | { | ||
| 155 | if (get_backlight_level() == 0) | ||
| 156 | breathing_min(); | ||
| 157 | else | ||
| 158 | breathing_max(); | ||
| 159 | breathing_halt = BREATHING_HALT_ON; | ||
| 160 | breathing_interrupt_enable(); | ||
| 161 | } | ||
| 162 | |||
| 163 | void breathing_disable(void) | ||
| 164 | { | ||
| 165 | printf("breathing_disable()\n"); | ||
| 166 | breathing_interrupt_disable(); | ||
| 167 | // Restore backlight level | ||
| 168 | backlight_set(get_backlight_level()); | ||
| 169 | } | ||
| 170 | |||
| 171 | void breathing_self_disable(void) | ||
| 172 | { | ||
| 173 | if (get_backlight_level() == 0) | ||
| 174 | breathing_halt = BREATHING_HALT_OFF; | ||
| 175 | else | ||
| 176 | breathing_halt = BREATHING_HALT_ON; | ||
| 177 | } | ||
| 178 | |||
| 179 | void breathing_toggle(void) { | ||
| 180 | if (is_breathing()){ | ||
| 181 | printf("disable breathing\n"); | ||
| 182 | breathing_disable(); | ||
| 183 | } else { | ||
| 184 | printf("enable breathing\n"); | ||
| 185 | breathing_enable(); | ||
| 186 | } | ||
| 187 | } | ||
| 188 | |||
| 189 | void breathing_period_set(uint8_t value) | ||
| 190 | { | ||
| 191 | if (!value) | ||
| 192 | value = 1; | ||
| 193 | breathing_period = value; | ||
| 194 | } | ||
| 195 | |||
| 196 | void breathing_period_default(void) { | ||
| 197 | breathing_period_set(BREATHING_PERIOD); | ||
| 198 | } | ||
| 199 | |||
| 200 | void breathing_period_inc(void) | ||
| 201 | { | ||
| 202 | breathing_period_set(breathing_period+1); | ||
| 203 | } | ||
| 204 | |||
| 205 | void breathing_period_dec(void) | ||
| 206 | { | ||
| 207 | breathing_period_set(breathing_period-1); | ||
| 208 | } | ||
| 209 | |||
| 210 | /* To generate breathing curve in python: | ||
| 211 | * from math import sin, pi; [int(sin(x/128.0*pi)**4*255) for x in range(128)] | ||
| 212 | */ | ||
| 213 | static const uint8_t breathing_table[BREATHING_STEPS] = {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}; | ||
| 214 | |||
| 215 | // Use this before the cie_lightness function. | ||
| 216 | static inline uint16_t scale_backlight(uint16_t v) { | ||
| 217 | return v / BACKLIGHT_LEVELS * get_backlight_level(); | ||
| 218 | } | ||
| 219 | |||
| 220 | static void breathing_callback(PWMDriver *pwmp) | ||
| 221 | { | ||
| 222 | (void)pwmp; | ||
| 223 | uint16_t interval = (uint16_t) breathing_period * 256 / BREATHING_STEPS; | ||
| 224 | // resetting after one period to prevent ugly reset at overflow. | ||
| 225 | breathing_counter = (breathing_counter + 1) % (breathing_period * 256); | ||
| 226 | uint8_t index = breathing_counter / interval % BREATHING_STEPS; | ||
| 227 | |||
| 228 | if (((breathing_halt == BREATHING_HALT_ON) && (index == BREATHING_STEPS / 2)) || | ||
| 229 | ((breathing_halt == BREATHING_HALT_OFF) && (index == BREATHING_STEPS - 1))) | ||
| 230 | { | ||
| 231 | breathing_interrupt_disable(); | ||
| 232 | } | ||
| 233 | |||
| 234 | uint32_t duty = cie_lightness(scale_backlight(breathing_table[index] * 256)); | ||
| 235 | |||
| 236 | chSysLockFromISR(); | ||
| 237 | pwmEnableChannelI( | ||
| 238 | &PWMD1, | ||
| 239 | 0, | ||
| 240 | PWM_FRACTION_TO_WIDTH( | ||
| 241 | &PWMD1, | ||
| 242 | 0xFFFF, | ||
| 243 | duty | ||
| 244 | ) | ||
| 245 | ); | ||
| 246 | chSysUnlockFromISR(); | ||
| 247 | } | ||
| 248 | |||
| 249 | |||
| 44 | void led_set(uint8_t usb_led) | 250 | void led_set(uint8_t usb_led) |
| 45 | { | 251 | { |
| 46 | if (usb_led & (1<<USB_LED_CAPS_LOCK)) { | 252 | if (usb_led & (1<<USB_LED_CAPS_LOCK)) { |
| 47 | palClearPad(GPIOC, 13); | 253 | palSetPad(GPIOC, 13); |
| 48 | } else { | 254 | } else { |
| 49 | palSetPad(GPIOC, 13); | 255 | palClearPad(GPIOC, 13); |
| 50 | } | 256 | } |
| 51 | } | 257 | } |
diff --git a/keyboards/handwired/practice60/led_custom.h b/keyboards/handwired/practice60/led_custom.h new file mode 100644 index 000000000..8451c06d6 --- /dev/null +++ b/keyboards/handwired/practice60/led_custom.h | |||
| @@ -0,0 +1,6 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | void backlight_task(void); | ||
| 4 | void breathing_interrupt_disable(void); | ||
| 5 | void breathing_interrupt_enable(void); | ||
| 6 | bool is_breathing(void); | ||
diff --git a/keyboards/handwired/practice60/mcuconf.h b/keyboards/handwired/practice60/mcuconf.h index 9945e7408..fced27289 100644 --- a/keyboards/handwired/practice60/mcuconf.h +++ b/keyboards/handwired/practice60/mcuconf.h | |||
| @@ -132,7 +132,7 @@ | |||
| 132 | * PWM driver system settings. | 132 | * PWM driver system settings. |
| 133 | */ | 133 | */ |
| 134 | #define STM32_PWM_USE_ADVANCED FALSE | 134 | #define STM32_PWM_USE_ADVANCED FALSE |
| 135 | #define STM32_PWM_USE_TIM1 FALSE | 135 | #define STM32_PWM_USE_TIM1 TRUE |
| 136 | #define STM32_PWM_USE_TIM2 FALSE | 136 | #define STM32_PWM_USE_TIM2 FALSE |
| 137 | #define STM32_PWM_USE_TIM3 FALSE | 137 | #define STM32_PWM_USE_TIM3 FALSE |
| 138 | #define STM32_PWM_USE_TIM4 FALSE | 138 | #define STM32_PWM_USE_TIM4 FALSE |
diff --git a/keyboards/handwired/practice60/practice60.c b/keyboards/handwired/practice60/practice60.c index 5d0fffbd0..acc0f00e5 100644 --- a/keyboards/handwired/practice60/practice60.c +++ b/keyboards/handwired/practice60/practice60.c | |||
| @@ -3,8 +3,7 @@ | |||
| 3 | 3 | ||
| 4 | #include "ch.h" | 4 | #include "ch.h" |
| 5 | #include "hal.h" | 5 | #include "hal.h" |
| 6 | 6 | #include "led_custom.h" | |
| 7 | #include "underglow.h" | ||
| 8 | #include "print.h" | 7 | #include "print.h" |
| 9 | #include "debug.h" | 8 | #include "debug.h" |
| 10 | #include "util.h" | 9 | #include "util.h" |
| @@ -19,14 +18,18 @@ | |||
| 19 | void matrix_init_kb(void){ | 18 | void matrix_init_kb(void){ |
| 20 | /* MOSI pin*/ | 19 | /* MOSI pin*/ |
| 21 | palSetPadMode(GPIOB, 15, PAL_MODE_STM32_ALTERNATE_PUSHPULL); | 20 | palSetPadMode(GPIOB, 15, PAL_MODE_STM32_ALTERNATE_PUSHPULL); |
| 22 | |||
| 23 | LED_ON(); | 21 | LED_ON(); |
| 24 | palSetPad(GPIOA, 8); | ||
| 25 | wait_ms(500); | 22 | wait_ms(500); |
| 26 | palClearPad(GPIOA, 8); | ||
| 27 | LED_OFF(); | 23 | LED_OFF(); |
| 28 | 24 | ||
| 25 | #ifdef RGBLIGHT_ENABLE | ||
| 29 | leds_init(); | 26 | leds_init(); |
| 27 | #endif | ||
| 28 | } | ||
| 30 | 29 | ||
| 31 | 30 | void matrix_scan_kb(void) | |
| 31 | { | ||
| 32 | #ifdef RGBLIGHT_ENABLE | ||
| 33 | rgblight_task(); | ||
| 34 | #endif | ||
| 32 | } | 35 | } |
diff --git a/keyboards/handwired/practice60/rules.mk b/keyboards/handwired/practice60/rules.mk index 915b07320..e417c91ee 100644 --- a/keyboards/handwired/practice60/rules.mk +++ b/keyboards/handwired/practice60/rules.mk | |||
| @@ -1,7 +1,6 @@ | |||
| 1 | # project specific files | 1 | # project specific files |
| 2 | SRC = led.c \ | 2 | SRC = led.c \ |
| 3 | underglow.c \ | 3 | ws2812.c |
| 4 | hsv2rgb.c | ||
| 5 | 4 | ||
| 6 | # GENERIC STM32F103C8T6 board - stm32duino bootloader | 5 | # GENERIC STM32F103C8T6 board - stm32duino bootloader |
| 7 | OPT_DEFS = -DCORTEX_VTOR_INIT=0x2000 | 6 | OPT_DEFS = -DCORTEX_VTOR_INIT=0x2000 |
| @@ -50,6 +49,7 @@ COMMAND_ENABLE = yes # Commands for debug and configuration | |||
| 50 | SLEEP_LED_ENABLE = yes # Breathing sleep LED during USB suspend | 49 | SLEEP_LED_ENABLE = yes # Breathing sleep LED during USB suspend |
| 51 | NKRO_ENABLE = yes # USB Nkey Rollover | 50 | NKRO_ENABLE = yes # USB Nkey Rollover |
| 52 | BACKLIGHT_ENABLE = yes | 51 | BACKLIGHT_ENABLE = yes |
| 52 | RGBLIGHT_ENABLE = yes | ||
| 53 | 53 | ||
| 54 | LAYOUTS = 60_ansi | 54 | LAYOUTS = 60_ansi |
| 55 | 55 | ||
diff --git a/keyboards/handwired/practice60/underglow.h b/keyboards/handwired/practice60/underglow.h deleted file mode 100644 index ff1195279..000000000 --- a/keyboards/handwired/practice60/underglow.h +++ /dev/null | |||
| @@ -1,10 +0,0 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include "hsv2rgb.h" | ||
| 4 | |||
| 5 | void set_leds_color_hsv(hsv_color color); | ||
| 6 | void set_leds_color_rgb(rgb_color color); | ||
| 7 | void set_led_color_hsv(hsv_color color, int pos); | ||
| 8 | void set_led_color_rgb(rgb_color color, int pos); | ||
| 9 | |||
| 10 | void leds_init(void); | ||
diff --git a/keyboards/handwired/practice60/underglow.c b/keyboards/handwired/practice60/ws2812.c index 1383af189..7d0f909c0 100644 --- a/keyboards/handwired/practice60/underglow.c +++ b/keyboards/handwired/practice60/ws2812.c | |||
| @@ -1,8 +1,12 @@ | |||
| 1 | #include "ch.h" | 1 | /* |
| 2 | #include "hal.h" | 2 | * LEDDriver.c |
| 3 | * | ||
| 4 | * Created on: Aug 26, 2013 | ||
| 5 | * Author: Omri Iluz | ||
| 6 | */ | ||
| 3 | 7 | ||
| 4 | #include "hsv2rgb.h" | 8 | #include "ws2812.h" |
| 5 | #include "underglow.h" | 9 | #include "stdlib.h" |
| 6 | 10 | ||
| 7 | #define BYTES_FOR_LED_BYTE 4 | 11 | #define BYTES_FOR_LED_BYTE 4 |
| 8 | #define NB_COLORS 3 | 12 | #define NB_COLORS 3 |
| @@ -10,18 +14,17 @@ | |||
| 10 | #define DATA_SIZE BYTES_FOR_LED*NB_LEDS | 14 | #define DATA_SIZE BYTES_FOR_LED*NB_LEDS |
| 11 | #define RESET_SIZE 200 | 15 | #define RESET_SIZE 200 |
| 12 | #define PREAMBLE_SIZE 4 | 16 | #define PREAMBLE_SIZE 4 |
| 13 | |||
| 14 | // Define the spi your LEDs are plugged to here | 17 | // Define the spi your LEDs are plugged to here |
| 15 | #define LEDS_SPI SPID2 | 18 | #define WS2812_SPI SPID2 |
| 16 | // Define the number of LEDs you wish to control in your LED strip | 19 | // Define the number of LEDs you wish to control in your LED strip |
| 17 | #define NB_LEDS 8 | 20 | #define NB_LEDS RGBLED_NUM |
| 18 | 21 | ||
| 19 | #define LED_SPIRAL 1 | 22 | #define LED_SPIRAL 1 |
| 20 | 23 | ||
| 21 | static uint8_t txbuf[PREAMBLE_SIZE + DATA_SIZE + RESET_SIZE]; | 24 | static uint8_t txbuf[PREAMBLE_SIZE + DATA_SIZE + RESET_SIZE]; |
| 22 | static uint8_t get_protocol_eq(uint8_t data, int pos); | 25 | static uint8_t get_protocol_eq(uint8_t data, int pos); |
| 23 | 26 | ||
| 24 | /* | 27 | /* |
| 25 | * This lib is meant to be used asynchronously, thus the colors contained in | 28 | * This lib is meant to be used asynchronously, thus the colors contained in |
| 26 | * the txbuf will be sent in loop, so that the colors are always the ones you | 29 | * the txbuf will be sent in loop, so that the colors are always the ones you |
| 27 | * put in the table (the user thus have less to worry about) | 30 | * put in the table (the user thus have less to worry about) |
| @@ -37,38 +40,18 @@ static THD_WORKING_AREA(LEDS_THREAD_WA, 128); | |||
| 37 | static THD_FUNCTION(ledsThread, arg) { | 40 | static THD_FUNCTION(ledsThread, arg) { |
| 38 | (void) arg; | 41 | (void) arg; |
| 39 | while(1){ | 42 | while(1){ |
| 40 | spiSend(&LEDS_SPI, PREAMBLE_SIZE + DATA_SIZE + RESET_SIZE, txbuf); | 43 | spiSend(&WS2812_SPI, PREAMBLE_SIZE + DATA_SIZE + RESET_SIZE, txbuf); |
| 41 | } | ||
| 42 | } | ||
| 43 | |||
| 44 | #if LED_SPIRAL | ||
| 45 | /* | ||
| 46 | * 'Led spiral' is a simple demo in which we put all the leds to the same | ||
| 47 | * color, where this color does all the hsv circle in loop. | ||
| 48 | * If you want to launch the thread that will chage the led colors to the | ||
| 49 | * appropriate value, simply set LED_SPIRAL to 1. | ||
| 50 | */ | ||
| 51 | static THD_WORKING_AREA(HSVTRANS_WA, 128); | ||
| 52 | static THD_FUNCTION(hsv_transThread, arg){ | ||
| 53 | (void) arg; | ||
| 54 | hsv_color color = {0, 255, 127}; | ||
| 55 | while(1){ | ||
| 56 | color.h += 1; | ||
| 57 | color.h %= 256; | ||
| 58 | set_leds_color_hsv(color); | ||
| 59 | chThdSleepMilliseconds(50); | ||
| 60 | } | 44 | } |
| 61 | } | 45 | } |
| 62 | #endif | ||
| 63 | 46 | ||
| 64 | static const SPIConfig spicfg = { | 47 | static const SPIConfig spicfg = { |
| 65 | NULL, | 48 | NULL, |
| 66 | GPIOB, | 49 | PORT_WS2812, |
| 67 | 15, | 50 | PIN_WS2812, |
| 68 | SPI_CR1_BR_1|SPI_CR1_BR_0 // baudrate : fpclk / 8 => 1tick is 0.32us | 51 | SPI_CR1_BR_1|SPI_CR1_BR_0 // baudrate : fpclk / 8 => 1tick is 0.32us (2.25 MHz) |
| 69 | }; | 52 | }; |
| 70 | 53 | ||
| 71 | /* | 54 | /* |
| 72 | * Function used to initialize the driver. | 55 | * Function used to initialize the driver. |
| 73 | * | 56 | * |
| 74 | * Starts by shutting off all the LEDs. | 57 | * Starts by shutting off all the LEDs. |
| @@ -77,21 +60,19 @@ static const SPIConfig spicfg = { | |||
| 77 | * txbuff values) | 60 | * txbuff values) |
| 78 | */ | 61 | */ |
| 79 | void leds_init(void){ | 62 | void leds_init(void){ |
| 63 | /* MOSI pin*/ | ||
| 64 | palSetPadMode(PORT_WS2812, PIN_WS2812, PAL_MODE_STM32_ALTERNATE_PUSHPULL); | ||
| 80 | for(int i = 0; i < RESET_SIZE; i++) | 65 | for(int i = 0; i < RESET_SIZE; i++) |
| 81 | txbuf[DATA_SIZE+i] = 0x00; | 66 | txbuf[DATA_SIZE+i] = 0x00; |
| 82 | for (int i=0; i<PREAMBLE_SIZE; i++) | 67 | for (int i=0; i<PREAMBLE_SIZE; i++) |
| 83 | txbuf[i] = 0x00; | 68 | txbuf[i] = 0x00; |
| 84 | spiAcquireBus(&LEDS_SPI); /* Acquire ownership of the bus. */ | 69 | spiAcquireBus(&WS2812_SPI); /* Acquire ownership of the bus. */ |
| 85 | spiStart(&LEDS_SPI, &spicfg); /* Setup transfer parameters. */ | 70 | spiStart(&WS2812_SPI, &spicfg); /* Setup transfer parameters. */ |
| 86 | spiSelect(&LEDS_SPI); /* Slave Select assertion. */ | 71 | spiSelect(&WS2812_SPI); /* Slave Select assertion. */ |
| 87 | chThdCreateStatic(LEDS_THREAD_WA, sizeof(LEDS_THREAD_WA),NORMALPRIO, ledsThread, NULL); | 72 | chThdCreateStatic(LEDS_THREAD_WA, sizeof(LEDS_THREAD_WA),NORMALPRIO, ledsThread, NULL); |
| 88 | #if LED_SPIRAL | ||
| 89 | chThdCreateStatic(HSVTRANS_WA, sizeof(HSVTRANS_WA), | ||
| 90 | NORMALPRIO, hsv_transThread, NULL); | ||
| 91 | #endif | ||
| 92 | } | 73 | } |
| 93 | 74 | ||
| 94 | /* | 75 | /* |
| 95 | * As the trick here is to use the SPI to send a huge pattern of 0 and 1 to | 76 | * As the trick here is to use the SPI to send a huge pattern of 0 and 1 to |
| 96 | * the ws2812b protocol, we use this helper function to translate bytes into | 77 | * the ws2812b protocol, we use this helper function to translate bytes into |
| 97 | * 0s and 1s for the LED (with the appropriate timing). | 78 | * 0s and 1s for the LED (with the appropriate timing). |
| @@ -109,20 +90,20 @@ static uint8_t get_protocol_eq(uint8_t data, int pos){ | |||
| 109 | return eq; | 90 | return eq; |
| 110 | } | 91 | } |
| 111 | 92 | ||
| 112 | /* | 93 | |
| 113 | * If you want to set a LED's color in the HSV color space, simply call this | 94 | void WS2812_init(void) { |
| 114 | * function with a hsv_color containing the desired color and the index of the | 95 | leds_init(); |
| 115 | * led on the LED strip (starting from 0, the first one being the closest the | ||
| 116 | * first plugged to the board) | ||
| 117 | * | ||
| 118 | * Only set the color of the LEDs through the functions given by this API | ||
| 119 | * (unless you really know what you are doing) | ||
| 120 | */ | ||
| 121 | void set_led_color_hsv(hsv_color color, int pos){ | ||
| 122 | set_led_color_rgb(hsv2rgb(color), pos); | ||
| 123 | } | 96 | } |
| 124 | 97 | ||
| 125 | /* | 98 | void ws2812_setleds(LED_TYPE *ledarray, uint16_t number_of_leds) { |
| 99 | uint8_t i = 0; | ||
| 100 | while (i < number_of_leds) { | ||
| 101 | set_led_color_rgb(ledarray[i], i); | ||
| 102 | i++; | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | /* | ||
| 126 | * If you want to set a LED's color in the RGB color space, simply call this | 107 | * If you want to set a LED's color in the RGB color space, simply call this |
| 127 | * function with a hsv_color containing the desired color and the index of the | 108 | * function with a hsv_color containing the desired color and the index of the |
| 128 | * led on the LED strip (starting from 0, the first one being the closest the | 109 | * led on the LED strip (starting from 0, the first one being the closest the |
| @@ -131,7 +112,7 @@ void set_led_color_hsv(hsv_color color, int pos){ | |||
| 131 | * Only set the color of the LEDs through the functions given by this API | 112 | * Only set the color of the LEDs through the functions given by this API |
| 132 | * (unless you really know what you are doing) | 113 | * (unless you really know what you are doing) |
| 133 | */ | 114 | */ |
| 134 | void set_led_color_rgb(rgb_color color, int pos){ | 115 | void set_led_color_rgb(LED_TYPE color, int pos){ |
| 135 | for(int j = 0; j < 4; j++) | 116 | for(int j = 0; j < 4; j++) |
| 136 | txbuf[PREAMBLE_SIZE + BYTES_FOR_LED*pos + j] = get_protocol_eq(color.g, j); | 117 | txbuf[PREAMBLE_SIZE + BYTES_FOR_LED*pos + j] = get_protocol_eq(color.g, j); |
| 137 | for(int j = 0; j < 4; j++) | 118 | for(int j = 0; j < 4; j++) |
| @@ -140,18 +121,12 @@ void set_led_color_rgb(rgb_color color, int pos){ | |||
| 140 | txbuf[PREAMBLE_SIZE + BYTES_FOR_LED*pos + BYTES_FOR_LED_BYTE*2+j] = get_protocol_eq(color.b, j); | 121 | txbuf[PREAMBLE_SIZE + BYTES_FOR_LED*pos + BYTES_FOR_LED_BYTE*2+j] = get_protocol_eq(color.b, j); |
| 141 | } | 122 | } |
| 142 | 123 | ||
| 143 | /* | 124 | void set_leds_color_rgb(LED_TYPE color){ |
| 144 | * Same as the two above, but sets all the LEDs in the LED strip (HSV) | ||
| 145 | */ | ||
| 146 | void set_leds_color_hsv(hsv_color color){ | ||
| 147 | for(int i = 0; i < NB_LEDS; i++) | 125 | for(int i = 0; i < NB_LEDS; i++) |
| 148 | set_led_color_hsv(color, i); | 126 | set_led_color_rgb(color, i); |
| 149 | } | 127 | } |
| 150 | 128 | ||
| 151 | /* | 129 | |
| 152 | * Same as the two above, but sets all the LEDs in the LED strip (RGB) | 130 | void ws2812_setleds_rgbw(LED_TYPE *ledarray, uint16_t number_of_leds) { |
| 153 | */ | 131 | |
| 154 | void set_leds_color_rgb(rgb_color color){ | 132 | } |
| 155 | for(int i = 0; i < NB_LEDS; i++) | ||
| 156 | set_led_color_rgb(color, i); | ||
| 157 | } \ No newline at end of file | ||
diff --git a/keyboards/handwired/practice60/ws2812.h b/keyboards/handwired/practice60/ws2812.h new file mode 100644 index 000000000..3b61ddcfa --- /dev/null +++ b/keyboards/handwired/practice60/ws2812.h | |||
| @@ -0,0 +1,20 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include "hal.h" | ||
| 4 | #include "rgblight_types.h" | ||
| 5 | |||
| 6 | |||
| 7 | void set_leds_color_rgb(LED_TYPE color); | ||
| 8 | void set_led_color_rgb(LED_TYPE color, int pos); | ||
| 9 | void leds_init(void); | ||
| 10 | |||
| 11 | |||
| 12 | // This is what users will use to interface with this | ||
| 13 | void ws2812_setleds(LED_TYPE *ledarray, uint16_t number_of_leds); | ||
| 14 | void ws2812_setleds_rgbw(LED_TYPE *ledarray, uint16_t number_of_leds); | ||
| 15 | |||
| 16 | |||
| 17 | void WS2812_init(void); | ||
| 18 | void WS2812_set_color( uint8_t index, uint8_t red, uint8_t green, uint8_t blue ); | ||
| 19 | void WS2812_set_color_all( uint8_t red, uint8_t green, uint8_t blue ); | ||
| 20 | void WS2812_send_colors(void); | ||
diff --git a/quantum/rgblight.c b/quantum/rgblight.c index cf2666eb4..22dce963c 100644 --- a/quantum/rgblight.c +++ b/quantum/rgblight.c | |||
| @@ -19,6 +19,11 @@ | |||
| 19 | #include <avr/eeprom.h> | 19 | #include <avr/eeprom.h> |
| 20 | #include <avr/interrupt.h> | 20 | #include <avr/interrupt.h> |
| 21 | #endif | 21 | #endif |
| 22 | #ifdef STM32_EEPROM_ENABLE | ||
| 23 | #include "hal.h" | ||
| 24 | #include "eeprom.h" | ||
| 25 | #include "eeprom_stm32.h" | ||
| 26 | #endif | ||
| 22 | #include "wait.h" | 27 | #include "wait.h" |
| 23 | #include "progmem.h" | 28 | #include "progmem.h" |
| 24 | #include "timer.h" | 29 | #include "timer.h" |
| @@ -120,14 +125,14 @@ void setrgb(uint8_t r, uint8_t g, uint8_t b, LED_TYPE *led1) { | |||
| 120 | 125 | ||
| 121 | 126 | ||
| 122 | uint32_t eeconfig_read_rgblight(void) { | 127 | uint32_t eeconfig_read_rgblight(void) { |
| 123 | #ifdef __AVR__ | 128 | #if defined(__AVR__) || defined(STM32_EEPROM_ENABLE) || defined(PROTOCOL_ARM_ATSAM) || defined(EEPROM_SIZE) |
| 124 | return eeprom_read_dword(EECONFIG_RGBLIGHT); | 129 | return eeprom_read_dword(EECONFIG_RGBLIGHT); |
| 125 | #else | 130 | #else |
| 126 | return 0; | 131 | return 0; |
| 127 | #endif | 132 | #endif |
| 128 | } | 133 | } |
| 129 | void eeconfig_update_rgblight(uint32_t val) { | 134 | void eeconfig_update_rgblight(uint32_t val) { |
| 130 | #ifdef __AVR__ | 135 | #if defined(__AVR__) || defined(STM32_EEPROM_ENABLE) || defined(PROTOCOL_ARM_ATSAM) || defined(EEPROM_SIZE) |
| 131 | if (eeconfig_read_rgblight() != val) { | 136 | if (eeconfig_read_rgblight() != val) { |
| 132 | eeprom_update_dword(EECONFIG_RGBLIGHT, val); | 137 | eeprom_update_dword(EECONFIG_RGBLIGHT, val); |
| 133 | } | 138 | } |
| @@ -333,7 +338,7 @@ void rgblight_disable_noeeprom(void) { | |||
| 333 | #ifdef RGBLIGHT_USE_TIMER | 338 | #ifdef RGBLIGHT_USE_TIMER |
| 334 | rgblight_timer_disable(); | 339 | rgblight_timer_disable(); |
| 335 | #endif | 340 | #endif |
| 336 | _delay_ms(50); | 341 | wait_ms(50); |
| 337 | rgblight_set(); | 342 | rgblight_set(); |
| 338 | } | 343 | } |
| 339 | 344 | ||
diff --git a/tmk_core/common/chibios/eeprom_stm32.c b/tmk_core/common/chibios/eeprom_stm32.c index a15430d67..17d38af6a 100755 --- a/tmk_core/common/chibios/eeprom_stm32.c +++ b/tmk_core/common/chibios/eeprom_stm32.c | |||
| @@ -75,17 +75,13 @@ uint16_t EEPROM_WriteDataByte (uint16_t Address, uint8_t DataByte) { | |||
| 75 | } | 75 | } |
| 76 | 76 | ||
| 77 | // calculate which page is affected (Pagenum1/Pagenum2...PagenumN) | 77 | // calculate which page is affected (Pagenum1/Pagenum2...PagenumN) |
| 78 | page = (FEE_PAGE_BASE_ADDRESS + FEE_ADDR_OFFSET(Address)) & 0x00000FFF; | 78 | page = FEE_ADDR_OFFSET(Address) / FEE_PAGE_SIZE; |
| 79 | |||
| 80 | if (page % FEE_PAGE_SIZE) page = page + FEE_PAGE_SIZE; | ||
| 81 | page = (page / FEE_PAGE_SIZE) - 1; | ||
| 82 | 79 | ||
| 83 | // if current data is 0xFF, the byte is empty, just overwrite with the new one | 80 | // if current data is 0xFF, the byte is empty, just overwrite with the new one |
| 84 | if ((*(__IO uint16_t*)(FEE_PAGE_BASE_ADDRESS + FEE_ADDR_OFFSET(Address))) == FEE_EMPTY_WORD) { | 81 | if ((*(__IO uint16_t*)(FEE_PAGE_BASE_ADDRESS + FEE_ADDR_OFFSET(Address))) == FEE_EMPTY_WORD) { |
| 85 | 82 | ||
| 86 | FlashStatus = FLASH_ProgramHalfWord(FEE_PAGE_BASE_ADDRESS + FEE_ADDR_OFFSET(Address), (uint16_t)(0x00FF & DataByte)); | 83 | FlashStatus = FLASH_ProgramHalfWord(FEE_PAGE_BASE_ADDRESS + FEE_ADDR_OFFSET(Address), (uint16_t)(0x00FF & DataByte)); |
| 87 | } | 84 | } else { |
| 88 | else { | ||
| 89 | 85 | ||
| 90 | // Copy Page to a buffer | 86 | // Copy Page to a buffer |
| 91 | memcpy(DataBuf, (uint8_t*)FEE_PAGE_BASE_ADDRESS + (page * FEE_PAGE_SIZE), FEE_PAGE_SIZE); // !!! Calculate base address for the desired page | 87 | memcpy(DataBuf, (uint8_t*)FEE_PAGE_BASE_ADDRESS + (page * FEE_PAGE_SIZE), FEE_PAGE_SIZE); // !!! Calculate base address for the desired page |
| @@ -96,18 +92,17 @@ uint16_t EEPROM_WriteDataByte (uint16_t Address, uint8_t DataByte) { | |||
| 96 | } | 92 | } |
| 97 | 93 | ||
| 98 | // manipulate desired data byte in temp data array if new byte is differ to the current | 94 | // manipulate desired data byte in temp data array if new byte is differ to the current |
| 99 | DataBuf[FEE_ADDR_OFFSET(Address)] = DataByte; | 95 | DataBuf[FEE_ADDR_OFFSET(Address) % FEE_PAGE_SIZE] = DataByte; |
| 100 | 96 | ||
| 101 | //Erase Page | 97 | //Erase Page |
| 102 | FlashStatus = FLASH_ErasePage(FEE_PAGE_BASE_ADDRESS + page); | 98 | FlashStatus = FLASH_ErasePage(FEE_PAGE_BASE_ADDRESS + (page * FEE_PAGE_SIZE)); |
| 103 | 99 | ||
| 104 | // Write new data (whole page) to flash if data has beed changed | 100 | // Write new data (whole page) to flash if data has been changed |
| 105 | for(i = 0; i < (FEE_PAGE_SIZE / 2); i++) { | 101 | for(i = 0; i < (FEE_PAGE_SIZE / 2); i++) { |
| 106 | if ((__IO uint16_t)(0xFF00 | DataBuf[FEE_ADDR_OFFSET(i)]) != 0xFFFF) { | 102 | if ((__IO uint16_t)(0xFF00 | DataBuf[FEE_ADDR_OFFSET(i)]) != 0xFFFF) { |
| 107 | FlashStatus = FLASH_ProgramHalfWord((FEE_PAGE_BASE_ADDRESS + (page * FEE_PAGE_SIZE)) + (i * 2), (uint16_t)(0xFF00 | DataBuf[FEE_ADDR_OFFSET(i)])); | 103 | FlashStatus = FLASH_ProgramHalfWord((FEE_PAGE_BASE_ADDRESS + (page * FEE_PAGE_SIZE)) + (i * 2), (uint16_t)(0xFF00 | DataBuf[FEE_ADDR_OFFSET(i)])); |
| 108 | } | 104 | } |
| 109 | } | 105 | } |
| 110 | |||
| 111 | } | 106 | } |
| 112 | return FlashStatus; | 107 | return FlashStatus; |
| 113 | } | 108 | } |
| @@ -168,7 +163,7 @@ void eeprom_update_word (uint16_t *Address, uint16_t Value) | |||
| 168 | uint32_t eeprom_read_dword (const uint32_t *Address) | 163 | uint32_t eeprom_read_dword (const uint32_t *Address) |
| 169 | { | 164 | { |
| 170 | const uint16_t p = (const uint32_t) Address; | 165 | const uint16_t p = (const uint32_t) Address; |
| 171 | return EEPROM_ReadDataByte(p) | (EEPROM_ReadDataByte(p+1) << 8) | 166 | return EEPROM_ReadDataByte(p) | (EEPROM_ReadDataByte(p+1) << 8) |
| 172 | | (EEPROM_ReadDataByte(p+2) << 16) | (EEPROM_ReadDataByte(p+3) << 24); | 167 | | (EEPROM_ReadDataByte(p+2) << 16) | (EEPROM_ReadDataByte(p+3) << 24); |
| 173 | } | 168 | } |
| 174 | 169 | ||
