Capteur humi/temp
Dependencies: BSP_DISCO_F746NG LCD_DISCO_F746NG TS_DISCO_F746NG mbed
main.cpp@0:3d803817104a, 2018-06-21 (annotated)
- Committer:
- theophile
- Date:
- Thu Jun 21 10:24:07 2018 +0000
- Revision:
- 0:3d803817104a
Code projet Interfa?age - Capteur Temp/Humi
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
theophile | 0:3d803817104a | 1 | //Library |
theophile | 0:3d803817104a | 2 | #include "mbed.h" |
theophile | 0:3d803817104a | 3 | #include "TS_DISCO_F746NG.h" |
theophile | 0:3d803817104a | 4 | #include "LCD_DISCO_F746NG.h" |
theophile | 0:3d803817104a | 5 | |
theophile | 0:3d803817104a | 6 | // Initialisation objet |
theophile | 0:3d803817104a | 7 | LCD_DISCO_F746NG lcd; |
theophile | 0:3d803817104a | 8 | TS_DISCO_F746NG ts; |
theophile | 0:3d803817104a | 9 | I2C i2c (D14,D15); |
theophile | 0:3d803817104a | 10 | |
theophile | 0:3d803817104a | 11 | uint8_t texttp[30]; |
theophile | 0:3d803817104a | 12 | uint8_t texthm[30]; |
theophile | 0:3d803817104a | 13 | uint8_t status; |
theophile | 0:3d803817104a | 14 | uint8_t idx; |
theophile | 0:3d803817104a | 15 | uint8_t cleared = 0; |
theophile | 0:3d803817104a | 16 | uint8_t prev_nb_touches = 0; |
theophile | 0:3d803817104a | 17 | uint8_t event_press = 0; |
theophile | 0:3d803817104a | 18 | uint16_t x, y; |
theophile | 0:3d803817104a | 19 | uint16_t pos_y = 210; |
theophile | 0:3d803817104a | 20 | uint16_t rayon = 0; |
theophile | 0:3d803817104a | 21 | |
theophile | 0:3d803817104a | 22 | int adress = 0x40 << 1; |
theophile | 0:3d803817104a | 23 | int touchtp; |
theophile | 0:3d803817104a | 24 | int touchhum; |
theophile | 0:3d803817104a | 25 | |
theophile | 0:3d803817104a | 26 | char ctemp[2] = {0x03,0x11}; |
theophile | 0:3d803817104a | 27 | char chumi[2] = {0x03,0x01}; |
theophile | 0:3d803817104a | 28 | char datatp[2] = { 0,0 }; |
theophile | 0:3d803817104a | 29 | char datahu [2] = { 0,0 }; |
theophile | 0:3d803817104a | 30 | |
theophile | 0:3d803817104a | 31 | float temp = 0; |
theophile | 0:3d803817104a | 32 | float temperature = 0; |
theophile | 0:3d803817104a | 33 | float humi = 0; |
theophile | 0:3d803817104a | 34 | float humidity = 0; |
theophile | 0:3d803817104a | 35 | |
theophile | 0:3d803817104a | 36 | int times = 0; |
theophile | 0:3d803817104a | 37 | |
theophile | 0:3d803817104a | 38 | int choice = 0; |
theophile | 0:3d803817104a | 39 | |
theophile | 0:3d803817104a | 40 | // Debug |
theophile | 0:3d803817104a | 41 | //Serial pc(USBTX, USBRX); |
theophile | 0:3d803817104a | 42 | |
theophile | 0:3d803817104a | 43 | // Fonction d'affichage de l'interface |
theophile | 0:3d803817104a | 44 | void Clear_avec_un_rectangle (void){ |
theophile | 0:3d803817104a | 45 | |
theophile | 0:3d803817104a | 46 | // Fond d'écran blanc |
theophile | 0:3d803817104a | 47 | lcd.Clear(LCD_COLOR_LIGHTGRAY); |
theophile | 0:3d803817104a | 48 | lcd.SetBackColor(LCD_COLOR_LIGHTGRAY); |
theophile | 0:3d803817104a | 49 | |
theophile | 0:3d803817104a | 50 | // Bonton rouge pour température |
theophile | 0:3d803817104a | 51 | lcd.SetTextColor(LCD_COLOR_RED); |
theophile | 0:3d803817104a | 52 | lcd.DrawRect(25,100,200,50); |
theophile | 0:3d803817104a | 53 | lcd.SetFont(&Font24); |
theophile | 0:3d803817104a | 54 | lcd.DisplayStringAt(33, 115, (uint8_t *)"TEMPERATURE", LEFT_MODE); |
theophile | 0:3d803817104a | 55 | |
theophile | 0:3d803817104a | 56 | // Bouton vert pour humitidé |
theophile | 0:3d803817104a | 57 | lcd.SetTextColor(LCD_COLOR_BLUE); |
theophile | 0:3d803817104a | 58 | lcd.DrawRect(250,100,200,50); |
theophile | 0:3d803817104a | 59 | lcd.SetFont(&Font24); |
theophile | 0:3d803817104a | 60 | lcd.DisplayStringAt(280, 115, (uint8_t *)"HUMIDITE", LEFT_MODE); |
theophile | 0:3d803817104a | 61 | |
theophile | 0:3d803817104a | 62 | // Titre de l'interface |
theophile | 0:3d803817104a | 63 | lcd.SetTextColor(LCD_COLOR_BLACK); |
theophile | 0:3d803817104a | 64 | lcd.SetFont(&Font20); |
theophile | 0:3d803817104a | 65 | lcd.DisplayStringAt(0, 10, (uint8_t *)"Capteur Humidite & Temperature", CENTER_MODE); |
theophile | 0:3d803817104a | 66 | } |
theophile | 0:3d803817104a | 67 | |
theophile | 0:3d803817104a | 68 | void humidi(void){ |
theophile | 0:3d803817104a | 69 | lcd.SetBackColor(LCD_COLOR_LIGHTGRAY); |
theophile | 0:3d803817104a | 70 | lcd.SetTextColor(LCD_COLOR_LIGHTGRAY); |
theophile | 0:3d803817104a | 71 | lcd.FillCircle(235,215,50); |
theophile | 0:3d803817104a | 72 | lcd.SetFont(&Font16); |
theophile | 0:3d803817104a | 73 | sprintf((char*)texthm,"%0.1d %%",(int)humidity); |
theophile | 0:3d803817104a | 74 | lcd.DisplayStringAt(0, pos_y, (uint8_t *)" ", CENTER_MODE); |
theophile | 0:3d803817104a | 75 | lcd.SetTextColor(LCD_COLOR_BLACK); |
theophile | 0:3d803817104a | 76 | lcd.DrawCircle(235,215,50); |
theophile | 0:3d803817104a | 77 | lcd.SetTextColor(LCD_COLOR_BLUE); |
theophile | 0:3d803817104a | 78 | rayon = 50*(humidity/100); |
theophile | 0:3d803817104a | 79 | if(rayon>=50) |
theophile | 0:3d803817104a | 80 | rayon =50; |
theophile | 0:3d803817104a | 81 | lcd.FillCircle(235,215,rayon); |
theophile | 0:3d803817104a | 82 | lcd.SetBackColor(LCD_COLOR_BLUE); |
theophile | 0:3d803817104a | 83 | lcd.SetTextColor(LCD_COLOR_BLACK); |
theophile | 0:3d803817104a | 84 | lcd.DisplayStringAt(0, pos_y, (uint8_t *)&texthm, CENTER_MODE); |
theophile | 0:3d803817104a | 85 | // prev_nb_touches = TS_State.touchDetected; |
theophile | 0:3d803817104a | 86 | cleared = 0; |
theophile | 0:3d803817104a | 87 | x= 1; |
theophile | 0:3d803817104a | 88 | |
theophile | 0:3d803817104a | 89 | } |
theophile | 0:3d803817104a | 90 | |
theophile | 0:3d803817104a | 91 | void temper(void) { |
theophile | 0:3d803817104a | 92 | lcd.SetBackColor(LCD_COLOR_LIGHTGRAY); |
theophile | 0:3d803817104a | 93 | lcd.SetTextColor(LCD_COLOR_LIGHTGRAY); |
theophile | 0:3d803817104a | 94 | lcd.FillCircle(235,215,50); |
theophile | 0:3d803817104a | 95 | |
theophile | 0:3d803817104a | 96 | sprintf((char*)texttp,"%0.1d c",(int)temperature); |
theophile | 0:3d803817104a | 97 | lcd.DisplayStringAt(0, pos_y, (uint8_t *)" ", CENTER_MODE); |
theophile | 0:3d803817104a | 98 | |
theophile | 0:3d803817104a | 99 | lcd.SetTextColor(LCD_COLOR_BLACK); |
theophile | 0:3d803817104a | 100 | lcd.DrawCircle(235,215,50); |
theophile | 0:3d803817104a | 101 | |
theophile | 0:3d803817104a | 102 | lcd.SetTextColor(LCD_COLOR_RED); |
theophile | 0:3d803817104a | 103 | rayon = 50*(temperature/50); |
theophile | 0:3d803817104a | 104 | if(rayon>=50) |
theophile | 0:3d803817104a | 105 | rayon =50; |
theophile | 0:3d803817104a | 106 | lcd.FillCircle(235,215,rayon); |
theophile | 0:3d803817104a | 107 | lcd.SetBackColor(LCD_COLOR_RED); |
theophile | 0:3d803817104a | 108 | lcd.SetTextColor(LCD_COLOR_BLACK); |
theophile | 0:3d803817104a | 109 | |
theophile | 0:3d803817104a | 110 | lcd.SetFont(&Font16); |
theophile | 0:3d803817104a | 111 | lcd.SetTextColor(LCD_COLOR_BLACK); |
theophile | 0:3d803817104a | 112 | lcd.DisplayStringAt(0, pos_y, (uint8_t *)&texttp, CENTER_MODE); |
theophile | 0:3d803817104a | 113 | |
theophile | 0:3d803817104a | 114 | // prev_nb_touches = TS_State.touchDetected; |
theophile | 0:3d803817104a | 115 | cleared = 0; |
theophile | 0:3d803817104a | 116 | x = 2; |
theophile | 0:3d803817104a | 117 | } |
theophile | 0:3d803817104a | 118 | |
theophile | 0:3d803817104a | 119 | void select_etat(void){ |
theophile | 0:3d803817104a | 120 | if ((x == 1) && (times == 10)) |
theophile | 0:3d803817104a | 121 | {humidi(); |
theophile | 0:3d803817104a | 122 | times = 0;} |
theophile | 0:3d803817104a | 123 | |
theophile | 0:3d803817104a | 124 | else if ((x == 2) && (times == 10)) |
theophile | 0:3d803817104a | 125 | {temper(); |
theophile | 0:3d803817104a | 126 | times = 0;} |
theophile | 0:3d803817104a | 127 | |
theophile | 0:3d803817104a | 128 | |
theophile | 0:3d803817104a | 129 | } |
theophile | 0:3d803817104a | 130 | |
theophile | 0:3d803817104a | 131 | |
theophile | 0:3d803817104a | 132 | //add capt 0x40 |
theophile | 0:3d803817104a | 133 | //f = 400kHz |
theophile | 0:3d803817104a | 134 | //float temp = (capt/32) -50; |
theophile | 0:3d803817104a | 135 | |
theophile | 0:3d803817104a | 136 | // Fonction MAIN |
theophile | 0:3d803817104a | 137 | int main() |
theophile | 0:3d803817104a | 138 | { |
theophile | 0:3d803817104a | 139 | // Debug |
theophile | 0:3d803817104a | 140 | //pc.baud(9600); |
theophile | 0:3d803817104a | 141 | |
theophile | 0:3d803817104a | 142 | // Paramétrage de l'I2C |
theophile | 0:3d803817104a | 143 | i2c.frequency(100000); |
theophile | 0:3d803817104a | 144 | |
theophile | 0:3d803817104a | 145 | // Initialisation variable |
theophile | 0:3d803817104a | 146 | TS_StateTypeDef TS_State; |
theophile | 0:3d803817104a | 147 | |
theophile | 0:3d803817104a | 148 | |
theophile | 0:3d803817104a | 149 | |
theophile | 0:3d803817104a | 150 | |
theophile | 0:3d803817104a | 151 | // Affichage de l'interface |
theophile | 0:3d803817104a | 152 | Clear_avec_un_rectangle(); |
theophile | 0:3d803817104a | 153 | |
theophile | 0:3d803817104a | 154 | while(1) { |
theophile | 0:3d803817104a | 155 | select_etat(); |
theophile | 0:3d803817104a | 156 | times = times + 1; |
theophile | 0:3d803817104a | 157 | |
theophile | 0:3d803817104a | 158 | // Demande de la valeur du registre température au capteur |
theophile | 0:3d803817104a | 159 | i2c.write(adress ,ctemp ,2, 0); |
theophile | 0:3d803817104a | 160 | wait_ms(35); |
theophile | 0:3d803817104a | 161 | i2c.read(adress,datatp,2); |
theophile | 0:3d803817104a | 162 | wait_ms(35); |
theophile | 0:3d803817104a | 163 | temp = datatp[1]<<6; |
theophile | 0:3d803817104a | 164 | temp = temp + (datatp[2]>>2); |
theophile | 0:3d803817104a | 165 | temperature = (temp/32) - 50; |
theophile | 0:3d803817104a | 166 | |
theophile | 0:3d803817104a | 167 | // Debug |
theophile | 0:3d803817104a | 168 | //pc.printf("\n\r temperature : %f",temperature); |
theophile | 0:3d803817104a | 169 | //pc.printf("\n\r data1 : %d ",(datatp[1]<<6) + (datatp[2] >> 2)); |
theophile | 0:3d803817104a | 170 | |
theophile | 0:3d803817104a | 171 | wait_ms(35); |
theophile | 0:3d803817104a | 172 | |
theophile | 0:3d803817104a | 173 | // Demande de la valeur deu registre humidité |
theophile | 0:3d803817104a | 174 | i2c.write(adress ,chumi ,2, 0); |
theophile | 0:3d803817104a | 175 | wait_ms(35); |
theophile | 0:3d803817104a | 176 | i2c.read(adress,datahu,2); |
theophile | 0:3d803817104a | 177 | wait_us(35); |
theophile | 0:3d803817104a | 178 | humi = datahu[1]<<4; |
theophile | 0:3d803817104a | 179 | humi = humi + (datahu[2]>>4); |
theophile | 0:3d803817104a | 180 | humidity = (humi/16) - 24; |
theophile | 0:3d803817104a | 181 | |
theophile | 0:3d803817104a | 182 | // Debug |
theophile | 0:3d803817104a | 183 | //pc.printf("\n\r Humidity : %f %",humidity); |
theophile | 0:3d803817104a | 184 | //pc.printf("\n\r data1 + data2 : %d ",(datahu[1]<<4) + (datahu[2]>>4)); |
theophile | 0:3d803817104a | 185 | |
theophile | 0:3d803817104a | 186 | wait_ms(35); |
theophile | 0:3d803817104a | 187 | |
theophile | 0:3d803817104a | 188 | // Détéction d'un apuie sur l'écran tactile |
theophile | 0:3d803817104a | 189 | ts.GetState(&TS_State); |
theophile | 0:3d803817104a | 190 | if (TS_State.touchDetected) { |
theophile | 0:3d803817104a | 191 | |
theophile | 0:3d803817104a | 192 | if (TS_State.touchDetected < prev_nb_touches) { |
theophile | 0:3d803817104a | 193 | for (idx = (TS_State.touchDetected + 1); idx <= 5; idx++) { |
theophile | 0:3d803817104a | 194 | lcd.ClearStringLine(idx); |
theophile | 0:3d803817104a | 195 | } |
theophile | 0:3d803817104a | 196 | } |
theophile | 0:3d803817104a | 197 | prev_nb_touches = TS_State.touchDetected; |
theophile | 0:3d803817104a | 198 | cleared = 0; |
theophile | 0:3d803817104a | 199 | |
theophile | 0:3d803817104a | 200 | // Enregistrement de la position de l'apuie |
theophile | 0:3d803817104a | 201 | for (idx = 0; idx < TS_State.touchDetected; idx++) { |
theophile | 0:3d803817104a | 202 | x = TS_State.touchX[idx]; |
theophile | 0:3d803817104a | 203 | y = TS_State.touchY[idx]; |
theophile | 0:3d803817104a | 204 | } |
theophile | 0:3d803817104a | 205 | |
theophile | 0:3d803817104a | 206 | // Détection d'un apuie sur le bouton humidité |
theophile | 0:3d803817104a | 207 | if((x>250) && (x<450) && (y>=100) && (y<150)) { |
theophile | 0:3d803817104a | 208 | touchhum=1; |
theophile | 0:3d803817104a | 209 | touchtp=0; |
theophile | 0:3d803817104a | 210 | } |
theophile | 0:3d803817104a | 211 | |
theophile | 0:3d803817104a | 212 | // Détection d'un apuie sur le bouton température |
theophile | 0:3d803817104a | 213 | else if ((x>25) && (x<200)&&(y>=100)&&(y<150)) { |
theophile | 0:3d803817104a | 214 | touchhum=0; |
theophile | 0:3d803817104a | 215 | touchtp=1; |
theophile | 0:3d803817104a | 216 | } |
theophile | 0:3d803817104a | 217 | |
theophile | 0:3d803817104a | 218 | // Si apuie sur le bouton humidité, affichage valeur envoyé par le capteur |
theophile | 0:3d803817104a | 219 | if (touchhum==1 && touchtp==0) { |
theophile | 0:3d803817104a | 220 | humidi(); |
theophile | 0:3d803817104a | 221 | } |
theophile | 0:3d803817104a | 222 | |
theophile | 0:3d803817104a | 223 | // Si apuie sur le bouton température, affichage valeur envoyé par le capteur |
theophile | 0:3d803817104a | 224 | else if (touchhum==0 && touchtp==1) { |
theophile | 0:3d803817104a | 225 | temper(); |
theophile | 0:3d803817104a | 226 | } |
theophile | 0:3d803817104a | 227 | } |
theophile | 0:3d803817104a | 228 | } |
theophile | 0:3d803817104a | 229 | } |
theophile | 0:3d803817104a | 230 | |
theophile | 0:3d803817104a | 231 | |
theophile | 0:3d803817104a | 232 | |
theophile | 0:3d803817104a | 233 | |
theophile | 0:3d803817104a | 234 | |
theophile | 0:3d803817104a | 235 | |
theophile | 0:3d803817104a | 236 | |
theophile | 0:3d803817104a | 237 | |
theophile | 0:3d803817104a | 238 | |
theophile | 0:3d803817104a | 239 | |
theophile | 0:3d803817104a | 240 | |
theophile | 0:3d803817104a | 241 | |
theophile | 0:3d803817104a | 242 | |
theophile | 0:3d803817104a | 243 | |
theophile | 0:3d803817104a | 244 | |
theophile | 0:3d803817104a | 245 | |
theophile | 0:3d803817104a | 246 | |
theophile | 0:3d803817104a | 247 | |
theophile | 0:3d803817104a | 248 | |
theophile | 0:3d803817104a | 249 | |
theophile | 0:3d803817104a | 250 | |
theophile | 0:3d803817104a | 251 |