Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Started work a simple scrolling text bit-mask idea. Currently still b…

…rainstorming.
  • Loading branch information...
commit d6c15480c225ef174d401716b21d743a3cf319ee 1 parent fc69471
Paul authored
Showing with 205 additions and 0 deletions.
  1. +129 −0 PaulTest/PaulTest.pde
  2. +76 −0 PaulTest/README
View
129 PaulTest/PaulTest.pde
@@ -0,0 +1,129 @@
+// 2009 Kenneth Finnegan
+// kennethfinnegan.blogspot.com
+
+// Pins.
+#define clockpin 13 // CI
+#define enablepin 10 // EI
+#define latchpin 9 // LI
+#define datapin 11 // DI
+
+// Can't be higher than 255
+#define MAXBRIGHT 1023
+#define NumLEDs 48
+#define FADENUM 4
+
+// s[tate] t[arget] colors
+int s[NumLEDs][3] = {0}, t[NumLEDs][3] = {0};
+
+int LEDChannels[NumLEDs][3] = {0};
+int SB_CommandMode;
+int SB_RedCommand;
+int SB_GreenCommand;
+int SB_BlueCommand;
+
+void setup() {
+
+ // Make the random MORE RANDOM!
+ randomSeed(analogRead(0));
+
+ pinMode(datapin, OUTPUT);
+ pinMode(latchpin, OUTPUT);
+ pinMode(enablepin, OUTPUT);
+ pinMode(clockpin, OUTPUT);
+ SPCR = (1<<SPE)|(1<<MSTR)|(0<<SPR1)|(0<<SPR0);
+ digitalWrite(latchpin, LOW);
+ digitalWrite(enablepin, LOW);
+
+ SB_CommandMode = B01; // Write to current control registers
+ for (int z = 0; z < NumLEDs; z++) SB_SendPacket();
+ delayMicroseconds(15);
+ digitalWrite(latchpin,HIGH); // latch data into registers
+ delayMicroseconds(15);
+ digitalWrite(latchpin,LOW);
+}
+
+
+void SB_SendPacket() {
+
+ if (SB_CommandMode == B01) {
+ SB_RedCommand = 90;
+ SB_GreenCommand = 75;
+ SB_BlueCommand = 75;
+ }
+
+ SPDR = SB_CommandMode << 6 | SB_BlueCommand>>4;
+ while(!(SPSR & (1<<SPIF)));
+ SPDR = SB_BlueCommand<<4 | SB_RedCommand>>6;
+ while(!(SPSR & (1<<SPIF)));
+ SPDR = SB_RedCommand << 2 | SB_GreenCommand>>8;
+ while(!(SPSR & (1<<SPIF)));
+ SPDR = SB_GreenCommand;
+ while(!(SPSR & (1<<SPIF)));
+
+}
+
+void WriteLEDArray() {
+
+ SB_CommandMode = B00; // Write to PWM control registers
+ for (int h = 0;h<NumLEDs;h++) {
+ SB_RedCommand = LEDChannels[h][0];
+ SB_GreenCommand = LEDChannels[h][1];
+ SB_BlueCommand = LEDChannels[h][2];
+ SB_SendPacket();
+ }
+
+ delayMicroseconds(15);
+ digitalWrite(latchpin,HIGH); // latch data into registers
+ delayMicroseconds(15);
+ digitalWrite(latchpin,LOW);
+
+/*
+ SB_CommandMode = B01; // Write to current control registers
+ for (int z = 0; z < NumLEDs; z++) SB_SendPacket();
+ delayMicroseconds(15);
+ digitalWrite(latchpin,HIGH); // latch data into registers
+ delayMicroseconds(15);
+ digitalWrite(latchpin,LOW);
+ */
+}
+
+
+void loop() {
+ int i,j, offset;
+
+ for (j = 0; j < NumLEDs; j++) {
+
+ // check if this LED got to target - shift if not
+ if(s[j][0]!=t[j][0] || s[j][1]!=t[j][1] || s[j][2] != t[j][2]) {
+ for (i = 0; i<3; i++) {
+ if (s[j][i] > t[j][i]) {
+ s[j][i] = s[j][i] - 1;
+ } else if (s[j][i] < t[j][i]) {
+ s[j][i] = s[j][i] + 1;
+ }
+ }
+
+ } else {
+
+ // Select the next target color
+ // Start from a random one of the three colors to prevent
+ // the cycle from being red biased.
+ offset = random(3);
+ t[j][offset] = random(MAXBRIGHT);
+ t[j][(offset+1)%3] = random(MAXBRIGHT - t[j][offset]);
+ t[j][(offset+2)%3] = MAXBRIGHT - t[j][offset] - t[j][(offset+1)%3];
+ }
+
+ LEDChannels[j][0] = s[j][0];
+ LEDChannels[j][1] = s[j][1];
+ LEDChannels[j][2] = s[j][2];
+ }
+
+ WriteLEDArray();
+ //delay(1);
+
+
+ // Let the viewer enjoy the new color before
+ // selecting the next target color.
+ //delay(500);
+}
View
76 PaulTest/README
@@ -0,0 +1,76 @@
+Plans & Thoughts
+- LED array is 7x8
+- This is 56 LEDs, therefore the On | Off state
+ can be represented by a 0 or 1 value in a 56
+ bit number.
+- Well, there are no 56 bit numbers, so what about a 64
+ bit number in arduino? Or at least 2x 32 bit numbers.
+- Therefore, 26 letters in the alphabet suddenly becomes
+ 64 bits * 26 words = 208 bytes.
+- The Arduino 328 has 2 KB of RAM.
+
+====== Side Scrolling Text? ==========
+- So lets think of our 64 bits as an array.
+
+ 0
+63.--0 0 0 0 0 0 0
+56. 0 1 0 0 0 1 0
+49. 0 1 0 0 0 1 0
+42. 0 1 0 0 0 1 0
+35. 0 1 1 1 1 1 0
+28. 0 1 0 0 0 1 0
+21. 0 1 0 0 0 1 0
+14. 0 1 0 0 0 1 0
+ 7. 0 0 0 0 0 0 0
+
+Hmm, too complex for a first aproximation. Lets start
+simple first, consider the following 3x3 pixel display.
+
+0 0 1
+0 1 0
+1 0 0
+
+This can be represented by the 9 bit string:
+ 001010100
+
+Now, we want to shift everything 1 pixel left:
+
+0 1 0
+1 0 0
+0 0 0
+
+This can be done by the following operation.
+int x = 001010100
+x = ((x & 7) << 1 | (x & 46) << 1 | (x & 428) << 1) & 511
+
+actually... now that I think about it, the above will not work.
+It will result in the following:
+0 1 0
+1 0 1
+0 0 0
+
+However, this should work I think:
+1: x = ((x << 1) & 7 | (x << 1) & 46 | (x << 1) & 428) & 511
+
+or, much more efficiently
+2: x = (x << 1) & 438
+
+Explanation for method #1:
+- screw it, this method sucks.
+
+Exmplanation for method #2:
+- Shift the entire array to the left 1
+ 010101000
+- AND the array with the following bitmask:
+ 010101000
+AND 110110110 (438)
+-------------
+ 010100000
+- The result:
+ 010100000
+
+ 0 1 0
+ 1 0 0
+ 0 0 0
+
+WIN!
Please sign in to comment.
Something went wrong with that request. Please try again.