This short program illustrates how to use the DS130x_I2C library. My objective is to share the same RTC with Microchip 18F MCU.

Dependencies:   mbed DebugLibrary

Committer:
Yann
Date:
Fri Feb 11 10:17:20 2011 +0000
Revision:
1:995212d326ca
Parent:
0:f30e2135b0db
V0.0.0.2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Yann 0:f30e2135b0db 1
Yann 0:f30e2135b0db 2 /*
Yann 0:f30e2135b0db 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
Yann 0:f30e2135b0db 4
Yann 0:f30e2135b0db 5 Permission is hereby granted, free of charge, to any person obtaining a copy
Yann 0:f30e2135b0db 6 of this software and associated documentation files (the "Software"), to deal
Yann 0:f30e2135b0db 7 in the Software without restriction, including without limitation the rights
Yann 0:f30e2135b0db 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Yann 0:f30e2135b0db 9 copies of the Software, and to permit persons to whom the Software is
Yann 0:f30e2135b0db 10 furnished to do so, subject to the following conditions:
Yann 0:f30e2135b0db 11
Yann 0:f30e2135b0db 12 The above copyright notice and this permission notice shall be included in
Yann 0:f30e2135b0db 13 all copies or substantial portions of the Software.
Yann 0:f30e2135b0db 14
Yann 0:f30e2135b0db 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Yann 0:f30e2135b0db 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Yann 0:f30e2135b0db 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Yann 0:f30e2135b0db 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Yann 0:f30e2135b0db 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Yann 0:f30e2135b0db 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Yann 0:f30e2135b0db 21 THE SOFTWARE.
Yann 0:f30e2135b0db 22 */
Yann 0:f30e2135b0db 23
Yann 0:f30e2135b0db 24 /**
Yann 0:f30e2135b0db 25 HTTP Request Handler header file.
Yann 0:f30e2135b0db 26 */
Yann 0:f30e2135b0db 27
Yann 0:f30e2135b0db 28 #ifndef HTTP_REQUEST_HANDLER_H
Yann 0:f30e2135b0db 29 #define HTTP_REQUEST_HANDLER_H
Yann 0:f30e2135b0db 30
Yann 0:f30e2135b0db 31 #include "api/TCPSocket.h"
Yann 0:f30e2135b0db 32 //#include "HTTPServer.h"
Yann 0:f30e2135b0db 33
Yann 0:f30e2135b0db 34 #include "mbed.h"
Yann 0:f30e2135b0db 35 #include "core/netservice.h"
Yann 0:f30e2135b0db 36
Yann 0:f30e2135b0db 37 #include <string>
Yann 0:f30e2135b0db 38 using std::string;
Yann 0:f30e2135b0db 39
Yann 0:f30e2135b0db 40 #include <map>
Yann 0:f30e2135b0db 41 using std::map;
Yann 0:f30e2135b0db 42
Yann 0:f30e2135b0db 43 ///HTTP Server's generic request handler
Yann 0:f30e2135b0db 44 class HTTPRequestHandler : public NetService
Yann 0:f30e2135b0db 45 {
Yann 0:f30e2135b0db 46 public:
Yann 0:f30e2135b0db 47 ///Instantiated by the HTTP Server
Yann 0:f30e2135b0db 48 HTTPRequestHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket);
Yann 0:f30e2135b0db 49 virtual ~HTTPRequestHandler();
Yann 0:f30e2135b0db 50
Yann 0:f30e2135b0db 51 //protected:
Yann 0:f30e2135b0db 52 virtual void doGet() = 0;
Yann 0:f30e2135b0db 53 virtual void doPost() = 0;
Yann 0:f30e2135b0db 54 virtual void doHead() = 0;
Yann 0:f30e2135b0db 55
Yann 0:f30e2135b0db 56 virtual void onReadable() = 0; //Data has been read
Yann 0:f30e2135b0db 57 virtual void onWriteable() = 0; //Data has been written & buf is free
Yann 0:f30e2135b0db 58 virtual void onTimeout(); //Connection has timed out
Yann 0:f30e2135b0db 59 virtual void onClose() = 0; //Connection is closing
Yann 0:f30e2135b0db 60
Yann 0:f30e2135b0db 61 virtual void close(); //Close socket and destroy data
Yann 0:f30e2135b0db 62
Yann 0:f30e2135b0db 63 protected:
Yann 0:f30e2135b0db 64 map<string, string>& reqHeaders() /*const*/;
Yann 0:f30e2135b0db 65 string& path() /*const*/;
Yann 0:f30e2135b0db 66 int dataLen() const;
Yann 0:f30e2135b0db 67 int readData(char* buf, int len);
Yann 0:f30e2135b0db 68 string& rootPath() /*const*/;
Yann 0:f30e2135b0db 69
Yann 0:f30e2135b0db 70 void setErrCode(int errc);
Yann 0:f30e2135b0db 71 void setContentLen(int len);
Yann 0:f30e2135b0db 72
Yann 0:f30e2135b0db 73 map<string, string>& respHeaders();
Yann 0:f30e2135b0db 74 int writeData(const char* buf, int len);
Yann 0:f30e2135b0db 75
Yann 0:f30e2135b0db 76 void setTimeout(int ms);
Yann 0:f30e2135b0db 77 void resetTimeout();
Yann 0:f30e2135b0db 78
Yann 0:f30e2135b0db 79 private:
Yann 0:f30e2135b0db 80 void readHeaders(); //Called at instanciation
Yann 0:f30e2135b0db 81 void writeHeaders(); //Called at the first writeData call
Yann 0:f30e2135b0db 82 void onTCPSocketEvent(TCPSocketEvent e);
Yann 0:f30e2135b0db 83
Yann 0:f30e2135b0db 84 TCPSocket* m_pTCPSocket;
Yann 0:f30e2135b0db 85 map<string, string> m_reqHeaders;
Yann 0:f30e2135b0db 86 map<string, string> m_respHeaders;
Yann 0:f30e2135b0db 87 string m_rootPath;
Yann 0:f30e2135b0db 88 string m_path;
Yann 0:f30e2135b0db 89 int m_errc; //Response code
Yann 0:f30e2135b0db 90
Yann 0:f30e2135b0db 91 Timeout m_watchdog;
Yann 0:f30e2135b0db 92 int m_timeout;
Yann 0:f30e2135b0db 93
Yann 0:f30e2135b0db 94 bool m_closed;
Yann 0:f30e2135b0db 95 bool m_headersSent;
Yann 0:f30e2135b0db 96
Yann 0:f30e2135b0db 97 int readLine(char* str, int maxLen);
Yann 0:f30e2135b0db 98
Yann 0:f30e2135b0db 99 };
Yann 0:f30e2135b0db 100
Yann 0:f30e2135b0db 101 #endif