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 #ifndef LWIPNETTCPSOCKET_H
cheungderek 0:e083abcfe7a8 25 #define LWIPNETTCPSOCKET_H
cheungderek 0:e083abcfe7a8 26
cheungderek 0:e083abcfe7a8 27 #define NET_LWIP_STACK 1
cheungderek 0:e083abcfe7a8 28 #include "if/net/nettcpsocket.h"
cheungderek 0:e083abcfe7a8 29 #include "LwipNetIf.h"
cheungderek 0:e083abcfe7a8 30
cheungderek 0:e083abcfe7a8 31 #include "stdint.h"
cheungderek 0:e083abcfe7a8 32
cheungderek 0:e083abcfe7a8 33 //Implements NetTcpSockets over lwIP raw API
cheungderek 0:e083abcfe7a8 34
cheungderek 0:e083abcfe7a8 35 struct tcp_pcb; //Represents a Tcp Connection, "Protocol Control Block", see rawapi.txt & tcp.h
cheungderek 0:e083abcfe7a8 36 struct pbuf; //Lwip Buffer Container
cheungderek 0:e083abcfe7a8 37
cheungderek 0:e083abcfe7a8 38 typedef signed char err_t;
cheungderek 0:e083abcfe7a8 39 typedef uint16_t u16_t;
cheungderek 0:e083abcfe7a8 40
cheungderek 0:e083abcfe7a8 41 class LwipNetTcpSocket: public NetTcpSocket
cheungderek 0:e083abcfe7a8 42 {
cheungderek 0:e083abcfe7a8 43 public:
cheungderek 0:e083abcfe7a8 44 LwipNetTcpSocket(tcp_pcb* pPcb = NULL); //Passes a pcb if already created (by an accept req for instance), in that case transfers ownership
cheungderek 0:e083abcfe7a8 45 virtual ~LwipNetTcpSocket();
cheungderek 0:e083abcfe7a8 46
cheungderek 0:e083abcfe7a8 47 virtual NetTcpSocketErr bind(const Host& me);
cheungderek 0:e083abcfe7a8 48 virtual NetTcpSocketErr listen();
cheungderek 0:e083abcfe7a8 49 virtual NetTcpSocketErr connect(const Host& host);
cheungderek 0:e083abcfe7a8 50 virtual NetTcpSocketErr accept(Host* pClient, NetTcpSocket** ppNewNetTcpSocket);
cheungderek 0:e083abcfe7a8 51
cheungderek 0:e083abcfe7a8 52 virtual int /*if < 0 : NetTcpSocketErr*/ send(const char* buf, int len);
cheungderek 0:e083abcfe7a8 53 virtual int /*if < 0 : NetTcpSocketErr*/ recv(char* buf, int len);
cheungderek 0:e083abcfe7a8 54
cheungderek 0:e083abcfe7a8 55 virtual NetTcpSocketErr close();
cheungderek 0:e083abcfe7a8 56
cheungderek 0:e083abcfe7a8 57 virtual NetTcpSocketErr poll();
cheungderek 0:e083abcfe7a8 58
cheungderek 0:e083abcfe7a8 59 protected:
cheungderek 0:e083abcfe7a8 60 volatile tcp_pcb* m_pPcb;
cheungderek 0:e083abcfe7a8 61
cheungderek 0:e083abcfe7a8 62 //Events callbacks from lwIp
cheungderek 0:e083abcfe7a8 63 err_t acceptCb(tcp_pcb* newpcb, err_t err);
cheungderek 0:e083abcfe7a8 64 err_t connectedCb(tcp_pcb* tpcb, err_t err);
cheungderek 0:e083abcfe7a8 65
cheungderek 0:e083abcfe7a8 66 void errCb(err_t err);
cheungderek 0:e083abcfe7a8 67
cheungderek 0:e083abcfe7a8 68 err_t sentCb(tcp_pcb* tpcb, u16_t len);
cheungderek 0:e083abcfe7a8 69 err_t recvCb(tcp_pcb* tpcb, pbuf *p, err_t err);
cheungderek 0:e083abcfe7a8 70
cheungderek 0:e083abcfe7a8 71 private:
cheungderek 0:e083abcfe7a8 72 void cleanUp(); //Flush input buffer
cheungderek 0:e083abcfe7a8 73
cheungderek 0:e083abcfe7a8 74 // queue<tcp_pcb*> m_lpInPcb; //Incoming connections that have not been accepted yet
cheungderek 0:e083abcfe7a8 75 queue<LwipNetTcpSocket*> m_lpInNetTcpSocket; //Incoming connections that have not been accepted yet
cheungderek 0:e083abcfe7a8 76
cheungderek 0:e083abcfe7a8 77 volatile pbuf* m_pReadPbuf; //Ptr to read buffer
cheungderek 0:e083abcfe7a8 78
cheungderek 0:e083abcfe7a8 79 //Static callbacks : Transforms into a C++ callback
cheungderek 0:e083abcfe7a8 80 static err_t sAcceptCb(void *arg, struct tcp_pcb *newpcb, err_t err);
cheungderek 0:e083abcfe7a8 81 static err_t sConnectedCb(void *arg, struct tcp_pcb *tpcb, err_t err);
cheungderek 0:e083abcfe7a8 82
cheungderek 0:e083abcfe7a8 83 static void sErrCb(void *arg, err_t err);
cheungderek 0:e083abcfe7a8 84
cheungderek 0:e083abcfe7a8 85 static err_t sSentCb(void *arg, struct tcp_pcb *tpcb, u16_t len);
cheungderek 0:e083abcfe7a8 86 static err_t sRecvCb(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err);
cheungderek 0:e083abcfe7a8 87
cheungderek 0:e083abcfe7a8 88 };
cheungderek 0:e083abcfe7a8 89
cheungderek 0:e083abcfe7a8 90 #endif