Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: DotStarWall DotStarPoi
DotStar.h
00001 #include "rtos.h" 00002 00003 00004 struct RedGreenBlue { 00005 int red, green, blue; 00006 }; 00007 00008 class DotStar { 00009 enum Init {brightness = 4, frequency = 500000, r = 35, g = 3, b = 0}; 00010 SPI* const spi_; 00011 const int ledNum_; 00012 int brightness_; 00013 int colors_[3]; 00014 public: 00015 enum { red = 0, green = 1, blue = 2 }; 00016 enum { redIndex = 2, greenIndex = 3, blueIndex = 4 }; 00017 enum {off = 0, dim = 8, half = 16, brightest = 31}; 00018 enum {pause = 100, sleep = 2000}; 00019 enum cmdType : char { 00020 setColor = '!', 00021 onOff = 'o', 00022 }; 00023 void set_color(const int color, const int val); 00024 void set_rgb(const RedGreenBlue& rgb); 00025 void set_leds(); 00026 DotStar(SPI* const spi, const int nLeds); 00027 void set_brightness(const int brightness); 00028 }; 00029 00030 class DotStarPair { 00031 DotStar* left; 00032 DotStar* right; 00033 public: 00034 DotStarPair(DotStar* l, DotStar* r); 00035 void set_brightness(const int brightness); 00036 void set_rgb(const RedGreenBlue& rgb); 00037 }; 00038 00039 DotStarPair::DotStarPair(DotStar* l, DotStar* r) : left(l), right(r) {} 00040 00041 void DotStarPair::set_brightness(const int brightness) { 00042 left->set_brightness(brightness); 00043 left->set_leds(); 00044 right->set_brightness(brightness); 00045 right->set_leds(); 00046 }; 00047 00048 void DotStarPair::set_rgb(const RedGreenBlue& rgb) { 00049 left->set_color(DotStar::blue, rgb.blue); 00050 left->set_color(DotStar::red, rgb.red); 00051 left->set_color(DotStar::green, rgb.green); 00052 left->set_leds(); 00053 right->set_color(DotStar::blue, rgb.blue); 00054 right->set_color(DotStar::red, rgb.red); 00055 right->set_color(DotStar::green, rgb.green); 00056 right->set_leds(); 00057 }; 00058 00059 DotStar::DotStar(SPI* const spi, const int nLeds) : spi_(spi), ledNum_(nLeds), 00060 brightness_(Init::brightness) { 00061 spi_->frequency(Init::frequency); 00062 colors_[DotStar::red] = Init::r; 00063 colors_[DotStar::blue] = Init::b; 00064 colors_[DotStar::green] = Init::g; 00065 set_leds(); 00066 } 00067 00068 void DotStar::set_leds() { 00069 const int brightnessFrame = (7<<5)|brightness_; 00070 const int blueFrame = (colors_[DotStar::blue] ) & 0xff; 00071 const int greenFrame = (colors_[DotStar::green]) & 0xff; 00072 const int redFrame = colors_[DotStar::red] & 0xff; 00073 int i; 00074 Thread::wait(DotStar::pause); 00075 for (i = 4; i --> 0; spi_->write(0)) { } // start frame 00076 for (i = 0; i < ledNum_ ; ++i) { 00077 spi_->write(brightnessFrame); // led frame 00078 spi_->write(blueFrame); // B 00079 spi_->write(greenFrame); // G 00080 spi_->write(redFrame); // R 00081 } 00082 for (i = 4; i --> 0; spi_->write(1)) { } // end frame 00083 } 00084 00085 void DotStar::set_brightness(const int brightness) { 00086 brightness_ = brightness; 00087 set_leds(); 00088 } 00089 00090 void DotStar::set_rgb(const RedGreenBlue& rgb) { 00091 set_color(DotStar::blue, rgb.blue); 00092 set_color(DotStar::red, rgb.red); 00093 set_color(DotStar::green, rgb.green); 00094 set_leds(); 00095 }; 00096 00097 void DotStar::set_color(const int c, const int val) { 00098 colors_[c] = val; 00099 };
Generated on Thu Jul 14 2022 01:20:56 by
1.7.2