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

Dependents:   Simulation_carte_maitresse

Committer:
Station_Meteo_Laos
Date:
Fri Jun 14 09:17:58 2019 +0000
Revision:
3:7715f6cccb3a
Parent:
2:559043c0120e
Child:
4:caf7ef3d4d6e
fonctions : send, filtreID, getTemperature, getHumidte, getIrradiance, getVitesseVent et getDirectonVent.

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 0:f442198ae6b0 4 meteoCAN::meteoCAN(PinName rd, PinName td):can1(rd, td){}
Station_Meteo_Laos 0:f442198ae6b0 5
Station_Meteo_Laos 0:f442198ae6b0 6 // meteoCAN
Station_Meteo_Laos 0:f442198ae6b0 7 bool meteoCAN::send(uint16_t canID, float temperature, float humidite, float irradiance, float vitesse_vent, char direction_vent){
Station_Meteo_Laos 0:f442198ae6b0 8 uint16_t temp, humi, irra, vite;
Station_Meteo_Laos 0:f442198ae6b0 9 char trame[50];
Station_Meteo_Laos 0:f442198ae6b0 10
Station_Meteo_Laos 0:f442198ae6b0 11 temp = uint16_t(temperature*100);
Station_Meteo_Laos 0:f442198ae6b0 12 trame[0] = temp & 255; //temperature octet faible
Station_Meteo_Laos 0:f442198ae6b0 13 trame[1] = temp >> 8; //temperature octet fort
Station_Meteo_Laos 0:f442198ae6b0 14
Station_Meteo_Laos 0:f442198ae6b0 15 humi = uint16_t(humidite*10);
Station_Meteo_Laos 0:f442198ae6b0 16 trame[2] = humi & 255; //humidite octet faible
Station_Meteo_Laos 0:f442198ae6b0 17 trame[3] = humi >> 8; //humidite octet fort
Station_Meteo_Laos 0:f442198ae6b0 18
Station_Meteo_Laos 0:f442198ae6b0 19 irra = uint16_t(irradiance);
Station_Meteo_Laos 0:f442198ae6b0 20 trame[4] = irra & 255; //irradiance octet faible
Station_Meteo_Laos 0:f442198ae6b0 21 trame[5] = irra >> 8; //irradiance octet fort
Station_Meteo_Laos 0:f442198ae6b0 22
Station_Meteo_Laos 0:f442198ae6b0 23 vite = uint16_t(vitesse_vent*100);
Station_Meteo_Laos 0:f442198ae6b0 24 trame[6] = vite & 255; //vitesse_vent octet faible
Station_Meteo_Laos 0:f442198ae6b0 25 trame[7] = vite >> 8; //vitesse_vent octet fort
Station_Meteo_Laos 0:f442198ae6b0 26
Station_Meteo_Laos 0:f442198ae6b0 27 trame[3] = (direction_vent<<4 & 0xF0) + (trame[3] & 0x0F);
Station_Meteo_Laos 0:f442198ae6b0 28
Station_Meteo_Laos 0:f442198ae6b0 29 // send value to CAN bus and monitor return value to check if CAN
Station_Meteo_Laos 0:f442198ae6b0 30 // message was sent successfully. If so display, increment and toggle
Station_Meteo_Laos 0:f442198ae6b0 31 if (can1.write(CANMessage(canID, trame, 8))) {
Station_Meteo_Laos 0:f442198ae6b0 32 }
Station_Meteo_Laos 0:f442198ae6b0 33 else{
Station_Meteo_Laos 0:f442198ae6b0 34 can1.reset(); // Reset CANbus if there is a problem
Station_Meteo_Laos 0:f442198ae6b0 35 }
Station_Meteo_Laos 0:f442198ae6b0 36 wait(1.0); // wait a second
Station_Meteo_Laos 0:f442198ae6b0 37
Station_Meteo_Laos 0:f442198ae6b0 38 return true;
Station_Meteo_Laos 1:0a49398a7bb3 39 }
Station_Meteo_Laos 1:0a49398a7bb3 40
Station_Meteo_Laos 1:0a49398a7bb3 41 bool meteoCAN::filtreID(uint16_t IDreceived, uint16_t IDtoFilter){
Station_Meteo_Laos 1:0a49398a7bb3 42 if(IDtoFilter == IDreceived) return true;
Station_Meteo_Laos 1:0a49398a7bb3 43 else return false;
Station_Meteo_Laos 2:559043c0120e 44 }
Station_Meteo_Laos 2:559043c0120e 45
Station_Meteo_Laos 2:559043c0120e 46 float meteoCAN::getTemperature(CANMessage &msg){
Station_Meteo_Laos 2:559043c0120e 47 return float(float(( msg.data[0] & 0xFF) + ( msg.data[1]<<8 & 0xFF00))/100);
Station_Meteo_Laos 2:559043c0120e 48 }
Station_Meteo_Laos 2:559043c0120e 49
Station_Meteo_Laos 2:559043c0120e 50 float meteoCAN::getHumidite(CANMessage &msg){
Station_Meteo_Laos 2:559043c0120e 51 return float(float((msg.data[2] & 0xFF) + (msg.data[3]<<8 & 0x0F00))/10);
Station_Meteo_Laos 2:559043c0120e 52 }
Station_Meteo_Laos 2:559043c0120e 53
Station_Meteo_Laos 2:559043c0120e 54 uint16_t meteoCAN::getIrradiance(CANMessage &msg){
Station_Meteo_Laos 2:559043c0120e 55 return uint16_t((msg.data[4] & 0xFF) + (msg.data[5]<<8 & 0xFF00));
Station_Meteo_Laos 2:559043c0120e 56 }
Station_Meteo_Laos 2:559043c0120e 57
Station_Meteo_Laos 3:7715f6cccb3a 58 float meteoCAN::getVitesseVent(CANMessage &msg){
Station_Meteo_Laos 2:559043c0120e 59 return float(float((msg.data[6] & 0xFF) + (msg.data[7]<<8 & 0xFF00))/100);
Station_Meteo_Laos 2:559043c0120e 60 }
Station_Meteo_Laos 2:559043c0120e 61
Station_Meteo_Laos 3:7715f6cccb3a 62 uint8_t meteoCAN::getDirectionVent(CANMessage &msg){
Station_Meteo_Laos 2:559043c0120e 63 return uint8_t((msg.data[3] & 0xF0)>>4);
Station_Meteo_Laos 2:559043c0120e 64 }
Station_Meteo_Laos 2:559043c0120e 65