grove stream updated JPA 01/09

Dependencies:   F7_Ethernet mbed MbedJSONValue LCD_DISCO_F746NG mbed-rtos BSP_DISCO_F746NG CANMsg NetworkAPI SDFileSystem_Warning_Fixed GroveStreamsmodified LcdDiscoF746NgTracer JSON

Committer:
jeanpierreaulas
Date:
Tue Sep 08 15:01:08 2020 +0000
Branch:
jpa
Revision:
9:332bd237b98f
Parent:
5:ceefd0cfa233
adjustment

Who changed what in which revision?

UserRevisionLine numberNew contents of line
38domo 0:48863a0964f5 1
38domo 0:48863a0964f5 2 #include "stm32746g_discovery_lcd.h"
38domo 0:48863a0964f5 3 #include "stm32746g_discovery_ts.h"
38domo 0:48863a0964f5 4 #include "mbed.h"
38domo 0:48863a0964f5 5 #include "GroveStreams.h"
38domo 0:48863a0964f5 6 #include "MbedJSONValue.h"
jeanpierreaulas 5:ceefd0cfa233 7 Timer timer_touch[16];
38domo 0:48863a0964f5 8
38domo 0:48863a0964f5 9 int initDisplayTemp();
38domo 0:48863a0964f5 10
38domo 0:48863a0964f5 11 // GroveStreams Settings
38domo 0:48863a0964f5 12 //extern const char gsApiKey[];// = "YOUR_SECRET_API_KEY_HERE"; //Change This!!!
38domo 0:48863a0964f5 13 extern const char gsCompName[];// = "STM32F746+Discovery"; //Optionally change. Set this to give your component a name when it initially registers. Encode special chars such as spaces.
38domo 0:48863a0964f5 14
38domo 0:48863a0964f5 15 extern const char gsCompTmplId[]; //Optional. Tells GS what template to use when the feed initially arrives and a new component needs to be created.
38domo 0:48863a0964f5 16
38domo 0:48863a0964f5 17 ////GroveStreams groveStreams(gsApiKey, &lcd);
38domo 0:48863a0964f5 18 extern GroveStreams groveStreams;
38domo 0:48863a0964f5 19
38domo 0:48863a0964f5 20 extern const char* myMac;
38domo 0:48863a0964f5 21
38domo 0:48863a0964f5 22 extern time_t seconds;
38domo 0:48863a0964f5 23 time_t seconds_touches = 0;
38domo 0:48863a0964f5 24 extern time_t lastToucheTime;
38domo 0:48863a0964f5 25
38domo 0:48863a0964f5 26 int touchInitJPA1()
38domo 0:48863a0964f5 27 { //touch screen init
38domo 0:48863a0964f5 28 //touch screen
38domo 0:48863a0964f5 29 uint8_t status;
38domo 0:48863a0964f5 30 BSP_LCD_Init();
38domo 0:48863a0964f5 31 BSP_LCD_LayerDefaultInit(LTDC_ACTIVE_LAYER, LCD_FB_START_ADDRESS);
38domo 0:48863a0964f5 32 BSP_LCD_SelectLayer(LTDC_ACTIVE_LAYER);
38domo 0:48863a0964f5 33 BSP_LCD_SetFont(&Font12);
38domo 0:48863a0964f5 34 status = BSP_TS_Init(BSP_LCD_GetXSize(), BSP_LCD_GetYSize());
38domo 0:48863a0964f5 35 if (status != TS_OK) {
38domo 0:48863a0964f5 36 BSP_LCD_Clear(LCD_COLOR_RED);
38domo 0:48863a0964f5 37 BSP_LCD_SetBackColor(LCD_COLOR_RED);
38domo 0:48863a0964f5 38 BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
38domo 0:48863a0964f5 39 BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT FAIL", CENTER_MODE);
38domo 0:48863a0964f5 40 }
38domo 0:48863a0964f5 41 else {
38domo 0:48863a0964f5 42 BSP_LCD_Clear(LCD_COLOR_DARKBLUE);
38domo 0:48863a0964f5 43 BSP_LCD_SetBackColor(LCD_COLOR_BLACK);
38domo 0:48863a0964f5 44 BSP_LCD_SetTextColor(LCD_COLOR_GREEN);
38domo 0:48863a0964f5 45 //BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT OK", CENTER_MODE);
38domo 0:48863a0964f5 46 }
jeanpierreaulas 5:ceefd0cfa233 47 int tt = 0;
jeanpierreaulas 5:ceefd0cfa233 48 for (tt=0;tt<16;tt++)
jeanpierreaulas 5:ceefd0cfa233 49 {if(timer_touch[tt].read_ms() > 500)
jeanpierreaulas 5:ceefd0cfa233 50 {timer_touch[tt].stop();timer_touch[tt].reset();}}
38domo 0:48863a0964f5 51 return 0;
38domo 0:48863a0964f5 52 }
38domo 0:48863a0964f5 53
38domo 0:48863a0964f5 54 int xButton[5],yButton[5],dxButton[5],dyButton[5],xTxtButton[5],yTxtButton[5];
38domo 0:48863a0964f5 55 uint32_t backColorButton[5], txtColorButton[5];
38domo 0:48863a0964f5 56 char textButton[5][30];
38domo 0:48863a0964f5 57 char text1Button[5][30];
38domo 0:48863a0964f5 58 char statusButton[5][30];
38domo 0:48863a0964f5 59
38domo 0:48863a0964f5 60 int touchButtonJPA1(int numButton)
38domo 0:48863a0964f5 61 {
38domo 0:48863a0964f5 62 if ((numButton > 0) && (numButton < 5))
38domo 0:48863a0964f5 63 {
38domo 0:48863a0964f5 64 BSP_LCD_SetBackColor(backColorButton[numButton]);
38domo 0:48863a0964f5 65 BSP_LCD_SetTextColor(backColorButton[numButton]);
38domo 0:48863a0964f5 66 BSP_LCD_FillRect(xButton[numButton], yButton[numButton], dxButton[numButton], dyButton[numButton]);
38domo 0:48863a0964f5 67 BSP_LCD_SetTextColor(txtColorButton[numButton]);
38domo 0:48863a0964f5 68 BSP_LCD_DrawRect(xButton[numButton], yButton[numButton], dxButton[numButton], dyButton[numButton]);
38domo 0:48863a0964f5 69 BSP_LCD_SetFont(&Font20);
38domo 0:48863a0964f5 70 BSP_LCD_DisplayStringAt(xTxtButton[numButton]+52, yTxtButton[numButton], (uint8_t *)statusButton[numButton], LEFT_MODE);
38domo 0:48863a0964f5 71 BSP_LCD_DisplayStringAt(xTxtButton[numButton], yTxtButton[numButton]+28, (uint8_t *)textButton[numButton], LEFT_MODE);
38domo 0:48863a0964f5 72 BSP_LCD_DisplayStringAt(xTxtButton[numButton], yTxtButton[numButton]+48, (uint8_t *)text1Button[numButton], LEFT_MODE);
38domo 0:48863a0964f5 73 }
38domo 0:48863a0964f5 74 return 0;
38domo 0:48863a0964f5 75 }
38domo 0:48863a0964f5 76
38domo 0:48863a0964f5 77
38domo 0:48863a0964f5 78 // generation de fonts : http://omz-software.com/pythonista/docs/ios/ImageFont.html
38domo 0:48863a0964f5 79 int touchInitGuiJPA1()
38domo 0:48863a0964f5 80 {
38domo 0:48863a0964f5 81 xButton[1]=10;yButton[1]=185;dxButton[1]=100;dyButton[1]=80;xTxtButton[1]=12;yTxtButton[1]=190;
38domo 0:48863a0964f5 82 backColorButton[1] = LCD_COLOR_GRAY; txtColorButton[1] = LCD_COLOR_GREEN;
38domo 0:48863a0964f5 83 sprintf(textButton[1],"Lampe ");
38domo 0:48863a0964f5 84 sprintf(text1Button[1],"Sejour");
38domo 0:48863a0964f5 85 sprintf(statusButton[1],"off");
38domo 0:48863a0964f5 86
38domo 0:48863a0964f5 87 xButton[2]=130;yButton[2]=185;dxButton[2]=100;dyButton[2]=80;xTxtButton[2]=132;yTxtButton[2]=190;
38domo 0:48863a0964f5 88 backColorButton[2] = LCD_COLOR_GRAY; txtColorButton[2] = LCD_COLOR_GREEN;
38domo 0:48863a0964f5 89 sprintf(textButton[2],"Lampe ");
38domo 0:48863a0964f5 90 sprintf(text1Button[2],"Sejours");
38domo 0:48863a0964f5 91 sprintf(statusButton[2],"off");
38domo 0:48863a0964f5 92
38domo 0:48863a0964f5 93 xButton[3]=250;yButton[3]=185;dxButton[3]=100;dyButton[3]=80;xTxtButton[3]=252;yTxtButton[3]=190;
38domo 0:48863a0964f5 94 backColorButton[3] = LCD_COLOR_GRAY; txtColorButton[3] = LCD_COLOR_GREEN;
38domo 0:48863a0964f5 95 sprintf(textButton[3],"Lampe ");
38domo 0:48863a0964f5 96 sprintf(text1Button[3],"Sejour");
38domo 0:48863a0964f5 97 sprintf(statusButton[3],"off");
38domo 0:48863a0964f5 98
38domo 0:48863a0964f5 99 xButton[4]=370;yButton[4]=185;dxButton[4]=100;dyButton[4]=80;xTxtButton[4]=372;yTxtButton[4]=190;
38domo 0:48863a0964f5 100 backColorButton[4] = LCD_COLOR_GRAY; txtColorButton[4] = LCD_COLOR_GREEN;
38domo 0:48863a0964f5 101 sprintf(textButton[4],"Lampe ");
38domo 0:48863a0964f5 102 sprintf(text1Button[4],"Sejour");
38domo 0:48863a0964f5 103 sprintf(statusButton[4],"off");
38domo 0:48863a0964f5 104
38domo 0:48863a0964f5 105 touchButtonJPA1(1);
38domo 0:48863a0964f5 106 touchButtonJPA1(2);
38domo 0:48863a0964f5 107 touchButtonJPA1(3);
38domo 0:48863a0964f5 108 touchButtonJPA1(4);
38domo 0:48863a0964f5 109
38domo 0:48863a0964f5 110 BSP_LCD_SetFont(&FontFreeMono64);
38domo 0:48863a0964f5 111 //BSP_LCD_DisplayStringAt(20, 30, (uint8_t *)"0123456789", LEFT_MODE);
38domo 0:48863a0964f5 112 //BSP_LCD_DisplayCharJPA(20, 90, '8');
38domo 0:48863a0964f5 113 BSP_LCD_SetFont(&Font12);
38domo 0:48863a0964f5 114 return 0;
38domo 0:48863a0964f5 115 }
38domo 0:48863a0964f5 116
38domo 0:48863a0964f5 117 int touchJPA1()
38domo 0:48863a0964f5 118 {
38domo 0:48863a0964f5 119 //touch screen
38domo 0:48863a0964f5 120 TS_StateTypeDef TS_State;
38domo 0:48863a0964f5 121 uint16_t x, y;
38domo 0:48863a0964f5 122 uint8_t text[30];
38domo 0:48863a0964f5 123 uint8_t status = 0;
38domo 0:48863a0964f5 124 uint8_t idx;
38domo 0:48863a0964f5 125 static uint8_t cleared = 0;
38domo 0:48863a0964f5 126 uint8_t prev_nb_touches = 0;
38domo 0:48863a0964f5 127
jeanpierreaulas 5:ceefd0cfa233 128 int tt = 0;
jeanpierreaulas 5:ceefd0cfa233 129 for (tt=0;tt<16;tt++)
jeanpierreaulas 5:ceefd0cfa233 130 {if(timer_touch[tt].read_ms() > 200)
jeanpierreaulas 5:ceefd0cfa233 131 {timer_touch[tt].stop();timer_touch[tt].reset();}}
38domo 0:48863a0964f5 132 //touch screen
38domo 0:48863a0964f5 133 BSP_TS_GetState(&TS_State);
38domo 0:48863a0964f5 134 if (TS_State.touchDetected) {
38domo 0:48863a0964f5 135 // Clear lines corresponding to old touches coordinates
38domo 0:48863a0964f5 136 if (TS_State.touchDetected < prev_nb_touches) {
38domo 0:48863a0964f5 137 for (idx = (TS_State.touchDetected + 1); idx <= 5; idx++) {
38domo 0:48863a0964f5 138 //BSP_LCD_ClearStringLine(idx);
38domo 0:48863a0964f5 139 }
38domo 0:48863a0964f5 140 }
38domo 0:48863a0964f5 141 prev_nb_touches = TS_State.touchDetected;
38domo 0:48863a0964f5 142
38domo 0:48863a0964f5 143 cleared = 0;
38domo 0:48863a0964f5 144
38domo 0:48863a0964f5 145 //sprintf((char*)text, "Touches: %d", TS_State.touchDetected);
38domo 0:48863a0964f5 146 //BSP_LCD_DisplayStringAt(0, LINE(0), (uint8_t *)&text, LEFT_MODE);
38domo 0:48863a0964f5 147
38domo 0:48863a0964f5 148 for (idx = 0; idx < TS_State.touchDetected; idx++) {
38domo 0:48863a0964f5 149 x = TS_State.touchX[idx];
38domo 0:48863a0964f5 150 y = TS_State.touchY[idx];
38domo 0:48863a0964f5 151 //sprintf((char*)text, "Touch %d: x=%d y=%d ", idx+1, x, y);
38domo 0:48863a0964f5 152 //BSP_LCD_DisplayStringAt(0, LINE(idx+1), (uint8_t *)&text, LEFT_MODE);
38domo 0:48863a0964f5 153 }
38domo 0:48863a0964f5 154
38domo 0:48863a0964f5 155 //BSP_LCD_DrawPixel(TS_State.touchX[0], TS_State.touchY[0], LCD_COLOR_ORANGE);
38domo 0:48863a0964f5 156
38domo 0:48863a0964f5 157 if ((TS_State.touchY[0] > 160) && (TS_State.touchY[0] < (160+80)))
38domo 0:48863a0964f5 158 {
38domo 0:48863a0964f5 159 int numTouche = 0;
jeanpierreaulas 5:ceefd0cfa233 160 if ((TS_State.touchX[0] > 10) && (TS_State.touchX[0] < (10+100)))
jeanpierreaulas 5:ceefd0cfa233 161 {if(timer_touch[0].read_ms() == 0) {numTouche = 1;timer_touch[0].start();}}
jeanpierreaulas 5:ceefd0cfa233 162 if ((TS_State.touchX[0] > 130) && (TS_State.touchX[0] < (130+100)))
jeanpierreaulas 5:ceefd0cfa233 163 {if(timer_touch[1].read_ms() == 0) {numTouche = 2;timer_touch[1].start();}}
jeanpierreaulas 5:ceefd0cfa233 164 if ((TS_State.touchX[0] > 250) && (TS_State.touchX[0] < (250+100)))
jeanpierreaulas 5:ceefd0cfa233 165 {if(timer_touch[2].read_ms() == 0) {numTouche = 3;timer_touch[2].start();}}
jeanpierreaulas 5:ceefd0cfa233 166 if ((TS_State.touchX[0] > 370) && (TS_State.touchX[0] < (370+100)))
jeanpierreaulas 5:ceefd0cfa233 167 {if(timer_touch[3].read_ms() == 0) {numTouche = 4;timer_touch[3].start();}}
38domo 0:48863a0964f5 168 if (numTouche >0)
38domo 0:48863a0964f5 169 {
38domo 0:48863a0964f5 170 printf("Touche : %d", numTouche);
38domo 0:48863a0964f5 171 sprintf((char*)text, "Touche : %d", numTouche);
38domo 0:48863a0964f5 172 //BSP_LCD_DisplayStringAt(200,0, (uint8_t *)&text, LEFT_MODE);
38domo 0:48863a0964f5 173 status = numTouche;
38domo 0:48863a0964f5 174 }
38domo 0:48863a0964f5 175 else {lastToucheTime = 0;}
38domo 0:48863a0964f5 176 }
38domo 0:48863a0964f5 177 } else {
38domo 0:48863a0964f5 178 lastToucheTime = 0;
38domo 0:48863a0964f5 179 if (!cleared) {
38domo 0:48863a0964f5 180 //BSP_LCD_Clear(LCD_COLOR_DARKBLUE);
38domo 0:48863a0964f5 181 //sprintf((char*)text, "Touches: 0");
38domo 0:48863a0964f5 182 //BSP_LCD_DisplayStringAt(0, LINE(0), (uint8_t *)&text, LEFT_MODE);
38domo 0:48863a0964f5 183 //cleared = 1;
38domo 0:48863a0964f5 184 //int ss = touchInitGuiJPA1();
38domo 0:48863a0964f5 185 //ss=initDisplayTemp();
38domo 0:48863a0964f5 186 }
38domo 0:48863a0964f5 187 }
38domo 0:48863a0964f5 188 //touch screen
38domo 0:48863a0964f5 189 return status;
38domo 0:48863a0964f5 190 }
38domo 0:48863a0964f5 191
38domo 0:48863a0964f5 192 int sendTouchJPA(int xx)
38domo 0:48863a0964f5 193 {
38domo 0:48863a0964f5 194 uint8_t status = 0;
38domo 0:48863a0964f5 195 seconds = time(NULL);
38domo 0:48863a0964f5 196 if((seconds - lastToucheTime) > 1)
38domo 0:48863a0964f5 197 {
38domo 0:48863a0964f5 198 char samples[64] = {0};char resultBuffer[2000]= {0};
38domo 0:48863a0964f5 199 sprintf(samples,"&Touche=%i",xx);
38domo 0:48863a0964f5 200 printf("\nSend Touche=%i\n",xx);
38domo 0:48863a0964f5 201 int sendResult = groveStreams.sendJPA(myMac, samples, gsCompName, gsCompTmplId, resultBuffer, sizeof resultBuffer);
38domo 0:48863a0964f5 202 if (sendResult == 0) {
38domo 0:48863a0964f5 203 ////lcd.printf("Send Successful");
38domo 0:48863a0964f5 204 printf("Send Successful\n");
38domo 0:48863a0964f5 205 if (strlen(resultBuffer) > 0) {
38domo 0:48863a0964f5 206 //printf("HTTP string : %s\n", resultBuffer);
38domo 0:48863a0964f5 207 MbedJSONValue mbedJson;
38domo 0:48863a0964f5 208 parse(mbedJson, resultBuffer);
38domo 0:48863a0964f5 209
38domo 0:48863a0964f5 210 int displayOn = 0;
38domo 0:48863a0964f5 211 string s;
38domo 0:48863a0964f5 212 if (mbedJson.hasMember("Touche1")) {
38domo 0:48863a0964f5 213 s = mbedJson["Touche1"]["BackColor"].get<std::string>();
38domo 0:48863a0964f5 214 backColorButton[1] = (uint32_t)strtoul(s.c_str(), NULL,16);
38domo 0:48863a0964f5 215 s = mbedJson["Touche1"]["TextColor"].get<std::string>();
38domo 0:48863a0964f5 216 txtColorButton[1] = (uint32_t)strtoul(s.c_str(), NULL,16);
38domo 0:48863a0964f5 217 s = mbedJson["Touche1"]["Status"].get<std::string>();
38domo 0:48863a0964f5 218 sprintf(statusButton[1],s.c_str());
38domo 0:48863a0964f5 219 displayOn = (0x02 | displayOn);}
38domo 0:48863a0964f5 220
38domo 0:48863a0964f5 221 if (mbedJson.hasMember("Touche2")) {
38domo 0:48863a0964f5 222 s = mbedJson["Touche2"]["BackColor"].get<std::string>();
38domo 0:48863a0964f5 223 backColorButton[2] = (uint32_t)strtoul(s.c_str(), NULL,16);
38domo 0:48863a0964f5 224 s = mbedJson["Touche2"]["TextColor"].get<std::string>();
38domo 0:48863a0964f5 225 txtColorButton[2] = (uint32_t)strtoul(s.c_str(), NULL,16);
38domo 0:48863a0964f5 226 s = mbedJson["Touche2"]["Status"].get<std::string>();
38domo 0:48863a0964f5 227 sprintf(statusButton[2],s.c_str());
38domo 0:48863a0964f5 228 displayOn = (0x04 | displayOn);}
38domo 0:48863a0964f5 229
38domo 0:48863a0964f5 230 if (mbedJson.hasMember("Touche3")) {
38domo 0:48863a0964f5 231 s = mbedJson["Touche3"]["BackColor"].get<std::string>();
38domo 0:48863a0964f5 232 backColorButton[3] = (uint32_t)strtoul(s.c_str(), NULL,16);
38domo 0:48863a0964f5 233 s = mbedJson["Touche3"]["TextColor"].get<std::string>();
38domo 0:48863a0964f5 234 txtColorButton[3] = (uint32_t)strtoul(s.c_str(), NULL,16);
38domo 0:48863a0964f5 235 s = mbedJson["Touche3"]["Status"].get<std::string>();
38domo 0:48863a0964f5 236 sprintf(statusButton[3],s.c_str());
38domo 0:48863a0964f5 237 displayOn = (0x08 | displayOn);}
38domo 0:48863a0964f5 238
38domo 0:48863a0964f5 239 if (mbedJson.hasMember("Touche4")) {
38domo 0:48863a0964f5 240 s = mbedJson["Touche4"]["BackColor"].get<std::string>();
38domo 0:48863a0964f5 241 backColorButton[4] = (uint32_t)strtoul(s.c_str(), NULL,16);
38domo 0:48863a0964f5 242 s = mbedJson["Touche4"]["TextColor"].get<std::string>();
38domo 0:48863a0964f5 243 txtColorButton[4] = (uint32_t)strtoul(s.c_str(), NULL,16);
38domo 0:48863a0964f5 244 s = mbedJson["Touche4"]["Status"].get<std::string>();
38domo 0:48863a0964f5 245 sprintf(statusButton[4],s.c_str());
38domo 0:48863a0964f5 246 displayOn = (0x10 | displayOn);}
38domo 0:48863a0964f5 247
38domo 0:48863a0964f5 248 if ((displayOn & 0x02) > 0) {touchButtonJPA1(1);}
38domo 0:48863a0964f5 249 if ((displayOn & 0x04) > 0) {touchButtonJPA1(2);}
38domo 0:48863a0964f5 250 if ((displayOn & 0x08) > 0) {touchButtonJPA1(3);}
38domo 0:48863a0964f5 251 if ((displayOn & 0x10) > 0) {touchButtonJPA1(4);}
38domo 0:48863a0964f5 252 }
38domo 0:48863a0964f5 253 }
38domo 0:48863a0964f5 254 }
38domo 0:48863a0964f5 255
38domo 0:48863a0964f5 256 return status;
38domo 0:48863a0964f5 257 }