SRF08 sur STM32f746NG en I2C avec configuration des registres. Affichage des boutons de réglage et des valeurs de luminosité et de distance
Dependencies: BSP_DISCO_F746NG LCD_DISCO_F746NG SRF08 TS_DISCO_F746NG mbed
Diff: main.cpp
- Revision:
- 1:726e6fd13180
- Parent:
- 0:6f45024d79bc
--- a/main.cpp Mon Feb 08 11:24:12 2016 +0000 +++ b/main.cpp Tue Jun 14 15:38:53 2016 +0000 @@ -2,79 +2,235 @@ #include "TS_DISCO_F746NG.h" #include "LCD_DISCO_F746NG.h" #include "SRF08.h" +#include "fond.h" + LCD_DISCO_F746NG lcd; TS_DISCO_F746NG ts; int main() { - SRF08 srf08(D14, D15, 0xE0); // Define SDA, SCL pin and I2C address - +uint32_t pixel=0x00; +char *tmp; +int rangeregisterflag=10; +int rangeregistertexte=0; +int gainregisterflag=10; +int gainregistertexte=0; +int gainreg; TS_StateTypeDef TS_State; uint16_t x, y; uint8_t text[30]; uint8_t status; uint8_t idx; - uint8_t cleared = 0; - uint8_t prev_nb_touches = 0; +int valeur1=0; +int valeur2=0; +int valeur3=0; +int value=0; +int gainoffsetx; +int gainoffsety; +int count=0; +int offsetx=0; +int offsety=0; + // uint8_t *fondecran=; - lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN DEMO", CENTER_MODE); - wait(1); status = ts.Init(lcd.GetXSize(), lcd.GetYSize()); if (status != TS_OK) { - lcd.Clear(LCD_COLOR_RED); - lcd.SetBackColor(LCD_COLOR_RED); - lcd.SetTextColor(LCD_COLOR_WHITE); - lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT FAIL", CENTER_MODE); + lcd.SetTextColor(LCD_COLOR_WHITE); + lcd.SetTextColor(LCD_COLOR_BLACK); + lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT FAIL", CENTER_MODE); } else { - lcd.Clear(LCD_COLOR_GREEN); - lcd.SetBackColor(LCD_COLOR_GREEN); - lcd.SetTextColor(LCD_COLOR_WHITE); - lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT OK", CENTER_MODE); + //lcd.Clear(LCD_COLOR_GREEN); + //lcd.SetTextColor(LCD_COLOR_BLACK); + //lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT OK", CENTER_MODE); } - wait(1); - lcd.SetFont(&Font12); - lcd.SetBackColor(LCD_COLOR_BLUE); - lcd.SetTextColor(LCD_COLOR_WHITE); - - printf("Measured range : %d cm\n",srf08.getRange()); - wait(0.1); - while(1) { - - - lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"test", CENTER_MODE); + lcd.SetFont(&Font12); + lcd.Clear(LCD_COLOR_BLACK); + BSP_LCD_SetBackColor(LCD_COLOR_BLACK); + BSP_LCD_SetTextColor(LCD_COLOR_WHITE); +int addr=0xe4; +int distance; +int Luminosite; + gainoffsetx=lcd.GetXSize()-100; + gainoffsety=0; +for (int j=0;j<100;j++) + { + for (int i=0;i<100;i++) + { + pixel=255<<24; + pixel+=range[count]<<18; + pixel+=range[count+1]<<10; + pixel+=range[count+2]<<2; + lcd.DrawPixel(offsetx+i,offsety+j,pixel); + count+=3; + } + } + count=0; + for (int j=0;j<100;j++) + { + for (int i=0;i<100;i++) + { + pixel=255<<24; + pixel+=gain[count]<<18; + pixel+=gain[count+1]<<10; + pixel+=gain[count+2]<<2; + lcd.DrawPixel(gainoffsetx+i,gainoffsety+j,pixel); + count+=3; + } + } + + while(1) + { + //srf08.setRangeRegister(2); +SRF08 srf08(D14, D15, addr); + + srf08.startRanging(); + while (srf08.rangingFinished()==false) + { + wait_ms(1); + } + //wait_ms(500); + + valeur1=srf08.getRange(); + Luminosite=srf08.getLightIntensity(); + Luminosite=Luminosite*100/255; + sprintf((char*)tmp, "Distance: %d cm |Luminosite :%d%%",distance,Luminosite); + valeur3=valeur2; + valeur2=valeur1; + distance= (valeur1+valeur2+valeur3)/3; + + + + //wait(1); + if (distance!=65535) + { ts.GetState(&TS_State); if (TS_State.touchDetected) { // Clear lines corresponding to old touches coordinates - if (TS_State.touchDetected < prev_nb_touches) { - for (idx = (TS_State.touchDetected + 1); idx <= 5; idx++) { - lcd.ClearStringLine(idx); + + + x = TS_State.touchX[0]; + y = TS_State.touchY[0]; + + // sprintf((char*)text, "x %d y %d",x,y); + // lcd.DisplayStringAt(0, LINE(9), (uint8_t *)&text, LEFT_MODE); + /// ----- recuperation et mise a la valeur du registre range + if ( x <100 && x>0 && y <100&& y>0){ + //The range is ((rangeVal x 43mm) + 43mm) + /* Setting the Range Register to 1 (0x01) gives a maximum range of 86mm. More usefully, + 24 (0x18) gives a range of 1 metre and 140 (0x8C) is 6 metres. + Setting 255 (0xFF) gives the original 11 metres */ + if (rangeregisterflag==10)rangeregisterflag=0; + switch (rangeregisterflag){ + case 0: + srf08.setRangeRegister(0x18); + rangeregistertexte=1; + rangeregisterflag=1; + break; + case 1: + srf08.setRangeRegister(0x8C); + rangeregistertexte=6; + rangeregisterflag=2; + break; + case 2: + + srf08.setRangeRegister(0xFF); + rangeregistertexte=11; + rangeregisterflag=0; + break; + } - } - prev_nb_touches = TS_State.touchDetected; - - cleared = 0; - - sprintf((char*)text, "Touches: %d", TS_State.touchDetected); - lcd.DisplayStringAt(0, LINE(0), (uint8_t *)&text, LEFT_MODE); + - for (idx = 0; idx < TS_State.touchDetected; idx++) { - x = TS_State.touchX[idx]; - y = TS_State.touchY[idx]; - sprintf((char*)text, "Touch %d: x=%d y=%d ", idx+1, x, y); - lcd.DisplayStringAt(0, LINE(idx+1), (uint8_t *)&text, LEFT_MODE); - } + } + /// ----- recuperation et mise a la valeur du registre analogue gain + if ( x >lcd.GetXSize()-100 && x<lcd.GetXSize() && y <100&& y>0){ + if (gainregisterflag==10)gainregisterflag=0; + + switch (gainregisterflag){ + case 0: + gainreg=0x00; + srf08.setMaxGainRegister(gainreg); + gainregistertexte=94; + gainregisterflag=1; + break; + case 1: + gainreg=0x06; + srf08.setMaxGainRegister(gainreg); + gainregistertexte=114; + gainregisterflag=2; + break; + case 2: + gainreg=0x12; + srf08.setMaxGainRegister(gainreg); + gainregistertexte=145; + gainregisterflag=3; + break; + case 3: + gainreg=0x18; + srf08.setMaxGainRegister(gainreg); + gainregistertexte=199; + gainregisterflag=4; + break; + case 4: + gainreg=0x24; + srf08.setMaxGainRegister(gainreg); + gainregistertexte=317; + gainregisterflag=5; + break; + case 5: + gainreg=0x30; + srf08.setMaxGainRegister(gainreg); + gainregistertexte=777; + gainregisterflag=6; + break; + case 6: + gainreg=0x31; + srf08.setMaxGainRegister(gainreg); + gainregistertexte=1025; + gainregisterflag=0; + break; + + + } + } + wait_ms(100); +// loop d'affichage par default + } else { + + + // lcd.Clear(LCD_COLOR_BLACK); + //srf08.setMaxGainRegister(0xFF); + lcd.ClearStringLine(9); + lcd.ClearStringLine(10); + lcd.ClearStringLine(15); + if (rangeregisterflag==10){ + sprintf((char*)text, "Presser le coin haut gauche pour changer le Range"); + lcd.DisplayStringAt(0, LINE(9), (uint8_t *)&text, LEFT_MODE); + }else { + sprintf((char*)text, "Range de %d metre",rangeregistertexte); + lcd.DisplayStringAt(0, LINE(9), (uint8_t *)&text, LEFT_MODE); + } + if (gainregisterflag==10){ + sprintf((char*)text, "Presser le coin haut droit pour changer le Gain"); + lcd.DisplayStringAt(0, LINE(10), (uint8_t *)&text, RIGHT_MODE); + }else { + sprintf((char*)text, "Gain de %d metre",gainregistertexte); + lcd.DisplayStringAt(0, LINE(10), (uint8_t *)&text, RIGHT_MODE); + } + lcd.DisplayStringAt(0, LINE(15), (uint8_t *)tmp, CENTER_MODE); + + + } - lcd.DrawPixel(TS_State.touchX[0], TS_State.touchY[0], LCD_COLOR_ORANGE); - } else { - if (!cleared) { - lcd.Clear(LCD_COLOR_BLUE); - sprintf((char*)text, "Touches: 0"); - lcd.DisplayStringAt(0, LINE(0), (uint8_t *)&text, LEFT_MODE); - cleared = 1; - } + } + else { + lcd.Clear(LCD_COLOR_BLACK); + lcd.DisplayStringAt(0, LINE(9), (uint8_t *)"Erreur d'initialisation du module SRF08", CENTER_MODE); } } + +//wait_ms(10); + }