Project Embedded Systems E-ict Denayer

Dependencies:   BSP_DISCO_F746NG F7_Ethernet LCD_DISCO_F746NG TS_DISCO_F746NG mbed-rtos mbed

main.cpp

Committer:
Ayrton_L
Date:
2017-01-26
Revision:
2:1a5565ee8219
Parent:
1:a2f7adf6db3d
Child:
3:7aef6c427f97

File content as of revision 2:1a5565ee8219:

#include "main.h"

uint32_t ul_ResetDisplay( void )
{
    display.Clear( LCD_COLOR_WHITE );
    display.SetBackColor( LCD_COLOR_WHITE );
    display.SetTextColor( LCD_COLOR_BLACK );
    return 0;
}

/*-----------------------------------------------------------*/

uint32_t ul_CheckDHCP( void )                                     
{
    uint32_t ul_MyReturn = 2;
    Network *x_DynNetwork = mpool.alloc();                                      //waarschijnlijk loopt het hier mis met een memory pool
     
    if( Eth.init( ) == 0 )
    {
        char c_NewIP[16];
        Eth.connect( );
        strncpy( c_NewIP, Eth.getIPAddress( ), 16 );
        if( strcmp( x_DynNetwork->c_IPAddress, c_NewIP ) == false )
        {
            strncpy( x_DynNetwork->c_IPAddress, c_NewIP, 16 );
            strncpy( x_DynNetwork->c_MACAddress, Eth.getMACAddress( ), 16 );
            strncpy( x_DynNetwork->c_Netmask, Eth.getNetworkMask( ), 16 );
            strncpy( x_DynNetwork->c_Gateway, Eth.getGateway( ), 16 );
            queue.put(x_DynNetwork);
        }
        ul_MyReturn = 0;
    }
    else
    {
            ul_MyReturn =1;
    }
    return ul_MyReturn;
}

/*-----------------------------------------------------------*/

uint32_t ul_CheckInternet( void )
{
    uint32_t ul_Counter = 0;
    uint32_t ul_PingCounter = 0;
    ip_addr_t x_PingTarget = { 0x08080808 }; 
    for( ul_Counter = 0; ul_Counter < 4; ul_Counter++)
    {    
        if( ul_Ping( &x_PingTarget ) == 0 )
        {
            ul_PingCounter++;
        }
    }
    if( ul_PingCounter >= 3 )
    {
        return 0; 
    }
    else
    {
        return 1;
    }
      
}

/*-----------------------------------------------------------*/

uint32_t ul_ShowDevices( void )
{
    uint32_t ul_GenIP = 0;
    uint32_t ul_MyIP = 0;
    uint32_t ul_Counter =0;
    uint32_t ul_DevCounter = 0;
    
    ip_addr_t x_ActDev[255];                     //max 255 adressen binnen sub, beter is malloc gebruik en redefine van array/vector => max IP adressen haalbaar: 4228250625
    
    osEvent x_Evt = queue.get();
    if (x_Evt.status == osEventMessage) 
    {
        Network *x_DynNetwork = (Network*)x_Evt.value.p;
        ul_MyIP = ( uint32_t ) x_DynNetwork->c_IPAddress ;
        mpool.free(x_DynNetwork);
        
        ul_GenIP = ul_MyIP & 0xFFFFFF00;
       
        for( ul_Counter = 1; ul_Counter < 255; ul_Counter++)
        {
            ul_GenIP++;
            if( ul_Ping( ( ip_addr_t * ) ul_GenIP ) == 0 )
            {
                ip_addr_t x_ConIP = { ul_GenIP };
                x_ActDev[ul_DevCounter] = x_ConIP;                          //voor een of andere reden is dit de enige manier dat werkt
                ul_DevCounter++;
            }
        }  
    }
    
    return 0;
}

uint32_t ul_CreateMenu( void )
{
    //480x272
    display.DrawRect( 0, 0, 125, 68);
    display.DisplayStringAt( 2, 34, ( uint8_t * )"Settings", LEFT_MODE );
    display.DrawRect( 0, 68, 125, 68);
    display.DisplayStringAt( 2, 102, ( uint8_t * )"PING", LEFT_MODE );
    return 0;
}

/*-----------------------------------------------------------*/

