From c8b3250f15ac75d60991b3ad2562209e853cc6f4 Mon Sep 17 00:00:00 2001 From: Brett Weiland Date: Fri, 1 Jan 2021 13:12:17 -0600 Subject: new file: gradient.h new file: led_clear.c new file: led_gradient.c new file: led_info.h new file: led_test.c new file: makefile new file: push.sh new file: shit.c --- gradient.h | 14 ++++ led_clear.c | 19 +++++ led_gradient.c | 214 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ led_info.h | 25 +++++++ led_test.c | 28 ++++++++ makefile | 6 ++ push.sh | 2 + shit.c | 95 +++++++++++++++++++++++++ 8 files changed, 403 insertions(+) create mode 100644 gradient.h create mode 100644 led_clear.c create mode 100644 led_gradient.c create mode 100644 led_info.h create mode 100644 led_test.c create mode 100644 makefile create mode 100755 push.sh create mode 100644 shit.c diff --git a/gradient.h b/gradient.h new file mode 100644 index 0000000..ea8635e --- /dev/null +++ b/gradient.h @@ -0,0 +1,14 @@ +//program modes (I'll switch to an emum once I'm done) +#define VIEW_MODE 0 +#define SELECT_MODE 1 +#define MODIFY_MODE 2 +#define MODIFY_R 3 +#define MODIFY_G 4 +#define MODIFY_B 5 +#define MODIFY_SIZE 6 +#define EXITED 7 + +typedef struct splits { + unsigned int color; + unsigned int size; +} segment_t; diff --git a/led_clear.c b/led_clear.c new file mode 100644 index 0000000..f723f2e --- /dev/null +++ b/led_clear.c @@ -0,0 +1,19 @@ +#include +#include + +#include "led_info.h" + +int main() { + printf("Clearing LED lights...\n"); + ws2811_return_t ret; + if ((ret = ws2811_init(&ledstring)) != WS2811_SUCCESS) { + printf("Error initilizing ws8211 strip: %s\n", ws2811_get_return_t_str(ret)); + return 1; + } + for(int i = 0; i < LED_COUNT; i++) { + ledstring.channel[0].leds[i] = 0; + } + ws2811_render(&ledstring); + ws2811_fini(&ledstring); + return 0; +} diff --git a/led_gradient.c b/led_gradient.c new file mode 100644 index 0000000..40c3fc5 --- /dev/null +++ b/led_gradient.c @@ -0,0 +1,214 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "led_info.h" +#include "gradient.h" + +void refresh_strip(ws2811_t *ledstring, segment_t *segments, unsigned int segment_amount) { + unsigned int s = 0; + unsigned int led; + for(; s < segment_amount; s++) { + for(; led < segments[s].size; led++) { + ledstring->channel[0].leds[led] = segments[s].color; + } + } + if(led < (LED_COUNT - 1)) { + for(; led < LED_COUNT; led++) { + ledstring->channel[0].leds[led] = segments[s].color; + } + } + ws2811_render(ledstring); +} + +unsigned int find_starting_pos(segment_t *segments, unsigned int index) { + unsigned int position = 0; + for(int i = 0; i < position; i++) position += segments[i].size; + return(position); +} + +int main() { + + segment_t *segments = malloc(sizeof(segment_t)); //just start with one + segments[0].size = LED_COUNT - 1; + + int prev_program_state = VIEW_MODE; + int program_state = VIEW_MODE; + int ui_selection = 0; + unsigned int i; + unsigned int starting_pos; + unsigned int segment_selection = 0; + unsigned int segment_quanity = 1; + + + + int selection_state = 0; //just for asthetics + + int user_key; + + char *main_msg = "Press the arrow keys to select the segment, and enter to edit it.\nTo create new segments, select the segment you want to divide.\n"; + char *modification_menu[6] = { + "Change red", + "Change green", + "Change blue", + "Change size", + "Subdivide (create segment)", + "Go back" + }; + int modification_menu_items = 6; + + for(;;) { + if(prev_program_state != program_state) { + switch(program_state) { + case MODIFY_MODE: + for(i = 0; i < modification_menu_items; i++) { + cbreak(); + if(ui_selection = i) attron(A_STANDOUT); + printw("%s\n", modification_menu); + if(ui_selection = i) attroff(A_STANDOUT); + } + refresh(); + break; + case MODIFY_R: + printw("Modifying red value.\n"); + refresh(); + break; + case MODIFY_G: + printw("Modifying green value.\n"); + refresh(); + break; + case MODIFY_B: + printw("Modifying blue value.\n"); + refresh(); + break; + case MODIFY_SIZE: + printw("Modifying size.\n"); + refresh(); + break; + case VIEW_MODE: + cbreak(); + break; + case SELECT_MODE: + halfdelay(1); + break; + case EXITED: + for(i = 0; i < LED_COUNT; i++) { + ledstring.channel[0].leds[i] = 0; + } + ws2811_render(&ledstring); + ws2811_fini(&ledstring); + clear(); + endwin(); + printf("goodbye\n"); + return 0; + break; + default: + break; + } + } + + if((user_key = getch()) != ERR) { + switch(user_key) { + + case KEY_LEFT: + switch(program_state) { + case VIEW_MODE: + program_state = SELECT_MODE; + segment_selection = segment_quanity; + break; + case SELECT_MODE: + if(segment_selection < segment_quanity) segment_selection++; + break; + default: + break; + } + break; + + case KEY_RIGHT: + switch(program_state) { + case VIEW_MODE: + program_state = SELECT_MODE; + segment_selection = 0; + break; + case SELECT_MODE: + if(segment_selection > 0) segment_selection--; + break; + default: + break; + } + break; + + case KEY_UP: + switch(program_state) { + case MODIFY_R: + if ((segments[segment_selection].color >> 16) < 0xff) segments[segment_selection].color += 0x10000; + break; + case MODIFY_G: + if ((segments[segment_selection].color >> 8 & 0xff) < 0xff) segments[segment_selection].color += 0x100; + break; + case MODIFY_B: + if ((segments[segment_selection].color & 0xff) < 0xff) segments[segment_selection].color += 1; + break; + case MODIFY_SIZE: + default: + break; + } + break; + + case KEY_DOWN: + switch(program_state) { + default: + break; + } + break; + + case KEY_ENTER: + switch(program_state) { + case VIEW_MODE: + program_state = SELECT_MODE; + default: + break; + } + break; + + case 'q': + switch(program_state) { + case VIEW_MODE: + program_state = EXITED; + break; + case SELECT_MODE: + program_state = VIEW_MODE; + break; + case MODIFY_MODE: + program_state = SELECT_MODE; + break; + case MODIFY_R: + case MODIFY_G: + case MODIFY_B: + case MODIFY_SIZE: + program_state = MODIFY_MODE; + default: + break; + } + break; + + default: + break; + } + prev_program_state = program_state; + } + else if(program_state = SELECT_MODE) { + starting_pos = find_starting_pos(segments, segment_selection); + for(i = starting_pos; i <= starting_pos + segments[segment_selection].size; i++) { + ledstring.channel[0].leds[i] = (selection_state ? (i % 2) : ((i+1) % 2)) ? 0xffffff : 0; + } + selection_state ? 0 : 1; + ws2811_render(&ledstring); + } + } + return 0; +} diff --git a/led_info.h b/led_info.h new file mode 100644 index 0000000..bfa5b0c --- /dev/null +++ b/led_info.h @@ -0,0 +1,25 @@ +#include +#define GPIO_PIN 18 +#define LED_COUNT 300 +#define STRIP_TYPE WS2812_STRIP +#define DMA 10 + +ws2811_t ledstring = { +.freq = WS2811_TARGET_FREQ, +.dmanum = DMA, +.channel = { +[0] = { + .gpionum = GPIO_PIN, + .count = LED_COUNT, + .invert = 0, + .brightness = 255, + .strip_type = STRIP_TYPE, +}, +[1] = { + .gpionum = 0, + .count = 0, + .invert = 0, + .brightness = 0, + }, + }, +}; diff --git a/led_test.c b/led_test.c new file mode 100644 index 0000000..e5aa1f2 --- /dev/null +++ b/led_test.c @@ -0,0 +1,28 @@ +#include +#include +#include +#include + +#include "led_info.h" + +int main() { + ws2811_return_t ret; + if ((ret = ws2811_init(&ledstring)) != WS2811_SUCCESS) { + printf("Error initilizing ws2811 strip: %s\n", ws2811_get_return_t_str(ret)); + return 1; + } + for(;;) { + for(int i = 0; i < LED_COUNT; i++) { + ledstring.channel[0].leds[i] = 0xff0000; + ws2811_render(&ledstring); + } + for(int i = 0; i < LED_COUNT; i++) { + ledstring.channel[0].leds[i] = 0x00ff00; + ws2811_render(&ledstring); + } + for(int i = 0; i < LED_COUNT; i++) { + ledstring.channel[0].leds[i] = 0x0000ff; + ws2811_render(&ledstring); + } + } +} diff --git a/makefile b/makefile new file mode 100644 index 0000000..0591691 --- /dev/null +++ b/makefile @@ -0,0 +1,6 @@ +make tester: + gcc -o led_test led_test.c -lws2811 -lm +make clear: + gcc -o led_clear led_clear.c -lws2811 -lm +make grad: + gcc -o grad led_gradient.c -lws2811 -lm -lncurses diff --git a/push.sh b/push.sh new file mode 100755 index 0000000..a2ee8ae --- /dev/null +++ b/push.sh @@ -0,0 +1,2 @@ +#!/usr/bin/bash +printf "rm /root/projects/ledstrip/*\nput /home/indigo/projects/ledstrip/* /root/projects/ledstrip/" | sftp root@ledcontroller diff --git a/shit.c b/shit.c new file mode 100644 index 0000000..cca992b --- /dev/null +++ b/shit.c @@ -0,0 +1,95 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "led_info.h" +#include "gradient.h" + + +void refresh_strip(ws2811_t *ledstring, split_t *splits, unsigned int split_amount) { + unsigned int on_led = 0; + for(unsigned int i = split_amount; i < split_amount; i++) { + for(; on_led < splits[i].size; on_led++) { + ledstring->channel[0].leds[on_led] = splits[i].color; + } + } + ws2811_render(&ledstring); +} + +unsigned int find_starting_pos(split_t *split, unsigned int index) { + unsigned int position = 0; + for(int i = 0; i < position; i++) position += split[i].size; + return(position); +} + +int main() { + split_t *splits = malloc(sizeof(split_t) * 1); + memcpy(splits, 0, sizeof(split_t)); + unsigned int split_amount = 1; + + int exited = 1; + int selection_mode = 0; + int section_highlighted = 0; + char usr_chr; + unsigned int selected_split = 0; + int selection_state = 0; + int ui_selection = 0; + unsigned int i = 0; + int modification_mode = 0; + + + char *usage_msg = "Press [n] to create a new split.\nSelect your section by pressing the arrow keys, and press enter to modify.\n"; + char *newsplit_msg = "Press the arrow keys to select an option, and enter to modify.\nPress escape once done configuring.\n"; + + char *ui_segment_options[5] = {"Red", "Green", "Blue", "Subdivide", "Delete"}; + + ws2811_return_t ret; + + if ((ret = ws2811_init(&ledstring)) != WS2811_SUCCESS) { + printf("Error initilizing ws2811 strip: %s\n", ws2811_get_return_t_str(ret)); + return 1; + } + + initscr(); + cbreak(); + noecho(); + + + while(!exited) { + clear(); + wprintf("%s", usage_msg); + if (selection_mode) { + halfdelay(10); + wprintf("Segment %i selected.\nPress enter to modify.", selected_split); + } + else if (modification_mode) { + cbreak(); + wprintf("Segment %i selected.\nPress the arrow keys to navigate, and press enter on the action or value you wish to modify."); + } + else { + cbreak(); + } + refresh(); + + printw("%s", usage_msg); + if((usr_chr = getch()) != ERR) { + switch(getch()) { + } + + } + else { + // selection_mode is set, we've timed out + // TODO: test to see if were actually timed out and handle error approprietly + for(i = find_starting_pos(splits, selection); i < split[selection].size; i++) { + ledstring.channel[0].leds[i] = (selection_state ? (i % 2) : ((i+1) % 2)) ? 0xffffff : 0; //what the fuck is wrong with me + } + selection_state ? 0 : 1; + ws2811_render(&ledstring); + } + + return 0; +} -- cgit v1.2.3