RGB LEDs Control
Dependencies: mbed QEI PololuLedStrip
Diff: main.cpp
- Revision:
- 8:2a4298cbae3f
- Parent:
- 6:0882f93da02f
diff -r 050f086f6f5c -r 2a4298cbae3f main.cpp --- a/main.cpp Wed Nov 01 23:15:43 2017 +0000 +++ b/main.cpp Sat May 04 18:51:43 2019 +0000 @@ -1,48 +1,154 @@ #include "mbed.h" #include "PololuLedStrip.h" +#include "QEI.h" +#include "MotorControl.h" +#include "RgbControl.h" -PololuLedStrip ledStrip(D8); +//Serial bt(USBTX,USBRX); + +Serial bt(D1,D0); + +InterruptIn LeftReset(PF_0); +InterruptIn RightReset(PB_4); + +QEI re(PA_5,PA_7,NC,220); -#define LED_COUNT 60 -rgb_color colors[LED_COUNT]; +Ticker timer1; //control Position +Timeout ResetTimer; + -Timer timer; +int targetDis; +char rxData[7]; +bool flagRx = 0; +int dayLed; -// Converts a color from the HSV representation to RGB. -rgb_color hsvToRgb(float h, float s, float v) +int R; +int G; +int B; + +void ReadData() { - int i = floor(h * 6); - float f = h * 6 - i; - float p = v * (1 - s); - float q = v * (1 - f * s); - float t = v * (1 - (1 - f) * s); - float r = 0, g = 0, b = 0; - switch(i % 6){ - case 0: r = v; g = t; b = p; break; - case 1: r = q; g = v; b = p; break; - case 2: r = p; g = v; b = t; break; - case 3: r = p; g = q; b = v; break; - case 4: r = t; g = p; b = v; break; - case 5: r = v; g = p; b = q; break; - } - return (rgb_color){r * 255, g * 255, b * 255}; + char inChar; + static char rxCount = 0; + static char rxBuf[7]; + + while(1 == bt.readable()) + { + + inChar = bt.getc(); + + if('<' == inChar) + { + rxCount = 1; + } + + else if (rxCount > 0 && rxCount <8) + { + rxBuf[rxCount-1] = inChar; + rxCount++; + } + + else if ( 8 == rxCount && '>' == inChar) + { + rxCount = 0; + flagRx = 1; + memcpy(rxData, rxBuf, 7); + + } + } +} + + +void Reset_L() +{ + MotorTest(0); + re.reset(); +} + +void Reset_R() +{ + MotorTest(0); +} + +void Stop() +{ + MotorTest(0); } int main() { - timer.start(); + + bt.baud(115200); + bt.puts("START\n"); + MotorTest(2); + ResetTimer.attach(&Stop,15); + - while(1) - { - // Update the colors array. - uint32_t time = timer.read_ms(); - for(int i = 0; i < LED_COUNT; i++) + LeftReset.rise(&Reset_L); + RightReset.rise(&Reset_R); + + char tmpCommand[4] = {0,}; // [ADD] command + int rxVal = 0; + + int modeMotor = 0; + int modeNum = 0; + bt.attach(&ReadData, Serial::RxIrq); + + while(1) { - uint8_t phase = (time >> 4) - (i << 2); - colors[i] = hsvToRgb(phase / 256.0, 1.0, 1.0); + + if (1 == flagRx) + { + flagRx = 0; + tmpCommand[0] = rxData[0]; + tmpCommand[1] = rxData[1]; + tmpCommand[2] = rxData[2]; + rxVal = atoi(rxData+3); + + if (0 == strcmp(tmpCommand,"MOT")) + { + bt.putc(tmpCommand[0]); + modeMotor = rxVal; + MotorTest(modeMotor); + + } + + else if (0 == strcmp(tmpCommand,"POS")) + { + bt.putc(tmpCommand[0]); + targetDis = rxVal; + timer1.attach(&ControlPos,0.1); + } + + else if (0 == strcmp(tmpCommand,"MOD")) + { + bt.putc(tmpCommand[0]); + modeNum = rxVal; + ModeSelect(modeNum); + + } + + else if (0 == strcmp(tmpCommand,"DAY")) + { + ModeSelect(4); + bt.putc(tmpCommand[0]); + targetDis = atoi(rxData+5); // [ADD] return 2 characters in the back of 4 characters + dayLed = 0.01*(atoi(rxData+3)-atoi(rxData+5)); // [ADD] return 2 cahracters in the front of 4 characters + timer1.attach(&ControlPos,0.1); + } + + } + +// int pulse = re.getPulses(); +// int d = pulse/220; +// int e = LeftReset.read(); +// bt.printf("%d %d \n",d,e); + + wait(0.1); + + } - - // Send the colors to the LED strip. - ledStrip.write(colors, LED_COUNT); - } -} + + + +} \ No newline at end of file