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 #include "TCPSocket.h"
Yann 0:f30e2135b0db 25 #include "if/net/nettcpsocket.h"
Yann 0:f30e2135b0db 26
Yann 0:f30e2135b0db 27 TCPSocket::TCPSocket() : m_pNetTcpSocket(NULL), m_pCbItem(NULL), m_pCbMeth(NULL), m_pCb(NULL)
Yann 0:f30e2135b0db 28 {
Yann 0:f30e2135b0db 29
Yann 0:f30e2135b0db 30 }
Yann 0:f30e2135b0db 31
Yann 0:f30e2135b0db 32 TCPSocket::TCPSocket(NetTcpSocket* pNetTcpSocket) : m_pNetTcpSocket(pNetTcpSocket), m_pCbItem(NULL), m_pCbMeth(NULL), m_pCb(NULL)
Yann 0:f30e2135b0db 33 {
Yann 0:f30e2135b0db 34 m_pNetTcpSocket->setOnEvent(this, &TCPSocket::onNetTcpSocketEvent);
Yann 0:f30e2135b0db 35 }
Yann 0:f30e2135b0db 36
Yann 0:f30e2135b0db 37 TCPSocket::~TCPSocket() //close()
Yann 0:f30e2135b0db 38 {
Yann 0:f30e2135b0db 39 close();
Yann 0:f30e2135b0db 40 }
Yann 0:f30e2135b0db 41
Yann 0:f30e2135b0db 42 TCPSocketErr TCPSocket::bind(const Host& me)
Yann 0:f30e2135b0db 43 {
Yann 0:f30e2135b0db 44 TCPSocketErr tcpSocketErr = checkInst();
Yann 0:f30e2135b0db 45 if(tcpSocketErr)
Yann 0:f30e2135b0db 46 return tcpSocketErr;
Yann 0:f30e2135b0db 47 return (TCPSocketErr) m_pNetTcpSocket->bind(me);
Yann 0:f30e2135b0db 48 }
Yann 0:f30e2135b0db 49
Yann 0:f30e2135b0db 50 TCPSocketErr TCPSocket::listen()
Yann 0:f30e2135b0db 51 {
Yann 0:f30e2135b0db 52 TCPSocketErr tcpSocketErr = checkInst();
Yann 0:f30e2135b0db 53 if(tcpSocketErr)
Yann 0:f30e2135b0db 54 return tcpSocketErr;
Yann 0:f30e2135b0db 55 return (TCPSocketErr) m_pNetTcpSocket->listen();
Yann 0:f30e2135b0db 56 }
Yann 0:f30e2135b0db 57
Yann 0:f30e2135b0db 58 TCPSocketErr TCPSocket::connect(const Host& host)
Yann 0:f30e2135b0db 59 {
Yann 0:f30e2135b0db 60 TCPSocketErr tcpSocketErr = checkInst();
Yann 0:f30e2135b0db 61 if(tcpSocketErr)
Yann 0:f30e2135b0db 62 return tcpSocketErr;
Yann 0:f30e2135b0db 63 return (TCPSocketErr) m_pNetTcpSocket->connect(host);
Yann 0:f30e2135b0db 64 }
Yann 0:f30e2135b0db 65
Yann 0:f30e2135b0db 66 TCPSocketErr TCPSocket::accept(Host* pClient, TCPSocket** ppNewTCPSocket)
Yann 0:f30e2135b0db 67 {
Yann 0:f30e2135b0db 68 TCPSocketErr tcpSocketErr = checkInst();
Yann 0:f30e2135b0db 69 if(tcpSocketErr)
Yann 0:f30e2135b0db 70 return tcpSocketErr;
Yann 0:f30e2135b0db 71 NetTcpSocket* pNewNetTcpSocket;
Yann 0:f30e2135b0db 72 tcpSocketErr = (TCPSocketErr) m_pNetTcpSocket->accept(pClient, &pNewNetTcpSocket);
Yann 0:f30e2135b0db 73 if(pNewNetTcpSocket)
Yann 0:f30e2135b0db 74 *ppNewTCPSocket = new TCPSocket(pNewNetTcpSocket);
Yann 0:f30e2135b0db 75 return tcpSocketErr;
Yann 0:f30e2135b0db 76 }
Yann 0:f30e2135b0db 77
Yann 0:f30e2135b0db 78 int /*if < 0 : TCPSocketErr*/ TCPSocket::send(const char* buf, int len)
Yann 0:f30e2135b0db 79 {
Yann 0:f30e2135b0db 80 TCPSocketErr tcpSocketErr = checkInst();
Yann 0:f30e2135b0db 81 if(tcpSocketErr)
Yann 0:f30e2135b0db 82 return tcpSocketErr;
Yann 0:f30e2135b0db 83 return m_pNetTcpSocket->send(buf, len);
Yann 0:f30e2135b0db 84 }
Yann 0:f30e2135b0db 85
Yann 0:f30e2135b0db 86 int /*if < 0 : TCPSocketErr*/ TCPSocket::recv(char* buf, int len)
Yann 0:f30e2135b0db 87 {
Yann 0:f30e2135b0db 88 TCPSocketErr tcpSocketErr = checkInst();
Yann 0:f30e2135b0db 89 if(tcpSocketErr)
Yann 0:f30e2135b0db 90 return tcpSocketErr;
Yann 0:f30e2135b0db 91 return m_pNetTcpSocket->recv(buf, len);
Yann 0:f30e2135b0db 92 }
Yann 0:f30e2135b0db 93
Yann 0:f30e2135b0db 94 TCPSocketErr TCPSocket::close()
Yann 0:f30e2135b0db 95 {
Yann 0:f30e2135b0db 96 if(!m_pNetTcpSocket)
Yann 0:f30e2135b0db 97 return TCPSOCKET_SETUP;
Yann 0:f30e2135b0db 98 m_pNetTcpSocket->resetOnEvent();
Yann 0:f30e2135b0db 99 TCPSocketErr tcpSocketErr = (TCPSocketErr) m_pNetTcpSocket->close(); //Close (can already be closed)
Yann 0:f30e2135b0db 100 Net::releaseTcpSocket(m_pNetTcpSocket); //And release it so it can be freed when properly removed
Yann 0:f30e2135b0db 101 m_pNetTcpSocket = NULL;
Yann 0:f30e2135b0db 102 return tcpSocketErr;
Yann 0:f30e2135b0db 103 }
Yann 0:f30e2135b0db 104
Yann 0:f30e2135b0db 105 //Callbacks
Yann 0:f30e2135b0db 106 void TCPSocket::setOnEvent( void (*pMethod)(TCPSocketEvent) )
Yann 0:f30e2135b0db 107 {
Yann 0:f30e2135b0db 108 m_pCb = pMethod;
Yann 0:f30e2135b0db 109 }
Yann 0:f30e2135b0db 110
Yann 0:f30e2135b0db 111 #if 0 //For info only
Yann 0:f30e2135b0db 112 template<class T>
Yann 0:f30e2135b0db 113 void TCPSocket::setOnEvent( T* pItem, void (T::*pMethod)(TCPSocketEvent) )
Yann 0:f30e2135b0db 114 {
Yann 0:f30e2135b0db 115 m_pCbItem = (CDummy*) pItem;
Yann 0:f30e2135b0db 116 m_pCbMeth = (void (CDummy::*)(TCPSocketEvent)) pMethod;
Yann 0:f30e2135b0db 117 }
Yann 0:f30e2135b0db 118 #endif
Yann 0:f30e2135b0db 119
Yann 0:f30e2135b0db 120 void TCPSocket::resetOnEvent() //Disable callback
Yann 0:f30e2135b0db 121 {
Yann 0:f30e2135b0db 122 m_pCb = NULL;
Yann 0:f30e2135b0db 123 m_pCbItem = NULL;
Yann 0:f30e2135b0db 124 m_pCbMeth = NULL;
Yann 0:f30e2135b0db 125 }
Yann 0:f30e2135b0db 126
Yann 0:f30e2135b0db 127 void TCPSocket::onNetTcpSocketEvent(NetTcpSocketEvent e)
Yann 0:f30e2135b0db 128 {
Yann 0:f30e2135b0db 129 if(m_pCbItem && m_pCbMeth)
Yann 0:f30e2135b0db 130 (m_pCbItem->*m_pCbMeth)((TCPSocketEvent) e);
Yann 0:f30e2135b0db 131 else if(m_pCb)
Yann 0:f30e2135b0db 132 m_pCb((TCPSocketEvent) e);
Yann 0:f30e2135b0db 133 }
Yann 0:f30e2135b0db 134
Yann 0:f30e2135b0db 135 TCPSocketErr TCPSocket::checkInst()
Yann 0:f30e2135b0db 136 {
Yann 0:f30e2135b0db 137 if(!m_pNetTcpSocket)
Yann 0:f30e2135b0db 138 {
Yann 0:f30e2135b0db 139 m_pNetTcpSocket = Net::tcpSocket();
Yann 0:f30e2135b0db 140 if(!m_pNetTcpSocket)
Yann 0:f30e2135b0db 141 {
Yann 0:f30e2135b0db 142 return TCPSOCKET_IF; //Interface did not return a socket (usually because a default interface does not exist)
Yann 0:f30e2135b0db 143 }
Yann 0:f30e2135b0db 144 m_pNetTcpSocket->setOnEvent(this, &TCPSocket::onNetTcpSocketEvent);
Yann 0:f30e2135b0db 145 }
Yann 0:f30e2135b0db 146 return TCPSOCKET_OK;
Yann 0:f30e2135b0db 147 }