uint32_t ul_ShowSettings( void )
{
    osEvent x_Evt = queue.get();
    if (x_Evt.status == osEventMessage) 
    {
        Network *x_DynNetwork = (Network*)x_Evt.value.p;
        ul_ResetDisplay( );
        ul_CreateMenu( );
        display.SetFont( &Font16 );
        display.DisplayStringAt( 130, 10, ( uint8_t * )"IP address: ", LEFT_MODE ); 
        display.DisplayStringAt( 270, 10, ( uint8_t * )x_DynNetwork->c_IPAddress, LEFT_MODE );
        display.DisplayStringAt( 130, 30, ( uint8_t * )"MAC Address: ", LEFT_MODE ); 
        display.DisplayStringAt( 270, 30, ( uint8_t * )x_DynNetwork->c_MACAddress, LEFT_MODE );  
        display.DisplayStringAt( 130, 50, ( uint8_t * )"Netmask: ", LEFT_MODE ); 
        display.DisplayStringAt( 270, 50, ( uint8_t * )x_DynNetwork->c_Netmask, LEFT_MODE ); 
        display.DisplayStringAt( 130, 70, ( uint8_t * )"Gateway: ", LEFT_MODE ); 
        display.DisplayStringAt( 270, 70, ( uint8_t * )x_DynNetwork->c_Gateway, LEFT_MODE );
        display.DisplayStringAt( 130, 90, ( uint8_t * )"Internet: ", LEFT_MODE ); 
        if( ul_CheckInternet == 0 )
        {
            display.DisplayStringAt( 270, 90, ( uint8_t * )"connected", LEFT_MODE );
        }
        else
        {
            display.DisplayStringAt( 270, 90, ( uint8_t * )"not connected", LEFT_MODE );
        }
        mpool.free(x_DynNetwork);
    }
    return 0;
}

/*-----------------------------------------------------------*/

void v_TouchThread( )
{
     Timer x_Timer1;
    uint32_t ul_Time =0;
    uint32_t ul_Counter =0;
    TS_StateTypeDef Touch_State; 
    ip_addr_t x_PingTarget = { 0x08080808 }; 
    
    Touch.Init( 420, 272 ); 
    
    while( 1 ) 
    {
        Touch.GetState( &Touch_State );
        if( ( Touch_State.touchDetected )&& ( Touch_State.touchX[0] < 125 )&& ( Touch_State.touchY[0] < 69 ) ) 
        {
            ul_ResetDisplay( );
            ul_CreateMenu( );
            ul_ShowSettings( );
        } 
        else if( ( Touch_State.touchDetected )&& ( Touch_State.touchX[0] < 125 )&& ( Touch_State.touchY[0] > 68 )&& ( Touch_State.touchY[0] < 137 ) ) 
        { 
            for( ul_Counter = 0; ul_Counter < 4; ul_Counter++)
            {
                x_Timer1.start( );
                ul_Ping( &x_PingTarget );
                x_Timer1.stop( );
                ul_Time = ul_Time + x_Timer1;
            }
            ul_ResetDisplay( );
            ul_CreateMenu( );
            ul_Time = ul_Time/4;
            display.DisplayStringAt( 130, 10, ( uint8_t * )"Internet: ", LEFT_MODE );
            if( ul_CheckInternet == 0 )
            {
                display.DisplayStringAt( 270, 90, ( uint8_t * )"connected", LEFT_MODE );
            }
            else
            {
                display.DisplayStringAt( 270, 90, ( uint8_t * )"not connected", LEFT_MODE );
            }
            display.DisplayStringAt( 130, 30, ( uint8_t * )"PING: ", LEFT_MODE ); 
            display.DisplayStringAt( 270, 30, ( uint8_t * )ul_Time, LEFT_MODE );
        }
        else
        {
            //niks doen
        }
    }
}

/*-----------------------------------------------------------*/

int main( void )
{   
    Thread x_T1(osPriorityNormal, (DEFAULT_STACK_SIZE * 2.25), NULL);
    x_T1.start( v_TouchThread );
    while( ul_CheckDHCP( ) != 0 )
    {
        ul_ResetDisplay( );
        ul_CreateMenu( );
        //Thread x_T1(osPriorityNormal, (DEFAULT_STACK_SIZE * 2.25), NULL);
        //x_T1.start( v_TouchThread );

        ul_ShowSettings( );
        ul_CheckInternet( );
        
        wait_ms(1000);                                           //kleinde delay => geen 1000 keer uitvoeren van checkdhcp => efficienter maar misschien iets trager
    }
    return 0;
}