Jian Chen
/
mini
demo program of color sensor
main.cpp
- Committer:
- chenjian221
- Date:
- 2013-10-16
- Revision:
- 0:07ac8eb1700b
- Child:
- 1:131a940a952c
File content as of revision 0:07ac8eb1700b:
#include "mbed.h" DigitalOut latch(p15); DigitalOut enable(p16); Serial color_sensor(p9, p10); DigitalOut out_latch(p22); DigitalOut out_enable(p21); SPI spi(p11, p12, p13); SPI out_spi(p5, p6, p7); void RGB_LED(int red, int green, int blue) { unsigned int low_color=0; unsigned int high_color=0; high_color=(blue<<4)|((red&0x3C0)>>6); low_color=(((red&0x3F)<<10)|(green)); spi.write(high_color); spi.write(low_color); latch=1; latch=0; } void out_RGB_LED(int red, int green, int blue) { unsigned int low_color=0; unsigned int high_color=0; high_color=(blue<<4)|((red&0x3C0)>>6); low_color=(((red&0x3F)<<10)|(green)); out_spi.write(high_color); out_spi.write(low_color); out_latch=1; out_latch=0; } void split_RGB(char* color_str) { char *save_ptr; char *p = strtok_r(color_str, ",", &save_ptr); int red = strtol(p, NULL, 10); p = strtok_r(NULL, ",", &save_ptr); int green = strtol(p, NULL, 10); p = strtok_r(NULL, ",", &save_ptr); int blue = strtol(p, NULL, 10); out_RGB_LED(red, green, blue); return; } void get_color() { if(color_sensor.readable()) { printf("get into the function\n"); char str[11]; int i = 0; char c; while(c=color_sensor.getc()) { //printf("read: %c\n", c); if(c!='\r') { str[i] = c; i++; } else { break; } } //color_sensor.scanf("%s", &str); printf("another read: "); printf("%s\n", str); printf("i=%d\n", i); split_RGB(str); } return; } int main() { int red=0; int green=0; int blue=0; spi.format(16,0); spi.frequency(500000); out_spi.format(16,0); out_spi.frequency(500000); enable=0; latch=0; out_enable=0; out_latch=0; color_sensor.baud(38400); wait(2); while(1) { red = rand() % 50; green = rand() % 50; blue = rand() % 50; RGB_LED( red, green, blue); get_color(); wait(1); } }