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.
Dependencies: PololuLedStrip mbed
LEDs.cpp
00001 #include "LEDs.h" 00002 00003 LEDs::LEDs(PinName main, PinName single): 00004 mainStrip(main), 00005 singleStrip(single) 00006 { 00007 currentIndex = 0; 00008 } 00009 00010 void LEDs::updateSingleLED(uint8_t colorH, uint8_t colorS, uint8_t colorV) { 00011 hsv_color hsv = {colorH, colorS, colorV}; 00012 for (uint8_t i=0; i < LEDS_SINGLE_STRIP_NUM_LEDS; i++) 00013 singleColours[i] = HsvToRgb(hsv); 00014 show(); 00015 } 00016 00017 void LEDs::updateStripLED(uint8_t index, uint8_t colorH, uint8_t colorS, uint8_t colorV) { 00018 hsv_color hsv = {colorH, colorS, colorV}; 00019 uint8_t stripIndex = LEDS_MAIN_STRIP_TOGGLE_OFFSET + index; 00020 mainColours[stripIndex] = HsvToRgb(hsv); 00021 show(); 00022 } 00023 00024 void LEDs::setStripLEDRGB(uint8_t index, uint8_t colorR, uint8_t colorG, uint8_t colorB) { 00025 rgb_color rgb = {colorR, colorG, colorB}; 00026 mainColours[index] = rgb; 00027 } 00028 00029 00030 void LEDs::setStripLED(uint8_t index, uint8_t colorH, uint8_t colorS, uint8_t colorV) { 00031 hsv_color hsv = {colorH, colorS, colorV}; 00032 mainColours[index] = HsvToRgb(hsv); 00033 } 00034 00035 void LEDs::turnOffStripLED(uint8_t index) { 00036 uint8_t stripIndex = LEDS_MAIN_STRIP_TOGGLE_OFFSET + index; 00037 rgb_color rgb = {0,0,0}; 00038 mainColours[stripIndex] = rgb; 00039 show(); 00040 } 00041 00042 bool LEDs::isStripLEDOn(uint8_t index) { 00043 uint8_t stripIndex = LEDS_MAIN_STRIP_TOGGLE_OFFSET + index; 00044 return mainColours[stripIndex].red != 0 || mainColours[stripIndex].green != 0 || mainColours[stripIndex].blue != 0; 00045 } 00046 00047 void LEDs::scrollStripToLeft() { 00048 rgb_color first = mainColours[0]; 00049 for (uint8_t i = 0; i < LEDS_MAIN_STRIP_NUM_LEDS - 1; i++) { 00050 mainColours[i] = mainColours[i + 1]; 00051 } 00052 mainColours[LEDS_MAIN_STRIP_NUM_LEDS - 1] = first; 00053 } 00054 00055 void LEDs::scrollStripToRight() { 00056 rgb_color last = mainColours[LEDS_MAIN_STRIP_NUM_LEDS - 1]; 00057 for (uint8_t i = LEDS_MAIN_STRIP_NUM_LEDS - 1; i > 0; i--) { 00058 mainColours[i] = mainColours[i - 1]; 00059 } 00060 mainColours[0] = last; 00061 } 00062 00063 void LEDs::show() { 00064 mainStrip.write(mainColours, LEDS_MAIN_STRIP_NUM_LEDS); 00065 singleStrip.write(singleColours, LEDS_SINGLE_STRIP_NUM_LEDS); 00066 } 00067 00068 void LEDs::off() { 00069 00070 rgb_color black = {0,0,0}; 00071 00072 for (uint8_t j = 0; j < 10; j++) { 00073 for (uint8_t i = 0; i < LEDS_MAIN_STRIP_NUM_LEDS; i++) 00074 { 00075 fadeToBlackBy(64,mainColours[i]); 00076 } 00077 00078 for (uint8_t i = 0; i < LEDS_SINGLE_STRIP_NUM_LEDS; i++) 00079 { 00080 fadeToBlackBy(64, singleColours[i]); 00081 } 00082 00083 show(); 00084 wait_ms(50); 00085 } 00086 00087 for (uint8_t i = 0; i < LEDS_MAIN_STRIP_NUM_LEDS; i++) { 00088 mainColours[i] = black; 00089 } 00090 for (uint8_t i = 0; i < LEDS_SINGLE_STRIP_NUM_LEDS; i++) { 00091 singleColours[i] = black; 00092 } 00093 00094 show(); 00095 } 00096 00097 rgb_color LEDs::HsvToRgb(hsv_color hsv) 00098 { 00099 rgb_color rgb; 00100 unsigned char region, p, q, t; 00101 unsigned int h, s, v, remainder; 00102 00103 if (hsv.s == 0) 00104 { 00105 rgb.red = hsv.v; 00106 rgb.green = hsv.v; 00107 rgb.blue = hsv.v; 00108 return rgb; 00109 } 00110 00111 // converting to 16 bit to prevent overflow 00112 h = hsv.h; 00113 s = hsv.s; 00114 v = hsv.v; 00115 00116 region = h / 43; 00117 remainder = (h - (region * 43)) * 6; 00118 00119 p = (v * (255 - s)) >> 8; 00120 q = (v * (255 - ((s * remainder) >> 8))) >> 8; 00121 t = (v * (255 - ((s * (255 - remainder)) >> 8))) >> 8; 00122 00123 switch (region) 00124 { 00125 case 0: 00126 rgb.red = v; 00127 rgb.green = t; 00128 rgb.blue = p; 00129 break; 00130 case 1: 00131 rgb.red = q; 00132 rgb.green = v; 00133 rgb.blue = p; 00134 break; 00135 case 2: 00136 rgb.red = p; 00137 rgb.green = v; 00138 rgb.blue = t; 00139 break; 00140 case 3: 00141 rgb.red = p; 00142 rgb.green = q; 00143 rgb.blue = v; 00144 break; 00145 case 4: 00146 rgb.red = t; 00147 rgb.green = p; 00148 rgb.blue = v; 00149 break; 00150 default: 00151 rgb.red = v; 00152 rgb.green = p; 00153 rgb.blue = q; 00154 break; 00155 } 00156 00157 return rgb; 00158 } 00159 00160 hsv_color LEDs::RgbToHsv(rgb_color rgb) 00161 { 00162 hsv_color hsv; 00163 unsigned char rgbMin, rgbMax; 00164 00165 rgbMin = rgb.red < rgb.green ? (rgb.red < rgb.blue ? rgb.red : rgb.blue) : (rgb.green < rgb.blue ? rgb.green : rgb.blue); 00166 rgbMax = rgb.red > rgb.green ? (rgb.red > rgb.blue ? rgb.red : rgb.blue) : (rgb.green > rgb.blue ? rgb.green : rgb.blue); 00167 00168 hsv.v = rgbMax; 00169 if (hsv.v == 0) 00170 { 00171 hsv.h = 0; 00172 hsv.s = 0; 00173 return hsv; 00174 } 00175 00176 hsv.s = 255 * ((long)(rgbMax - rgbMin)) / hsv.v; 00177 if (hsv.s == 0) 00178 { 00179 hsv.h = 0; 00180 return hsv; 00181 } 00182 00183 if (rgbMax == rgb.red) 00184 hsv.h = 0 + 43 * (rgb.green - rgb.blue) / (rgbMax - rgbMin); 00185 else if (rgbMax == rgb.green) 00186 hsv.h = 85 + 43 * (rgb.blue - rgb.red) / (rgbMax - rgbMin); 00187 else 00188 hsv.h = 171 + 43 * (rgb.red - rgb.green) / (rgbMax - rgbMin); 00189 00190 return hsv; 00191 } 00192 00193 void LEDs::saveStateToEEPROM() { 00194 for (uint8_t i = 0; i < LEDS_MAIN_STRIP_NUM_LEDS; i++) 00195 { 00196 eeprom.update(i * 3 + 0, mainColours[i].red); 00197 eeprom.update(i * 3 + 1, mainColours[i].green); 00198 eeprom.update(i * 3 + 2, mainColours[i].blue); 00199 } 00200 } 00201 00202 void LEDs::loadStateFromEEPROM() { 00203 for (int16_t j = 255; j > 0; j -= 26) 00204 { 00205 loadStateFromEEPROMAndDim(j); 00206 wait_ms(30); 00207 } 00208 loadStateFromEEPROMAndDim(0); 00209 } 00210 00211 void LEDs::loadStateFromEEPROMAndDim(uint8_t dimAmount) { 00212 for (uint8_t i = 0; i < LEDS_MAIN_STRIP_NUM_LEDS; i++) { 00213 mainColours[i].red = eeprom.read(i * 3 + 0); 00214 mainColours[i].green = eeprom.read(i * 3 + 1); 00215 mainColours[i].blue = eeprom.read(i * 3 + 2); 00216 fadeToBlackBy(dimAmount, mainColours[i]); 00217 } 00218 show(); 00219 } 00220 00221 void LEDs::fadeToBlackBy(uint8_t amount, rgb_color& colour) 00222 { 00223 //fades colour by 'amount' 256ths 00224 if (amount) 00225 { 00226 colour.red = (uint8_t)(((uint16_t)colour.red * (uint16_t)amount + 128)/256); 00227 colour.green = (uint8_t)(((uint16_t)colour.green * (uint16_t)amount + 128)/256); 00228 colour.blue = (uint8_t)(((uint16_t)colour.blue * (uint16_t)amount + 128)/256); 00229 } 00230 }
Generated on Tue Jul 19 2022 02:43:48 by
1.7.2