Project Embedded Systems E-ict Denayer
Dependencies: BSP_DISCO_F746NG F7_Ethernet LCD_DISCO_F746NG TS_DISCO_F746NG mbed-rtos mbed
main.cpp@3:7aef6c427f97, 2017-07-17 (annotated)
- Committer:
- Ayrton_L
- Date:
- Mon Jul 17 01:00:39 2017 +0000
- Revision:
- 3:7aef6c427f97
- Parent:
- 2:1a5565ee8219
- Child:
- 4:860566e5814e
first remote
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Ayrton_L | 0:16bcf70d262e | 1 | #include "main.h" |
Ayrton_L | 0:16bcf70d262e | 2 | |
Ayrton_L | 3:7aef6c427f97 | 3 | int32_t l_ResetDisplay( void ) |
Ayrton_L | 0:16bcf70d262e | 4 | { |
Ayrton_L | 0:16bcf70d262e | 5 | display.Clear( LCD_COLOR_WHITE ); |
Ayrton_L | 0:16bcf70d262e | 6 | display.SetBackColor( LCD_COLOR_WHITE ); |
Ayrton_L | 0:16bcf70d262e | 7 | display.SetTextColor( LCD_COLOR_BLACK ); |
Ayrton_L | 0:16bcf70d262e | 8 | return 0; |
Ayrton_L | 0:16bcf70d262e | 9 | } |
Ayrton_L | 0:16bcf70d262e | 10 | |
Ayrton_L | 1:a2f7adf6db3d | 11 | /*-----------------------------------------------------------*/ |
Ayrton_L | 1:a2f7adf6db3d | 12 | |
Ayrton_L | 3:7aef6c427f97 | 13 | int32_t l_CheckDHCP( void ) |
Ayrton_L | 0:16bcf70d262e | 14 | { |
Ayrton_L | 1:a2f7adf6db3d | 15 | uint32_t ul_MyReturn = 2; |
Ayrton_L | 2:1a5565ee8219 | 16 | Network *x_DynNetwork = mpool.alloc(); //waarschijnlijk loopt het hier mis met een memory pool |
Ayrton_L | 3:7aef6c427f97 | 17 | //Network *x_DynNetwork; |
Ayrton_L | 3:7aef6c427f97 | 18 | |
Ayrton_L | 0:16bcf70d262e | 19 | if( Eth.init( ) == 0 ) |
Ayrton_L | 0:16bcf70d262e | 20 | { |
Ayrton_L | 0:16bcf70d262e | 21 | char c_NewIP[16]; |
Ayrton_L | 0:16bcf70d262e | 22 | Eth.connect( ); |
Ayrton_L | 0:16bcf70d262e | 23 | strncpy( c_NewIP, Eth.getIPAddress( ), 16 ); |
Ayrton_L | 1:a2f7adf6db3d | 24 | if( strcmp( x_DynNetwork->c_IPAddress, c_NewIP ) == false ) |
Ayrton_L | 0:16bcf70d262e | 25 | { |
Ayrton_L | 1:a2f7adf6db3d | 26 | strncpy( x_DynNetwork->c_IPAddress, c_NewIP, 16 ); |
Ayrton_L | 3:7aef6c427f97 | 27 | //display.DisplayStringAt( 270, 10, ( uint8_t * )x_DynNetwork->c_IPAddress, LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 28 | //display.DisplayStringAt( 270, 50, ( uint8_t * )Eth.getIPAddress( ), LEFT_MODE ); |
Ayrton_L | 1:a2f7adf6db3d | 29 | strncpy( x_DynNetwork->c_MACAddress, Eth.getMACAddress( ), 16 ); |
Ayrton_L | 1:a2f7adf6db3d | 30 | strncpy( x_DynNetwork->c_Netmask, Eth.getNetworkMask( ), 16 ); |
Ayrton_L | 1:a2f7adf6db3d | 31 | strncpy( x_DynNetwork->c_Gateway, Eth.getGateway( ), 16 ); |
Ayrton_L | 1:a2f7adf6db3d | 32 | queue.put(x_DynNetwork); |
Ayrton_L | 0:16bcf70d262e | 33 | } |
Ayrton_L | 1:a2f7adf6db3d | 34 | ul_MyReturn = 0; |
Ayrton_L | 1:a2f7adf6db3d | 35 | } |
Ayrton_L | 1:a2f7adf6db3d | 36 | else |
Ayrton_L | 1:a2f7adf6db3d | 37 | { |
Ayrton_L | 1:a2f7adf6db3d | 38 | ul_MyReturn =1; |
Ayrton_L | 1:a2f7adf6db3d | 39 | } |
Ayrton_L | 1:a2f7adf6db3d | 40 | return ul_MyReturn; |
Ayrton_L | 1:a2f7adf6db3d | 41 | } |
Ayrton_L | 1:a2f7adf6db3d | 42 | |
Ayrton_L | 1:a2f7adf6db3d | 43 | /*-----------------------------------------------------------*/ |
Ayrton_L | 1:a2f7adf6db3d | 44 | |
Ayrton_L | 3:7aef6c427f97 | 45 | int32_t l_CreateMenu( void ) |
Ayrton_L | 1:a2f7adf6db3d | 46 | { |
Ayrton_L | 3:7aef6c427f97 | 47 | //480x272 |
Ayrton_L | 3:7aef6c427f97 | 48 | display.DrawRect( 0, 0, 105, 68); |
Ayrton_L | 3:7aef6c427f97 | 49 | display.DisplayStringAt( 2, 34, ( uint8_t * )"DHCP", LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 50 | display.DrawRect( 0, 68, 105, 68); |
Ayrton_L | 3:7aef6c427f97 | 51 | display.DisplayStringAt( 2, 102, ( uint8_t * )"STATIC", LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 52 | return 0; |
Ayrton_L | 0:16bcf70d262e | 53 | } |
Ayrton_L | 0:16bcf70d262e | 54 | |
Ayrton_L | 1:a2f7adf6db3d | 55 | /*-----------------------------------------------------------*/ |
Ayrton_L | 0:16bcf70d262e | 56 | |
Ayrton_L | 3:7aef6c427f97 | 57 | int32_t l_ShowSettings( void ) |
Ayrton_L | 0:16bcf70d262e | 58 | { |
Ayrton_L | 3:7aef6c427f97 | 59 | l_ResetDisplay( ); |
Ayrton_L | 3:7aef6c427f97 | 60 | display.SetFont( &Font16 ); |
Ayrton_L | 3:7aef6c427f97 | 61 | display.DisplayStringAt( 10, 10, ( uint8_t * )"Starting application", LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 62 | display.DisplayStringAt( 10, 30, ( uint8_t * )"Initiating EthernetInterface", LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 63 | if( Eth.init( ) == 0 ) |
Ayrton_L | 1:a2f7adf6db3d | 64 | { |
Ayrton_L | 3:7aef6c427f97 | 65 | display.DisplayStringAt( 10, 50, ( uint8_t * )"Initiating EthernetInterface: done", LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 66 | display.DisplayStringAt( 10, 70, ( uint8_t * )"Connecting to EthernetInterface", LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 67 | |
Ayrton_L | 3:7aef6c427f97 | 68 | char c_NewIP[16]; |
Ayrton_L | 3:7aef6c427f97 | 69 | Eth.connect( ); |
Ayrton_L | 3:7aef6c427f97 | 70 | display.DisplayStringAt( 10, 90, ( uint8_t * )"Initiating EthernetInterface: done", LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 71 | display.DisplayStringAt( 10, 110, ( uint8_t * )"Receiving network information", LEFT_MODE ); |
Ayrton_L | 1:a2f7adf6db3d | 72 | |
Ayrton_L | 3:7aef6c427f97 | 73 | strncpy( c_NewIP, Eth.getIPAddress( ), 16 ); |
Ayrton_L | 3:7aef6c427f97 | 74 | l_ResetDisplay( ); |
Ayrton_L | 3:7aef6c427f97 | 75 | l_CreateMenu( ); |
Ayrton_L | 3:7aef6c427f97 | 76 | |
Ayrton_L | 3:7aef6c427f97 | 77 | display.DisplayStringAt( 130, 10, ( uint8_t * )"IP address: ", LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 78 | display.DisplayStringAt( 270, 10, ( uint8_t * )c_NewIP, LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 79 | display.DisplayStringAt( 130, 30, ( uint8_t * )"Netmask: ", LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 80 | display.DisplayStringAt( 270, 30, ( uint8_t * )Eth.getNetworkMask( ), LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 81 | display.DisplayStringAt( 130, 50, ( uint8_t * )"Gateway: ", LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 82 | display.DisplayStringAt( 270, 50, ( uint8_t * )Eth.getGateway( ), LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 83 | display.DisplayStringAt( 130, 70, ( uint8_t * )"Internet: ", LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 84 | l_sendNTPpacket(); |
Ayrton_L | 1:a2f7adf6db3d | 85 | } |
Ayrton_L | 1:a2f7adf6db3d | 86 | |
Ayrton_L | 0:16bcf70d262e | 87 | return 0; |
Ayrton_L | 0:16bcf70d262e | 88 | } |
Ayrton_L | 0:16bcf70d262e | 89 | |
Ayrton_L | 1:a2f7adf6db3d | 90 | /*-----------------------------------------------------------*/ |
Ayrton_L | 1:a2f7adf6db3d | 91 | |
Ayrton_L | 3:7aef6c427f97 | 92 | Serial pc(USBTX, USBRX); // tx, rx |
Ayrton_L | 0:16bcf70d262e | 93 | |
Ayrton_L | 3:7aef6c427f97 | 94 | int32_t l_sendNTPpacket(void) |
Ayrton_L | 0:16bcf70d262e | 95 | { |
Ayrton_L | 3:7aef6c427f97 | 96 | UDPSocket x_Sock; |
Ayrton_L | 3:7aef6c427f97 | 97 | int32_t l_ErrorCatch; |
Ayrton_L | 3:7aef6c427f97 | 98 | uint32_t ul_Epoch; |
Ayrton_L | 3:7aef6c427f97 | 99 | uint16_t us_LowWord; |
Ayrton_L | 3:7aef6c427f97 | 100 | uint16_t us_HighWord; |
Ayrton_L | 3:7aef6c427f97 | 101 | uint8_t uc_PacketBuffer[NTP_PACKET_SIZE]; |
Ayrton_L | 0:16bcf70d262e | 102 | |
Ayrton_L | 3:7aef6c427f97 | 103 | uc_PacketBuffer[0] = 0b11100011; |
Ayrton_L | 3:7aef6c427f97 | 104 | uc_PacketBuffer[1] = 0; |
Ayrton_L | 3:7aef6c427f97 | 105 | uc_PacketBuffer[2] = 6; |
Ayrton_L | 3:7aef6c427f97 | 106 | uc_PacketBuffer[3] = 0xEC; |
Ayrton_L | 3:7aef6c427f97 | 107 | uc_PacketBuffer[12] = 49; |
Ayrton_L | 3:7aef6c427f97 | 108 | uc_PacketBuffer[13] = 0x4E; |
Ayrton_L | 3:7aef6c427f97 | 109 | uc_PacketBuffer[14] = 49; |
Ayrton_L | 3:7aef6c427f97 | 110 | uc_PacketBuffer[15] = 52; |
Ayrton_L | 3:7aef6c427f97 | 111 | |
Ayrton_L | 3:7aef6c427f97 | 112 | l_ErrorCatch = x_Sock.init(); |
Ayrton_L | 0:16bcf70d262e | 113 | |
Ayrton_L | 3:7aef6c427f97 | 114 | if( l_ErrorCatch == -1 ) |
Ayrton_L | 0:16bcf70d262e | 115 | { |
Ayrton_L | 3:7aef6c427f97 | 116 | display.DisplayStringAt( 270, 70, ( uint8_t * )"Not Connected" , LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 117 | return -1; |
Ayrton_L | 3:7aef6c427f97 | 118 | } |
Ayrton_L | 3:7aef6c427f97 | 119 | |
Ayrton_L | 3:7aef6c427f97 | 120 | Endpoint x_NTPServer; |
Ayrton_L | 3:7aef6c427f97 | 121 | l_ErrorCatch = x_NTPServer.set_address(NTP_SERVER_ADDRESS, NTP_SERVER_PORT); |
Ayrton_L | 3:7aef6c427f97 | 122 | if( l_ErrorCatch == -1 ) |
Ayrton_L | 3:7aef6c427f97 | 123 | { |
Ayrton_L | 3:7aef6c427f97 | 124 | l_ErrorCatch = x_NTPServer.set_address(NTP_SERVER_ADDRESS_IP, NTP_SERVER_PORT); |
Ayrton_L | 3:7aef6c427f97 | 125 | if( l_ErrorCatch == -1 ) |
Ayrton_L | 0:16bcf70d262e | 126 | { |
Ayrton_L | 3:7aef6c427f97 | 127 | display.DisplayStringAt( 270, 70, ( uint8_t * )"Not Connected" , LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 128 | return -2; |
Ayrton_L | 2:1a5565ee8219 | 129 | } |
Ayrton_L | 0:16bcf70d262e | 130 | } |
Ayrton_L | 3:7aef6c427f97 | 131 | |
Ayrton_L | 3:7aef6c427f97 | 132 | l_ErrorCatch= x_Sock.sendTo(x_NTPServer, ( char* )uc_PacketBuffer, NTP_PACKET_SIZE); |
Ayrton_L | 3:7aef6c427f97 | 133 | |
Ayrton_L | 3:7aef6c427f97 | 134 | if( l_ErrorCatch == -1 ) |
Ayrton_L | 3:7aef6c427f97 | 135 | { |
Ayrton_L | 3:7aef6c427f97 | 136 | display.DisplayStringAt( 270, 70, ( uint8_t * )"Not Connected" , LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 137 | return -1; |
Ayrton_L | 3:7aef6c427f97 | 138 | } |
Ayrton_L | 3:7aef6c427f97 | 139 | |
Ayrton_L | 3:7aef6c427f97 | 140 | l_ErrorCatch = x_Sock.receiveFrom(x_NTPServer, ( char* ) uc_PacketBuffer, NTP_PACKET_SIZE); |
Ayrton_L | 3:7aef6c427f97 | 141 | |
Ayrton_L | 3:7aef6c427f97 | 142 | if( l_ErrorCatch == -1 ) |
Ayrton_L | 3:7aef6c427f97 | 143 | { |
Ayrton_L | 3:7aef6c427f97 | 144 | display.DisplayStringAt( 270, 70, ( uint8_t * )"Not Connected" , LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 145 | return -1; |
Ayrton_L | 3:7aef6c427f97 | 146 | } |
Ayrton_L | 3:7aef6c427f97 | 147 | |
Ayrton_L | 3:7aef6c427f97 | 148 | us_HighWord = (uc_PacketBuffer[40] << 8 ) | uc_PacketBuffer[41]; |
Ayrton_L | 3:7aef6c427f97 | 149 | us_LowWord = (uc_PacketBuffer[42] << 8 ) | uc_PacketBuffer[43]; |
Ayrton_L | 3:7aef6c427f97 | 150 | ul_Epoch = ((us_HighWord << 16) | us_LowWord ); |
Ayrton_L | 3:7aef6c427f97 | 151 | ul_Epoch = ul_Epoch - 2208988800; |
Ayrton_L | 3:7aef6c427f97 | 152 | |
Ayrton_L | 3:7aef6c427f97 | 153 | display.SetFont( &Font16 ); |
Ayrton_L | 3:7aef6c427f97 | 154 | display.DisplayStringAt( 270, 70, ( uint8_t * )"Connected" , LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 155 | |
Ayrton_L | 3:7aef6c427f97 | 156 | x_Sock.close(); |
Ayrton_L | 3:7aef6c427f97 | 157 | return 0; |
Ayrton_L | 0:16bcf70d262e | 158 | } |
Ayrton_L | 0:16bcf70d262e | 159 | |
Ayrton_L | 1:a2f7adf6db3d | 160 | /*-----------------------------------------------------------*/ |
Ayrton_L | 1:a2f7adf6db3d | 161 | |
Ayrton_L | 1:a2f7adf6db3d | 162 | int main( void ) |
Ayrton_L | 0:16bcf70d262e | 163 | { |
Ayrton_L | 3:7aef6c427f97 | 164 | l_ShowSettings( ); |
Ayrton_L | 3:7aef6c427f97 | 165 | Eth.disconnect(); |
Ayrton_L | 3:7aef6c427f97 | 166 | } |