a demo of GUI on DISCOF7 consisting of a few buttons and feeders

Dependencies:   ADXL345 BSP_DISCO_F746NG LCD_DISCO_F746NG TS_DISCO_F746NG

main.cpp

Committer:
habiburrahman
Date:
2016-08-05
Revision:
2:76522cfa03cd
Parent:
1:4f356fb65677
Child:
3:afd2bb1331a4

File content as of revision 2:76522cfa03cd:

#include "mbed.h"
#include "TS_DISCO_F746NG.h"
#include "LCD_DISCO_F746NG.h"
#include "ESP8266.h"
#include "TCPSocketConnection.h"
#include <string>
using namespace std;
 

 
LCD_DISCO_F746NG lcd;
TS_DISCO_F746NG ts;
TS_StateTypeDef TS_State;



bool touch_test_rectangle(uint16_t tx, uint16_t ty, uint16_t x, uint16_t y, uint16_t width, uint16_t height){

    bool x_is_in =0;
    bool y_is_in = 0;
    
    if( (tx>x) && (tx<(x+width)))  x_is_in = 1;
    if( (ty>y) && (ty<(y+height)))  y_is_in = 1;
    
    bool buttest;
    buttest = x_is_in & y_is_in;
    
    return (bool) buttest;
}


uint16_t h_feeder(uint16_t tx, uint16_t ty,uint16_t x, uint16_t y, uint16_t width, uint16_t height){
    
    bool x_is_in =0;
    bool y_is_in =0;
    
    if( (tx>x) && (tx<(x+width)))  x_is_in = 1;
    if( (ty>y) && (ty<(y+height)))  y_is_in = 1;
    
    bool buttest;
    buttest = x_is_in & y_is_in;
    
    if(buttest){
        return tx - x;
    }
    
    
}

bool but1=0,but2=0,but3=0;
uint16_t feed1_dist = 0;
uint16_t feed2_dist = 0;

int main2()
{
   
    uint16_t x, y;
    uint8_t text[30];
    uint8_t status;
    uint8_t idx;
    uint8_t cleared = 0;
    uint8_t prev_nb_touches = 0;

    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_BLUE);
        lcd.SetBackColor(LCD_COLOR_BLUE);
        lcd.SetTextColor(LCD_COLOR_WHITE);
        lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT OK", CENTER_MODE);
    }
    
    lcd.SetBackColor(LCD_COLOR_YELLOW);
    lcd.SetTextColor(LCD_COLOR_RED);
    
    lcd.Clear(LCD_COLOR_WHITE);
    
    
    lcd.SetTextColor(LCD_COLOR_RED);
    lcd.FillRect(48, 68, 96, 60);
    lcd.FillRect(192, 68, 96, 60);
    lcd.FillRect(338, 68, 96, 60);
    
    lcd.FillRect(48, 152, 384, 30);
    lcd.FillRect(48, 212, 384, 30);
    
    wait(1);

    
    while(1){
        
        but1=0;
        but2=0;
        but3=0;
        
        ts.GetState(&TS_State);
        if (TS_State.touchDetected) {
            
            feed1_dist = h_feeder(TS_State.touchX[0],TS_State.touchY[0],48, 152, 384, 30);
            feed2_dist = h_feeder(TS_State.touchX[0],TS_State.touchY[0],48, 212, 384, 30);
            but1 = touch_test_rectangle(TS_State.touchX[0],TS_State.touchY[0], 48, 68, 96, 60);
            but2 = touch_test_rectangle(TS_State.touchX[0],TS_State.touchY[0], 192, 68, 96, 60);
            but3 = touch_test_rectangle(TS_State.touchX[0],TS_State.touchY[0], 338, 68, 96, 60);
            
            if(feed1_dist>0){
            lcd.SetTextColor(LCD_COLOR_RED);
            lcd.FillRect(48, 152, 384, 30);
            lcd.SetTextColor(LCD_COLOR_GREEN);
            lcd.FillRect(48, 152, feed1_dist, 30);
            }
            
            if(feed2_dist>0){
            lcd.SetTextColor(LCD_COLOR_RED);
            lcd.FillRect(48, 212, 384, 30);
            lcd.SetTextColor(LCD_COLOR_GREEN);
            lcd.FillRect(48, 212, feed2_dist, 30);
            }   
        }
        
        if(but1){
                lcd.SetTextColor(LCD_COLOR_GREEN);
                lcd.FillRect(48, 68, 96, 60);        
        }
        else{
                lcd.SetTextColor(LCD_COLOR_RED);
                lcd.FillRect(48, 68, 96, 60);   
        }
        
        if(but2){
                lcd.SetTextColor(LCD_COLOR_GREEN);
                lcd.FillRect(192, 68, 96, 60);
        }
        else{
                lcd.SetTextColor(LCD_COLOR_RED);
                lcd.FillRect(192, 68, 96, 60); 
        }
        if(but3){
                lcd.SetTextColor(LCD_COLOR_GREEN);
                lcd.FillRect(338, 68, 96, 60);
        }
        else{
                lcd.SetTextColor(LCD_COLOR_RED);
                lcd.FillRect(338, 68, 96, 60);   
        }        
        
    }
}


/////////////////////////////////////////////////////////////////////
//ACCELEROMETER TEST

#include "ADXL345.h"
 
ADXL345 accelerometer(D11,D12,D13,D10);
Serial pc(USBTX, USBRX);

int main() {
 
    int readings[3] = {0, 0, 0};
    
    pc.printf("Starting ADXL345 test...\n");
    pc.printf("Device ID is: 0x%02x\n", accelerometer.getDevId());
 
    //Go into standby mode to configure the device.
    accelerometer.setPowerControl(0x00);
 
    //Full resolution, +/-16g, 4mg/LSB.
    accelerometer.setDataFormatControl(0x0B);
    
    //3.2kHz data rate.
    accelerometer.setDataRate(ADXL345_3200HZ);
 
    //Measurement mode.
    accelerometer.setPowerControl(0x08);
 
    while (1) {
    
        wait(0.1);
        
        accelerometer.getOutput(readings);
        
        //13-bit, sign extended values.
        pc.printf("%i, %i, %i\n", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]);
 
    }
 
}