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 /** \file
Yann 0:f30e2135b0db 25 UDP Socket header file
Yann 0:f30e2135b0db 26 */
Yann 0:f30e2135b0db 27
Yann 0:f30e2135b0db 28 #ifndef UDPSOCKET_H
Yann 0:f30e2135b0db 29 #define UDPSOCKET_H
Yann 0:f30e2135b0db 30
Yann 0:f30e2135b0db 31 #include "core/net.h"
Yann 0:f30e2135b0db 32 #include "core/host.h"
Yann 0:f30e2135b0db 33 //Essentially it is a safe interface to NetUdpSocket
Yann 0:f30e2135b0db 34
Yann 0:f30e2135b0db 35 ///UDP Socket error codes
Yann 0:f30e2135b0db 36 enum UDPSocketErr
Yann 0:f30e2135b0db 37 {
Yann 0:f30e2135b0db 38 __UDPSOCKET_MIN = -0xFFFF,
Yann 0:f30e2135b0db 39 UDPSOCKET_SETUP, ///<UDPSocket not properly configured
Yann 0:f30e2135b0db 40 UDPSOCKET_IF, ///<Interface has problems, does not exist or is not initialized
Yann 0:f30e2135b0db 41 UDPSOCKET_MEM, ///<Not enough mem
Yann 0:f30e2135b0db 42 UDPSOCKET_INUSE, ///<Interface / Port is in use
Yann 0:f30e2135b0db 43 //...
Yann 0:f30e2135b0db 44 UDPSOCKET_OK = 0 ///<Success
Yann 0:f30e2135b0db 45 };
Yann 0:f30e2135b0db 46
Yann 0:f30e2135b0db 47 ///UDP Socket Event(s)
Yann 0:f30e2135b0db 48 enum UDPSocketEvent //Only one event here for now, but keeps that model in case we need to implement some others
Yann 0:f30e2135b0db 49 {
Yann 0:f30e2135b0db 50 UDPSOCKET_READABLE, ///<Data in buf
Yann 0:f30e2135b0db 51 };
Yann 0:f30e2135b0db 52
Yann 0:f30e2135b0db 53 class NetUdpSocket;
Yann 0:f30e2135b0db 54 enum NetUdpSocketEvent;
Yann 0:f30e2135b0db 55
Yann 0:f30e2135b0db 56 ///This is a simple UDP Socket class
Yann 0:f30e2135b0db 57 /**
Yann 0:f30e2135b0db 58 This class exposes an API to deal with UDP Sockets
Yann 0:f30e2135b0db 59 */
Yann 0:f30e2135b0db 60 class UDPSocket
Yann 0:f30e2135b0db 61 {
Yann 0:f30e2135b0db 62 public:
Yann 0:f30e2135b0db 63 ///Creates a new socket
Yann 0:f30e2135b0db 64 UDPSocket();
Yann 0:f30e2135b0db 65
Yann 0:f30e2135b0db 66 ///Closes and destroys socket
Yann 0:f30e2135b0db 67 ~UDPSocket(); //close()
Yann 0:f30e2135b0db 68
Yann 0:f30e2135b0db 69 ///Binds the socket to local host or a multicast address
Yann 0:f30e2135b0db 70 UDPSocketErr bind(const Host& me);
Yann 0:f30e2135b0db 71
Yann 0:f30e2135b0db 72 ///Sends data
Yann 0:f30e2135b0db 73 /*
Yann 0:f30e2135b0db 74 @param pHost : host to send data to
Yann 0:f30e2135b0db 75 @return a negative error code or the number of bytes transmitted
Yann 0:f30e2135b0db 76 */
Yann 0:f30e2135b0db 77 int /*if < 0 : UDPSocketErr*/ sendto(const char* buf, int len, Host* pHost);
Yann 0:f30e2135b0db 78
Yann 0:f30e2135b0db 79 ///Receives data
Yann 0:f30e2135b0db 80 /*
Yann 0:f30e2135b0db 81 @param pHost : host from which this piece of data comes from
Yann 0:f30e2135b0db 82 @return a negative error code or the number of bytes received
Yann 0:f30e2135b0db 83 */
Yann 0:f30e2135b0db 84 int /*if < 0 : UDPSocketErr*/ recvfrom(char* buf, int len, Host* pHost);
Yann 0:f30e2135b0db 85
Yann 0:f30e2135b0db 86 /* TODO NTH : printf / scanf helpers that call send/recv */
Yann 0:f30e2135b0db 87
Yann 0:f30e2135b0db 88 ///Closes socket
Yann 0:f30e2135b0db 89 UDPSocketErr close();
Yann 0:f30e2135b0db 90
Yann 0:f30e2135b0db 91 //Callbacks
Yann 0:f30e2135b0db 92 ///Setups callback
Yann 0:f30e2135b0db 93 /**
Yann 0:f30e2135b0db 94 @param pMethod : callback function
Yann 0:f30e2135b0db 95 */
Yann 0:f30e2135b0db 96 void setOnEvent( void (*pMethod)(UDPSocketEvent) );
Yann 0:f30e2135b0db 97
Yann 0:f30e2135b0db 98 class CDummy;
Yann 0:f30e2135b0db 99 ///Setups callback
Yann 0:f30e2135b0db 100 /**
Yann 0:f30e2135b0db 101 @param pItem : instance of class on which to execute the callback method
Yann 0:f30e2135b0db 102 @param pMethod : callback method
Yann 0:f30e2135b0db 103 */
Yann 0:f30e2135b0db 104 template<class T>
Yann 0:f30e2135b0db 105 void setOnEvent( T* pItem, void (T::*pMethod)(UDPSocketEvent) )
Yann 0:f30e2135b0db 106 {
Yann 0:f30e2135b0db 107 m_pCbItem = (CDummy*) pItem;
Yann 0:f30e2135b0db 108 m_pCbMeth = (void (CDummy::*)(UDPSocketEvent)) pMethod;
Yann 0:f30e2135b0db 109 }
Yann 0:f30e2135b0db 110
Yann 0:f30e2135b0db 111 ///Disables callback
Yann 0:f30e2135b0db 112 void resetOnEvent();
Yann 0:f30e2135b0db 113
Yann 0:f30e2135b0db 114 protected:
Yann 0:f30e2135b0db 115 void onNetUdpSocketEvent(NetUdpSocketEvent e);
Yann 0:f30e2135b0db 116 UDPSocketErr checkInst();
Yann 0:f30e2135b0db 117
Yann 0:f30e2135b0db 118 private:
Yann 0:f30e2135b0db 119 NetUdpSocket* m_pNetUdpSocket;
Yann 0:f30e2135b0db 120
Yann 0:f30e2135b0db 121 CDummy* m_pCbItem;
Yann 0:f30e2135b0db 122 void (CDummy::*m_pCbMeth)(UDPSocketEvent);
Yann 0:f30e2135b0db 123
Yann 0:f30e2135b0db 124 void (*m_pCb)(UDPSocketEvent);
Yann 0:f30e2135b0db 125
Yann 0:f30e2135b0db 126 };
Yann 0:f30e2135b0db 127
Yann 0:f30e2135b0db 128 #endif