build using platformio
[Arduino] / tb276 / tb276.ino
1 /*
2
3 scrolling led on buttons
4
5  */
6
7 extern "C" {
8 #include <dev/io.h> // needed for INB
9 }
10
11 #define NR_LEDS 8
12 int leds[NR_LEDS] = {8,9,10,11,12,13,14,15};           // the pin that the LED is attached to
13         // I don't really understand where this numbers are comming from yet :-(
14 int brightness = 0;    // how bright the LED is
15 int fadeAmount = 5;    // how many points to fade the LED by
16 int count = 0;
17 int pos = 0;
18 int old_key = -1;
19 int key_micros = 0;
20
21 int sdi = 0;
22 int sdo = 0xAA;
23 int mask = 0x01;
24
25 int sdi_pin = 10;
26 int sdo_pin = 9;
27 int sclk_pin = 8;
28
29 void sdo_isr(void) {
30         Serial.println("sdo_isr");
31         if (! mask) return;
32         digitalWrite( sdo_pin, sdo & mask ? HIGH : LOW );
33 }
34
35 void sdi_isr(void) {    
36         Serial.println("sdi_isr");
37         if (! mask) return;
38         if ( digitalRead( sdi_pin ) ) {
39                 sdi_pin |= mask;
40         }
41         mask <<= 1;
42         if (! mask) Serial.println(sdi_pin, HEX);
43 }
44
45
46 // the setup routine runs once when you press reset:
47 void setup() {
48   // declare pin 9 to be an output:
49   for(int i=0; i<NR_LEDS; i++) {
50         pinMode(leds[i], OUTPUT);
51         digitalWrite(leds[i], LOW);
52   }
53
54   analogWriteResolution(12);
55
56   Serial.begin(115200);
57   Serial.println("setup");
58
59         attachInterrupt(sclk_pin, sdi_isr, RISING);
60         attachInterrupt(sclk_pin, sdo_isr, FALLING);
61 }
62
63 // the loop routine runs over and over again forever:
64 void loop() {
65   static int mode = 0;
66   // set the brightness of pin 9:
67   mode++;
68   mode &= 16383;
69   if((mode & 8192) == 0)
70   {
71     analogWrite(leds[pos], brightness);
72   }
73   else
74   {
75     digitalWrite(leds[pos], (mode & 256) == 0 ? LOW  : HIGH);
76   }
77
78   // change the brightness for next time through the loop:
79   brightness = brightness + fadeAmount;
80
81   // reverse the direction of the fading at the ends of the fade:
82   if (brightness == 0 || brightness == 4095) {
83     fadeAmount = -fadeAmount ;
84     Serial.print(micros());
85     Serial.print(" ");
86     Serial.println(count++);
87   }
88
89
90   int old_pos = pos;
91   int key;
92   INB(key, IO_PUSHBTN);
93   if ( key != old_key && micros() - key_micros > 300) {
94         old_key = key;
95         if (key & BTN_RIGHT && pos >= 0) pos--;
96         if (key & BTN_LEFT  && pos < (NR_LEDS-1)) pos++;
97         if ( pos != old_pos ) {
98                 Serial.print("key ");
99                 Serial.print(key);
100                 Serial.print(" pos=");
101                 Serial.print(pos);
102                 Serial.print(" micros diff=");
103                 Serial.println(micros()-key_micros);
104                 digitalWrite(leds[old_pos], LOW);
105                 digitalWrite(leds[pos], HIGH);
106         }
107         key_micros = micros();
108   }
109
110
111   // wait for 30 milliseconds to see the dimming effect
112   delay(1);
113 }