Project Embedded Systems E-ict Denayer
Dependencies: BSP_DISCO_F746NG F7_Ethernet LCD_DISCO_F746NG TS_DISCO_F746NG mbed-rtos mbed
main.cpp@4:860566e5814e, 2017-08-17 (annotated)
- Committer:
- Ayrton_L
- Date:
- Thu Aug 17 13:32:25 2017 +0000
- Revision:
- 4:860566e5814e
- Parent:
- 3:7aef6c427f97
- Child:
- 5:0518cef07365
latest;
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_ShowSettings( void ) |
Ayrton_L | 0:16bcf70d262e | 14 | { |
Ayrton_L | 3:7aef6c427f97 | 15 | l_ResetDisplay( ); |
Ayrton_L | 3:7aef6c427f97 | 16 | display.SetFont( &Font16 ); |
Ayrton_L | 3:7aef6c427f97 | 17 | display.DisplayStringAt( 10, 10, ( uint8_t * )"Starting application", LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 18 | display.DisplayStringAt( 10, 30, ( uint8_t * )"Initiating EthernetInterface", LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 19 | if( Eth.init( ) == 0 ) |
Ayrton_L | 1:a2f7adf6db3d | 20 | { |
Ayrton_L | 3:7aef6c427f97 | 21 | display.DisplayStringAt( 10, 50, ( uint8_t * )"Initiating EthernetInterface: done", LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 22 | display.DisplayStringAt( 10, 70, ( uint8_t * )"Connecting to EthernetInterface", LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 23 | |
Ayrton_L | 3:7aef6c427f97 | 24 | char c_NewIP[16]; |
Ayrton_L | 3:7aef6c427f97 | 25 | Eth.connect( ); |
Ayrton_L | 3:7aef6c427f97 | 26 | display.DisplayStringAt( 10, 90, ( uint8_t * )"Initiating EthernetInterface: done", LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 27 | display.DisplayStringAt( 10, 110, ( uint8_t * )"Receiving network information", LEFT_MODE ); |
Ayrton_L | 1:a2f7adf6db3d | 28 | |
Ayrton_L | 3:7aef6c427f97 | 29 | strncpy( c_NewIP, Eth.getIPAddress( ), 16 ); |
Ayrton_L | 3:7aef6c427f97 | 30 | l_ResetDisplay( ); |
Ayrton_L | 3:7aef6c427f97 | 31 | |
Ayrton_L | 3:7aef6c427f97 | 32 | display.DisplayStringAt( 130, 10, ( uint8_t * )"IP address: ", LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 33 | display.DisplayStringAt( 270, 10, ( uint8_t * )c_NewIP, LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 34 | display.DisplayStringAt( 130, 30, ( uint8_t * )"Netmask: ", LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 35 | display.DisplayStringAt( 270, 30, ( uint8_t * )Eth.getNetworkMask( ), LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 36 | display.DisplayStringAt( 130, 50, ( uint8_t * )"Gateway: ", LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 37 | display.DisplayStringAt( 270, 50, ( uint8_t * )Eth.getGateway( ), LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 38 | display.DisplayStringAt( 130, 70, ( uint8_t * )"Internet: ", LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 39 | l_sendNTPpacket(); |
Ayrton_L | 1:a2f7adf6db3d | 40 | } |
Ayrton_L | 1:a2f7adf6db3d | 41 | |
Ayrton_L | 0:16bcf70d262e | 42 | return 0; |
Ayrton_L | 0:16bcf70d262e | 43 | } |
Ayrton_L | 0:16bcf70d262e | 44 | |
Ayrton_L | 1:a2f7adf6db3d | 45 | /*-----------------------------------------------------------*/ |
Ayrton_L | 3:7aef6c427f97 | 46 | int32_t l_sendNTPpacket(void) |
Ayrton_L | 0:16bcf70d262e | 47 | { |
Ayrton_L | 3:7aef6c427f97 | 48 | UDPSocket x_Sock; |
Ayrton_L | 3:7aef6c427f97 | 49 | int32_t l_ErrorCatch; |
Ayrton_L | 3:7aef6c427f97 | 50 | uint32_t ul_Epoch; |
Ayrton_L | 3:7aef6c427f97 | 51 | uint16_t us_LowWord; |
Ayrton_L | 3:7aef6c427f97 | 52 | uint16_t us_HighWord; |
Ayrton_L | 3:7aef6c427f97 | 53 | uint8_t uc_PacketBuffer[NTP_PACKET_SIZE]; |
Ayrton_L | 0:16bcf70d262e | 54 | |
Ayrton_L | 3:7aef6c427f97 | 55 | uc_PacketBuffer[0] = 0b11100011; |
Ayrton_L | 3:7aef6c427f97 | 56 | uc_PacketBuffer[1] = 0; |
Ayrton_L | 3:7aef6c427f97 | 57 | uc_PacketBuffer[2] = 6; |
Ayrton_L | 3:7aef6c427f97 | 58 | uc_PacketBuffer[3] = 0xEC; |
Ayrton_L | 3:7aef6c427f97 | 59 | uc_PacketBuffer[12] = 49; |
Ayrton_L | 3:7aef6c427f97 | 60 | uc_PacketBuffer[13] = 0x4E; |
Ayrton_L | 3:7aef6c427f97 | 61 | uc_PacketBuffer[14] = 49; |
Ayrton_L | 3:7aef6c427f97 | 62 | uc_PacketBuffer[15] = 52; |
Ayrton_L | 3:7aef6c427f97 | 63 | |
Ayrton_L | 3:7aef6c427f97 | 64 | l_ErrorCatch = x_Sock.init(); |
Ayrton_L | 0:16bcf70d262e | 65 | |
Ayrton_L | 3:7aef6c427f97 | 66 | if( l_ErrorCatch == -1 ) |
Ayrton_L | 0:16bcf70d262e | 67 | { |
Ayrton_L | 3:7aef6c427f97 | 68 | display.DisplayStringAt( 270, 70, ( uint8_t * )"Not Connected" , LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 69 | return -1; |
Ayrton_L | 3:7aef6c427f97 | 70 | } |
Ayrton_L | 3:7aef6c427f97 | 71 | |
Ayrton_L | 3:7aef6c427f97 | 72 | Endpoint x_NTPServer; |
Ayrton_L | 3:7aef6c427f97 | 73 | l_ErrorCatch = x_NTPServer.set_address(NTP_SERVER_ADDRESS, NTP_SERVER_PORT); |
Ayrton_L | 3:7aef6c427f97 | 74 | if( l_ErrorCatch == -1 ) |
Ayrton_L | 3:7aef6c427f97 | 75 | { |
Ayrton_L | 3:7aef6c427f97 | 76 | l_ErrorCatch = x_NTPServer.set_address(NTP_SERVER_ADDRESS_IP, NTP_SERVER_PORT); |
Ayrton_L | 3:7aef6c427f97 | 77 | if( l_ErrorCatch == -1 ) |
Ayrton_L | 0:16bcf70d262e | 78 | { |
Ayrton_L | 3:7aef6c427f97 | 79 | display.DisplayStringAt( 270, 70, ( uint8_t * )"Not Connected" , LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 80 | return -2; |
Ayrton_L | 2:1a5565ee8219 | 81 | } |
Ayrton_L | 0:16bcf70d262e | 82 | } |
Ayrton_L | 3:7aef6c427f97 | 83 | |
Ayrton_L | 3:7aef6c427f97 | 84 | l_ErrorCatch= x_Sock.sendTo(x_NTPServer, ( char* )uc_PacketBuffer, NTP_PACKET_SIZE); |
Ayrton_L | 3:7aef6c427f97 | 85 | |
Ayrton_L | 3:7aef6c427f97 | 86 | if( l_ErrorCatch == -1 ) |
Ayrton_L | 3:7aef6c427f97 | 87 | { |
Ayrton_L | 3:7aef6c427f97 | 88 | display.DisplayStringAt( 270, 70, ( uint8_t * )"Not Connected" , LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 89 | return -1; |
Ayrton_L | 3:7aef6c427f97 | 90 | } |
Ayrton_L | 3:7aef6c427f97 | 91 | |
Ayrton_L | 3:7aef6c427f97 | 92 | l_ErrorCatch = x_Sock.receiveFrom(x_NTPServer, ( char* ) uc_PacketBuffer, NTP_PACKET_SIZE); |
Ayrton_L | 3:7aef6c427f97 | 93 | |
Ayrton_L | 3:7aef6c427f97 | 94 | if( l_ErrorCatch == -1 ) |
Ayrton_L | 3:7aef6c427f97 | 95 | { |
Ayrton_L | 3:7aef6c427f97 | 96 | display.DisplayStringAt( 270, 70, ( uint8_t * )"Not Connected" , LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 97 | return -1; |
Ayrton_L | 3:7aef6c427f97 | 98 | } |
Ayrton_L | 3:7aef6c427f97 | 99 | |
Ayrton_L | 3:7aef6c427f97 | 100 | us_HighWord = (uc_PacketBuffer[40] << 8 ) | uc_PacketBuffer[41]; |
Ayrton_L | 3:7aef6c427f97 | 101 | us_LowWord = (uc_PacketBuffer[42] << 8 ) | uc_PacketBuffer[43]; |
Ayrton_L | 3:7aef6c427f97 | 102 | ul_Epoch = ((us_HighWord << 16) | us_LowWord ); |
Ayrton_L | 3:7aef6c427f97 | 103 | ul_Epoch = ul_Epoch - 2208988800; |
Ayrton_L | 3:7aef6c427f97 | 104 | |
Ayrton_L | 3:7aef6c427f97 | 105 | display.SetFont( &Font16 ); |
Ayrton_L | 3:7aef6c427f97 | 106 | display.DisplayStringAt( 270, 70, ( uint8_t * )"Connected" , LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 107 | |
Ayrton_L | 3:7aef6c427f97 | 108 | x_Sock.close(); |
Ayrton_L | 3:7aef6c427f97 | 109 | return 0; |
Ayrton_L | 0:16bcf70d262e | 110 | } |
Ayrton_L | 0:16bcf70d262e | 111 | |
Ayrton_L | 1:a2f7adf6db3d | 112 | /*-----------------------------------------------------------*/ |
Ayrton_L | 1:a2f7adf6db3d | 113 | |
Ayrton_L | 1:a2f7adf6db3d | 114 | int main( void ) |
Ayrton_L | 0:16bcf70d262e | 115 | { |
Ayrton_L | 3:7aef6c427f97 | 116 | l_ShowSettings( ); |
Ayrton_L | 3:7aef6c427f97 | 117 | Eth.disconnect(); |
Ayrton_L | 3:7aef6c427f97 | 118 | } |