![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Programme detection distance Hugo BERARD
Dependencies: mbed SRF02 BSP_DISCO_F746NG
main.cpp@0:d19398bb9ba3, 2021-06-22 (annotated)
- Committer:
- berard_hugo
- Date:
- Tue Jun 22 13:43:49 2021 +0000
- Revision:
- 0:d19398bb9ba3
Programme detection distance
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
berard_hugo | 0:d19398bb9ba3 | 1 | #include "mbed.h" |
berard_hugo | 0:d19398bb9ba3 | 2 | #include "SRF02.h" |
berard_hugo | 0:d19398bb9ba3 | 3 | #include "stm32746g_discovery_lcd.h" |
berard_hugo | 0:d19398bb9ba3 | 4 | #include "stm32746g_discovery_ts.h" |
berard_hugo | 0:d19398bb9ba3 | 5 | |
berard_hugo | 0:d19398bb9ba3 | 6 | SRF02 sensor (0xE0, PB_9, PB_8); |
berard_hugo | 0:d19398bb9ba3 | 7 | Serial LCD(USBTX, USBRX); |
berard_hugo | 0:d19398bb9ba3 | 8 | |
berard_hugo | 0:d19398bb9ba3 | 9 | int main() { |
berard_hugo | 0:d19398bb9ba3 | 10 | LCD.baud(115200); |
berard_hugo | 0:d19398bb9ba3 | 11 | int distance = sensor.GetCentimeters(); |
berard_hugo | 0:d19398bb9ba3 | 12 | LCD.printf("Distance = %d cm\n",distance); |
berard_hugo | 0:d19398bb9ba3 | 13 | wait(1); |
berard_hugo | 0:d19398bb9ba3 | 14 | |
berard_hugo | 0:d19398bb9ba3 | 15 | TS_StateTypeDef TS_State; |
berard_hugo | 0:d19398bb9ba3 | 16 | uint16_t x, y; |
berard_hugo | 0:d19398bb9ba3 | 17 | uint8_t text[30]; |
berard_hugo | 0:d19398bb9ba3 | 18 | uint8_t status; |
berard_hugo | 0:d19398bb9ba3 | 19 | uint8_t idx; |
berard_hugo | 0:d19398bb9ba3 | 20 | uint8_t cleared; |
berard_hugo | 0:d19398bb9ba3 | 21 | uint8_t prev_nb_touches = 1; |
berard_hugo | 0:d19398bb9ba3 | 22 | |
berard_hugo | 0:d19398bb9ba3 | 23 | int old_val=0; |
berard_hugo | 0:d19398bb9ba3 | 24 | int new_val=0; |
berard_hugo | 0:d19398bb9ba3 | 25 | |
berard_hugo | 0:d19398bb9ba3 | 26 | // Initialisation du LCD intégrée dans le STM32F746G |
berard_hugo | 0:d19398bb9ba3 | 27 | BSP_LCD_Init(); |
berard_hugo | 0:d19398bb9ba3 | 28 | BSP_LCD_LayerDefaultInit(LTDC_ACTIVE_LAYER, LCD_FB_START_ADDRESS); |
berard_hugo | 0:d19398bb9ba3 | 29 | BSP_LCD_SelectLayer(LTDC_ACTIVE_LAYER); |
berard_hugo | 0:d19398bb9ba3 | 30 | BSP_LCD_Clear(LCD_COLOR_BLACK); |
berard_hugo | 0:d19398bb9ba3 | 31 | BSP_LCD_SetTextColor(LCD_COLOR_WHITE); |
berard_hugo | 0:d19398bb9ba3 | 32 | BSP_LCD_SetBackColor(LCD_COLOR_BLACK); |
berard_hugo | 0:d19398bb9ba3 | 33 | BSP_LCD_SetFont(&LCD_DEFAULT_FONT); |
berard_hugo | 0:d19398bb9ba3 | 34 | BSP_LCD_DisplayStringAt(0, 1, (uint8_t *)"Capteur SRF02 distance", CENTER_MODE); |
berard_hugo | 0:d19398bb9ba3 | 35 | BSP_LCD_SetTextColor(LCD_COLOR_RED); |
berard_hugo | 0:d19398bb9ba3 | 36 | BSP_LCD_DisplayStringAt(0, 65, (uint8_t *)"en centimetre", CENTER_MODE); |
berard_hugo | 0:d19398bb9ba3 | 37 | BSP_LCD_SetTextColor(LCD_COLOR_RED); |
berard_hugo | 0:d19398bb9ba3 | 38 | BSP_LCD_DisplayStringAt(0, 30, (uint8_t *)"Avec obstacle", CENTER_MODE); |
berard_hugo | 0:d19398bb9ba3 | 39 | |
berard_hugo | 0:d19398bb9ba3 | 40 | HAL_Delay(1000); |
berard_hugo | 0:d19398bb9ba3 | 41 | |
berard_hugo | 0:d19398bb9ba3 | 42 | status = BSP_TS_Init(BSP_LCD_GetXSize(), BSP_LCD_GetYSize()); |
berard_hugo | 0:d19398bb9ba3 | 43 | if (status != TS_OK) { |
berard_hugo | 0:d19398bb9ba3 | 44 | BSP_LCD_Clear(LCD_COLOR_BLACK); |
berard_hugo | 0:d19398bb9ba3 | 45 | BSP_LCD_SetBackColor(LCD_COLOR_BLACK); |
berard_hugo | 0:d19398bb9ba3 | 46 | BSP_LCD_SetTextColor(LCD_COLOR_WHITE); |
berard_hugo | 0:d19398bb9ba3 | 47 | BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"ALARM NON ACTIVE", CENTER_MODE); |
berard_hugo | 0:d19398bb9ba3 | 48 | } |
berard_hugo | 0:d19398bb9ba3 | 49 | |
berard_hugo | 0:d19398bb9ba3 | 50 | else { |
berard_hugo | 0:d19398bb9ba3 | 51 | BSP_LCD_Clear(LCD_COLOR_BLUE); |
berard_hugo | 0:d19398bb9ba3 | 52 | BSP_LCD_SetBackColor(LCD_COLOR_BLUE); |
berard_hugo | 0:d19398bb9ba3 | 53 | BSP_LCD_SetTextColor(LCD_COLOR_WHITE); |
berard_hugo | 0:d19398bb9ba3 | 54 | BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"ALARM ACTIVE", CENTER_MODE); |
berard_hugo | 0:d19398bb9ba3 | 55 | } |
berard_hugo | 0:d19398bb9ba3 | 56 | |
berard_hugo | 0:d19398bb9ba3 | 57 | HAL_Delay(1000); |
berard_hugo | 0:d19398bb9ba3 | 58 | |
berard_hugo | 0:d19398bb9ba3 | 59 | |
berard_hugo | 0:d19398bb9ba3 | 60 | while(1) { |
berard_hugo | 0:d19398bb9ba3 | 61 | BSP_LCD_Clear(LCD_COLOR_BLUE); |
berard_hugo | 0:d19398bb9ba3 | 62 | int distance = sensor.GetCentimeters(); |
berard_hugo | 0:d19398bb9ba3 | 63 | old_val = new_val; |
berard_hugo | 0:d19398bb9ba3 | 64 | new_val = distance; |
berard_hugo | 0:d19398bb9ba3 | 65 | |
berard_hugo | 0:d19398bb9ba3 | 66 | if(new_val - old_val >= 5) BSP_LCD_DisplayStringAt(0, LINE(1), (uint8_t *)"Detecte", CENTER_MODE); |
berard_hugo | 0:d19398bb9ba3 | 67 | sprintf((char*)text, "distance %d cm", distance); |
berard_hugo | 0:d19398bb9ba3 | 68 | BSP_LCD_DisplayStringAt(0, LINE(0), (uint8_t *)&text, CENTER_MODE); |
berard_hugo | 0:d19398bb9ba3 | 69 | BSP_LCD_DrawRect( distance*2, 100, 30,50); |
berard_hugo | 0:d19398bb9ba3 | 70 | BSP_LCD_FillRect( distance*2, 100, 30,50); |
berard_hugo | 0:d19398bb9ba3 | 71 | HAL_Delay(5000); |
berard_hugo | 0:d19398bb9ba3 | 72 | } |
berard_hugo | 0:d19398bb9ba3 | 73 | } |
berard_hugo | 0:d19398bb9ba3 | 74 | |
berard_hugo | 0:d19398bb9ba3 | 75 | |
berard_hugo | 0:d19398bb9ba3 | 76 |