dot star led strips

Dependents:   DotStarWall DotStarPoi

Committer:
Nathan Yonkee
Date:
Tue Aug 15 08:37:20 2017 -0600
Revision:
1:ead031f6faa9
Parent:
0:41708086172d
change initialization struct

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tulanthoar 0:41708086172d 1 #include "rtos.h"
tulanthoar 0:41708086172d 2
Nathan Yonkee 1:ead031f6faa9 3
Nathan Yonkee 1:ead031f6faa9 4 struct RedGreenBlue {
Nathan Yonkee 1:ead031f6faa9 5 int red, green, blue;
Nathan Yonkee 1:ead031f6faa9 6 };
Nathan Yonkee 1:ead031f6faa9 7
tulanthoar 0:41708086172d 8 class DotStar {
Nathan Yonkee 1:ead031f6faa9 9 enum Init {brightness = 4, frequency = 500000, r = 35, g = 3, b = 0};
tulanthoar 0:41708086172d 10 SPI* const spi_;
tulanthoar 0:41708086172d 11 const int ledNum_;
tulanthoar 0:41708086172d 12 int brightness_;
tulanthoar 0:41708086172d 13 int colors_[3];
tulanthoar 0:41708086172d 14 public:
tulanthoar 0:41708086172d 15 enum { red = 0, green = 1, blue = 2 };
tulanthoar 0:41708086172d 16 enum { redIndex = 2, greenIndex = 3, blueIndex = 4 };
tulanthoar 0:41708086172d 17 enum {off = 0, dim = 8, half = 16, brightest = 31};
tulanthoar 0:41708086172d 18 enum {pause = 100, sleep = 2000};
tulanthoar 0:41708086172d 19 enum cmdType : char {
tulanthoar 0:41708086172d 20 setColor = '!',
tulanthoar 0:41708086172d 21 onOff = 'o',
tulanthoar 0:41708086172d 22 };
tulanthoar 0:41708086172d 23 void set_color(const int color, const int val);
Nathan Yonkee 1:ead031f6faa9 24 void set_rgb(const RedGreenBlue& rgb);
tulanthoar 0:41708086172d 25 void set_leds();
tulanthoar 0:41708086172d 26 DotStar(SPI* const spi, const int nLeds);
tulanthoar 0:41708086172d 27 void set_brightness(const int brightness);
tulanthoar 0:41708086172d 28 };
tulanthoar 0:41708086172d 29
tulanthoar 0:41708086172d 30 class DotStarPair {
tulanthoar 0:41708086172d 31 DotStar* left;
tulanthoar 0:41708086172d 32 DotStar* right;
tulanthoar 0:41708086172d 33 public:
tulanthoar 0:41708086172d 34 DotStarPair(DotStar* l, DotStar* r);
tulanthoar 0:41708086172d 35 void set_brightness(const int brightness);
tulanthoar 0:41708086172d 36 void set_rgb(const RedGreenBlue& rgb);
tulanthoar 0:41708086172d 37 };
tulanthoar 0:41708086172d 38
tulanthoar 0:41708086172d 39 DotStarPair::DotStarPair(DotStar* l, DotStar* r) : left(l), right(r) {}
tulanthoar 0:41708086172d 40
tulanthoar 0:41708086172d 41 void DotStarPair::set_brightness(const int brightness) {
tulanthoar 0:41708086172d 42 left->set_brightness(brightness);
tulanthoar 0:41708086172d 43 left->set_leds();
tulanthoar 0:41708086172d 44 right->set_brightness(brightness);
tulanthoar 0:41708086172d 45 right->set_leds();
tulanthoar 0:41708086172d 46 };
tulanthoar 0:41708086172d 47
tulanthoar 0:41708086172d 48 void DotStarPair::set_rgb(const RedGreenBlue& rgb) {
tulanthoar 0:41708086172d 49 left->set_color(DotStar::blue, rgb.blue);
tulanthoar 0:41708086172d 50 left->set_color(DotStar::red, rgb.red);
tulanthoar 0:41708086172d 51 left->set_color(DotStar::green, rgb.green);
tulanthoar 0:41708086172d 52 left->set_leds();
tulanthoar 0:41708086172d 53 right->set_color(DotStar::blue, rgb.blue);
tulanthoar 0:41708086172d 54 right->set_color(DotStar::red, rgb.red);
tulanthoar 0:41708086172d 55 right->set_color(DotStar::green, rgb.green);
tulanthoar 0:41708086172d 56 right->set_leds();
tulanthoar 0:41708086172d 57 };
tulanthoar 0:41708086172d 58
tulanthoar 0:41708086172d 59 DotStar::DotStar(SPI* const spi, const int nLeds) : spi_(spi), ledNum_(nLeds),
tulanthoar 0:41708086172d 60 brightness_(Init::brightness) {
tulanthoar 0:41708086172d 61 spi_->frequency(Init::frequency);
Nathan Yonkee 1:ead031f6faa9 62 colors_[DotStar::red] = Init::r;
Nathan Yonkee 1:ead031f6faa9 63 colors_[DotStar::blue] = Init::b;
Nathan Yonkee 1:ead031f6faa9 64 colors_[DotStar::green] = Init::g;
tulanthoar 0:41708086172d 65 set_leds();
tulanthoar 0:41708086172d 66 }
tulanthoar 0:41708086172d 67
tulanthoar 0:41708086172d 68 void DotStar::set_leds() {
tulanthoar 0:41708086172d 69 const int brightnessFrame = (7<<5)|brightness_;
tulanthoar 0:41708086172d 70 const int blueFrame = (colors_[DotStar::blue] ) & 0xff;
tulanthoar 0:41708086172d 71 const int greenFrame = (colors_[DotStar::green]) & 0xff;
tulanthoar 0:41708086172d 72 const int redFrame = colors_[DotStar::red] & 0xff;
tulanthoar 0:41708086172d 73 int i;
tulanthoar 0:41708086172d 74 Thread::wait(DotStar::pause);
tulanthoar 0:41708086172d 75 for (i = 4; i --> 0; spi_->write(0)) { } // start frame
tulanthoar 0:41708086172d 76 for (i = 0; i < ledNum_ ; ++i) {
tulanthoar 0:41708086172d 77 spi_->write(brightnessFrame); // led frame
tulanthoar 0:41708086172d 78 spi_->write(blueFrame); // B
tulanthoar 0:41708086172d 79 spi_->write(greenFrame); // G
tulanthoar 0:41708086172d 80 spi_->write(redFrame); // R
tulanthoar 0:41708086172d 81 }
tulanthoar 0:41708086172d 82 for (i = 4; i --> 0; spi_->write(1)) { } // end frame
tulanthoar 0:41708086172d 83 }
tulanthoar 0:41708086172d 84
tulanthoar 0:41708086172d 85 void DotStar::set_brightness(const int brightness) {
tulanthoar 0:41708086172d 86 brightness_ = brightness;
Nathan Yonkee 1:ead031f6faa9 87 set_leds();
tulanthoar 0:41708086172d 88 }
tulanthoar 0:41708086172d 89
Nathan Yonkee 1:ead031f6faa9 90 void DotStar::set_rgb(const RedGreenBlue& rgb) {
Nathan Yonkee 1:ead031f6faa9 91 set_color(DotStar::blue, rgb.blue);
Nathan Yonkee 1:ead031f6faa9 92 set_color(DotStar::red, rgb.red);
Nathan Yonkee 1:ead031f6faa9 93 set_color(DotStar::green, rgb.green);
Nathan Yonkee 1:ead031f6faa9 94 set_leds();
Nathan Yonkee 1:ead031f6faa9 95 };
Nathan Yonkee 1:ead031f6faa9 96
tulanthoar 0:41708086172d 97 void DotStar::set_color(const int c, const int val) {
tulanthoar 0:41708086172d 98 colors_[c] = val;
tulanthoar 0:41708086172d 99 };