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:
Wed Feb 09 13:57:49 2011 +0000
Revision:
0:f30e2135b0db
V0.0.0.1

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 "netservice.h"
Yann 0:f30e2135b0db 25 #include "net.h"
Yann 0:f30e2135b0db 26
Yann 0:f30e2135b0db 27 //#define __DEBUG
Yann 0:f30e2135b0db 28 #include "dbg/dbg.h"
Yann 0:f30e2135b0db 29
Yann 0:f30e2135b0db 30 NetService::NetService(bool owned /*= true*/) : m_closed(false), m_removed(false), m_owned(owned)
Yann 0:f30e2135b0db 31 {
Yann 0:f30e2135b0db 32 NetService::lpServices().push_back(this);
Yann 0:f30e2135b0db 33 DBG("\r\n[ + %p ] %d\r\n", (void*)this, lpServices().size());
Yann 0:f30e2135b0db 34 }
Yann 0:f30e2135b0db 35
Yann 0:f30e2135b0db 36 NetService::~NetService()
Yann 0:f30e2135b0db 37 {
Yann 0:f30e2135b0db 38 // DBG("\r\nService removed\r\n");
Yann 0:f30e2135b0db 39 // DBG("\r\nNow %d services running\r\n", lpServices().size()-1);
Yann 0:f30e2135b0db 40 DBG("\r\n[ - %p ] %d\r\n", (void*)this, lpServices().size()-1);
Yann 0:f30e2135b0db 41 if((!m_owned) || (!m_removed)) //Destructor was not called by servicesPoll()
Yann 0:f30e2135b0db 42 {
Yann 0:f30e2135b0db 43 if(m_owned)
Yann 0:f30e2135b0db 44 DBG("\r\nWARN!!!Service removed in dtor!!!\r\n");
Yann 0:f30e2135b0db 45 NetService::lpServices().remove(this);
Yann 0:f30e2135b0db 46 }
Yann 0:f30e2135b0db 47 }
Yann 0:f30e2135b0db 48
Yann 0:f30e2135b0db 49 void NetService::poll()
Yann 0:f30e2135b0db 50 {
Yann 0:f30e2135b0db 51
Yann 0:f30e2135b0db 52 }
Yann 0:f30e2135b0db 53
Yann 0:f30e2135b0db 54 void NetService::servicesPoll() //Poll all registered services & destroy closed ones
Yann 0:f30e2135b0db 55 {
Yann 0:f30e2135b0db 56 list<NetService*>::iterator it;
Yann 0:f30e2135b0db 57 DBG("\r\nServices polling over %d services\r\n", lpServices().size());
Yann 0:f30e2135b0db 58 for( it = lpServices().begin(); it != lpServices().end(); )
Yann 0:f30e2135b0db 59 {
Yann 0:f30e2135b0db 60 if( (*it)->m_owned && (*it)->m_closed )
Yann 0:f30e2135b0db 61 {
Yann 0:f30e2135b0db 62 DBG("\r\nService %p is flagged as closed\r\n", (*it));
Yann 0:f30e2135b0db 63 (*it)->m_removed = true;
Yann 0:f30e2135b0db 64 delete (*it);
Yann 0:f30e2135b0db 65 it = lpServices().erase(it);
Yann 0:f30e2135b0db 66 }
Yann 0:f30e2135b0db 67 else
Yann 0:f30e2135b0db 68 {
Yann 0:f30e2135b0db 69 //DBG("Service %p polling start\n", (*it));
Yann 0:f30e2135b0db 70 (*it)->poll();
Yann 0:f30e2135b0db 71 //DBG("Service %p polling end\n", (*it));
Yann 0:f30e2135b0db 72 it++;
Yann 0:f30e2135b0db 73 }
Yann 0:f30e2135b0db 74 }
Yann 0:f30e2135b0db 75
Yann 0:f30e2135b0db 76 }
Yann 0:f30e2135b0db 77
Yann 0:f30e2135b0db 78 void NetService::close()
Yann 0:f30e2135b0db 79 {
Yann 0:f30e2135b0db 80 DBG("\r\nService %p to be closed (owned = %d)\r\n", this, m_owned);
Yann 0:f30e2135b0db 81 m_closed = true;
Yann 0:f30e2135b0db 82 }
Yann 0:f30e2135b0db 83
Yann 0:f30e2135b0db 84 list<NetService*>& NetService::lpServices()
Yann 0:f30e2135b0db 85 {
Yann 0:f30e2135b0db 86 static list<NetService*>* pInst = new list<NetService*>(); //Called only once
Yann 0:f30e2135b0db 87 return *pInst;
Yann 0:f30e2135b0db 88 }