Permet de créer, envoyer, filtrer et décrypter la trame meteo.

Dependents:   Simulation_carte_maitresse

Committer:
Station_Meteo_Laos
Date:
Sat Jun 29 20:14:50 2019 +0000
Revision:
5:a90b50a19ae4
Parent:
4:caf7ef3d4d6e
Permet de creer, envoyer, filtrer et decrypter la trame meteo.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Station_Meteo_Laos 0:f442198ae6b0 1 #include "meteoCAN.h"
Station_Meteo_Laos 0:f442198ae6b0 2 #include "mbed.h"
Station_Meteo_Laos 0:f442198ae6b0 3
Station_Meteo_Laos 5:a90b50a19ae4 4 //Notre Constructeur, prend en argument les pins utiliser pour communiquer en CAN
Station_Meteo_Laos 0:f442198ae6b0 5 meteoCAN::meteoCAN(PinName rd, PinName td):can1(rd, td){}
Station_Meteo_Laos 0:f442198ae6b0 6
Station_Meteo_Laos 5:a90b50a19ae4 7 //Créer et envoie la trame météo*, avec l’ID donnée en argument.
Station_Meteo_Laos 0:f442198ae6b0 8 bool meteoCAN::send(uint16_t canID, float temperature, float humidite, float irradiance, float vitesse_vent, char direction_vent){
Station_Meteo_Laos 0:f442198ae6b0 9 uint16_t temp, humi, irra, vite;
Station_Meteo_Laos 0:f442198ae6b0 10 char trame[50];
Station_Meteo_Laos 0:f442198ae6b0 11
Station_Meteo_Laos 0:f442198ae6b0 12 temp = uint16_t(temperature*100);
Station_Meteo_Laos 0:f442198ae6b0 13 trame[0] = temp & 255; //temperature octet faible
Station_Meteo_Laos 0:f442198ae6b0 14 trame[1] = temp >> 8; //temperature octet fort
Station_Meteo_Laos 0:f442198ae6b0 15
Station_Meteo_Laos 0:f442198ae6b0 16 humi = uint16_t(humidite*10);
Station_Meteo_Laos 0:f442198ae6b0 17 trame[2] = humi & 255; //humidite octet faible
Station_Meteo_Laos 0:f442198ae6b0 18 trame[3] = humi >> 8; //humidite octet fort
Station_Meteo_Laos 0:f442198ae6b0 19
Station_Meteo_Laos 0:f442198ae6b0 20 irra = uint16_t(irradiance);
Station_Meteo_Laos 0:f442198ae6b0 21 trame[4] = irra & 255; //irradiance octet faible
Station_Meteo_Laos 0:f442198ae6b0 22 trame[5] = irra >> 8; //irradiance octet fort
Station_Meteo_Laos 0:f442198ae6b0 23
Station_Meteo_Laos 0:f442198ae6b0 24 vite = uint16_t(vitesse_vent*100);
Station_Meteo_Laos 0:f442198ae6b0 25 trame[6] = vite & 255; //vitesse_vent octet faible
Station_Meteo_Laos 0:f442198ae6b0 26 trame[7] = vite >> 8; //vitesse_vent octet fort
Station_Meteo_Laos 0:f442198ae6b0 27
Station_Meteo_Laos 0:f442198ae6b0 28 trame[3] = (direction_vent<<4 & 0xF0) + (trame[3] & 0x0F);
Station_Meteo_Laos 0:f442198ae6b0 29
Station_Meteo_Laos 0:f442198ae6b0 30 // send value to CAN bus and monitor return value to check if CAN
Station_Meteo_Laos 0:f442198ae6b0 31 // message was sent successfully. If so display, increment and toggle
Station_Meteo_Laos 0:f442198ae6b0 32 if (can1.write(CANMessage(canID, trame, 8))) {
Station_Meteo_Laos 0:f442198ae6b0 33 }
Station_Meteo_Laos 0:f442198ae6b0 34 else{
Station_Meteo_Laos 0:f442198ae6b0 35 can1.reset(); // Reset CANbus if there is a problem
Station_Meteo_Laos 0:f442198ae6b0 36 }
Station_Meteo_Laos 0:f442198ae6b0 37 wait(1.0); // wait a second
Station_Meteo_Laos 0:f442198ae6b0 38
Station_Meteo_Laos 0:f442198ae6b0 39 return true;
Station_Meteo_Laos 1:0a49398a7bb3 40 }
Station_Meteo_Laos 1:0a49398a7bb3 41
Station_Meteo_Laos 5:a90b50a19ae4 42 //Renvoie vrai si l’ID du message CAN reçu correspond à l’ID donnée en argument. Renvoie faux sinon
Station_Meteo_Laos 4:caf7ef3d4d6e 43 bool meteoCAN::filtreID(CANMessage &msg, uint16_t IDtoFilter){
Station_Meteo_Laos 4:caf7ef3d4d6e 44 if(IDtoFilter == msg.id) return true;
Station_Meteo_Laos 1:0a49398a7bb3 45 else return false;
Station_Meteo_Laos 2:559043c0120e 46 }
Station_Meteo_Laos 2:559043c0120e 47
Station_Meteo_Laos 5:a90b50a19ae4 48 //Décrypte la trame météo renvoie en float la température.
Station_Meteo_Laos 2:559043c0120e 49 float meteoCAN::getTemperature(CANMessage &msg){
Station_Meteo_Laos 2:559043c0120e 50 return float(float(( msg.data[0] & 0xFF) + ( msg.data[1]<<8 & 0xFF00))/100);
Station_Meteo_Laos 2:559043c0120e 51 }
Station_Meteo_Laos 2:559043c0120e 52
Station_Meteo_Laos 5:a90b50a19ae4 53 //Décrypte la trame météo renvoie en float l’humidite.
Station_Meteo_Laos 2:559043c0120e 54 float meteoCAN::getHumidite(CANMessage &msg){
Station_Meteo_Laos 2:559043c0120e 55 return float(float((msg.data[2] & 0xFF) + (msg.data[3]<<8 & 0x0F00))/10);
Station_Meteo_Laos 2:559043c0120e 56 }
Station_Meteo_Laos 2:559043c0120e 57
Station_Meteo_Laos 5:a90b50a19ae4 58 //Décrypte la trame météo renvoie uint16_t l’irradiance.
Station_Meteo_Laos 2:559043c0120e 59 uint16_t meteoCAN::getIrradiance(CANMessage &msg){
Station_Meteo_Laos 2:559043c0120e 60 return uint16_t((msg.data[4] & 0xFF) + (msg.data[5]<<8 & 0xFF00));
Station_Meteo_Laos 2:559043c0120e 61 }
Station_Meteo_Laos 2:559043c0120e 62
Station_Meteo_Laos 5:a90b50a19ae4 63 //Décrypte la trame météo renvoie en float la vitesse du vent.
Station_Meteo_Laos 3:7715f6cccb3a 64 float meteoCAN::getVitesseVent(CANMessage &msg){
Station_Meteo_Laos 2:559043c0120e 65 return float(float((msg.data[6] & 0xFF) + (msg.data[7]<<8 & 0xFF00))/100);
Station_Meteo_Laos 2:559043c0120e 66 }
Station_Meteo_Laos 2:559043c0120e 67
Station_Meteo_Laos 5:a90b50a19ae4 68 //Décrypte la trame météo renvoie en uint8_t la direction du vent.
Station_Meteo_Laos 3:7715f6cccb3a 69 uint8_t meteoCAN::getDirectionVent(CANMessage &msg){
Station_Meteo_Laos 2:559043c0120e 70 return uint8_t((msg.data[3] & 0xF0)>>4);
Station_Meteo_Laos 2:559043c0120e 71 }
Station_Meteo_Laos 2:559043c0120e 72