From 3f6fc7f6029be47c7f5a4f2bca97c7a9bc16bb79 Mon Sep 17 00:00:00 2001 From: Brett Weiland Date: Sat, 2 Jan 2021 01:27:37 -0600 Subject: modified: gradient.h modified: led_gradient.c modified: push.sh deleted: shit.c --- gradient.h | 11 +++ led_gradient.c | 221 ++++++++++++++++++++++++++++++++++++++++----------------- push.sh | 3 +- shit.c | 95 ------------------------- 4 files changed, 167 insertions(+), 163 deletions(-) delete mode 100644 shit.c diff --git a/gradient.h b/gradient.h index e10f16f..a0b9dfe 100644 --- a/gradient.h +++ b/gradient.h @@ -8,6 +8,17 @@ #define MODIFY_SIZE 6 #define EXITED 7 +#define M_RED 0 +#define M_GREEN 1 +#define M_BLUE 2 +#define M_SIZE 3 +#define M_SIZE_EQ 4 +#define M_CREATE_EQ 5 +#define M_CREATE_SUB 6 +#define M_DEL 7 +#define M_BACK 8 + + typedef struct splits { unsigned int color; diff --git a/led_gradient.c b/led_gradient.c index b09708f..a02bca8 100644 --- a/led_gradient.c +++ b/led_gradient.c @@ -9,25 +9,58 @@ #include "led_info.h" #include "gradient.h" +void set_program_state(int *state, int *prev_state, int newstate){ + *prev_state = *state; + *state = newstate; +} + +void redraw_mod_menu(char **mod_menu, int items, int selection){ + clear(); + printw("view mode>select mode>modify mode : segment %i\n\n", selection); + for(int i = 0; i < items; i++) { + cbreak(); + if(selection == i) attron(A_STANDOUT); + printw("%s\n", mod_menu[i]); + if(selection == i) attroff(A_STANDOUT); + } + refresh(); +} + +void redraw_seg_select(int segment) { + clear(); + printw("view mode>select mode : segment %i\n\n", segment); + hline('-', 10); + printw("Press enter to modify segment %i, or [q] to return.\n", segment); + refresh(); +} + +void resize_segments(segment_t *segments, unsigned int segment_amount, ws2811_t *ledstrip) { + for(int i = 0; i < segment_amount; i++) { + segments[i].size = LED_COUNT / segment_amount; + } + ws2811_render(ledstrip); +} + + void refresh_strip(ws2811_t *ledstring, segment_t *segments, unsigned int segment_amount) { unsigned int s = 0; unsigned int led = 0; + unsigned int led_pos = 0; for(; s < segment_amount; s++) { - for(; led < segments[s].size; led++) { - ledstring->channel[0].leds[led] = segments[s].color; + for(led = 0; led < segments[s].size; led++) { + ledstring->channel[0].leds[led_pos] = segments[s].color; + led_pos++; } } - if(led < (LED_COUNT - 1)) { - for(; led < LED_COUNT; led++) { - ledstring->channel[0].leds[led] = segments[s].color; - } + for(led = led_pos; led < LED_COUNT; led++) { + ledstring->channel[0].leds[led] = segments[s - 1].color; } - ws2811_render(ledstring); + 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; + for(int i = 0; i < index; i++) position += segments[i].size; return(position); } @@ -66,31 +99,28 @@ int main() { 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[7] = { + char *mod_menu[9] = { "Change red", "Change green", "Change blue", "Change size", - "Subdivide (create segment)", + "Equalize sizes", + "Create segment and equalize sizes", + "Subdivide segment", "Delete", "Go back" }; - int modification_menu_items = 7; + int mod_items = 9; for(;;) { - //prev_program_state = program_state; + if(prev_program_state != program_state) { + prev_program_state = program_state; switch(program_state) { case MODIFY_MODE: - clear(); - for(i = 0; i < modification_menu_items; i++) { - cbreak(); - if(ui_selection == i) attron(A_STANDOUT); - printw("%s, %i, %i\n", modification_menu[i], ui_selection, user_key); - if(ui_selection == i) attroff(A_STANDOUT); - } - refresh(); - break; + refresh_strip(&ledstring, segments, segment_quanity); + redraw_mod_menu(mod_menu, mod_items, ui_selection); + break; case MODIFY_R: printw("Modifying red value.\n"); refresh_strip(&ledstring, segments, segment_quanity); @@ -114,13 +144,13 @@ int main() { case VIEW_MODE: clear(); refresh_strip(&ledstring, segments, segment_quanity); + printw("view mode\n\n"); printw("%s\n", main_msg); cbreak(); break; case SELECT_MODE: - clear(); - printw("Press enter to modify segment %i, or [q] to return.\n", segment_selection); - halfdelay(10); + redraw_seg_select(segment_selection); + halfdelay(4); break; case EXITED: for(i = 0; i < LED_COUNT; i++) { @@ -142,29 +172,43 @@ int main() { switch(user_key) { case KEY_UP: - case KEY_LEFT: switch(program_state) { case VIEW_MODE: - program_state = SELECT_MODE; + set_program_state(&program_state, &prev_program_state, SELECT_MODE); segment_selection = segment_quanity; break; case SELECT_MODE: - if(segment_selection < segment_quanity) segment_selection++; + if(segment_selection < segment_quanity - 1) segment_selection++; + refresh_strip(&ledstring, segments, segment_quanity); + redraw_seg_select(segment_selection); break; case MODIFY_MODE: - if(ui_selection < modification_menu_items) ui_selection++; + if(ui_selection > 0) ui_selection--; + redraw_mod_menu(mod_menu, mod_items, ui_selection); break; case MODIFY_R: - if ((segments[segment_selection].color >> 16) < 0xff) segments[segment_selection].color += 0x10000; + if ((segments[segment_selection].color >> 16) < 0xff) { + segments[segment_selection].color += 0x10000; + refresh_strip(&ledstring, segments, segment_quanity); + } break; case MODIFY_G: - if ((segments[segment_selection].color >> 8 & 0xff) < 0xff) segments[segment_selection].color += 0x100; + if ((segments[segment_selection].color >> 8 & 0xff) < 0xff) { + segments[segment_selection].color += 0x100; + refresh_strip(&ledstring, segments, segment_quanity); + } break; case MODIFY_B: - if ((segments[segment_selection].color & 0xff) < 0xff) segments[segment_selection].color += 1; + if ((segments[segment_selection].color & 0xff) < 0xff) { + segments[segment_selection].color += 1; + refresh_strip(&ledstring, segments, segment_quanity); + } break; case MODIFY_SIZE: - if(find_starting_pos(segments, segment_quanity - 1) < LED_COUNT - 1) segments[segment_selection].size++; + if(find_starting_pos(segments, segment_quanity - 1) + segments[segment_quanity - 1].size < LED_COUNT - 1) { + segments[segment_selection].size++; + refresh_strip(&ledstring, segments, segment_quanity); + } break; default: break; @@ -172,28 +216,43 @@ int main() { break; case KEY_DOWN: - case KEY_RIGHT: switch(program_state) { case VIEW_MODE: - program_state = SELECT_MODE; + set_program_state(&program_state, &prev_program_state, SELECT_MODE); segment_selection = 0; break; case SELECT_MODE: if(segment_selection > 0) segment_selection--; + refresh_strip(&ledstring, segments, segment_quanity); + redraw_seg_select(segment_selection); break; case MODIFY_MODE: - if(ui_selection > 0) ui_selection--; + if(ui_selection < mod_items - 1) ui_selection++; + redraw_mod_menu(mod_menu, mod_items, ui_selection); + break; case MODIFY_R: - if (!(segments[segment_selection].color >> 16)) segments[segment_selection].color -= 0x10000; + if (segments[segment_selection].color >> 16) { + segments[segment_selection].color -= 0x10000; + refresh_strip(&ledstring, segments, segment_quanity); + } break; case MODIFY_G: - if (!(segments[segment_selection].color >> 8 & 0xff)) segments[segment_selection].color -= 0x100; + if (segments[segment_selection].color >> 8 & 0xff) { + segments[segment_selection].color -= 0x100; + refresh_strip(&ledstring, segments, segment_quanity); + } break; case MODIFY_B: - if (!(segments[segment_selection].color & 0xff)) segments[segment_selection].color -= 1; + if (segments[segment_selection].color & 0xff) { + segments[segment_selection].color -= 1; + refresh_strip(&ledstring, segments, segment_quanity); + } break; case MODIFY_SIZE: - if(segments[segment_selection].size >= 4) segments[segment_selection].size--; + if(segments[segment_selection].size >= 4) { + segments[segment_selection].size--; + refresh_strip(&ledstring, segments, segment_quanity); + } break; default: @@ -205,29 +264,47 @@ int main() { case 10: switch(program_state) { case VIEW_MODE: - program_state = SELECT_MODE; + set_program_state(&program_state, &prev_program_state, SELECT_MODE); break; case SELECT_MODE: - program_state = MODIFY_MODE; + set_program_state(&program_state, &prev_program_state, MODIFY_MODE); break; case MODIFY_MODE: switch(ui_selection) { - case 0: - program_state = MODIFY_R; + case M_RED: + set_program_state(&program_state, &prev_program_state, MODIFY_R); + break; + case M_GREEN: + set_program_state(&program_state, &prev_program_state, MODIFY_G); break; - case 1: - program_state = MODIFY_G; + case M_BLUE: + set_program_state(&program_state, &prev_program_state, MODIFY_B); break; - case 2: - program_state = MODIFY_B; + case M_SIZE: + set_program_state(&program_state, &prev_program_state, MODIFY_SIZE); break; - case 3: - program_state = MODIFY_SIZE; + case M_SIZE_EQ: + resize_segments(segments, segment_quanity, &ledstring); break; - case 4: + + case M_CREATE_EQ: + set_program_state(&program_state, &prev_program_state, MODIFY_MODE); + segments[segment_selection].size = segments[segment_selection].size / 2; + segment_quanity++; + segments = realloc(segments, sizeof(segment_t) * segment_quanity); + memmove(&segments[segment_selection + 2], + &segments[segment_selection] + sizeof(segment_t), + &segments[segment_quanity - 1] - &segments[segment_selection + 1]); + memcpy(&segments[segment_selection + 1], &segments[segment_selection], sizeof(segment_t)); + segment_selection++; + resize_segments(segments, segment_quanity, &ledstring); + + break; + + case M_CREATE_SUB: //create new segment - //TODO: prevent user stupidity - program_state = MODIFY_MODE; + //TODO: account for rounding + set_program_state(&program_state, &prev_program_state, MODIFY_MODE); segments[segment_selection].size = segments[segment_selection].size / 2; segment_quanity++; segments = realloc(segments, sizeof(segment_t) * segment_quanity); @@ -237,15 +314,25 @@ int main() { memcpy(&segments[segment_selection + 1], &segments[segment_selection], sizeof(segment_t)); segment_selection++; break; - case 5: - program_state = VIEW_MODE; - segments[segment_selection + 1].size += segments[segment_selection].size; - memmove(&segments[segment_selection], &segments[segment_selection + 1], &segments[segment_quanity] - &segments[segment_selection + 1]); - segment_quanity--; - segments = realloc(&segments, &segments[segment_quanity] - &segments[0]); - break; - case 6: - program_state = VIEW_MODE; + case M_DEL: //delete code + //TODO: resize + if(segment_quanity - 1 > 0) { + set_program_state(&program_state, &prev_program_state, VIEW_MODE); + if(segment_selection == (segment_quanity - 1)) { + segments[segment_selection - 1].size += segments[segment_selection].size; + } + else { + segments[segment_selection + 1].size += segments[segment_selection].size; + } + memmove(&segments[segment_selection], &segments[segment_selection + 1], &segments[segment_quanity] - &segments[segment_selection + 1]); + segment_quanity--; + segment_selection--; + segments = realloc(segments, &segments[segment_quanity] - &segments[0]); + refresh(); + break; + } + case M_BACK: + set_program_state(&program_state, &prev_program_state, VIEW_MODE); break; default: break; @@ -255,7 +342,7 @@ int main() { case MODIFY_G: case MODIFY_B: case MODIFY_SIZE: - program_state = MODIFY_MODE; + set_program_state(&program_state, &prev_program_state, MODIFY_MODE); break; default: break; @@ -265,19 +352,19 @@ int main() { case 'q': switch(program_state) { case VIEW_MODE: - program_state = EXITED; + set_program_state(&program_state, &prev_program_state, EXITED); break; case SELECT_MODE: - program_state = VIEW_MODE; + set_program_state(&program_state, &prev_program_state, VIEW_MODE); break; case MODIFY_MODE: - program_state = SELECT_MODE; + set_program_state(&program_state, &prev_program_state, SELECT_MODE); break; case MODIFY_R: case MODIFY_G: case MODIFY_B: case MODIFY_SIZE: - program_state = MODIFY_MODE; + set_program_state(&program_state, &prev_program_state, MODIFY_MODE); default: break; } @@ -287,7 +374,7 @@ int main() { break; } } - else if(program_state = SELECT_MODE) { + 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; diff --git a/push.sh b/push.sh index a2ee8ae..9db1164 100755 --- a/push.sh +++ b/push.sh @@ -1,2 +1,3 @@ #!/usr/bin/bash -printf "rm /root/projects/ledstrip/*\nput /home/indigo/projects/ledstrip/* /root/projects/ledstrip/" | sftp root@ledcontroller +printf "put /home/indigo/projects/ledstrip/* /root/projects/ledstrip/" | sftp root@ledcontroller +ssh root@ledcontroller "cd /root/projects/ledstrip/; rm grad; make grad" diff --git a/shit.c b/shit.c deleted file mode 100644 index cca992b..0000000 --- a/shit.c +++ /dev/null @@ -1,95 +0,0 @@ -#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