Decide right or left for you
Dependencies: mbed LCD_DISCO_F469NI TS_DISCO_F469NI BSP_DISCO_F469NI
Project description
Nothing more simple: this is a high quality project allowing the user to download cosmos random to find a direction in your life when you have no idea where to go.
The decisions are restrained to RIGHT or LEFT.
Features
- Cosmos download via SCP (secure copy using SSH for data transfer).
- Countdown displays the remaining downloading time.
- Pick randomly RIGHT or LEFT and display the choice with an arrow and text.
- Track the number of decision.
- Home button to download again the cosmos.
Instructions
- Plug the board to USB to supply the board with some electricity (if using USB micro ('USB USER' plug) you must put the jumper JP2 on 'USB', if using USB mini ('USB ST-LINK' plug) you must put the jumper JP2 on 'STLK').
- Wait a very short moment so the board boot and the touchscreen initialize (a green screen should appears).
- Touch anywhere on the touchscreen to start the cosmos downloading.
- Observe the random direction extracted from the cosmos.
- Touch again as many time as needed to decide again (the number of decision is displayed in the top left corner). NB: each cosmos download contains an infinity of random direction.
- If desired you can download again the cosmos via the home screen by touching the home button.
- If needed (the cosmos may be busy or broken), you can reset the board and go back on the home screen by pushing the black button or by plugging/unplugging the USB power cable.
Disclaimer
Injuries
Any injury resulting from an abnormal of the decider code cannot be imputed to the author of this code.
Joke
This is a fantasy, don't take it for serious.
Diff: main.cpp
- Revision:
- 4:0f34a6ad1f4c
- Parent:
- 0:0e730157c767
- Child:
- 5:ef44b84a149f
--- a/main.cpp Wed Jul 05 06:50:17 2017 +0000 +++ b/main.cpp Thu May 16 13:00:16 2019 +0000 @@ -1,6 +1,14 @@ #include "mbed.h" #include "TS_DISCO_F469NI.h" #include "LCD_DISCO_F469NI.h" +#include <stdio.h> +#include <stdlib.h> + +enum { + LEFT, + RIGHT +}; + LCD_DISCO_F469NI lcd; TS_DISCO_F469NI ts; @@ -8,77 +16,109 @@ int main() { TS_StateTypeDef TS_State; - uint16_t x, y; - uint8_t text[30]; + uint8_t direction, decisionCounter=1; + uint8_t text[30] = ""; uint8_t status; - uint8_t idx; - uint8_t cleared = 0; - uint8_t prev_nb_touches = 0; - + uint8_t fingerPress = 0; + time_t t; + Point points[4]; + + /* Intializes random number generator */ + srand((unsigned) time(&t)); + BSP_LCD_SetFont(&Font24); - - 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); - } - 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); + 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); + } else { + lcd.Clear(LCD_COLOR_DARKGREEN); + lcd.SetBackColor(LCD_COLOR_DARKGREEN); + lcd.SetTextColor(LCD_COLOR_WHITE); + lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT OK", CENTER_MODE); + lcd.DisplayStringAt(0, LINE(6), (uint8_t *)"READY TO CHOSE YOUR LIFE", CENTER_MODE); + lcd.DisplayStringAt(0, LINE(8), (uint8_t *)"TOUCH TO DECIDE", CENTER_MODE); } - wait(1); - lcd.SetBackColor(LCD_COLOR_BLUE); - lcd.SetTextColor(LCD_COLOR_WHITE); - - while(1) - { - - 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); - } + lcd.SetBackColor(LCD_COLOR_LIGHTGRAY); + lcd.SetTextColor(LCD_COLOR_BLACK); + + while(1) { + ts.GetState(&TS_State); + if (TS_State.touchDetected && fingerPress==0) {//finger lands + fingerPress = 1; + /* background */ + lcd.Clear(LCD_COLOR_LIGHTGRAY); + wait(0.1); + /* decision counter */ + sprintf((char*)text, "Decision#:%d", decisionCounter++); + lcd.DisplayStringAt(0, LINE(0), (uint8_t *)&text, LEFT_MODE); + /* decision of direction*/ + direction = rand()%2; + switch(direction) { + case LEFT: + sprintf((char*)text, "LEFT"); + //horizontal bar + lcd.FillRect(250,300,300,20); + //top diagonal + points[0].X=250; + points[0].Y=300; + points[1].X=250+80; + points[1].Y=300-70; + points[2].X=250+20+80; + points[2].Y=300-70; + points[3].X=250+20; + points[3].Y=300; + lcd.FillPolygon(points, 4); + //bottom diagonal + points[0].X=250; + points[0].Y=300+20; + points[1].X=250+80; + points[1].Y=300+70+20; + points[2].X=250+20+80; + points[2].Y=300+70+20; + points[3].X=250+20; + points[3].Y=300+20; + lcd.FillPolygon(points, 4); + break; + case RIGHT: + sprintf((char*)text, "RIGHT"); + //horizontal bar + lcd.FillRect(250,300,300,20); + //top diagonal + points[0].X=250+300-20; + points[0].Y=300; + points[1].X=250+300-20-80; + points[1].Y=300-70; + points[2].X=250+300-80; + points[2].Y=300-70; + points[3].X=250+300; + points[3].Y=300; + lcd.FillPolygon(points, 4); + //bottom diagonal + points[0].X=250+300-20; + points[0].Y=300+20; + points[1].X=250+300-20-80; + points[1].Y=300+70+20; + points[2].X=250+300-80; + points[2].Y=300+70+20; + points[3].X=250+300; + points[3].Y=300+20; + lcd.FillPolygon(points, 4); + break; + } + //display text of direction + lcd.DisplayStringAt(0, LINE(6), (uint8_t *)&text, CENTER_MODE); } - 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); - lcd.FillCircle(TS_State.touchX[idx], TS_State.touchY[idx], 20); + if(!TS_State.touchDetected && fingerPress==1) {//finger takes off + fingerPress = 0; } - } - 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; + if(!TS_State.touchDetected) {//finger is flying } - } + if(TS_State.touchDetected) {//finger is on the ground + } } }