Droni e Droidi / Mbed OS F746NG_TestAll

Dependencies:   TS_DISCO_F746NG LCD_DISCO_F746NG BSP_DISCO_F746NG BUTTON_GROUP Arduino

main.cpp

Committer:
MaxScorda
Date:
2020-12-21
Revision:
4:0ce191d3f0ce
Parent:
3:e7e663758e6d
Child:
5:19f1743cecb1

File content as of revision 4:0ce191d3f0ce:

//https://os.mbed.com/teams/ST/code/BSP_DISCO_F746NG/docs/tip/stm32746g__discovery__lcd_8c.html
#include "mbed.h"
#include "EthernetInterface.h"

#include "stm32746g_discovery_lcd.h"
#include "stm32746g_discovery_ts.h"

#include "button_group.hpp"
using namespace ButGrp;
TS_DISCO_F746NG ts_;
LCD_DISCO_F746NG BSP_LCD_;

char str[255];
#define IP         "192.168.1.175"
#define GATEWAY    "192.168.1.1"
#define MASK       "255.255.255.0"


// Network interface
EthernetInterface net;

DigitalOut led1(LED1);
InterruptIn button(USER_BUTTON);

//
// Setting of button group
const int Y0 = 5;
const int X1 = 30;
const int Y1 = 100;
const int DimX=BSP_LCD_GetXSize()-1;
const int DimY=BSP_LCD_GetYSize()-1;

const uint32_t BACK_COLOR = 0xFF006A6C;             // teal green
const uint32_t INACTIVE = BACK_COLOR & 0xE0FFFFFF;  // color for inactive button
const string STR[4] = {"Button1", "Button2", "Button3", "Button4"};
ButtonGroup bGroup(BSP_LCD_, ts_, 10, Y0, 66, 40, LCD_COLOR_BLUE, BACK_COLOR, 4, STR, 5, 5, 3);

const string STR_SW[2] = {"ON", "OFF"};
ButtonGroup sw(BSP_LCD_, ts_, 10, 160, 66, 40, LCD_COLOR_GREEN, BACK_COLOR, 2, STR_SW, 5, 0, 2);
Button reset(BSP_LCD_, ts_, 410, Y0, 60, 40, LCD_COLOR_BLUE, BACK_COLOR, "Reset", Font12);


const string MULTI[2] = {"ON", "OFF"};
ButtonGroup multiTouch(BSP_LCD_, ts_, 300, Y0+100, 60, 40, LCD_COLOR_BLUE, BACK_COLOR, 2, MULTI, 5, 0, 2);
// End of button group setting

//Touch
# define NUMLAYER 2
TS_StateTypeDef TS_State;
uint8_t status;

#include "Functions.h"

// Socket demo
int main()
{
    //Tasto
    button.rise(&PressButton);

    //MILLIS
    //  millisStart();

    //display
    BSP_LCD_Init();
    BSP_LCD_DisplayOn();
    /* Initialize the LCD Layers */
    BSP_LCD_LayerRgb565Init(0, LCD_FB_START_ADDRESS);
    BSP_LCD_LayerRgb565Init(1, LCD_FB_START_ADDRESS+(BSP_LCD_GetXSize()*BSP_LCD_GetYSize()*NUMLAYER)); //Reserve memory art SRAM acc. resolution and color format
    SetLayer( 0);
    //  BSP_LCD_LayerDefaultInit(LTDC_ACTIVE_LAYER, LCD_FB_START_ADDRESS);
    //  BSP_LCD_SelectLayer(LTDC_ACTIVE_LAYER);
    BSP_LCD_Clear(LCD_COLOR_BLACK);
    BSP_LCD_SetBackColor(LCD_COLOR_BLACK);
    BSP_LCD_SetTextColor(LCD_COLOR_YELLOW);

    BSP_LCD_DrawRect(0, 0, DimX/3, 40);
    BSP_LCD_DrawRect(DimX/3+5, 0, ((DimX*2)/3)-5, 40);
    BSP_LCD_DrawRect(40, 45, DimX-80, DimY-45-45);
    BSP_LCD_DrawRect(0, DimY-40, DimX, 40);
    BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
    BSP_LCD_.SetFont(&Font8);
    BSP_LCD_DisplayStringAt(0, LINE(2), (uint8_t *)"Test All 0.2", LEFT_MODE);

    // touch
    status = BSP_TS_Init(BSP_LCD_GetXSize(), BSP_LCD_GetYSize());
        BSP_LCD_.SetFont(&Font12);
    if (status != TS_OK) {
        BSP_LCD_DisplayStringAt(100, LINE(3), (uint8_t *)"TOUCHSCREEN INIT FAIL", LEFT_MODE);
    } else {
        BSP_LCD_DisplayStringAt(100, LINE(3), (uint8_t *)"TOUCHSCREEN INIT OK", LEFT_MODE);
    }

// Buttons
    reset.Draw(INACTIVE, LCD_COLOR_GRAY);
    multiTouch.Draw(0, LCD_COLOR_DARKBLUE);
    BSP_LCD_.DisplayStringAt(250, Y0+80, (uint8_t *)"Multi-touch ON, OFF", LEFT_MODE);
    BSP_LCD_.SetFont(&Font20);
    BSP_LCD_.DisplayStringAt(120, 250, (uint8_t *)"02/22 22:36", LEFT_MODE);


    // ethernet interface
    printf("Ethernet socket example\n");
    int i=net.set_network(IP,MASK,GATEWAY);
    net.connect();

    // Show the network address
    const char *ip = net.get_ip_address();
    const char *netmask = net.get_netmask();
    const char *gateway = net.get_gateway();
    printf("IP address: %s\n", ip ? ip : "None");
    printf("Netmask: %s\n", netmask ? netmask : "None");
    printf("Gateway: %s\n", gateway ? gateway : "None");

    // Open a socket on the network interface, and create a TCP connection to mbed.org
    TCPSocket socket;

    socket.open(&net);
    socket.connect("api.ipify.org", 80);
    char *buffer = new char[256];

    // Send an HTTP request
    strcpy(buffer, "GET / HTTP/1.1\r\nHost: api.ipify.org\r\n\r\n");
    int scount = socket.send(buffer, strlen(buffer));
    printf("sent %d [%.*s]\n", scount, strstr(buffer, "\r\n")-buffer, buffer);

    // Recieve an HTTP response and print out the response line
    int rcount = socket.recv(buffer, 256);
    printf("recv %d [%.*s]\n", rcount, strstr(buffer, "\r\n")-buffer, buffer);

    // The api.ipify.org service also gives us the device's external IP address
    const char *payload = strstr(buffer, "\r\n\r\n")+4;
    printf("External IP address: %.*s\n", rcount-(payload-buffer), payload);

    // Close the socket to return its memory and bring down the network interface
    socket.close();
    delete[] buffer;

    // Bring down the ethernet interface
    net.disconnect();
    printf("Done\n");



    while(1) {
        GestioneButton();
        GestioneTouch();
    }
}