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.
main.cpp@6:9aeb18bebe85, 2019-12-13 (annotated)
- Committer:
- guilhemMBED
- Date:
- Fri Dec 13 16:06:03 2019 +0000
- Revision:
- 6:9aeb18bebe85
- Parent:
- 5:7a63cb7ec666
- Child:
- 7:278339a9eeba
CAN + capteur couleur working
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
guilhemMBED | 0:eace82fb39de | 1 | #include "mbed.h" |
guilhemMBED | 0:eace82fb39de | 2 | #include "glibr.h" |
guilhemMBED | 1:7cd9426abb58 | 3 | |
guilhemMBED | 0:eace82fb39de | 4 | Serial USB_link(USBTX, USBRX); // USB initialization |
guilhemMBED | 3:f41f244de0b3 | 5 | |
guilhemMBED | 6:9aeb18bebe85 | 6 | //PwmOut LED(A2); // LED initialization |
guilhemMBED | 5:7a63cb7ec666 | 7 | |
guilhemMBED | 3:f41f244de0b3 | 8 | glibr capt1(D4,D5); // I²C initialization : D4 = SDA ; D5 = SCL |
guilhemMBED | 5:7a63cb7ec666 | 9 | |
guilhemMBED | 6:9aeb18bebe85 | 10 | CAN can1(PA_11, PA_12); // CAN initialization |
guilhemMBED | 6:9aeb18bebe85 | 11 | |
guilhemMBED | 6:9aeb18bebe85 | 12 | CANMessage msg; |
guilhemMBED | 0:eace82fb39de | 13 | |
guilhemMBED | 3:f41f244de0b3 | 14 | bool initialization(void) |
guilhemMBED | 3:f41f244de0b3 | 15 | { |
guilhemMBED | 3:f41f244de0b3 | 16 | // USB initialization |
guilhemMBED | 3:f41f244de0b3 | 17 | USB_link.baud(115200); |
guilhemMBED | 6:9aeb18bebe85 | 18 | USB_link.printf("Debut prog\r\n"); |
guilhemMBED | 6:9aeb18bebe85 | 19 | |
guilhemMBED | 6:9aeb18bebe85 | 20 | // CAN init |
guilhemMBED | 6:9aeb18bebe85 | 21 | can1.frequency(1000000); |
guilhemMBED | 3:f41f244de0b3 | 22 | |
guilhemMBED | 5:7a63cb7ec666 | 23 | // LED initialization |
guilhemMBED | 6:9aeb18bebe85 | 24 | // LED.period_ms(10); |
guilhemMBED | 6:9aeb18bebe85 | 25 | // LED.write(0.5); |
guilhemMBED | 5:7a63cb7ec666 | 26 | |
guilhemMBED | 5:7a63cb7ec666 | 27 | // sensor initalization |
guilhemMBED | 5:7a63cb7ec666 | 28 | if( (capt1.ginit()) && (capt1.enableLightSensor(true)) && (capt1.enableProximitySensor(true)) ) { |
guilhemMBED | 3:f41f244de0b3 | 29 | return true; |
guilhemMBED | 3:f41f244de0b3 | 30 | } else { |
guilhemMBED | 3:f41f244de0b3 | 31 | return false; |
guilhemMBED | 3:f41f244de0b3 | 32 | } |
guilhemMBED | 3:f41f244de0b3 | 33 | } |
guilhemMBED | 0:eace82fb39de | 34 | |
guilhemMBED | 1:7cd9426abb58 | 35 | int main (void) |
guilhemMBED | 1:7cd9426abb58 | 36 | { |
guilhemMBED | 6:9aeb18bebe85 | 37 | uint16_t r,g,b; |
guilhemMBED | 6:9aeb18bebe85 | 38 | uint8_t a ; |
guilhemMBED | 5:7a63cb7ec666 | 39 | char color; |
guilhemMBED | 6:9aeb18bebe85 | 40 | |
guilhemMBED | 5:7a63cb7ec666 | 41 | if (initialization()) USB_link.printf("Initialization complete \r\n"); |
guilhemMBED | 5:7a63cb7ec666 | 42 | else USB_link.printf("Error during initialization\r\n"); |
guilhemMBED | 6:9aeb18bebe85 | 43 | |
guilhemMBED | 0:eace82fb39de | 44 | while(1) { |
guilhemMBED | 5:7a63cb7ec666 | 45 | capt1.readRedLight(r); |
guilhemMBED | 5:7a63cb7ec666 | 46 | capt1.readGreenLight(g); |
guilhemMBED | 5:7a63cb7ec666 | 47 | capt1.readBlueLight(b); |
guilhemMBED | 5:7a63cb7ec666 | 48 | capt1.readProximity(a); |
guilhemMBED | 6:9aeb18bebe85 | 49 | |
guilhemMBED | 5:7a63cb7ec666 | 50 | //display of red, green and proximty variables |
guilhemMBED | 6:9aeb18bebe85 | 51 | //USB_link.printf("r: %hu g : %hu prox : %hu \r\n ",r,g,a); |
guilhemMBED | 3:f41f244de0b3 | 52 | |
guilhemMBED | 6:9aeb18bebe85 | 53 | if (a<200) { |
guilhemMBED | 5:7a63cb7ec666 | 54 | color = 0 ; // 0 means no object being measured |
guilhemMBED | 5:7a63cb7ec666 | 55 | } else if (r > g ) { |
guilhemMBED | 5:7a63cb7ec666 | 56 | color = 1 ; // 1 means red |
guilhemMBED | 6:9aeb18bebe85 | 57 | } else { |
guilhemMBED | 5:7a63cb7ec666 | 58 | color = 2 ; // 2 means green |
guilhemMBED | 5:7a63cb7ec666 | 59 | } |
guilhemMBED | 6:9aeb18bebe85 | 60 | //USB_link.printf("color : %hu \r\n", color); |
guilhemMBED | 6:9aeb18bebe85 | 61 | |
guilhemMBED | 6:9aeb18bebe85 | 62 | // read of the CAN |
guilhemMBED | 6:9aeb18bebe85 | 63 | if (can1.read(msg)) { |
guilhemMBED | 6:9aeb18bebe85 | 64 | // verification of the ID ( 0x4B0 ) |
guilhemMBED | 6:9aeb18bebe85 | 65 | if (msg.id==1200) { |
guilhemMBED | 6:9aeb18bebe85 | 66 | can1.write(CANMessage(1200,&color,1)); // writing of the color value on the CAN bus |
guilhemMBED | 6:9aeb18bebe85 | 67 | } |
guilhemMBED | 6:9aeb18bebe85 | 68 | } |
guilhemMBED | 6:9aeb18bebe85 | 69 | |
guilhemMBED | 5:7a63cb7ec666 | 70 | } |
guilhemMBED | 3:f41f244de0b3 | 71 | |
guilhemMBED | 1:7cd9426abb58 | 72 | } |