Read NTP time from Ethernet and drive six 7-segment LEDs using a MAX7221 to show time, date, month, week, year etc....

Dependencies:   NTPClient_NetServices mbed

Committer:
cheungderek
Date:
Wed Jun 22 01:31:47 2011 +0000
Revision:
0:e083abcfe7a8
First release.
Just some testing but working code to read NTP time and drive 6 LEDs to display time, date, month, year etc...

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cheungderek 0:e083abcfe7a8 1
cheungderek 0:e083abcfe7a8 2 /*
cheungderek 0:e083abcfe7a8 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
cheungderek 0:e083abcfe7a8 4
cheungderek 0:e083abcfe7a8 5 Permission is hereby granted, free of charge, to any person obtaining a copy
cheungderek 0:e083abcfe7a8 6 of this software and associated documentation files (the "Software"), to deal
cheungderek 0:e083abcfe7a8 7 in the Software without restriction, including without limitation the rights
cheungderek 0:e083abcfe7a8 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
cheungderek 0:e083abcfe7a8 9 copies of the Software, and to permit persons to whom the Software is
cheungderek 0:e083abcfe7a8 10 furnished to do so, subject to the following conditions:
cheungderek 0:e083abcfe7a8 11
cheungderek 0:e083abcfe7a8 12 The above copyright notice and this permission notice shall be included in
cheungderek 0:e083abcfe7a8 13 all copies or substantial portions of the Software.
cheungderek 0:e083abcfe7a8 14
cheungderek 0:e083abcfe7a8 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
cheungderek 0:e083abcfe7a8 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
cheungderek 0:e083abcfe7a8 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
cheungderek 0:e083abcfe7a8 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
cheungderek 0:e083abcfe7a8 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
cheungderek 0:e083abcfe7a8 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
cheungderek 0:e083abcfe7a8 21 THE SOFTWARE.
cheungderek 0:e083abcfe7a8 22 */
cheungderek 0:e083abcfe7a8 23
cheungderek 0:e083abcfe7a8 24 /** \file
cheungderek 0:e083abcfe7a8 25 TCP Socket header file
cheungderek 0:e083abcfe7a8 26 */
cheungderek 0:e083abcfe7a8 27
cheungderek 0:e083abcfe7a8 28 #ifndef TCPSOCKET_H
cheungderek 0:e083abcfe7a8 29 #define TCPSOCKET_H
cheungderek 0:e083abcfe7a8 30
cheungderek 0:e083abcfe7a8 31 #include "core/net.h"
cheungderek 0:e083abcfe7a8 32 #include "core/host.h"
cheungderek 0:e083abcfe7a8 33 //Essentially it is a safe interface to NetTcpSocket
cheungderek 0:e083abcfe7a8 34
cheungderek 0:e083abcfe7a8 35 ///TCP Socket error codes
cheungderek 0:e083abcfe7a8 36 enum TCPSocketErr
cheungderek 0:e083abcfe7a8 37 {
cheungderek 0:e083abcfe7a8 38 __TCPSOCKET_MIN = -0xFFFF,
cheungderek 0:e083abcfe7a8 39 TCPSOCKET_SETUP, ///<TCPSocket not properly configured
cheungderek 0:e083abcfe7a8 40 TCPSOCKET_TIMEOUT, ///<Connection timed out
cheungderek 0:e083abcfe7a8 41 TCPSOCKET_IF, ///<Interface has problems, does not exist or is not initialized
cheungderek 0:e083abcfe7a8 42 TCPSOCKET_MEM, ///<Not enough mem
cheungderek 0:e083abcfe7a8 43 TCPSOCKET_INUSE, ///<Interface / Port is in use
cheungderek 0:e083abcfe7a8 44 TCPSOCKET_EMPTY, ///<Connections queue is empty
cheungderek 0:e083abcfe7a8 45 TCPSOCKET_RST, ///<Connection was reset by remote host
cheungderek 0:e083abcfe7a8 46 //...
cheungderek 0:e083abcfe7a8 47 TCPSOCKET_OK = 0 ///<Success
cheungderek 0:e083abcfe7a8 48 };
cheungderek 0:e083abcfe7a8 49
cheungderek 0:e083abcfe7a8 50 ///TCP Socket Events
cheungderek 0:e083abcfe7a8 51 enum TCPSocketEvent
cheungderek 0:e083abcfe7a8 52 {
cheungderek 0:e083abcfe7a8 53 TCPSOCKET_CONNECTED, ///<Connected to host
cheungderek 0:e083abcfe7a8 54 TCPSOCKET_ACCEPT, ///<Client is connected, must call accept() to get a new Socket
cheungderek 0:e083abcfe7a8 55 TCPSOCKET_READABLE, ///<Data in buf
cheungderek 0:e083abcfe7a8 56 TCPSOCKET_WRITEABLE, ///<Can write data to buf
cheungderek 0:e083abcfe7a8 57 TCPSOCKET_CONTIMEOUT, ///<Connection timed out
cheungderek 0:e083abcfe7a8 58 TCPSOCKET_CONRST, ///<Connection was reset by remote host
cheungderek 0:e083abcfe7a8 59 TCPSOCKET_CONABRT, ///<Connection was aborted
cheungderek 0:e083abcfe7a8 60 TCPSOCKET_ERROR, ///<Unknown error
cheungderek 0:e083abcfe7a8 61 TCPSOCKET_DISCONNECTED ///<Disconnected
cheungderek 0:e083abcfe7a8 62 };
cheungderek 0:e083abcfe7a8 63
cheungderek 0:e083abcfe7a8 64 class NetTcpSocket;
cheungderek 0:e083abcfe7a8 65 enum NetTcpSocketEvent;
cheungderek 0:e083abcfe7a8 66
cheungderek 0:e083abcfe7a8 67 ///This is a simple TCP Socket class
cheungderek 0:e083abcfe7a8 68 /**
cheungderek 0:e083abcfe7a8 69 This class exposes an API to deal with TCP Sockets
cheungderek 0:e083abcfe7a8 70 */
cheungderek 0:e083abcfe7a8 71 class TCPSocket
cheungderek 0:e083abcfe7a8 72 {
cheungderek 0:e083abcfe7a8 73 public:
cheungderek 0:e083abcfe7a8 74 ///Creates a new socket
cheungderek 0:e083abcfe7a8 75 TCPSocket();
cheungderek 0:e083abcfe7a8 76 protected:
cheungderek 0:e083abcfe7a8 77 TCPSocket(NetTcpSocket* pNetTcpSocket);
cheungderek 0:e083abcfe7a8 78 public:
cheungderek 0:e083abcfe7a8 79 ///Closes if needed and destroys the socket
cheungderek 0:e083abcfe7a8 80 ~TCPSocket(); //close()
cheungderek 0:e083abcfe7a8 81
cheungderek 0:e083abcfe7a8 82 ///Binds the socket to (local) host
cheungderek 0:e083abcfe7a8 83 TCPSocketErr bind(const Host& me);
cheungderek 0:e083abcfe7a8 84
cheungderek 0:e083abcfe7a8 85 ///Starts listening
cheungderek 0:e083abcfe7a8 86 TCPSocketErr listen();
cheungderek 0:e083abcfe7a8 87
cheungderek 0:e083abcfe7a8 88 ///Connects socket to host
cheungderek 0:e083abcfe7a8 89 TCPSocketErr connect(const Host& host);
cheungderek 0:e083abcfe7a8 90
cheungderek 0:e083abcfe7a8 91 ///Accepts connection from client and gets connected socket
cheungderek 0:e083abcfe7a8 92 TCPSocketErr accept(Host* pClient, TCPSocket** ppNewTcpSocket);
cheungderek 0:e083abcfe7a8 93
cheungderek 0:e083abcfe7a8 94 ///Sends data
cheungderek 0:e083abcfe7a8 95 /*
cheungderek 0:e083abcfe7a8 96 @return a negative error code or the number of bytes transmitted
cheungderek 0:e083abcfe7a8 97 */
cheungderek 0:e083abcfe7a8 98 int /*if < 0 : TCPSocketErr*/ send(const char* buf, int len);
cheungderek 0:e083abcfe7a8 99
cheungderek 0:e083abcfe7a8 100 ///Receives data
cheungderek 0:e083abcfe7a8 101 /*
cheungderek 0:e083abcfe7a8 102 @return a negative error code or the number of bytes received
cheungderek 0:e083abcfe7a8 103 */
cheungderek 0:e083abcfe7a8 104 int /*if < 0 : TCPSocketErr*/ recv(char* buf, int len);
cheungderek 0:e083abcfe7a8 105
cheungderek 0:e083abcfe7a8 106 /* TODO NTH : printf / scanf helpers that call send/recv */
cheungderek 0:e083abcfe7a8 107
cheungderek 0:e083abcfe7a8 108 ///Closes socket
cheungderek 0:e083abcfe7a8 109 TCPSocketErr close();
cheungderek 0:e083abcfe7a8 110
cheungderek 0:e083abcfe7a8 111 //Callbacks
cheungderek 0:e083abcfe7a8 112 ///Setups callback
cheungderek 0:e083abcfe7a8 113 /**
cheungderek 0:e083abcfe7a8 114 @param pMethod : callback function
cheungderek 0:e083abcfe7a8 115 */
cheungderek 0:e083abcfe7a8 116 void setOnEvent( void (*pMethod)(TCPSocketEvent) );
cheungderek 0:e083abcfe7a8 117
cheungderek 0:e083abcfe7a8 118 class CDummy;
cheungderek 0:e083abcfe7a8 119 ///Setups callback
cheungderek 0:e083abcfe7a8 120 /**
cheungderek 0:e083abcfe7a8 121 @param pItem : instance of class on which to execute the callback method
cheungderek 0:e083abcfe7a8 122 @param pMethod : callback method
cheungderek 0:e083abcfe7a8 123 */
cheungderek 0:e083abcfe7a8 124 template<class T>
cheungderek 0:e083abcfe7a8 125 void setOnEvent( T* pItem, void (T::*pMethod)(TCPSocketEvent) )
cheungderek 0:e083abcfe7a8 126 {
cheungderek 0:e083abcfe7a8 127 m_pCbItem = (CDummy*) pItem;
cheungderek 0:e083abcfe7a8 128 m_pCbMeth = (void (CDummy::*)(TCPSocketEvent)) pMethod;
cheungderek 0:e083abcfe7a8 129 }
cheungderek 0:e083abcfe7a8 130
cheungderek 0:e083abcfe7a8 131 ///Disables callback
cheungderek 0:e083abcfe7a8 132 void resetOnEvent();
cheungderek 0:e083abcfe7a8 133
cheungderek 0:e083abcfe7a8 134 protected:
cheungderek 0:e083abcfe7a8 135 void onNetTcpSocketEvent(NetTcpSocketEvent e);
cheungderek 0:e083abcfe7a8 136 TCPSocketErr checkInst();
cheungderek 0:e083abcfe7a8 137
cheungderek 0:e083abcfe7a8 138 private:
cheungderek 0:e083abcfe7a8 139 NetTcpSocket* m_pNetTcpSocket;
cheungderek 0:e083abcfe7a8 140
cheungderek 0:e083abcfe7a8 141 CDummy* m_pCbItem;
cheungderek 0:e083abcfe7a8 142 void (CDummy::*m_pCbMeth)(TCPSocketEvent);
cheungderek 0:e083abcfe7a8 143
cheungderek 0:e083abcfe7a8 144 void (*m_pCb)(TCPSocketEvent);
cheungderek 0:e083abcfe7a8 145
cheungderek 0:e083abcfe7a8 146 };
cheungderek 0:e083abcfe7a8 147
cheungderek 0:e083abcfe7a8 148 #endif