qmk_firmware

QMK firmware for my keyboards (Corne, Sweep Ferris) and trackball (Ploopy Adept)
Log | Files | Refs | Submodules | LICENSE

led_controls.ino (9661B)


      1 #include <FastGPIO.h>
      2 #include <TimerOne.h>
      3 
      4 int iByte;
      5 byte col = 0;
      6 byte leds[12][4];
      7 byte pass = 1;
      8 int fadecount = 1;
      9 const int fadelimit = 3000;
     10 const int fadelimitshort = 1000;
     11 byte mode = 4;
     12 byte brightness = 2;
     13 boolean changemode = 0;
     14 int rain = 0;
     15 const int rainlimit = 5000;
     16 const int rainfade = 5000;
     17 byte rx = 0;
     18 byte ry = 0;
     19 
     20 // pin[xx] on led matrix connected to nn on Arduino (-1 is dummy to make array start at pos 1)
     21 int pins[17] = {
     22   -1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 16, 14, 15, 18, 19, 20, 21
     23 };
     24 
     25 // col[xx] of leds = pin yy on led matrix
     26 int cols[12] = {
     27   pins[8], pins[7], pins[6], pins[5], pins[9], pins[10], pins[11], pins[12], pins[13], pins[14], pins[15], pins[16]
     28 };
     29 
     30 // row[xx] of leds = pin yy on led matrix
     31 int rows[4] = {
     32   pins[1], pins[2], pins[3], pins[4]
     33 };
     34 
     35 
     36 #define DELAY 0
     37 extern byte leds[12][4];
     38 
     39 void setup() {
     40   Serial1.begin(9600);
     41   setupLeds();
     42   for (int s = 0; s < 5; s++) {
     43     for ( int r = 1; r  < 9; r++)  {
     44       delayMicroseconds(65000);
     45       delayMicroseconds(65000);
     46       for (int j = 0; j < 4; j++) {
     47         for (int i = 0; i < 12; i++) {
     48           leds[i][j] = 1;
     49           for (int p = 0; p < 25; p++) {
     50           }
     51           leds[i][j] = r;
     52         }
     53       }
     54     }
     55     for ( int r = 9; r  > 0; r--)  {
     56       delayMicroseconds(65000);
     57       delayMicroseconds(65000);
     58       delayMicroseconds(65000);
     59       for (int j = 0; j < 4; j++) {
     60         for (int i = 0; i < 12; i++) {
     61           leds[i][j] = 1;
     62           for (int p = 0; p < 25; p++) {
     63           }
     64           leds[i][j] = r;
     65         }
     66       }
     67     }
     68   }
     69 }
     70 
     71 void loop() {
     72 
     73   switch (mode) {
     74     case 0:
     75       //Blacklight
     76       for (int i = 0; i < 12; i++) {
     77         for (int j = 0; j < 4; j++) {
     78           leds[i][j] = brightness;
     79         }
     80       }
     81       checkserial();
     82       break;
     83     case 1:
     84       //Breathing
     85       for ( int r = 1; r  < 9; r++)  {
     86         checkserial();
     87         if (changemode == 0) {
     88           delayMicroseconds(65000);
     89           delayMicroseconds(65000);
     90           delayMicroseconds(65000);
     91           for (int j = 0; j < 4; j++) {
     92             for (int i = 0; i < 12; i++) {
     93               leds[i][j] = 1;
     94               for (int p = 0; p < 25; p++) {
     95               }
     96               leds[i][j] = r;
     97             }
     98           }
     99         }
    100         else {
    101           break;
    102         }
    103       }
    104       for ( int r = 9; r  > 0; r--)  {
    105         checkserial();
    106         if (changemode == 0) {
    107           delayMicroseconds(65000);
    108           delayMicroseconds(65000);
    109           delayMicroseconds(65000);
    110           delayMicroseconds(65000);
    111           for (int j = 0; j < 4; j++) {
    112             for (int i = 0; i < 12; i++) {
    113               leds[i][j] = 1;
    114               for (int p = 0; p < 25; p++) {
    115               }
    116               leds[i][j] = r;
    117             }
    118           }
    119         }
    120         else {
    121           break;
    122         }
    123       }
    124       for ( int r = 1; r  < 30; r++)  {
    125         checkserial();
    126         if (changemode == 0) {
    127           delayMicroseconds(65000);
    128           delayMicroseconds(65000);
    129         }
    130         else {
    131           break;
    132         }
    133       }
    134       break;
    135     case 2:
    136       //Random
    137       leds[random(12)][random(4)] = random(8);
    138       delayMicroseconds(10000);
    139       checkserial();
    140       break;
    141     case 3:
    142       //Rain
    143       rain++;
    144       if (rain > rainlimit) {
    145         rain = 0;
    146         rx = random(12);
    147         ry = random(4);
    148         if (leds[rx][ry] == 0) {
    149           leds[rx][ry] = 18;
    150         }
    151       }
    152       fadecount++;
    153       if (fadecount > rainfade) {
    154         fadecount = 1;
    155         for (int i = 0; i < 12; i++) {
    156           for (int j = 0; j < 4; j++) {
    157             if (leds[i][j] > 0) {
    158               leds[i][j] = leds[i][j] - 1;
    159             }
    160           }
    161         }
    162       }
    163       checkserial();
    164       break;
    165     case 4:
    166       //Reactive
    167       fadecount++;
    168       if (fadecount > fadelimit) {
    169         fadecount = 1;
    170         for (int i = 0; i < 12; i++) {
    171           for (int j = 0; j < 4; j++) {
    172             if (leds[i][j] > 0) {
    173               leds[i][j] = leds[i][j] - 1;
    174             }
    175           }
    176         }
    177       }
    178       checkserial();
    179       break;
    180     case 5:
    181       //Reactive Target
    182       fadecount++;
    183       if (fadecount > fadelimitshort) {
    184         fadecount = 1;
    185         for (int i = 0; i < 12; i++) {
    186           for (int j = 0; j < 4; j++) {
    187             if (leds[i][j] > 0) {
    188               leds[i][j] = leds[i][j] - 1;
    189             }
    190           }
    191         }
    192       }
    193       checkserial();
    194       break;
    195     default:
    196       mode = 0;
    197       break;
    198   }
    199   changemode = 0;
    200 }
    201 
    202 void checkserial() {
    203   if (Serial1.available() > 0) {
    204     iByte = Serial1.read();
    205     if (iByte == 100) {
    206       brightness++;
    207       if (brightness > 9) {
    208         brightness = 1;
    209       }
    210     }
    211     if (iByte == 101) {
    212       mode++;
    213     }
    214     if (iByte < 100) {
    215       if (mode == 4) {
    216         byte row = iByte / 16;
    217         byte col = iByte % 16;
    218         leds[col][row] = 18;
    219       }
    220       if (mode == 5) {
    221         byte row = iByte / 16;
    222         byte col = iByte % 16;
    223         for (byte i = 0; i < 12;  i++) {
    224           leds[i][row] = 18;
    225         }
    226         for (byte p = 0; p < 4;  p++) {
    227           leds[col][p] = 18;
    228         }
    229       }
    230     }
    231   }
    232 }
    233 
    234 void setupLeds() {
    235   // sets the pins as output
    236   FastGPIO::Pin<2>::setOutputLow();
    237   FastGPIO::Pin<3>::setOutputLow();
    238   FastGPIO::Pin<4>::setOutputLow();
    239   FastGPIO::Pin<5>::setOutputLow();
    240   FastGPIO::Pin<6>::setOutputLow();
    241   FastGPIO::Pin<7>::setOutputLow();
    242   FastGPIO::Pin<8>::setOutputLow();
    243   FastGPIO::Pin<9>::setOutputLow();
    244   FastGPIO::Pin<10>::setOutputLow();
    245   FastGPIO::Pin<16>::setOutputLow();
    246   FastGPIO::Pin<14>::setOutputLow();
    247   FastGPIO::Pin<15>::setOutputLow();
    248   FastGPIO::Pin<18>::setOutputLow();
    249   FastGPIO::Pin<19>::setOutputLow();
    250   FastGPIO::Pin<20>::setOutputLow();
    251   FastGPIO::Pin<21>::setOutputLow();
    252 
    253   // set up Cols
    254   FastGPIO::Pin<6>::setOutputValueLow();
    255   FastGPIO::Pin<7>::setOutputValueLow();
    256   FastGPIO::Pin<8>::setOutputValueLow();
    257   FastGPIO::Pin<9>::setOutputValueLow();
    258   FastGPIO::Pin<10>::setOutputValueLow();
    259   FastGPIO::Pin<16>::setOutputValueLow();
    260   FastGPIO::Pin<14>::setOutputValueLow();
    261   FastGPIO::Pin<15>::setOutputValueLow();
    262   FastGPIO::Pin<18>::setOutputValueLow();
    263   FastGPIO::Pin<19>::setOutputValueLow();
    264   FastGPIO::Pin<20>::setOutputValueLow();
    265   FastGPIO::Pin<21>::setOutputValueLow();
    266 
    267   // set up Rows
    268   FastGPIO::Pin<2>::setOutputValueLow();
    269   FastGPIO::Pin<3>::setOutputValueLow();
    270   FastGPIO::Pin<4>::setOutputValueLow();
    271   FastGPIO::Pin<5>::setOutputValueLow();
    272 
    273   clearLeds();
    274   Timer1.initialize(25);
    275   Timer1.attachInterrupt(display);
    276 
    277 }
    278 
    279 void clearLeds() {
    280   // Clear display array
    281   for (int i = 0; i < 12; i++) {
    282     for (int j = 0; j < 4; j++) {
    283       leds[i][j] = 0;
    284     }
    285   }
    286 }
    287 
    288 void onLeds() {
    289   // Clear display array
    290   for (int i = 0; i < 12; i++) {
    291     for (int j = 0; j < 4; j++) {
    292       leds[i][j] = 7;
    293     }
    294   }
    295 }
    296 
    297 // Interrupt routine
    298 void display() {
    299 
    300   switch (col) { // Turn whole previous column off
    301     case 0:
    302       FastGPIO::Pin<6>::setOutputValueLow();
    303       break;
    304     case 1:
    305       FastGPIO::Pin<7>::setOutputValueLow();
    306       break;
    307     case 2:
    308       FastGPIO::Pin<8>::setOutputValueLow();
    309       break;
    310     case 3:
    311       FastGPIO::Pin<9>::setOutputValueLow();
    312       break;
    313     case 4:
    314       FastGPIO::Pin<10>::setOutputValueLow();
    315       break;
    316     case 5:
    317       FastGPIO::Pin<16>::setOutputValueLow();
    318       break;
    319     case 6:
    320       FastGPIO::Pin<14>::setOutputValueLow();
    321       break;
    322     case 7:
    323       FastGPIO::Pin<15>::setOutputValueLow();
    324       break;
    325     case 8:
    326       FastGPIO::Pin<18>::setOutputValueLow();
    327       break;
    328     case 9:
    329       FastGPIO::Pin<19>::setOutputValueLow();
    330       break;
    331     case 10:
    332       FastGPIO::Pin<20>::setOutputValueLow();
    333       break;
    334     case 11:
    335       FastGPIO::Pin<21>::setOutputValueLow();
    336       break;
    337   }
    338 
    339   col++;
    340   if (col == 12) {
    341     col = 0;
    342     pass++;
    343     if (pass > 8) {
    344       pass = 1;
    345     }
    346   }
    347   for (int row = 0; row < 4; row++) {
    348     if (leds[col][row] > pass) {
    349       switch (row) { // Turn on this led
    350         case 0:
    351           FastGPIO::Pin<2>::setOutputValueLow();
    352           break;
    353         case 1:
    354           FastGPIO::Pin<3>::setOutputValueLow();
    355           break;
    356         case 2:
    357           FastGPIO::Pin<4>::setOutputValueLow();
    358           break;
    359         case 3:
    360           FastGPIO::Pin<5>::setOutputValueLow();
    361           break;
    362       }
    363     }
    364     else {
    365       switch (row) { // Turn off this led
    366         case 0:
    367           FastGPIO::Pin<2>::setOutputValueHigh();
    368           break;
    369         case 1:
    370           FastGPIO::Pin<3>::setOutputValueHigh();
    371           break;
    372         case 2:
    373           FastGPIO::Pin<4>::setOutputValueHigh();
    374           break;
    375         case 3:
    376           FastGPIO::Pin<5>::setOutputValueHigh();
    377           break;
    378       }
    379     }
    380   }
    381   switch (col) { // Turn column on
    382     case 0:
    383       FastGPIO::Pin<6>::setOutputValueHigh();
    384       break;
    385     case 1:
    386       FastGPIO::Pin<7>::setOutputValueHigh();
    387       break;
    388     case 2:
    389       FastGPIO::Pin<8>::setOutputValueHigh();
    390       break;
    391     case 3:
    392       FastGPIO::Pin<9>::setOutputValueHigh();
    393       break;
    394     case 4:
    395       FastGPIO::Pin<10>::setOutputValueHigh();
    396       break;
    397     case 5:
    398       FastGPIO::Pin<16>::setOutputValueHigh();
    399       break;
    400     case 6:
    401       FastGPIO::Pin<14>::setOutputValueHigh();
    402       break;
    403     case 7:
    404       FastGPIO::Pin<15>::setOutputValueHigh();
    405       break;
    406     case 8:
    407       FastGPIO::Pin<18>::setOutputValueHigh();
    408       break;
    409     case 9:
    410       FastGPIO::Pin<19>::setOutputValueHigh();
    411       break;
    412     case 10:
    413       FastGPIO::Pin<20>::setOutputValueHigh();
    414       break;
    415     case 11:
    416       FastGPIO::Pin<21>::setOutputValueHigh();
    417       break;
    418   }
    419 
    420 }