From e653cc198e3a535f23ae33d677115f192979ee55 Mon Sep 17 00:00:00 2001 From: Andrew Kannan Date: Mon, 12 Nov 2018 19:31:22 -0500 Subject: The "Practice60" Board, Blue Pill powered (#4407) * Starting point for blue pill based practice60 * Changes * add * try raw bin no botloader * swap back to bootloader version * edit * Remove debug LED flash * Disable JTAG to open up B3 and B4 * Add led backlight support (no breathing yet) * Update matrix for correctness * RGB Underglow working in a very simple state * not as bright * Move to handwired * revert ChibiOS_Test changes * Changes based on PR comments * Address PR comments v2 * Move files --- keyboards/handwired/practice60/underglow.c | 157 +++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 keyboards/handwired/practice60/underglow.c (limited to 'keyboards/handwired/practice60/underglow.c') diff --git a/keyboards/handwired/practice60/underglow.c b/keyboards/handwired/practice60/underglow.c new file mode 100644 index 000000000..1383af189 --- /dev/null +++ b/keyboards/handwired/practice60/underglow.c @@ -0,0 +1,157 @@ +#include "ch.h" +#include "hal.h" + +#include "hsv2rgb.h" +#include "underglow.h" + +#define BYTES_FOR_LED_BYTE 4 +#define NB_COLORS 3 +#define BYTES_FOR_LED BYTES_FOR_LED_BYTE*NB_COLORS +#define DATA_SIZE BYTES_FOR_LED*NB_LEDS +#define RESET_SIZE 200 +#define PREAMBLE_SIZE 4 + +// Define the spi your LEDs are plugged to here +#define LEDS_SPI SPID2 +// Define the number of LEDs you wish to control in your LED strip +#define NB_LEDS 8 + +#define LED_SPIRAL 1 + +static uint8_t txbuf[PREAMBLE_SIZE + DATA_SIZE + RESET_SIZE]; +static uint8_t get_protocol_eq(uint8_t data, int pos); + +/* + * This lib is meant to be used asynchronously, thus the colors contained in + * the txbuf will be sent in loop, so that the colors are always the ones you + * put in the table (the user thus have less to worry about) + * + * Since the data are sent via DMA, and the call to spiSend is a blocking one, + * the processor ressources are not used to much, if you see your program being + * too slow, simply add a: + * chThdSleepMilliseconds(x); + * after the spiSend, where you increment x untill you are satisfied with your + * program speed, another trick may be to lower this thread priority : your call + */ +static THD_WORKING_AREA(LEDS_THREAD_WA, 128); +static THD_FUNCTION(ledsThread, arg) { + (void) arg; + while(1){ + spiSend(&LEDS_SPI, PREAMBLE_SIZE + DATA_SIZE + RESET_SIZE, txbuf); + } +} + +#if LED_SPIRAL +/* + * 'Led spiral' is a simple demo in which we put all the leds to the same + * color, where this color does all the hsv circle in loop. + * If you want to launch the thread that will chage the led colors to the + * appropriate value, simply set LED_SPIRAL to 1. + */ +static THD_WORKING_AREA(HSVTRANS_WA, 128); +static THD_FUNCTION(hsv_transThread, arg){ + (void) arg; + hsv_color color = {0, 255, 127}; + while(1){ + color.h += 1; + color.h %= 256; + set_leds_color_hsv(color); + chThdSleepMilliseconds(50); + } +} +#endif + +static const SPIConfig spicfg = { + NULL, + GPIOB, + 15, + SPI_CR1_BR_1|SPI_CR1_BR_0 // baudrate : fpclk / 8 => 1tick is 0.32us +}; + +/* + * Function used to initialize the driver. + * + * Starts by shutting off all the LEDs. + * Then gets access on the LED_SPI driver. + * May eventually launch an animation on the LEDs (e.g. a thread setting the + * txbuff values) + */ +void leds_init(void){ + for(int i = 0; i < RESET_SIZE; i++) + txbuf[DATA_SIZE+i] = 0x00; + for (int i=0; i