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.
Diff: main.cpp
- Revision:
- 10:6c00fc4555fd
- Parent:
- 8:190a86f06a07
- Child:
- 11:9d1f00fd8f06
--- a/main.cpp Mon Jan 06 07:58:04 2020 +0000 +++ b/main.cpp Fri Apr 03 19:14:22 2020 +0000 @@ -4,12 +4,7 @@ // Adress #define adress_color_sensor 0x4B0 // same for every sensor #define data_adress_general 0xFE // will talk to every sensor -#define data_adress_sensor 0x00 // Specific to each sensor (between 0x00 and 0xFD) -#define data_adress_return 0xFF // Adress used by every sensor to respond to requests - -// Setup commands -#define setup_LED 0x10 -#define setup_PROXIMITY_THRESHOLD 0x11 +#define data_adress_sensor 0x00 // Specific to each sensor (between 0x00 and 0xFD) // Request commands #define send_RGB 0x00 @@ -17,7 +12,11 @@ #define send_GREEN 0x02 #define send_BLUE 0x03 #define send_PROXIMITY 0x04 -#define send_COLOR 0x08 +#define send_COLOR 0x05 + +// Setup commands +#define setup_LED 0x08 +#define setup_PROXIMITY_THRESHOLD 0x09 // Masks #define HIGH 0xFF00 @@ -25,14 +24,17 @@ Serial USB_link(USBTX, USBRX); // USB initialization -//PwmOut LED(A2); // LED initialization +PwmOut LED(D9); // LED initialization glibr capt1(D4,D5); // I²C initialization : D4 = SDA ; D5 = SCL CAN can1(PA_11, PA_12); // CAN initialization : PA_11 = RD ; PA_12 = TD + + CANMessage msg; - +RawSerial tx(PA_9, NC); +RawSerial rx(NC, PA_10); bool initialization(void) { @@ -42,10 +44,11 @@ // CAN initialization can1.frequency(1000000); - + rx.baud(115200); + // LED initialization - // LED.period_ms(10); - // LED.write(0.5); + LED.period_ms(10); + LED.write(0); // Sensor initalization if( (capt1.ginit()) && (capt1.enableLightSensor(true)) && (capt1.enableProximitySensor(true)) ) { @@ -57,12 +60,11 @@ int main (void) { - + char c; uint16_t r,g,b ; // RGB values in 2 bytes uint8_t a ; // proximity value in 1 byte - char proximity_tresh = 254, color, state, message[8]; - message[0] = data_adress_return; - message[1] = data_adress_sensor; + char proximity_tresh = 250, color, state, message[8]; + message[0] = data_adress_sensor; // (PID) if (initialization()) USB_link.printf("Initialization complete \r\n"); else USB_link.printf("Error during initialization\r\n"); @@ -83,6 +85,12 @@ color = 2 ; // 2 means green } + // serial test : + if(rx.readable()){ + c=rx.getc(); + USB_link.printf("%c",c); + } + //display of red, green and proximty variables //USB_link.printf("r: %hu g : %hu prox : %hu \r\n ",r,g,a); @@ -96,9 +104,10 @@ if ((msg.id==adress_color_sensor)&((msg.data[0]==data_adress_general)|(msg.data[0]==data_adress_sensor))) { state=msg.data[1]; + message[1]=state+0x40; // command switch (state) { - case send_RGB: - message[2]= (char)((r & HIGH)>>8); + case send_RGB: + message[2]= (char)((r & HIGH)>>8); // data message[3]= (char) (r & LOW); message[4]= (char)((g & HIGH)>>8); message[5]= (char) (g & LOW); @@ -135,7 +144,7 @@ break; case setup_LED: - //LED.write(msg.data[2]/258.0); + LED.write(msg.data[2]/258.0); break; case setup_PROXIMITY_THRESHOLD :