STM32F7-Discovery - TFT + Touch and Count down Timer Ver.1.0.5

Dependencies:   BSP_DISCO_F746NG LCD_DISCO_F746NG TS_DISCO_F746NG mbed

Fork of F746NG_TIMER by Enrico Marinoni

main.cpp

Committer:
emcu
Date:
2016-02-27
Revision:
0:ef97dc9b062e
Child:
1:478d8983fdde

File content as of revision 0:ef97dc9b062e:

/*
    
    TIMER
    
    STM32F7-Discovery Demo by www.emcu.it
    
    Date: Feb.2016
    Ver.: 1.0.1

*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*

*/

#include "TS_DISCO_F746NG.h"
#include "LCD_DISCO_F746NG.h"

LCD_DISCO_F746NG lcd;
TS_DISCO_F746NG ts;
DigitalOut led1(LED1);

int pos = 1;
uint8_t min = 45;
uint8_t Mem_min = 45;
uint16_t xpos=20, ypos=5;
int n=0;
int tcount=min;

void Logo(void);
void RedBox(int x, int y, int larg, int alt);


int main()
{  
    led1 = 1;
    
    TS_StateTypeDef TS_State;

    uint16_t x, y;
    uint8_t idx;
    uint8_t text[30];
    int Count=0;
    int Minuti=0;
    int n=0;
   
    
    lcd.SetBackColor(LCD_COLOR_BLUE);
    lcd.SetTextColor(LCD_COLOR_WHITE);
  
    lcd.DisplayStringAt(0, LINE(0), (uint8_t *)"                         ", CENTER_MODE);
    lcd.DisplayStringAt(0, LINE(1), (uint8_t *)"   By:  www.emcu.it      ", CENTER_MODE);
    lcd.DisplayStringAt(0, LINE(2), (uint8_t *)"                         ", CENTER_MODE);
    lcd.DisplayStringAt(0, LINE(3), (uint8_t *)"   Written using mBed    ", CENTER_MODE);
    lcd.DisplayStringAt(0, LINE(4), (uint8_t *)"                         ", CENTER_MODE);
    lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"   Touch the screen      ", CENTER_MODE);
    lcd.DisplayStringAt(0, LINE(6), (uint8_t *)"   for continue.         ", CENTER_MODE);
    lcd.DisplayStringAt(0, LINE(7), (uint8_t *)"                         ", CENTER_MODE);
  
    ts.GetState(&TS_State);
    while(TS_State.touchDetected == 0)
        {
        ts.GetState(&TS_State);
        }
 
