| 1 | /* -*- c -*- */ |
| 2 | /* Arduino rgb led driver */ |
| 3 | /* Copyright (c) 2014 Clinton Ebadi <clinton@unknownlamer.org> */ |
| 4 | /* This program is free software: you can redistribute it and/or modify */ |
| 5 | /* it under the terms of the GNU General Public License as published by */ |
| 6 | /* the Free Software Foundation, either version 3 of the License, or */ |
| 7 | /* (at your option) any later version. */ |
| 8 | |
| 9 | /* This program is distributed in the hope that it will be useful, */ |
| 10 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ |
| 11 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ |
| 12 | /* GNU General Public License for more details. */ |
| 13 | |
| 14 | /* You should have received a copy of the GNU General Public License */ |
| 15 | /* along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 16 | |
| 17 | #include <avr/pgmspace.h> |
| 18 | #include "gamma.h" |
| 19 | |
| 20 | const int dither_steps = pow (2, DITHER_BITS); |
| 21 | |
| 22 | typedef struct rgb_pins |
| 23 | { |
| 24 | int red; |
| 25 | int green; |
| 26 | int blue; |
| 27 | } rgb_pins; |
| 28 | |
| 29 | const int rgb_banks = 2; |
| 30 | |
| 31 | const rgb_pins pins[rgb_banks] = { {10, 9, 11}, |
| 32 | {5, 6, 3} }; |
| 33 | |
| 34 | static int led_colors[rgb_banks * 3]; |
| 35 | |
| 36 | void set_color_x (int r, int g, int b, int dither_step = 0) |
| 37 | { |
| 38 | int offset = dither_step * 256; |
| 39 | |
| 40 | for (int bank = 0; bank < rgb_banks; bank++) |
| 41 | { |
| 42 | analogWrite (pins[bank].red, pgm_read_byte_near (gammaTable + (r >= 0 ? offset : 0) + r)); |
| 43 | analogWrite (pins[bank].green, pgm_read_byte_near (gammaTable + (g >= 0 ? offset : 0) + g)); |
| 44 | analogWrite (pins[bank].blue, pgm_read_byte_near (gammaTable + (b >= 0 ? offset : 0) + b)); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | void setup () |
| 49 | { |
| 50 | Serial.begin (115200); |
| 51 | Serial.setTimeout (100000); |
| 52 | |
| 53 | for (int bank = 0; bank < rgb_banks; bank++) |
| 54 | { |
| 55 | pinMode (pins[bank].red, OUTPUT); |
| 56 | pinMode (pins[bank].green, OUTPUT); |
| 57 | pinMode (pins[bank].blue, OUTPUT); |
| 58 | } |
| 59 | |
| 60 | // Test sequence to confirm wiring |
| 61 | set_color_x (255, 255, 255); |
| 62 | delay(500); |
| 63 | set_color_x (255, 0, 0); |
| 64 | delay (500); |
| 65 | set_color_x (0, 255, 0); |
| 66 | delay (500); |
| 67 | set_color_x (0, 0, 255); |
| 68 | delay (500); |
| 69 | } |
| 70 | |
| 71 | void loop () |
| 72 | { |
| 73 | static int r = 192; |
| 74 | static int g = 192; |
| 75 | static int b = 192; |
| 76 | static int step = -1; |
| 77 | |
| 78 | //step = (step + 1) % dither_steps; |
| 79 | step = 0; // dithering is bogus/i'm-not-doing-it-right |
| 80 | |
| 81 | if (Serial.available () >= 3) |
| 82 | { |
| 83 | r = Serial.read (); |
| 84 | g = Serial.read (); |
| 85 | b = Serial.read (); |
| 86 | |
| 87 | Serial.print ("set color "); |
| 88 | Serial.print (r); |
| 89 | Serial.print (" "); |
| 90 | Serial.print (g); |
| 91 | Serial.print (" "); |
| 92 | Serial.print (b); |
| 93 | Serial.println (); |
| 94 | |
| 95 | set_color_x (r, g, b); |
| 96 | } |
| 97 | |
| 98 | set_color_x (r, g, b, step); |
| 99 | |
| 100 | } |