poi using dot stars as lights

Dependencies:   DotStarStrips

Fork of DotStarWall by Nathan Yonkee

Committer:
tulanthoar
Date:
Fri Aug 11 18:14:51 2017 +0000
Revision:
0:eacf3b13ce5a
Child:
4:41434d1bd6cc
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tulanthoar 0:eacf3b13ce5a 1 #include "mbed.h"
tulanthoar 0:eacf3b13ce5a 2 #include "rtos.h"
tulanthoar 0:eacf3b13ce5a 3
tulanthoar 0:eacf3b13ce5a 4 RawSerial dev(PA_9, PA_10);
tulanthoar 0:eacf3b13ce5a 5 SPI spiOne(PB_5, PB_4, PB_3);
tulanthoar 0:eacf3b13ce5a 6 SPI spiTwo(PA_7, PA_6, PA_5);
tulanthoar 0:eacf3b13ce5a 7 const int nLedsA = 455;
tulanthoar 0:eacf3b13ce5a 8 const int nLedsB = 455;
tulanthoar 0:eacf3b13ce5a 9 enum Init {brightness = 4, frequency = 500000, red = 35, green = 3, blue = 0, baud = 9600};
tulanthoar 0:eacf3b13ce5a 10
tulanthoar 0:eacf3b13ce5a 11 class DotStar {
tulanthoar 0:eacf3b13ce5a 12 SPI* const spi_;
tulanthoar 0:eacf3b13ce5a 13 const int ledNum_;
tulanthoar 0:eacf3b13ce5a 14 int brightness_;
tulanthoar 0:eacf3b13ce5a 15 int colors_[3];
tulanthoar 0:eacf3b13ce5a 16 public:
tulanthoar 0:eacf3b13ce5a 17 enum { red = 0, green = 1, blue = 2 };
tulanthoar 0:eacf3b13ce5a 18 enum { redIndex = 2, greenIndex = 3, blueIndex = 4 };
tulanthoar 0:eacf3b13ce5a 19 enum {off = 0, dim = 8, half = 16, brightest = 31};
tulanthoar 0:eacf3b13ce5a 20 enum {pause = 100, sleep = 2000};
tulanthoar 0:eacf3b13ce5a 21 enum cmdType : char {
tulanthoar 0:eacf3b13ce5a 22 setColor = '!',
tulanthoar 0:eacf3b13ce5a 23 onOff = 'o',
tulanthoar 0:eacf3b13ce5a 24 switchStrip = 's'
tulanthoar 0:eacf3b13ce5a 25 };
tulanthoar 0:eacf3b13ce5a 26 void set_color(const int color, const int val);
tulanthoar 0:eacf3b13ce5a 27 void set_leds();
tulanthoar 0:eacf3b13ce5a 28 DotStar(SPI* const spi, const int nLeds);
tulanthoar 0:eacf3b13ce5a 29 void set_brightness(const int brightness);
tulanthoar 0:eacf3b13ce5a 30 };
tulanthoar 0:eacf3b13ce5a 31
tulanthoar 0:eacf3b13ce5a 32 struct RedGreenBlue {
tulanthoar 0:eacf3b13ce5a 33 int red, green, blue;
tulanthoar 0:eacf3b13ce5a 34 };
tulanthoar 0:eacf3b13ce5a 35
tulanthoar 0:eacf3b13ce5a 36 class DotStarPair {
tulanthoar 0:eacf3b13ce5a 37 DotStar* left;
tulanthoar 0:eacf3b13ce5a 38 DotStar* right;
tulanthoar 0:eacf3b13ce5a 39 public:
tulanthoar 0:eacf3b13ce5a 40 DotStarPair(DotStar* l, DotStar* r);
tulanthoar 0:eacf3b13ce5a 41 void set_brightness(const int brightness);
tulanthoar 0:eacf3b13ce5a 42 void set_rgb(const RedGreenBlue& rgb);
tulanthoar 0:eacf3b13ce5a 43 };
tulanthoar 0:eacf3b13ce5a 44
tulanthoar 0:eacf3b13ce5a 45 DotStarPair::DotStarPair(DotStar* l, DotStar* r) : left(l), right(r) {}
tulanthoar 0:eacf3b13ce5a 46
tulanthoar 0:eacf3b13ce5a 47 void DotStarPair::set_brightness(const int brightness) {
tulanthoar 0:eacf3b13ce5a 48 left->set_brightness(brightness);
tulanthoar 0:eacf3b13ce5a 49 left->set_leds();
tulanthoar 0:eacf3b13ce5a 50 right->set_brightness(brightness);
tulanthoar 0:eacf3b13ce5a 51 right->set_leds();
tulanthoar 0:eacf3b13ce5a 52 };
tulanthoar 0:eacf3b13ce5a 53
tulanthoar 0:eacf3b13ce5a 54 void DotStarPair::set_rgb(const RedGreenBlue& rgb) {
tulanthoar 0:eacf3b13ce5a 55 left->set_color(DotStar::blue, rgb.blue);
tulanthoar 0:eacf3b13ce5a 56 left->set_color(DotStar::red, rgb.red);
tulanthoar 0:eacf3b13ce5a 57 left->set_color(DotStar::green, rgb.green);
tulanthoar 0:eacf3b13ce5a 58 left->set_leds();
tulanthoar 0:eacf3b13ce5a 59 right->set_color(DotStar::blue, rgb.blue);
tulanthoar 0:eacf3b13ce5a 60 right->set_color(DotStar::red, rgb.red);
tulanthoar 0:eacf3b13ce5a 61 right->set_color(DotStar::green, rgb.green);
tulanthoar 0:eacf3b13ce5a 62 right->set_leds();
tulanthoar 0:eacf3b13ce5a 63 };
tulanthoar 0:eacf3b13ce5a 64
tulanthoar 0:eacf3b13ce5a 65 DotStar::DotStar(SPI* const spi, const int nLeds) : spi_(spi), ledNum_(nLeds),
tulanthoar 0:eacf3b13ce5a 66 brightness_(Init::brightness) {
tulanthoar 0:eacf3b13ce5a 67 spi_->frequency(Init::frequency);
tulanthoar 0:eacf3b13ce5a 68 colors_[DotStar::red] = Init::red;
tulanthoar 0:eacf3b13ce5a 69 colors_[DotStar::blue] = Init::blue;
tulanthoar 0:eacf3b13ce5a 70 colors_[DotStar::green] = Init::green;
tulanthoar 0:eacf3b13ce5a 71 set_leds();
tulanthoar 0:eacf3b13ce5a 72 }
tulanthoar 0:eacf3b13ce5a 73
tulanthoar 0:eacf3b13ce5a 74
tulanthoar 0:eacf3b13ce5a 75 void DotStar::set_leds() {
tulanthoar 0:eacf3b13ce5a 76 const int brightnessFrame = (7<<5)|brightness_;
tulanthoar 0:eacf3b13ce5a 77 const int blueFrame = (colors_[DotStar::blue] ) & 0xff;
tulanthoar 0:eacf3b13ce5a 78 const int greenFrame = (colors_[DotStar::green]) & 0xff;
tulanthoar 0:eacf3b13ce5a 79 const int redFrame = colors_[DotStar::red] & 0xff;
tulanthoar 0:eacf3b13ce5a 80 int i;
tulanthoar 0:eacf3b13ce5a 81 Thread::wait(DotStar::pause);
tulanthoar 0:eacf3b13ce5a 82 for (i = 4; i --> 0; spi_->write(0)) { } // start frame
tulanthoar 0:eacf3b13ce5a 83 for (i = 0; i < ledNum_ ; ++i) {
tulanthoar 0:eacf3b13ce5a 84 spi_->write(brightnessFrame); // led frame
tulanthoar 0:eacf3b13ce5a 85 spi_->write(blueFrame); // B
tulanthoar 0:eacf3b13ce5a 86 spi_->write(greenFrame); // G
tulanthoar 0:eacf3b13ce5a 87 spi_->write(redFrame); // R
tulanthoar 0:eacf3b13ce5a 88 }
tulanthoar 0:eacf3b13ce5a 89 for (i = 4; i --> 0; spi_->write(1)) { } // end frame
tulanthoar 0:eacf3b13ce5a 90 }
tulanthoar 0:eacf3b13ce5a 91
tulanthoar 0:eacf3b13ce5a 92 void DotStar::set_brightness(const int brightness) {
tulanthoar 0:eacf3b13ce5a 93 brightness_ = brightness;
tulanthoar 0:eacf3b13ce5a 94 }
tulanthoar 0:eacf3b13ce5a 95
tulanthoar 0:eacf3b13ce5a 96 void DotStar::set_color(const int c, const int val) {
tulanthoar 0:eacf3b13ce5a 97 colors_[c] = val;
tulanthoar 0:eacf3b13ce5a 98 };
tulanthoar 0:eacf3b13ce5a 99
tulanthoar 0:eacf3b13ce5a 100 DotStar dotStarA(&spiOne, nLedsA);
tulanthoar 0:eacf3b13ce5a 101 DotStar dotStarB(&spiTwo, nLedsB);
tulanthoar 0:eacf3b13ce5a 102
tulanthoar 0:eacf3b13ce5a 103 DotStarPair strips(&dotStarA, &dotStarB);
tulanthoar 0:eacf3b13ce5a 104
tulanthoar 0:eacf3b13ce5a 105 int main() {
tulanthoar 0:eacf3b13ce5a 106 dev.baud(Init::baud);
tulanthoar 0:eacf3b13ce5a 107 for (int i = 1; i < DotStar::brightest; i += 4) {
tulanthoar 0:eacf3b13ce5a 108 strips.set_brightness(i);
tulanthoar 0:eacf3b13ce5a 109 }
tulanthoar 0:eacf3b13ce5a 110 bool sleeping = false;
tulanthoar 0:eacf3b13ce5a 111 #define STR_BUFF_SIZE_VAR 6
tulanthoar 0:eacf3b13ce5a 112 char bleCmdString[STR_BUFF_SIZE_VAR] = {};
tulanthoar 0:eacf3b13ce5a 113 for (; ; ) {
tulanthoar 0:eacf3b13ce5a 114 for (int i = 0; i < STR_BUFF_SIZE_VAR; i++) {
tulanthoar 0:eacf3b13ce5a 115 bleCmdString[i] = 0;
tulanthoar 0:eacf3b13ce5a 116 }
tulanthoar 0:eacf3b13ce5a 117 dev.putc('a');
tulanthoar 0:eacf3b13ce5a 118 strips.set_brightness(DotStar::half);
tulanthoar 0:eacf3b13ce5a 119 int i = 0;
tulanthoar 0:eacf3b13ce5a 120 int j = 0;
tulanthoar 0:eacf3b13ce5a 121 while (i < STR_BUFF_SIZE_VAR) {
tulanthoar 0:eacf3b13ce5a 122 while(dev.readable()) {
tulanthoar 0:eacf3b13ce5a 123 bleCmdString[i] = dev.getc();
tulanthoar 0:eacf3b13ce5a 124 ++i;
tulanthoar 0:eacf3b13ce5a 125 }
tulanthoar 0:eacf3b13ce5a 126 if(j++ > 30000000) break;
tulanthoar 0:eacf3b13ce5a 127 }
tulanthoar 0:eacf3b13ce5a 128 /* strips.set_brightness(DotStar::dim); */
tulanthoar 0:eacf3b13ce5a 129 auto cmdPrefix = bleCmdString[0];
tulanthoar 0:eacf3b13ce5a 130 if (cmdPrefix == DotStar::cmdType::setColor) {
tulanthoar 0:eacf3b13ce5a 131 RedGreenBlue rgb;
tulanthoar 0:eacf3b13ce5a 132 rgb.red = (int)(bleCmdString[DotStar::redIndex]);
tulanthoar 0:eacf3b13ce5a 133 rgb.green = (int)(bleCmdString[DotStar::greenIndex]);
tulanthoar 0:eacf3b13ce5a 134 rgb.blue = (int)(bleCmdString[DotStar::blueIndex]);
tulanthoar 0:eacf3b13ce5a 135 strips.set_rgb(rgb);
tulanthoar 0:eacf3b13ce5a 136 /* strips.set_brightness(DotStar::half); */
tulanthoar 0:eacf3b13ce5a 137 /* Thread::wait(DotStar::sleep); */
tulanthoar 0:eacf3b13ce5a 138 } else if (cmdPrefix == DotStar::cmdType::onOff) {
tulanthoar 0:eacf3b13ce5a 139 sleeping = true;
tulanthoar 0:eacf3b13ce5a 140 strips.set_brightness(DotStar::dim);
tulanthoar 0:eacf3b13ce5a 141 Thread::wait(DotStar::pause);
tulanthoar 0:eacf3b13ce5a 142 }
tulanthoar 0:eacf3b13ce5a 143 while (sleeping) {
tulanthoar 0:eacf3b13ce5a 144 strips.set_brightness(DotStar::off);
tulanthoar 0:eacf3b13ce5a 145 Thread::wait(DotStar::sleep);
tulanthoar 0:eacf3b13ce5a 146 while (dev.readable()) {
tulanthoar 0:eacf3b13ce5a 147 if (dev.getc() == DotStar::cmdType::onOff) {
tulanthoar 0:eacf3b13ce5a 148 sleeping = false;
tulanthoar 0:eacf3b13ce5a 149 strips.set_brightness(DotStar::dim);
tulanthoar 0:eacf3b13ce5a 150 Thread::wait(DotStar::pause);
tulanthoar 0:eacf3b13ce5a 151 }
tulanthoar 0:eacf3b13ce5a 152 }
tulanthoar 0:eacf3b13ce5a 153 }
tulanthoar 0:eacf3b13ce5a 154 }
tulanthoar 0:eacf3b13ce5a 155
tulanthoar 0:eacf3b13ce5a 156 }