RGB LEDs Control
Dependencies: mbed QEI PololuLedStrip
Revision 8:2a4298cbae3f, committed 2019-05-04
- Comitter:
- whutsup
- Date:
- Sat May 04 18:51:43 2019 +0000
- Parent:
- 7:050f086f6f5c
- Commit message:
- Lune RGB LEDs control;
Changed in this revision
diff -r 050f086f6f5c -r 2a4298cbae3f MotorControl.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MotorControl.cpp Sat May 04 18:51:43 2019 +0000 @@ -0,0 +1,83 @@ +#include "mbed.h" +#include "QEI.h" + +extern Serial bt; +DigitalOut Enable(A0); +DigitalOut M1(PA_1); +DigitalOut M2(PF_1); + + +extern QEI re; +extern Ticker timer1; +extern int targetDis; + +void MotorTest(int mode) +{ + + switch (mode) + + { + case 0 : //stop + + Enable = 0; + timer1.detach(); + + break; + + case 1 : // right + + Enable = 1; + M1 = 1; + M2 = 0; + + break; + + case 2 : // left + + Enable = 1; + M1 = 0; + M2 = 1; + + break; + + } + + +} + +void ControlPos() +{ + + int pulse = re.getPulses(); + int d = pulse/200; + + float ref_d = (targetDis-d); + float abs_ref_d = abs(ref_d); +// float s; +// s = abs_ref_d; + + if(-0.8f > ref_d) + { + Enable = 1; + M1 = 0; + M2 = 1; //(s<3)?s/5:1; + + } + + else if ( 0.8f <ref_d) + { + + Enable = 1; + M1 = 1; //(s<3)?s/5:1; + M2 = 0; + + } + + if (abs_ref_d < 0.8f) + { + + Enable = 0; + timer1.detach(); + + } +} \ No newline at end of file
diff -r 050f086f6f5c -r 2a4298cbae3f MotorControl.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MotorControl.h Sat May 04 18:51:43 2019 +0000 @@ -0,0 +1,2 @@ +void MotorTest(int mode); +void ControlPos(); \ No newline at end of file
diff -r 050f086f6f5c -r 2a4298cbae3f QEI.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/QEI.lib Sat May 04 18:51:43 2019 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/aberk/code/QEI/#5c2ad81551aa
diff -r 050f086f6f5c -r 2a4298cbae3f RgbControl.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RgbControl.cpp Sat May 04 18:51:43 2019 +0000 @@ -0,0 +1,656 @@ +#include "mbed.h" +#include "QEI.h" +#include "PololuLedStrip.h" + + +PololuLedStrip led7(PB_0); +PololuLedStrip led6(PA_12); +PololuLedStrip led5(PB_6); +PololuLedStrip led4(PB_1); +PololuLedStrip led3(PA_8); +PololuLedStrip led2(PA_11); +PololuLedStrip led1(PA_3); +PololuLedStrip led0(PA_4); + +#define LED_COUNT 16 + +rgb_color colors0[LED_COUNT]; +rgb_color colors1[LED_COUNT]; +rgb_color colors2[LED_COUNT]; +rgb_color colors3[LED_COUNT]; +rgb_color colors4[LED_COUNT]; +rgb_color colors5[LED_COUNT]; +rgb_color colors6[LED_COUNT]; +rgb_color colors7[LED_COUNT]; + +Ticker led; + +extern QEI re; +extern int R; +extern int G; +extern int B; +extern Serial bt; +extern int dayLed; + +void LedOn1() +{ + + int pulse = re.getPulses(); + int d = pulse/200; + + if(48>=d) + { + + uint8_t r[8]; + uint8_t g[8]; + uint8_t b[8]; + + for (int i=0 ; i<8 ; i++) + { + int n = (i+1); + int moveL_r = ((R/6)*(d-1*n*6))+R; + int moveL_g = ((G/6)*(d-1*n*6))+G; + int moveL_b = ((B/6)*(d-1*n*6))+B; + + moveL_r = (0 >= moveL_r) ? 0: moveL_r; + moveL_g = (0 >= moveL_g) ? 0: moveL_g; + moveL_b = (0 >= moveL_b) ? 0: moveL_b; + + moveL_r = (R <= moveL_r) ? R: moveL_r; + moveL_g = (G <= moveL_g) ? G: moveL_g; + moveL_b = (B <= moveL_b) ? B: moveL_b; + + + r[i] = moveL_r; + g[i] = moveL_g; + b[i] = moveL_b; + + } + + + + for(int i=0; i<16; i++) + { + colors0[i] = (rgb_color){ r[0], g[0], b[0] }; + colors1[i] = (rgb_color){ r[1], g[1], b[1] }; + colors2[i] = (rgb_color){ r[2], g[2], b[2] }; + colors3[i] = (rgb_color){ r[3], g[3], b[3] }; + colors4[i] = (rgb_color){ r[4], g[4], b[4] }; + colors5[i] = (rgb_color){ r[5], g[5], b[5] }; + colors6[i] = (rgb_color){ r[6], g[6], b[6] }; + colors7[i] = (rgb_color){ r[7], g[7], b[7] }; + } + + + led0.write(colors0, LED_COUNT); + led1.write(colors1, LED_COUNT); + led2.write(colors2, LED_COUNT); + led3.write(colors3, LED_COUNT); + led4.write(colors4, LED_COUNT); + led5.write(colors5, LED_COUNT); + led6.write(colors6, LED_COUNT); + led7.write(colors7, LED_COUNT); + + } + + else if(52<=d) + { + uint8_t r[8]; + uint8_t g[8]; + uint8_t b[8]; + + for (int i=0 ; i<8 ; i++) + { + int moveR_r = -1*(R/6)*(d-1*(52+(i+1)*6))+R; + int moveR_g = -1*(G/6)*(d-1*(52+(i+1)*6))+G; + int moveR_b = -1*(B/6)*(d-1*(52+(i+1)*6))+B; + + + moveR_r = (0 >= moveR_r) ? 0: moveR_r; + moveR_g = (0 >= moveR_g) ? 0: moveR_g; + moveR_b = (0 >= moveR_b) ? 0: moveR_b; + + moveR_r = (R <= moveR_r) ? R: moveR_r; + moveR_g = (G <= moveR_g) ? G: moveR_g; + moveR_b = (B <= moveR_b) ? B: moveR_b; + + + r[i] = moveR_r; + g[i] = moveR_g; + b[i] = moveR_b; + } + + + + for(int i=0; i<16; i++) + { + colors0[i] = (rgb_color){ r[0], g[0], b[0] }; + colors1[i] = (rgb_color){ r[1], g[1], b[1] }; + colors2[i] = (rgb_color){ r[2], g[2], b[2] }; + colors3[i] = (rgb_color){ r[3], g[3], b[3] }; + colors4[i] = (rgb_color){ r[4], g[4], b[4] }; + colors5[i] = (rgb_color){ r[5], g[5], b[5] }; + colors6[i] = (rgb_color){ r[6], g[6], b[6] }; + colors7[i] = (rgb_color){ r[7], g[7], b[7] }; + } + + + led0.write(colors0, LED_COUNT); + led1.write(colors1, LED_COUNT); + led2.write(colors2, LED_COUNT); + led3.write(colors3, LED_COUNT); + led4.write(colors4, LED_COUNT); + led5.write(colors5, LED_COUNT); + led6.write(colors6, LED_COUNT); + led7.write(colors7, LED_COUNT); + + } + + else + { + + for(int i=0; i<16; i++) + { + colors0[i] = (rgb_color){ R, G, B }; + colors1[i] = (rgb_color){ R, G, B }; + colors2[i] = (rgb_color){ R, G, B }; + colors3[i] = (rgb_color){ R, G, B }; + colors4[i] = (rgb_color){ R, G, B }; + colors5[i] = (rgb_color){ R, G, B }; + colors6[i] = (rgb_color){ R, G, B }; + colors7[i] = (rgb_color){ R, G, B }; + } + + + led0.write(colors0, LED_COUNT); + led1.write(colors1, LED_COUNT); + led2.write(colors2, LED_COUNT); + led3.write(colors3, LED_COUNT); + led4.write(colors4, LED_COUNT); + led5.write(colors5, LED_COUNT); + led6.write(colors6, LED_COUNT); + led7.write(colors7, LED_COUNT); + } + +} + + +void LedOn2() +{ + + int pulse = re.getPulses(); + int p = pulse/200; + uint8_t k[8]; + int n = 8; + int d = 6; + double bi= 0.02f; + + uint8_t r[8]; + uint8_t g[8]; + uint8_t b[8]; + + for (int i=0 ; i<8 ; i++) + { + k[i] = ((n-i)*d)/log((1/bi)+1); + } + + + if(48>=p) + { + + + for (int i=0 ; i<8 ; i++) + { + + int moveL_r = R*bi*(10^((p-1*i*d)/k[i])-1); + int moveL_g = G*bi*(10^((p-1*i*d)/k[i])-1); + int moveL_b = B*bi*(10^((p-1*i*d)/k[i])-1);; + + moveL_r = (0 >= moveL_r) ? 0: moveL_r; + moveL_g = (0 >= moveL_g) ? 0: moveL_g; + moveL_b = (0 >= moveL_b) ? 0: moveL_b; + + moveL_r = (R <= moveL_r) ? R: moveL_r; + moveL_g = (G <= moveL_g) ? G: moveL_g; + moveL_b = (B <= moveL_b) ? B: moveL_b; + + r[i] = moveL_r; + g[i] = moveL_g; + b[i] = moveL_b; + + } + + + for(int i=0; i<16; i++) + { + colors0[i] = (rgb_color){ r[0], g[0], b[0] }; + colors1[i] = (rgb_color){ r[1], g[1], b[1] }; + colors2[i] = (rgb_color){ r[2], g[2], b[2] }; + colors3[i] = (rgb_color){ r[3], g[3], b[3] }; + colors4[i] = (rgb_color){ r[4], g[4], b[4] }; + colors5[i] = (rgb_color){ r[5], g[5], b[5] }; + colors6[i] = (rgb_color){ r[6], g[6], b[6] }; + colors7[i] = (rgb_color){ r[7], g[7], b[7] }; + } + + + led0.write(colors0, LED_COUNT); + led1.write(colors1, LED_COUNT); + led2.write(colors2, LED_COUNT); + led3.write(colors3, LED_COUNT); + led4.write(colors4, LED_COUNT); + led5.write(colors5, LED_COUNT); + led6.write(colors6, LED_COUNT); + led7.write(colors7, LED_COUNT); + + + + } + + else if(49<=p) + { + + + for (int i=0 ; i<8 ; i++) + { + + int moveR_r = R*bi*(10^((-1*p+(n+1+i)*d)/k[n-1-i])-1); + int moveR_g = G*bi*(10^((-1*p+(n+1+i)*d)/k[n-1-i])-1); + int moveR_b = B*bi*(10^((-1*p+(n+1+i)*d)/k[n-1-i])-1); + + moveR_r = (0 >= moveR_r) ? 0: moveR_r; + moveR_g = (0 >= moveR_g) ? 0: moveR_g; + moveR_b = (0 >= moveR_b) ? 0: moveR_b; + + moveR_r = (R <= moveR_r) ? R: moveR_r; + moveR_g = (G <= moveR_g) ? G: moveR_g; + moveR_b = (B <= moveR_b) ? B: moveR_b; + + r[i] = moveR_r; + g[i] = moveR_g; + b[i] = moveR_b; + + } + + + + for(int i=0; i<16; i++) + { + colors0[i] = (rgb_color){ r[0], g[0], b[0] }; + colors1[i] = (rgb_color){ r[1], g[1], b[1] }; + colors2[i] = (rgb_color){ r[2], g[2], b[2] }; + colors3[i] = (rgb_color){ r[3], g[3], b[3] }; + colors4[i] = (rgb_color){ r[4], g[4], b[4] }; + colors5[i] = (rgb_color){ r[5], g[5], b[5] }; + colors6[i] = (rgb_color){ r[6], g[6], b[6] }; + colors7[i] = (rgb_color){ r[7], g[7], b[7] }; + } + + + led0.write(colors0, LED_COUNT); + led1.write(colors1, LED_COUNT); + led2.write(colors2, LED_COUNT); + led3.write(colors3, LED_COUNT); + led4.write(colors4, LED_COUNT); + led5.write(colors5, LED_COUNT); + led6.write(colors6, LED_COUNT); + led7.write(colors7, LED_COUNT); + + } + +// else +// { +// +// for(int i=0; i<16; i++) +// { +// colors0[i] = (rgb_color){ R, G, B }; +// colors1[i] = (rgb_color){ R, G, B }; +// colors2[i] = (rgb_color){ R, G, B }; +// colors3[i] = (rgb_color){ R, G, B }; +// colors4[i] = (rgb_color){ R, G, B }; +// colors5[i] = (rgb_color){ R, G, B }; +// colors6[i] = (rgb_color){ R, G, B }; +// colors7[i] = (rgb_color){ R, G, B }; +// } +// +// +// led0.write(colors0, LED_COUNT); +// led1.write(colors1, LED_COUNT); +// led2.write(colors2, LED_COUNT); +// led3.write(colors3, LED_COUNT); +// led4.write(colors4, LED_COUNT); +// led5.write(colors5, LED_COUNT); +// led6.write(colors6, LED_COUNT); +// led7.write(colors7, LED_COUNT); +// } + +} + +void LedOn3() +{ + + int pulse = re.getPulses(); + int d = pulse/200; + + + if(48>=d) + { + + uint8_t r[8]; + uint8_t g[8]; + uint8_t b[8]; + + + for (int i=0 ; i<8 ; i++) + { + int moveL_r = ((R/(48-6*(i)))+1)*(d-48)+R; + int moveL_g = ((G/(48-6*(i)))+1)*(d-48)+G; + int moveL_b = ((B/(48-6*(i)))+1)*(d-48)+B; + + r[i] = (0 >= moveL_r) ? 0: moveL_r; + g[i] = (0 >= moveL_g) ? 0: moveL_g; + b[i] = (0 >= moveL_b) ? 0: moveL_b; + } + + + + for(int i=0; i<16; i++) + { + colors0[i] = (rgb_color){ r[0], g[0], b[0] }; + colors1[i] = (rgb_color){ r[1], g[1], b[1] }; + colors2[i] = (rgb_color){ r[2], g[2], b[2] }; + colors3[i] = (rgb_color){ r[3], g[3], b[3] }; + colors4[i] = (rgb_color){ r[4], g[4], b[4] }; + colors5[i] = (rgb_color){ r[5], g[5], b[5] }; + colors6[i] = (rgb_color){ r[6], g[6], b[6] }; + colors7[i] = (rgb_color){ r[7], g[7], b[7] }; + } + + + led0.write(colors0, LED_COUNT); + led1.write(colors1, LED_COUNT); + led2.write(colors2, LED_COUNT); + led3.write(colors3, LED_COUNT); + led4.write(colors4, LED_COUNT); + led5.write(colors5, LED_COUNT); + led6.write(colors6, LED_COUNT); + led7.write(colors7, LED_COUNT); + + } + + else if(52<=d) + { + uint8_t r[8]; + uint8_t g[8]; + uint8_t b[8]; + + for (int i=0 ; i<8 ; i++) + { + int moveR_r = ((-1*R/(6+6*(i)))-1)*(d-52)+R; + int moveR_g = ((-1*G/(6+6*(i)))-1)*(d-52)+G; + int moveR_b = ((-1*B/(6+6*(i)))-1)*(d-52)+B; + + r[i] = (0 >= moveR_r) ? 0:moveR_r ; + g[i] = (0 >= moveR_g) ? 0:moveR_g ; + b[i] = (0 >= moveR_b) ? 0:moveR_b ; + } + + + + for(int i=0; i<16; i++) + { + colors0[i] = (rgb_color){ r[0], g[0], b[0] }; + colors1[i] = (rgb_color){ r[1], g[1], b[1] }; + colors2[i] = (rgb_color){ r[2], g[2], b[2] }; + colors3[i] = (rgb_color){ r[3], g[3], b[3] }; + colors4[i] = (rgb_color){ r[4], g[4], b[4] }; + colors5[i] = (rgb_color){ r[5], g[5], b[5] }; + colors6[i] = (rgb_color){ r[6], g[6], b[6] }; + colors7[i] = (rgb_color){ r[7], g[7], b[7] }; + } + + + led0.write(colors0, LED_COUNT); + led1.write(colors1, LED_COUNT); + led2.write(colors2, LED_COUNT); + led3.write(colors3, LED_COUNT); + led4.write(colors4, LED_COUNT); + led5.write(colors5, LED_COUNT); + led6.write(colors6, LED_COUNT); + led7.write(colors7, LED_COUNT); + + } + + else + { + + for(int i=0; i<16; i++) + { + colors0[i] = (rgb_color){ R, G, B }; + colors1[i] = (rgb_color){ R, G, B }; + colors2[i] = (rgb_color){ R, G, B }; + colors3[i] = (rgb_color){ R, G, B }; + colors4[i] = (rgb_color){ R, G, B }; + colors5[i] = (rgb_color){ R, G, B }; + colors6[i] = (rgb_color){ R, G, B }; + colors7[i] = (rgb_color){ R, G, B }; + } + + + led0.write(colors0, LED_COUNT); + led1.write(colors1, LED_COUNT); + led2.write(colors2, LED_COUNT); + led3.write(colors3, LED_COUNT); + led4.write(colors4, LED_COUNT); + led5.write(colors5, LED_COUNT); + led6.write(colors6, LED_COUNT); + led7.write(colors7, LED_COUNT); + } + +} + +void MoonPhase() +{ + + int d = dayLed;; + + if(48>=d) + { + + uint8_t r[8]; + uint8_t g[8]; + uint8_t b[8]; + + for (int i=0 ; i<8 ; i++) + { + int n = (i+1); + int moveL_r = ((R/6)*(d-1*n*6))+R; + int moveL_g = ((G/6)*(d-1*n*6))+G; + int moveL_b = ((B/6)*(d-1*n*6))+B; + + moveL_r = (0 >= moveL_r) ? 0: moveL_r; + moveL_g = (0 >= moveL_g) ? 0: moveL_g; + moveL_b = (0 >= moveL_b) ? 0: moveL_b; + + moveL_r = (R <= moveL_r) ? R: moveL_r; + moveL_g = (G <= moveL_g) ? G: moveL_g; + moveL_b = (B <= moveL_b) ? B: moveL_b; + + + r[i] = moveL_r; + g[i] = moveL_g; + b[i] = moveL_b; + + } + + + + for(int i=0; i<16; i++) + { + colors0[i] = (rgb_color){ r[0], g[0], b[0] }; + colors1[i] = (rgb_color){ r[1], g[1], b[1] }; + colors2[i] = (rgb_color){ r[2], g[2], b[2] }; + colors3[i] = (rgb_color){ r[3], g[3], b[3] }; + colors4[i] = (rgb_color){ r[4], g[4], b[4] }; + colors5[i] = (rgb_color){ r[5], g[5], b[5] }; + colors6[i] = (rgb_color){ r[6], g[6], b[6] }; + colors7[i] = (rgb_color){ r[7], g[7], b[7] }; + } + + + led0.write(colors0, LED_COUNT); + led1.write(colors1, LED_COUNT); + led2.write(colors2, LED_COUNT); + led3.write(colors3, LED_COUNT); + led4.write(colors4, LED_COUNT); + led5.write(colors5, LED_COUNT); + led6.write(colors6, LED_COUNT); + led7.write(colors7, LED_COUNT); + + } + + else if(52<=d) + { + uint8_t r[8]; + uint8_t g[8]; + uint8_t b[8]; + + for (int i=0 ; i<8 ; i++) + { + int moveR_r = -1*(R/6)*(d-1*(52+(i+1)*6))+R; + int moveR_g = -1*(G/6)*(d-1*(52+(i+1)*6))+G; + int moveR_b = -1*(B/6)*(d-1*(52+(i+1)*6))+B; + + + moveR_r = (0 >= moveR_r) ? 0: moveR_r; + moveR_g = (0 >= moveR_g) ? 0: moveR_g; + moveR_b = (0 >= moveR_b) ? 0: moveR_b; + + moveR_r = (R <= moveR_r) ? R: moveR_r; + moveR_g = (G <= moveR_g) ? G: moveR_g; + moveR_b = (B <= moveR_b) ? B: moveR_b; + + + r[i] = moveR_r; + g[i] = moveR_g; + b[i] = moveR_b; + } + + + + for(int i=0; i<16; i++) + { + colors0[i] = (rgb_color){ r[0], g[0], b[0] }; + colors1[i] = (rgb_color){ r[1], g[1], b[1] }; + colors2[i] = (rgb_color){ r[2], g[2], b[2] }; + colors3[i] = (rgb_color){ r[3], g[3], b[3] }; + colors4[i] = (rgb_color){ r[4], g[4], b[4] }; + colors5[i] = (rgb_color){ r[5], g[5], b[5] }; + colors6[i] = (rgb_color){ r[6], g[6], b[6] }; + colors7[i] = (rgb_color){ r[7], g[7], b[7] }; + } + + + led0.write(colors0, LED_COUNT); + led1.write(colors1, LED_COUNT); + led2.write(colors2, LED_COUNT); + led3.write(colors3, LED_COUNT); + led4.write(colors4, LED_COUNT); + led5.write(colors5, LED_COUNT); + led6.write(colors6, LED_COUNT); + led7.write(colors7, LED_COUNT); + + } + + else + { + + for(int i=0; i<16; i++) + { + colors0[i] = (rgb_color){ R, G, B }; + colors1[i] = (rgb_color){ R, G, B }; + colors2[i] = (rgb_color){ R, G, B }; + colors3[i] = (rgb_color){ R, G, B }; + colors4[i] = (rgb_color){ R, G, B }; + colors5[i] = (rgb_color){ R, G, B }; + colors6[i] = (rgb_color){ R, G, B }; + colors7[i] = (rgb_color){ R, G, B }; + } + + + led0.write(colors0, LED_COUNT); + led1.write(colors1, LED_COUNT); + led2.write(colors2, LED_COUNT); + led3.write(colors3, LED_COUNT); + led4.write(colors4, LED_COUNT); + led5.write(colors5, LED_COUNT); + led6.write(colors6, LED_COUNT); + led7.write(colors7, LED_COUNT); + } + +} + + +void ModeSelect(int mode) +{ + + switch(mode) + { + + case 0 : + + led.detach(); + + + break; + + case 1 : + led.detach(); + + R = 119; + G = 66; + B = 255; + + led.attach(&LedOn1, 0.05); + + break; + + case 2 : + + led.detach(); + + R = 119; + G = 66; + B = 255; + + led.attach(&LedOn2, 0.05); + break; + + case 3 : + + led.detach(); + + R = 119; + G = 66; + B = 255; + + led.attach(&LedOn3, 0.05); + + break; + + case 4 : + + led.detach(); + + R = 119; + G = 66; + B = 255; + + led.attach(&MoonPhase, 0.05); + + break; + + } + +} +
diff -r 050f086f6f5c -r 2a4298cbae3f RgbControl.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RgbControl.h Sat May 04 18:51:43 2019 +0000 @@ -0,0 +1,5 @@ +void LedOn1(); +void LedOn2(); +void LedOn3(); +void MoonPhase(); +void ModeSelect(int mode); \ No newline at end of file
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
diff -r 050f086f6f5c -r 2a4298cbae3f mbed.bld --- a/mbed.bld Wed Nov 01 23:15:43 2017 +0000 +++ b/mbed.bld Sat May 04 18:51:43 2019 +0000 @@ -1,1 +1,1 @@ -https://os.mbed.com/users/mbed_official/code/mbed/builds/fb8e0ae1cceb \ No newline at end of file +https://os.mbed.com/users/mbed_official/code/mbed/builds/3a7713b1edbc \ No newline at end of file