From 3ad1adc596a9382eb6e7d623fdc91e6dc9977add Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Thu, 8 Jun 2017 16:25:01 +0200 Subject: [PATCH] display fixes and improved neopixels dimming --- CMakeLists.txt | 2 +- display.c | 4 ++++ display.h | 1 + leds.c | 10 ++++++++++ leds.h | 38 ++++++++++++++++++++++++++++++++++++++ lib/wsrgb.c | 2 ++ main.c | 13 ++++++++----- 7 files changed, 64 insertions(+), 6 deletions(-) create mode 100644 leds.c create mode 100644 leds.h diff --git a/CMakeLists.txt b/CMakeLists.txt index b7411e7..aac0c63 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,7 +25,7 @@ set(SOURCE_FILES lib/wsrgb.h pinout.h display.c - display.h) + display.h leds.c leds.h) include_directories(lib /usr/avr/include/) diff --git a/display.c b/display.c index 317fe0e..81f50f6 100644 --- a/display.c +++ b/display.c @@ -13,8 +13,12 @@ const uint8_t disp_digits[10] = { DIGIT_0, DIGIT_1, DIGIT_2, DIGIT_3, DIGIT_4, DIGIT_5, DIGIT_6, DIGIT_7, DIGIT_8, DIGIT_9 }; +volatile uint8_t disp_brightness; + void display_show(uint8_t dig0, uint8_t dig1) { + spi_send(0); + spi_send(0); spi_send(dig1); spi_send(dig0); pin_down(PIN_DISP_STR); diff --git a/display.h b/display.h index 81c7960..4f3b7cd 100644 --- a/display.h +++ b/display.h @@ -28,6 +28,7 @@ #define DIGIT_9 (SEG_A|SEG_B|SEG_C|SEG_D|SEG_F|SEG_G) extern const uint8_t disp_digits[10]; +extern volatile uint8_t disp_brightness; // place to globally store display brightness void display_show(uint8_t dig0, uint8_t dig1); void display_show_number(uint8_t num); diff --git a/leds.c b/leds.c new file mode 100644 index 0000000..6d27f20 --- /dev/null +++ b/leds.c @@ -0,0 +1,10 @@ +// +// Created by MightyPork on 2017/06/08. +// + +#include "lib/color.h" +#include "leds.h" + +void leds_set(uint32_t L1, uint32_t L2, uint32_t L3, uint32_t L4); + +void leds_show(void); diff --git a/leds.h b/leds.h new file mode 100644 index 0000000..f75c7ac --- /dev/null +++ b/leds.h @@ -0,0 +1,38 @@ +// +// Created by MightyPork on 2017/06/08. +// + +#include +#include "lib/color.h" +#include "lib/wsrgb.h" +#include "display.h" +#include + +#ifndef FIRMWARE_LEDS_H +#define FIRMWARE_LEDS_H + +uint32_t leds[4]; + +void leds_set(uint32_t L1, uint32_t L2, uint32_t L3, uint32_t L4) +{ + leds[0] = L1; + leds[1] = L2; + leds[2] = L3; + leds[3] = L4; +} + +void leds_show(void) +{ + xrgb_t arr[4]; + for (uint8_t i = 0; i < 4; i++) { + float db = (float)disp_brightness / 255.0f; + arr[i].r = (uint8_t) ((float)rgb24_r(leds[i]) * db); + arr[i].g = (uint8_t) ((float)rgb24_g(leds[i]) * db); + arr[i].b = (uint8_t) ((float)rgb24_b(leds[i]) * db); + } + + ws_send_xrgb_array(arr, 4); + //ws_send_rgb24_array(leds, 4); +} + +#endif //FIRMWARE_LEDS_H diff --git a/lib/wsrgb.c b/lib/wsrgb.c index bd66e69..554beca 100644 --- a/lib/wsrgb.c +++ b/lib/wsrgb.c @@ -72,12 +72,14 @@ void ws_send_rgb24(rgb24_t rgb) /** Send array of colors */ void ws_send_xrgb_array(const xrgb_t rgbs[], const uint8_t length) { + cli(); for (uint8_t i = 0; i < length; i++) { const xrgb_t c = rgbs[i]; ws_send_byte(c.g); ws_send_byte(c.r); ws_send_byte(c.b); } + sei(); } /** Send array of colors */ diff --git a/main.c b/main.c index 9a93168..645e48e 100644 --- a/main.c +++ b/main.c @@ -16,6 +16,7 @@ #include "pinout.h" #include "display.h" +#include "leds.h" /** * Configure pins @@ -24,6 +25,7 @@ void setup_io(void) { as_output(PIN_DISP_CP); as_output(PIN_DISP_D); + pin_up(PIN_DISP_STR); as_output(PIN_DISP_STR); as_output(PIN_DISP_OE); @@ -43,7 +45,7 @@ void setup_io(void) } // --- LED display brightness control --- -volatile uint8_t disp_brightness; +// disp brightness defined in display.h #define LIGHT_ADC_CHANNEL 6 /** @@ -141,6 +143,8 @@ ISR(TIMER1_COMPA_vect) // shut down pin_down(PIN_PWR_HOLD); } + + leds_show(); } @@ -163,7 +167,7 @@ void main() // SPI conf // TODO verify the cpha and cpol. those seem to work, but it's a guess - spi_init_master(SPI_LSB_FIRST, CPOL_1, CPHA_0, SPI_DIV_2); + spi_init_master(SPI_LSB_FIRST, CPOL_1, CPHA_0, SPI_DIV_4); adc_init(ADC_PRESC_128); setup_pwm(); @@ -177,7 +181,7 @@ void main() // globally enable interrupts sei(); - uint32_t pixels[4] = {0xFF0000, 0xFFFF00, 0x00FF00, 0x0000FF}; + leds_set(0xFFFF00, 0x00FF00, 0x0000FF, 0xFF0000); uint8_t cnt = 0; @@ -187,8 +191,7 @@ void main() cnt++; cnt = cnt % 100; - ws_send_rgb24_array(pixels, 4); - _delay_ms(300); + _delay_ms(150); sprintf(buf, "BRT = %d\r\n", disp_brightness); usart_puts(buf);