Project Embedded Systems E-ict Denayer
Dependencies: BSP_DISCO_F746NG F7_Ethernet LCD_DISCO_F746NG TS_DISCO_F746NG mbed-rtos mbed
main.cpp@5:0518cef07365, 2017-08-17 (annotated)
- Committer:
- Ayrton_L
- Date:
- Thu Aug 17 14:36:34 2017 +0000
- Revision:
- 5:0518cef07365
- Parent:
- 4:860566e5814e
Final publish; Wrong branch was added last time
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 | 5:0518cef07365 | 13 | int32_t ul_IpStringToNumber( char* pux_IpAddr, uint32_t * pul_IpAddr ) |
Ayrton_L | 5:0518cef07365 | 14 | { |
Ayrton_L | 5:0518cef07365 | 15 | uint8_t uc_Byte3; |
Ayrton_L | 5:0518cef07365 | 16 | uint8_t uc_Byte2; |
Ayrton_L | 5:0518cef07365 | 17 | uint8_t uc_Byte1; |
Ayrton_L | 5:0518cef07365 | 18 | uint8_t uc_Byte0; |
Ayrton_L | 5:0518cef07365 | 19 | char ux_DummyString[2]; //dummy -> als er na %ls nog een char komt -> geen correct adres |
Ayrton_L | 5:0518cef07365 | 20 | |
Ayrton_L | 5:0518cef07365 | 21 | if ( sscanf( pux_IpAddr, "%u.%u.%u.%u%1s", &uc_Byte3, &uc_Byte2, &uc_Byte1, &uc_Byte0, ux_DummyString ) == 4 ) |
Ayrton_L | 5:0518cef07365 | 22 | { |
Ayrton_L | 5:0518cef07365 | 23 | if( ( uc_Byte3 < 256 ) && ( uc_Byte2 < 256 ) && ( uc_Byte1 < 256 ) && ( uc_Byte0 < 256 ) ) |
Ayrton_L | 5:0518cef07365 | 24 | { |
Ayrton_L | 5:0518cef07365 | 25 | *pul_IpAddr = ( uc_Byte3 << 24 ) + ( uc_Byte2 << 16 ) + ( uc_Byte1 << 8 ) + uc_Byte0; |
Ayrton_L | 5:0518cef07365 | 26 | return 1; |
Ayrton_L | 5:0518cef07365 | 27 | } |
Ayrton_L | 5:0518cef07365 | 28 | } |
Ayrton_L | 5:0518cef07365 | 29 | |
Ayrton_L | 5:0518cef07365 | 30 | return 0; |
Ayrton_L | 5:0518cef07365 | 31 | } |
Ayrton_L | 5:0518cef07365 | 32 | |
Ayrton_L | 5:0518cef07365 | 33 | /*-----------------------------------------------------------*/ |
Ayrton_L | 5:0518cef07365 | 34 | |
Ayrton_L | 5:0518cef07365 | 35 | int32_t ul_ShowDevices( void ) |
Ayrton_L | 5:0518cef07365 | 36 | { |
Ayrton_L | 5:0518cef07365 | 37 | uint32_t ul_GenIP = 0; |
Ayrton_L | 5:0518cef07365 | 38 | uint32_t ul_MyIP = 0; |
Ayrton_L | 5:0518cef07365 | 39 | uint32_t ul_Counter =0; |
Ayrton_L | 5:0518cef07365 | 40 | uint32_t ul_DevCounter = 0; |
Ayrton_L | 5:0518cef07365 | 41 | |
Ayrton_L | 5:0518cef07365 | 42 | ul_IpStringToNumber(Eth.getIPAddress( ) , &ul_MyIP); |
Ayrton_L | 5:0518cef07365 | 43 | |
Ayrton_L | 5:0518cef07365 | 44 | 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 |
Ayrton_L | 5:0518cef07365 | 45 | |
Ayrton_L | 5:0518cef07365 | 46 | ul_GenIP = ul_MyIP & 0xFFFFFF00; |
Ayrton_L | 5:0518cef07365 | 47 | display.SetFont( &Font16 ); |
Ayrton_L | 5:0518cef07365 | 48 | display.DisplayStringAt( 10, 120, ( uint8_t * )"PING: ", LEFT_MODE ); |
Ayrton_L | 5:0518cef07365 | 49 | |
Ayrton_L | 5:0518cef07365 | 50 | for( ul_Counter = 1; ul_Counter < 255; ul_Counter++) |
Ayrton_L | 5:0518cef07365 | 51 | { |
Ayrton_L | 5:0518cef07365 | 52 | ul_GenIP++; |
Ayrton_L | 5:0518cef07365 | 53 | if( ul_Ping( ( ip_addr_t * ) ul_GenIP ) == 0 ) |
Ayrton_L | 5:0518cef07365 | 54 | { |
Ayrton_L | 5:0518cef07365 | 55 | ip_addr_t ux_ConIP = { ul_GenIP }; |
Ayrton_L | 5:0518cef07365 | 56 | x_ActDev[ul_DevCounter] = ux_ConIP; //voor een of andere reden is dit de enige manier dat werkt |
Ayrton_L | 5:0518cef07365 | 57 | display.DisplayStringAt( 10, ( ul_DevCounter * 10 ) + 120 , ( uint8_t * )ul_GenIP, LEFT_MODE ); |
Ayrton_L | 5:0518cef07365 | 58 | ul_DevCounter++; |
Ayrton_L | 5:0518cef07365 | 59 | } |
Ayrton_L | 5:0518cef07365 | 60 | } |
Ayrton_L | 5:0518cef07365 | 61 | |
Ayrton_L | 5:0518cef07365 | 62 | return 0; |
Ayrton_L | 5:0518cef07365 | 63 | } |
Ayrton_L | 5:0518cef07365 | 64 | |
Ayrton_L | 5:0518cef07365 | 65 | /*-----------------------------------------------------------*/ |
Ayrton_L | 5:0518cef07365 | 66 | |
Ayrton_L | 3:7aef6c427f97 | 67 | int32_t l_ShowSettings( void ) |
Ayrton_L | 0:16bcf70d262e | 68 | { |
Ayrton_L | 3:7aef6c427f97 | 69 | l_ResetDisplay( ); |
Ayrton_L | 3:7aef6c427f97 | 70 | display.SetFont( &Font16 ); |
Ayrton_L | 3:7aef6c427f97 | 71 | display.DisplayStringAt( 10, 10, ( uint8_t * )"Starting application", LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 72 | display.DisplayStringAt( 10, 30, ( uint8_t * )"Initiating EthernetInterface", LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 73 | if( Eth.init( ) == 0 ) |
Ayrton_L | 1:a2f7adf6db3d | 74 | { |
Ayrton_L | 3:7aef6c427f97 | 75 | display.DisplayStringAt( 10, 50, ( uint8_t * )"Initiating EthernetInterface: done", LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 76 | display.DisplayStringAt( 10, 70, ( uint8_t * )"Connecting to EthernetInterface", LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 77 | |
Ayrton_L | 3:7aef6c427f97 | 78 | char c_NewIP[16]; |
Ayrton_L | 3:7aef6c427f97 | 79 | Eth.connect( ); |
Ayrton_L | 3:7aef6c427f97 | 80 | display.DisplayStringAt( 10, 90, ( uint8_t * )"Initiating EthernetInterface: done", LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 81 | display.DisplayStringAt( 10, 110, ( uint8_t * )"Receiving network information", LEFT_MODE ); |
Ayrton_L | 1:a2f7adf6db3d | 82 | |
Ayrton_L | 3:7aef6c427f97 | 83 | strncpy( c_NewIP, Eth.getIPAddress( ), 16 ); |
Ayrton_L | 3:7aef6c427f97 | 84 | l_ResetDisplay( ); |
Ayrton_L | 3:7aef6c427f97 | 85 | |
Ayrton_L | 3:7aef6c427f97 | 86 | display.DisplayStringAt( 130, 10, ( uint8_t * )"IP address: ", LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 87 | display.DisplayStringAt( 270, 10, ( uint8_t * )c_NewIP, LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 88 | display.DisplayStringAt( 130, 30, ( uint8_t * )"Netmask: ", LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 89 | display.DisplayStringAt( 270, 30, ( uint8_t * )Eth.getNetworkMask( ), LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 90 | display.DisplayStringAt( 130, 50, ( uint8_t * )"Gateway: ", LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 91 | display.DisplayStringAt( 270, 50, ( uint8_t * )Eth.getGateway( ), LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 92 | display.DisplayStringAt( 130, 70, ( uint8_t * )"Internet: ", LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 93 | l_sendNTPpacket(); |
Ayrton_L | 1:a2f7adf6db3d | 94 | } |
Ayrton_L | 1:a2f7adf6db3d | 95 | |
Ayrton_L | 0:16bcf70d262e | 96 | return 0; |
Ayrton_L | 0:16bcf70d262e | 97 | } |
Ayrton_L | 0:16bcf70d262e | 98 | |
Ayrton_L | 1:a2f7adf6db3d | 99 | /*-----------------------------------------------------------*/ |
Ayrton_L | 3:7aef6c427f97 | 100 | int32_t l_sendNTPpacket(void) |
Ayrton_L | 0:16bcf70d262e | 101 | { |
Ayrton_L | 3:7aef6c427f97 | 102 | UDPSocket x_Sock; |
Ayrton_L | 3:7aef6c427f97 | 103 | int32_t l_ErrorCatch; |
Ayrton_L | 3:7aef6c427f97 | 104 | uint32_t ul_Epoch; |
Ayrton_L | 3:7aef6c427f97 | 105 | uint16_t us_LowWord; |
Ayrton_L | 3:7aef6c427f97 | 106 | uint16_t us_HighWord; |
Ayrton_L | 3:7aef6c427f97 | 107 | uint8_t uc_PacketBuffer[NTP_PACKET_SIZE]; |
Ayrton_L | 0:16bcf70d262e | 108 | |
Ayrton_L | 3:7aef6c427f97 | 109 | uc_PacketBuffer[0] = 0b11100011; |
Ayrton_L | 3:7aef6c427f97 | 110 | uc_PacketBuffer[1] = 0; |
Ayrton_L | 3:7aef6c427f97 | 111 | uc_PacketBuffer[2] = 6; |
Ayrton_L | 3:7aef6c427f97 | 112 | uc_PacketBuffer[3] = 0xEC; |
Ayrton_L | 3:7aef6c427f97 | 113 | uc_PacketBuffer[12] = 49; |
Ayrton_L | 3:7aef6c427f97 | 114 | uc_PacketBuffer[13] = 0x4E; |
Ayrton_L | 3:7aef6c427f97 | 115 | uc_PacketBuffer[14] = 49; |
Ayrton_L | 3:7aef6c427f97 | 116 | uc_PacketBuffer[15] = 52; |
Ayrton_L | 3:7aef6c427f97 | 117 | |
Ayrton_L | 3:7aef6c427f97 | 118 | l_ErrorCatch = x_Sock.init(); |
Ayrton_L | 0:16bcf70d262e | 119 | |
Ayrton_L | 3:7aef6c427f97 | 120 | if( l_ErrorCatch == -1 ) |
Ayrton_L | 0:16bcf70d262e | 121 | { |
Ayrton_L | 3:7aef6c427f97 | 122 | display.DisplayStringAt( 270, 70, ( uint8_t * )"Not Connected" , LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 123 | return -1; |
Ayrton_L | 3:7aef6c427f97 | 124 | } |
Ayrton_L | 3:7aef6c427f97 | 125 | |
Ayrton_L | 3:7aef6c427f97 | 126 | Endpoint x_NTPServer; |
Ayrton_L | 3:7aef6c427f97 | 127 | l_ErrorCatch = x_NTPServer.set_address(NTP_SERVER_ADDRESS, NTP_SERVER_PORT); |
Ayrton_L | 3:7aef6c427f97 | 128 | if( l_ErrorCatch == -1 ) |
Ayrton_L | 3:7aef6c427f97 | 129 | { |
Ayrton_L | 3:7aef6c427f97 | 130 | l_ErrorCatch = x_NTPServer.set_address(NTP_SERVER_ADDRESS_IP, NTP_SERVER_PORT); |
Ayrton_L | 3:7aef6c427f97 | 131 | if( l_ErrorCatch == -1 ) |
Ayrton_L | 0:16bcf70d262e | 132 | { |
Ayrton_L | 3:7aef6c427f97 | 133 | display.DisplayStringAt( 270, 70, ( uint8_t * )"Not Connected" , LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 134 | return -2; |
Ayrton_L | 2:1a5565ee8219 | 135 | } |
Ayrton_L | 0:16bcf70d262e | 136 | } |
Ayrton_L | 3:7aef6c427f97 | 137 | |
Ayrton_L | 3:7aef6c427f97 | 138 | l_ErrorCatch= x_Sock.sendTo(x_NTPServer, ( char* )uc_PacketBuffer, NTP_PACKET_SIZE); |
Ayrton_L | 3:7aef6c427f97 | 139 | |
Ayrton_L | 3:7aef6c427f97 | 140 | if( l_ErrorCatch == -1 ) |
Ayrton_L | 3:7aef6c427f97 | 141 | { |
Ayrton_L | 3:7aef6c427f97 | 142 | display.DisplayStringAt( 270, 70, ( uint8_t * )"Not Connected" , LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 143 | return -1; |
Ayrton_L | 3:7aef6c427f97 | 144 | } |
Ayrton_L | 3:7aef6c427f97 | 145 | |
Ayrton_L | 3:7aef6c427f97 | 146 | l_ErrorCatch = x_Sock.receiveFrom(x_NTPServer, ( char* ) uc_PacketBuffer, NTP_PACKET_SIZE); |
Ayrton_L | 3:7aef6c427f97 | 147 | |
Ayrton_L | 3:7aef6c427f97 | 148 | if( l_ErrorCatch == -1 ) |
Ayrton_L | 3:7aef6c427f97 | 149 | { |
Ayrton_L | 3:7aef6c427f97 | 150 | display.DisplayStringAt( 270, 70, ( uint8_t * )"Not Connected" , LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 151 | return -1; |
Ayrton_L | 3:7aef6c427f97 | 152 | } |
Ayrton_L | 3:7aef6c427f97 | 153 | |
Ayrton_L | 3:7aef6c427f97 | 154 | us_HighWord = (uc_PacketBuffer[40] << 8 ) | uc_PacketBuffer[41]; |
Ayrton_L | 3:7aef6c427f97 | 155 | us_LowWord = (uc_PacketBuffer[42] << 8 ) | uc_PacketBuffer[43]; |
Ayrton_L | 3:7aef6c427f97 | 156 | ul_Epoch = ((us_HighWord << 16) | us_LowWord ); |
Ayrton_L | 3:7aef6c427f97 | 157 | ul_Epoch = ul_Epoch - 2208988800; |
Ayrton_L | 3:7aef6c427f97 | 158 | |
Ayrton_L | 3:7aef6c427f97 | 159 | display.SetFont( &Font16 ); |
Ayrton_L | 3:7aef6c427f97 | 160 | display.DisplayStringAt( 270, 70, ( uint8_t * )"Connected" , LEFT_MODE ); |
Ayrton_L | 3:7aef6c427f97 | 161 | |
Ayrton_L | 3:7aef6c427f97 | 162 | x_Sock.close(); |
Ayrton_L | 3:7aef6c427f97 | 163 | return 0; |
Ayrton_L | 0:16bcf70d262e | 164 | } |
Ayrton_L | 0:16bcf70d262e | 165 | |
Ayrton_L | 1:a2f7adf6db3d | 166 | /*-----------------------------------------------------------*/ |
Ayrton_L | 1:a2f7adf6db3d | 167 | |
Ayrton_L | 1:a2f7adf6db3d | 168 | int main( void ) |
Ayrton_L | 0:16bcf70d262e | 169 | { |
Ayrton_L | 3:7aef6c427f97 | 170 | l_ShowSettings( ); |
Ayrton_L | 5:0518cef07365 | 171 | ul_ShowDevices( ); |
Ayrton_L | 3:7aef6c427f97 | 172 | Eth.disconnect(); |
Ayrton_L | 3:7aef6c427f97 | 173 | } |