Bus Can
Dependencies: mbed ssd1306_library
Diff: main_Carte2.cpp
- Revision:
- 0:793b62d1f1ec
- Child:
- 1:068cd7adbe4d
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main_Carte2.cpp Thu Feb 11 17:05:35 2021 +0000 @@ -0,0 +1,232 @@ +/* + * Copyright (c) 2017-2020 Arm Limited and affiliates. + * SPDX-License-Identifier: Apache-2.0 + */ + +#if !DEVICE_CAN +#error [NOT_SUPPORTED] CAN not supported for this target +#endif + + +#include "mbed.h" +//#include "can.h" +#include "ssd1306.h" + +SSD1306 OLED (I2C_SDA, I2C_SCL); // assumes default I2C address of 0x78 + + +//Ticker ticker; + +DigitalOut ledD9(PA_4); +//DigitalOut ledD8(PA_5); // NON, désouder SB16 si utilisation I2c +//DigitalOut ledD7(PA_6); // NON, désouder SB18 si utilisation I2c +DigitalOut ledD6(PA_7); + +DigitalIn SW4_1(PA_1); +DigitalIn SW4_0(PA_3); + +InterruptIn SW1(PB_4, PullUp); +InterruptIn SW2(PB_5, PullUp); +InterruptIn SW3(PA_8, PullUp); + +/** The constructor takes in RX, and TX pin respectively. + * These pins, for this example, are defined in mbed_app.json + */ +//CAN can1(MBED_CONF_APP_CAN1_RD, MBED_CONF_APP_CAN1_TD); + +CAN can(PA_11, PA_12); +Serial pc(USBTX, USBRX); + +char counter = 123; +char Carte2[] = "K2"; +char Carte2bb[] = "K2bb"; +static int Nb_messT = 0; +int value_SW4=0; + +/* +void send() +{ + //pc.printf("Reception \n"); + if (can.write(CANMessage(1337, &counter, 1))) { + //pc.printf("wloop()\n"); + counter++; + //pc.printf("Message sent: %d\n", counter); + ledD7 = !ledD7; + } +} +*/ + +void AppuiSW1() { + if (can.write(CANMessage(2021, Carte2, 3 , CANData, CANStandard))) {// Rmq : tableau = pointeur + ledD6 = !ledD6; + Nb_messT++; + } +} + +void AppuiSW3() { + if (value_SW4 == 2) { + if (can.write(CANMessage(1111, Carte2bb, 3 , CANData, CANStandard))) {// Rmq : tableau = pointeur + ledD6 = !ledD6; + Nb_messT++; + } + } + if (value_SW4 == 3) { + if (can.write(CANMessage(2222, Carte2bb, 3 , CANData, CANStandard))) {// Rmq : tableau = pointeur + ledD6 = !ledD6; + Nb_messT++; + } + } +} + +int LectureSW4(){ + int ETAT; + int value_SW4_0 = SW4_0.read(); + int value_SW4_1 = SW4_1.read(); + + if (value_SW4_1 == 1) { + if (value_SW4_0 == 1) + ETAT = 3; + else + ETAT = 2; + } + else { + if (value_SW4_0 == 1) + ETAT = 1; + else + ETAT = 0; + } + return ETAT; +} + +/* Reset controleur après court-circuit */ +/* +void AppuiSW3() { + can.reset(); + OLED.cls(); + OLED.locate (0,0); // set text cursor to line 3, column 1 + OLED.printf ("GEII - Bus CAN"); // print to frame buffer + OLED.locate (2,0); // set text cursor to line 3, column 1 + OLED.printf ("SW1"); // print to frame buffer + OLED.locate (3,0); // set text cursor to line 3, column 1 + OLED.printf ("Envoi trame"); // print to frame buffer + OLED.redraw(); +} +*/ +/* +char *conversionIntChaine(int longueurChaine, int Valeur_int, char *ChaineAffichage) +{ + while (longueurChaine >= 0) + { + ChaineAffichage[longueurChaine] = (Valeur_int % 10) + 48; + Valeur_int /= 10; + longueurChaine--; + } + return (ChaineAffichage); +} +*/ +int main() +{ + pc.baud(115200); + can.frequency(500000); + pc.printf("main()\n"); + //ticker.attach(&send, 2.0); + CANMessage msg; + + char Donnees[8]=""; + static int Nb_messR = 0; + + OLED.speed(SSD1306::Medium); // set working frequency + OLED.init(); // initialize SSD1306 + OLED.cls(); // clear frame buffer + /* + OLED.locate (0,0); // set text cursor to line 3, column 1 + OLED.printf ("GEII - Bus CAN"); // print to frame buffer + OLED.locate (2,0); // set text cursor to line 3, column 1 + OLED.printf ("SW1"); // print to frame buffer + OLED.locate (3,0); // set text cursor to line 3, column 1 + OLED.printf ("Envoi trame"); // print to frame buffer + OLED.redraw(); + */ + + SW1.fall(&AppuiSW1); + SW3.fall(&AppuiSW3); + + OLED.locate (0,0); + OLED.printf ("Nb Rx = 0"); + OLED.locate (1,0); + OLED.printf ("Nb Tx = 0"); + + + while (1) { + //pc.printf("Reception message \n"); + value_SW4 = LectureSW4(); + + if (can.read(msg)) { + Nb_messR++; + ledD9 = !ledD9; + + for (int i =0; i < msg.len; i++) + Donnees[i] = msg.data[i]; + + pc.printf("ID = 0x%.3x\r\n", msg.id); //ID sous forme 0x suivi de l'id + pc.printf("Length = %d\r\n", msg.len); + pc.printf("CAN rderrors : %d, CAN tderrors : %d\n", can.rderror(), can.tderror()); + + } + OLED.locate (0,0); + OLED.printf ("Nb Rx = %d", Nb_messR); + OLED.locate (1,0); + OLED.printf ("Nb Tx = %d", Nb_messT); + OLED.locate (3,0); + OLED.printf ("Message recu "); + OLED.locate (4,0); + OLED.printf ("=> "); + OLED.puts(Donnees); + OLED.locate (6,0); + OLED.printf("Valeur REC=%d", can.rderror()); + OLED.locate (7,0); + OLED.printf("Valeur TEC=%d", can.tderror()); + OLED.redraw(); + wait(0.2); + + /* + if(!SW2) //Detect Switch Press + { + ledD7 = !ledD7; + while(!SW2); //Wait for Key release + } + */ + } +} + + + +/* +char * +itoa (int n) +{ + + char *a = malloc (long_int (n) + 1); + int i; + for (i = 0; n > 0; i++) + { + a[i] = n % 10 + 48; + n = n / 10; + } + a[i + 1] = '\0'; + return retourne (a); +} + +char *create_str(int len, int n, int neg, char *str) +{ +while (len >= 0) +{ +str[len] = (n % 10) + 48; +n /= 10; +len--; +} +if (neg < 0) +str[0] = '-'; +return (str); +} +*/