From 4a5bf47fbccd3bae82c705831018c889ca4d7016 Mon Sep 17 00:00:00 2001 From: joakim1999 Date: Fri, 12 May 2017 13:33:24 +0200 Subject: [PATCH] update work (#6) * Translate and extend getNeighborLED * Rotate function This is so stupid, but we felt pretty clever making it. Pls no haet * Fix led selection to be side independant would only rotate the first side of the cube before! * debrilliantized rotate function Keep it simple stupid * Math is unused * Add missing equal sign That could have screwed up some things dude * Delete semicolon creating error It was a misplaced semicolon * Update work, pair programming yeee --- School/vg2/borg/borg.h | 142 ++++++++++++++++++++++++++------ School/vg2/borg/demos/rubix.ino | 39 ++++++++- 2 files changed, 153 insertions(+), 28 deletions(-) diff --git a/School/vg2/borg/borg.h b/School/vg2/borg/borg.h index 85ebe34..5a003ef 100644 --- a/School/vg2/borg/borg.h +++ b/School/vg2/borg/borg.h @@ -32,9 +32,20 @@ byte ROTTODIR[6][6]; // Initializing mapping for directions void initMap(void); -// Decodes side, column, row into n LED -int decodeLED(LEDSelect selection); +// Encodes n led into a LEDSelect void encodeLED(int n, LEDSelect* Result); +// Decodes side, column, row into n LED +inline int decodeLED(LEDSelect selection); +// Compares two LEDSelects, returns true if equal, false if unequal +inline bool LEDSelectCmp(LEDSelect a, LEDSelect b); + + +// Moves pixel on single screen, edits selection. +void forceMove(LEDSelect* selection, byte direction); +//Gets neighbor led with sky directions, nice for single screen movement, but can also handle cross screen. +void getNeighborLED(LEDSelect* origin, byte origin_dir, LEDSelect* Result); +// Gets neighbor led with set directions in reference to side 0, lets you move in one direction with one vector. +void getRotNeighborLED(LEDSelect* origin, byte rot, LEDSelect* Result); /* ** Set Color @@ -45,18 +56,16 @@ void encodeLED(int n, LEDSelect* Result); bool setColor(LEDSelect selection, CRGB color, CRGB* leds); // Sets every color that isn't black bool updateColors(LEDSelect selection, CRGB* leds); -// Mirrors one side to every other side -bool mirror(byte side, CRGB* leds); -//Prints large X in a given color -void printError(CRGB color, CRGB* leds); -//Gets neighbor led with sky directions, nice for single screen movement, but can also handle cross screen. -void getNeighborLED(LEDSelect* origin, byte origin_dir, LEDSelect* Result); -// Gets neighbor led with set directions in reference to side 0, lets you move in one direction with one vector. -void getRotNeighborLED(LEDSelect* origin, byte rot, LEDSelect* Result); -// +// Copies colors from one LEDSelect to another LEDSelect of the same size void translate(LEDSelect src, LEDSelect dst, CRGB* leds); // Rotates a side, in given direction, 0 - Clockwise, 1 - Anticlockwise void rotate(byte side, bool dir, byte n, CRGB* leds); +// Mirrors one side to every other side +bool mirror(byte side, CRGB* leds); + +//Prints large X in a given color +void printError(CRGB color, CRGB* leds); + void initMap(void) { @@ -132,13 +141,6 @@ void initMap(void) ROTTODIR[5][LEFT] = 255; ROTTODIR[5][CLOCKW] = WEST; ROTTODIR[5][ACLOCKW] = EAST; - - -} - -int decodeLED(LEDSelect selection) -{ - return 9 * selection.side + 3 * selection.row + selection.column; } void encodeLED(int n, LEDSelect* Result) @@ -148,6 +150,15 @@ void encodeLED(int n, LEDSelect* Result) Result->row = n % 9 % 3; } +inline int decodeLED(LEDSelect selection) +{ + return 9 * selection.side + 3 * selection.row + selection.column; +} + +inline bool LEDSelectCmp(LEDSelect a, LEDSelect b) +{ + return a.side != b.side || a.column !=b.column || a.row != b.row; +} void forceMove(LEDSelect* selection, byte direction) { @@ -382,7 +393,10 @@ void rotate(byte side, bool direction, byte n, CRGB* leds) bool setColor(LEDSelect selection, CRGB color, CRGB* leds) { if (selection.side == 255) { - return false; + for (byte n = 0; n < 9 * 6; n++) { + leds[n] = color; + } + return true; } if (selection.column == 255 && selection.row == 255) { for (byte n = 0; n < 9; n++) { @@ -411,7 +425,7 @@ bool setColor(LEDSelect selection, CRGB color, CRGB* leds) } } - +// remake? kind of an useless function at the moment bool updateColors(LEDSelect selection, CRGB color, CRGB* leds) { if (selection.side == 255) { @@ -420,7 +434,7 @@ bool updateColors(LEDSelect selection, CRGB color, CRGB* leds) if (selection.column == 255 && selection.row == 255) { - for (int n = 0; n < 9; n++) { + for (byte n = 0; n < 9; n++) { CRGB* led = &leds[selection.side * 9 + n]; if (*led != (CRGB) 0x000000) { *led = color; @@ -429,7 +443,7 @@ bool updateColors(LEDSelect selection, CRGB color, CRGB* leds) return true; } else if (selection.column == 255 && selection.row != 255) { - for (int n = 0; n < 3; n++) { + for (byte n = 0; n < 3; n++) { CRGB* led = &leds[decodeLED({ selection.side, n, selection.row })]; if (*led != (CRGB)0x000000) { *led = color; @@ -438,7 +452,7 @@ bool updateColors(LEDSelect selection, CRGB color, CRGB* leds) return true; } else if (selection.column != 255 && selection.row == 255) { - for (int n = 0; n < 3; n++) { + for (byte n = 0; n < 3; n++) { CRGB* led = &leds[decodeLED({ selection.side, selection.column, n })]; if (*led != (CRGB) 0x000000) { *led = color; @@ -458,13 +472,91 @@ bool updateColors(LEDSelect selection, CRGB color, CRGB* leds) } } +void translate(LEDSelect src, LEDSelect dst, CRGB* leds){ + byte side = src.side; + byte column = src.column; + byte row = src.row; + + if(column == 255 && row == 255 && + dst.column == 255 && dst.row == 255){ + src.column = 0; + src.row = 0; + + dst.column = 0; + dst.row = 0; + + for(int i = 0; i < 9; i++){ + leds[decodeLED(dst) + i] = leds[decodeLED(src) + i]; + } + return; + } + else if(column != 255 && row == 255 && + dst.column != 255 && dst.row == 255){ + src.row = 0; + dst.row = 0; + + for(int i = 0; i < 9; i += 3){ + leds[decodeLED(dst) + i] = leds[decodeLED(src) + i]; + } + return; + } + else if(column == 255 && row != 255 && dst.column == 255 && dst.row != 255){ + src.column = 0; + dst.column = 0; + + for(int i = 0; i < 3; i++){ + leds[decodeLED(dst) + i] = leds[decodeLED(src) + i]; + } + return; + } + + leds[decodeLED(dst)] = leds[decodeLED(src)]; +} + +void rotate(byte side, bool direction, byte n, CRGB* leds) +{ + int firstLED = decodeLED({side, 0, 0}); + byte cyclus[8] = {0, 3, 6, 7, 8, 5, 2, 1}; + for (byte i = 0; i < n; i++) + { + if (direction == 0) { + CRGB Saved = leds[cyclus[0] + firstLED]; + for(byte j = 0; j < 7; j++) { + leds[cyclus[j] + firstLED] = leds[cyclus[j + 1] + firstLED]; + } + leds[cyclus[7] + firstLED] = Saved; + } + else { + CRGB Saved = leds[cyclus[0] + firstLED]; + for(byte j = 7; j > 0; j--) { + leds[cyclus[j] + firstLED] = leds[cyclus[j - 1] + firstLED]; + } + leds[cyclus[1] + firstLED] = Saved; + } + } +} + +void new_rotaterot(LEDSelect selection, byte dir, byte n, CRGB* leds) +{ + for (byte i = 0; i < n; i++) { + LEDSelect head[2] = {selection, selection}; + CRGB saved = leds[decodeLED(head[0])]; + do { + getRotNeighborLED(&head[1], (dir > 3) ? 4 + !(dir - 4) : (dir + 2) % 4, &head[0]); + leds[decodeLED(head[1])] = leds[decodeLED(head[0])]; + } + while (!LEDSelectCmp(head[0], selection)); + leds[decodeLED(head[1])] = saved; + } +} + bool mirror(byte side, CRGB* leds) { /*TODO: figure out memory structure, copy the nine leds to the the different memory parts, so it the text is displayed on all sides. should probably use some form of modulo */ - for(int i = 0; i < sizeof(leds) / sizeof(CRGB); i+=9) { + for(int i = 0; i < sizeof(leds) / sizeof(CRGB); i += 9) { memcpy(&leds[i], &leds[decodeLED({side, 0, 0})], sizeof(CRGB) * 9); } } @@ -479,4 +571,4 @@ void printError(CRGB color, CRGB* leds) mirror(0, leds); } -#endif +#endif \ No newline at end of file diff --git a/School/vg2/borg/demos/rubix.ino b/School/vg2/borg/demos/rubix.ino index 1c1a50d..e38cf62 100644 --- a/School/vg2/borg/demos/rubix.ino +++ b/School/vg2/borg/demos/rubix.ino @@ -8,16 +8,27 @@ #define BLUE (CRGB) 0x0000FF #define RED (CRGB) 0xFF0000 #define GREEN (CRGB) 0x00FF00 -#define ORANGE (CRGB) 0xFF8C00 +#define ORANGE (CRGB) 0xFF7000 #define WHITE (CRGB) 0xFFFFFF #define YELLOW (CRGB) 0xFFFF00 CRGB leds[NUM_LEDS]; CRGB solution[NUM_LEDS]; +byte METAROTTODIR[6]; + +void rubixRot(byte side, bool dir); void setup() { FastLED.addLeds(leds, NUM_LEDS); initMap(); + + METAROTTODIR[0]=CLOCKW; + METAROTTODIR[1]=UP; + METAROTTODIR[2]=ACLOCKW; + METAROTTODIR[3]=DOWN; + METAROTTODIR[4]=LEFT; + METAROTTODIR[5]=RIGHT; + randomSeed(analogRead(0)); FastLED.clear(); FastLED.show(); @@ -72,7 +83,29 @@ void setup() { FastLED.show(); } -void loop() { - // put your main code here, to run repeatedly: +void loop() { + delay(300); + rubixRot(random(0, 7), random(0, 1)); + if (memcmp(leds, solution, sizeof(CRGB) * NUM_LEDS) == 0) { + while(true) { + setColor({255, 255, 255}, (CRGB) 0x0, leds); + delay(200); + setColor({255, 255, 255}, GREEN, leds); + delay(200); + } + } +} + +void rubixRot(byte side, bool dir){ + rotate(side, dir, 2, leds); + LEDSelect origin = {side, 0, 0}; + getNeighborLED(&origin, NORTH, &origin); + if(dir == 0){ + new_rotaterot(origin, METAROTTODIR[side], 3, leds); + }else{ + for(byte i = 0; i < 3; i++){ + rubixRot(side,0); + } + } }