// ReStart:
            
    while(1)
      {
        lcd.SetFont(&Font24);
        lcd.SetBackColor(LCD_COLOR_BLUE);
        lcd.SetTextColor(LCD_COLOR_WHITE);
        lcd.DisplayStringAt(0, LINE(0), (uint8_t *)" By: www.emcu.it         ", CENTER_MODE);
        lcd.DisplayStringAt(0, LINE(1), (uint8_t *)" Touch the screen for:   ", CENTER_MODE);
        lcd.DisplayStringAt(0, LINE(2), (uint8_t *)" LEFT   for decrease MIN ", CENTER_MODE);
        lcd.DisplayStringAt(0, LINE(3), (uint8_t *)" RIGHT  for increase MIN ", CENTER_MODE);
        lcd.DisplayStringAt(0, LINE(4), (uint8_t *)" REDbox for start TIMER  ", CENTER_MODE);
        lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"                         ", CENTER_MODE);
        sprintf((char*)text, " MIN: %d ", min);
        lcd.DisplayStringAt(xpos, LINE(7), (uint8_t *)&text, LEFT_MODE);
        
        // Draw the RED box that indicate the area to touch for start Timer
        RedBox(250, 220, 50, 50);        
        
        // Whait touch        
        ts.GetState(&TS_State);
        while(TS_State.touchDetected == 0)
            {
            ts.GetState(&TS_State);
            }
        
        
        // Increment
        ts.GetState(&TS_State);
        if ((TS_State.touchDetected) && (TS_State.touchX[0] > 400))
            {
            min++;
            if (min>254)
                min=254;
            lcd.SetBackColor(LCD_COLOR_BLUE);
            lcd.SetTextColor(LCD_COLOR_WHITE);                
            sprintf((char*)text, " MIN: %d ", min);
            lcd.DisplayStringAt(xpos, LINE(7), (uint8_t *)&text, LEFT_MODE);
            }
        // Decrement
        ts.GetState(&TS_State);
        if ((TS_State.touchDetected) && (TS_State.touchX[0] < 200))
            {
            min--;
            if (min<1)
                min=1;
            lcd.SetBackColor(LCD_COLOR_BLUE);
            lcd.SetTextColor(LCD_COLOR_WHITE);                
            sprintf((char*)text, " MIN: %d ", min);
            lcd.DisplayStringAt(xpos, LINE(7), (uint8_t *)&text, LEFT_MODE);
            }            
            
        // Whait touch        
        ts.GetState(&TS_State);
        while(TS_State.touchDetected == 0)
            {
            ts.GetState(&TS_State);
            }
                        
        wait(0.1);
        
        // Draw the RED box that indicate the area to touch for start Timer
        RedBox(250, 220, 50, 50);

        /*
        for (n=1; n<50; n++)
            {
            lcd.DrawRect(250, 220, 50, 50-n);
            //lcd.DrawRect(250, 220, 50-n, 50-n);
            }
        */
        
        Count=0;
        
        // Get the touch position for Start Timer
        for (idx = 0; idx < TS_State.touchDetected; idx++) 
            {        
            x = TS_State.touchX[idx];
            y = TS_State.touchY[idx];
            if ((x>260 & x<300) && (y>250 & y<280))
                {
                lcd.Clear(LCD_COLOR_BLACK);
                lcd.SetFont(&Font24);
                lcd.SetBackColor(LCD_COLOR_BLACK);
                lcd.SetTextColor(LCD_COLOR_WHITE);                
                sprintf((char*)text, " Minutes that remain:  %d ", min);
                lcd.DisplayStringAt(xpos, LINE(5), (uint8_t *)&text, LEFT_MODE);
                Logo();
                                
                tcount=min;
                tcount=tcount*60;
                Mem_min=min;
                Minuti=0;
                for (n=0; n<tcount; n++)
                    {
                    wait(1); // 60 == 1min
                    
                    // Show an * for highlight that the program is running
                    Count++;
                    if (Count == 1)
                        lcd.DisplayStringAt(0, LINE(2), (uint8_t *)"*", CENTER_MODE);                                                                    
                    if (Count == 2)
                        {
                        lcd.DisplayStringAt(0, LINE(2), (uint8_t *)" ", CENTER_MODE);                     
                        Count=0;
                        }
                     
                    // Counter for minutes   
                    Minuti++;
                    if (Minuti==60)
                        {
                        min--;
                        Minuti=0;
                        }
                    else;
                    
                    // Display the time remaining
                    if (min < 10)
                        {
                        //lcd.Clear(LCD_COLOR_RED);
                        lcd.SetFont(&Font24);                    
                        lcd.SetBackColor(LCD_COLOR_RED);
                        lcd.SetTextColor(LCD_COLOR_WHITE);                    
                        sprintf((char*)text, " Minutes that remain:  %d ", min);
                        lcd.DisplayStringAt(xpos, LINE(5), (uint8_t *)&text, LEFT_MODE);
                        Logo();                             
                        }
                    else
                        {
                        //lcd.Clear(LCD_COLOR_BLACK);
                        lcd.SetFont(&Font24);                    
                        lcd.SetBackColor(LCD_COLOR_BLACK);
                        lcd.SetTextColor(LCD_COLOR_WHITE);                    
                        sprintf((char*)text, " Minutes that remain:  %d ", min);
                        lcd.DisplayStringAt(xpos, LINE(5), (uint8_t *)&text, LEFT_MODE);
                        Logo();     
                        }             
                    }
                
                min=Mem_min;
                lcd.SetFont(&Font24);
                
                lcd.Clear(LCD_COLOR_BLACK);
                lcd.SetFont(&Font24);
                
                // Highlight the end of time            

                // Waiting Touch the screen for restart the program
                ts.GetState(&TS_State);
                while(TS_State.touchDetected == 0)
                    {
                    lcd.Clear(LCD_COLOR_RED);
                    lcd.SetBackColor(LCD_COLOR_RED);                                       
                    lcd.SetTextColor(LCD_COLOR_WHITE);         
                    lcd.DisplayStringAt(0, LINE(2), (uint8_t *)"                           ", CENTER_MODE);           
                    lcd.DisplayStringAt(0, LINE(3), (uint8_t *)" END time...               ", CENTER_MODE);
                    lcd.DisplayStringAt(0, LINE(4), (uint8_t *)" STOP the presentation !!! ", CENTER_MODE);
                    lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"                           ", CENTER_MODE);
                    lcd.DisplayStringAt(0, LINE(6), (uint8_t *)"                           ", CENTER_MODE);
                    lcd.DisplayStringAt(0, LINE(7), (uint8_t *)" Touch screen to restart   ", CENTER_MODE);
                    lcd.DisplayStringAt(0, LINE(8), (uint8_t *)"                           ", CENTER_MODE);                    
                    wait(0.5);
                    
                    lcd.Clear(LCD_COLOR_BLUE);
                    lcd.SetBackColor(LCD_COLOR_BLUE);                                       
                    lcd.SetTextColor(LCD_COLOR_WHITE);         
                    lcd.DisplayStringAt(0, LINE(2), (uint8_t *)"                           ", CENTER_MODE);           
                    lcd.DisplayStringAt(0, LINE(3), (uint8_t *)" END time...               ", CENTER_MODE);
                    lcd.DisplayStringAt(0, LINE(4), (uint8_t *)" STOP the presentation !!! ", CENTER_MODE);
                    lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"                           ", CENTER_MODE);
                    lcd.DisplayStringAt(0, LINE(6), (uint8_t *)"                           ", CENTER_MODE);
                    lcd.DisplayStringAt(0, LINE(7), (uint8_t *)" Touch screen to restart   ", CENTER_MODE);
                    lcd.DisplayStringAt(0, LINE(8), (uint8_t *)"                           ", CENTER_MODE);                      
                    wait(0.5);
                    
                    ts.GetState(&TS_State);
                    }
                
                
                
                lcd.Clear(LCD_COLOR_BLACK);
                lcd.SetTextColor(LCD_COLOR_WHITE);         
                lcd.DisplayStringAt(0, LINE(2), (uint8_t *)"                           ", CENTER_MODE);           
                lcd.DisplayStringAt(0, LINE(3), (uint8_t *)"                           ", CENTER_MODE);
                lcd.DisplayStringAt(0, LINE(4), (uint8_t *)" Remove your finger from   ", CENTER_MODE);
                lcd.DisplayStringAt(0, LINE(5), (uint8_t *)" the screen                ", CENTER_MODE);
                lcd.DisplayStringAt(0, LINE(6), (uint8_t *)"                           ", CENTER_MODE);
                lcd.DisplayStringAt(0, LINE(7), (uint8_t *)"                           ", CENTER_MODE);
                lcd.DisplayStringAt(0, LINE(8), (uint8_t *)"                           ", CENTER_MODE); 
                // Whait remove touch        
                ts.GetState(&TS_State);
                while(TS_State.touchDetected == 1)
                    {
                    ts.GetState(&TS_State);
                    }
                lcd.Clear(LCD_COLOR_BLACK);                
                                    
                // goto ReStart;
                }
            }
      }
}

void Logo(void)
{
    lcd.SetFont(&Font16);
    lcd.SetBackColor(LCD_COLOR_BLACK);
    lcd.SetTextColor(LCD_COLOR_WHITE);
    lcd.DisplayStringAt(0, LINE(0), (uint8_t *)" By: www.emcu.it", LEFT_MODE);
    lcd.SetFont(&Font24);
}

void RedBox(int x, int y, int larg, int alt)
{
    // Draw the RED box that indicate the area to touch for start Timer
    lcd.SetTextColor(LCD_COLOR_RED);
    lcd.FillRect(x, y, larg, alt);
}