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
NetServices/drv/at/ATIf.h@1:995212d326ca, 2011-02-11 (annotated)
- 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?
User | Revision | Line number | New 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 | #ifndef ATIF_H |
Yann | 0:f30e2135b0db | 25 | #define ATIF_H |
Yann | 0:f30e2135b0db | 26 | |
Yann | 0:f30e2135b0db | 27 | #include "netCfg.h" |
Yann | 0:f30e2135b0db | 28 | |
Yann | 0:f30e2135b0db | 29 | #include "mbed.h" |
Yann | 0:f30e2135b0db | 30 | #include "drv/serial/buf/SerialBuf.h" |
Yann | 0:f30e2135b0db | 31 | #include <list> |
Yann | 0:f30e2135b0db | 32 | using std::list; |
Yann | 0:f30e2135b0db | 33 | |
Yann | 0:f30e2135b0db | 34 | //class Serial; //Pb w forward decl |
Yann | 0:f30e2135b0db | 35 | |
Yann | 0:f30e2135b0db | 36 | enum ATErr |
Yann | 0:f30e2135b0db | 37 | { |
Yann | 0:f30e2135b0db | 38 | __AT_MIN = -0xFFFF, |
Yann | 0:f30e2135b0db | 39 | AT_CLOSED, |
Yann | 0:f30e2135b0db | 40 | AT_NOANSWER, |
Yann | 0:f30e2135b0db | 41 | AT_ERROR, |
Yann | 0:f30e2135b0db | 42 | AT_TIMEOUT, |
Yann | 0:f30e2135b0db | 43 | AT_BUSY, |
Yann | 0:f30e2135b0db | 44 | AT_PARSE, |
Yann | 0:f30e2135b0db | 45 | AT_INCOMPLETE, |
Yann | 0:f30e2135b0db | 46 | AT_OK = 0 |
Yann | 0:f30e2135b0db | 47 | }; |
Yann | 0:f30e2135b0db | 48 | |
Yann | 0:f30e2135b0db | 49 | class ATIf : public SerialBuf |
Yann | 0:f30e2135b0db | 50 | { |
Yann | 0:f30e2135b0db | 51 | public: |
Yann | 0:f30e2135b0db | 52 | |
Yann | 0:f30e2135b0db | 53 | ATIf(); |
Yann | 0:f30e2135b0db | 54 | virtual ~ATIf(); |
Yann | 0:f30e2135b0db | 55 | |
Yann | 0:f30e2135b0db | 56 | #if 0 |
Yann | 0:f30e2135b0db | 57 | template<class T> |
Yann | 0:f30e2135b0db | 58 | //void attachSignal( const char* sigName, T* pItem, bool (T::*pMethod)(ATIf*, bool, bool*) ); //Attach Signal ("Unsollicited response code" in Telit_AT_Reference_Guide.pdf) to an handler fn |
Yann | 0:f30e2135b0db | 59 | //Linker bug : Must be defined here :( |
Yann | 0:f30e2135b0db | 60 | void attachSignal( const char* sigName, T* pItem, bool (T::*pMethod)(ATIf*, bool, bool*) ) //Attach Signal ("Unsollicited response code" in Telit_AT_Reference_Guide.pdf) to an handler fn |
Yann | 0:f30e2135b0db | 61 | { |
Yann | 0:f30e2135b0db | 62 | ATSigHandler sig(sigName, (ATSigHandler::CDummy*)pItem, (bool (ATSigHandler::CDummy::*)(ATIf*, bool, bool*))pMethod); |
Yann | 0:f30e2135b0db | 63 | m_signals.push_back(sig); |
Yann | 0:f30e2135b0db | 64 | } |
Yann | 0:f30e2135b0db | 65 | void detachSignal( const char* sigName ); |
Yann | 0:f30e2135b0db | 66 | #endif |
Yann | 0:f30e2135b0db | 67 | |
Yann | 0:f30e2135b0db | 68 | ATErr open(Serial* pSerial); //Deactivate echo, etc |
Yann | 0:f30e2135b0db | 69 | #if NET_USB_SERIAL |
Yann | 0:f30e2135b0db | 70 | ATErr open(UsbSerial* pUsbSerial); //Deactivate echo, etc |
Yann | 0:f30e2135b0db | 71 | #endif |
Yann | 0:f30e2135b0db | 72 | ATErr close(); //Release port |
Yann | 0:f30e2135b0db | 73 | |
Yann | 0:f30e2135b0db | 74 | int printf(const char* format, ... ); |
Yann | 0:f30e2135b0db | 75 | int scanf(const char* format, ... ); |
Yann | 0:f30e2135b0db | 76 | void setTimeout(int timeout); //used by scanf |
Yann | 0:f30e2135b0db | 77 | void setLineMode(bool lineMode); //Switch btw line & raw fns |
Yann | 0:f30e2135b0db | 78 | //void setSignals(bool signalsEnable); |
Yann | 0:f30e2135b0db | 79 | ATErr flushBuffer(); //Discard input buffer |
Yann | 0:f30e2135b0db | 80 | ATErr flushLine(int timeout); //Discard input buffer until CRLF is encountered |
Yann | 0:f30e2135b0db | 81 | |
Yann | 0:f30e2135b0db | 82 | protected: |
Yann | 0:f30e2135b0db | 83 | //virtual bool onRead(); //Inherited from SerialBuf, return true if data is incomplete |
Yann | 0:f30e2135b0db | 84 | ATErr rawOpen(Serial* pSerial, int baudrate); //Simple open function for similar non-conforming protocols |
Yann | 0:f30e2135b0db | 85 | |
Yann | 0:f30e2135b0db | 86 | public: |
Yann | 0:f30e2135b0db | 87 | /* ATErr command(const char* cmd, char* result, int resultLen, int timeout); */ //Kinda useless |
Yann | 0:f30e2135b0db | 88 | ATErr write(const char* cmd, bool lineMode = false); |
Yann | 0:f30e2135b0db | 89 | ATErr read(char* result, int resultMaxLen, int timeout, bool lineMode = false, int resultMinLen = 0); |
Yann | 0:f30e2135b0db | 90 | bool isOpen(); |
Yann | 0:f30e2135b0db | 91 | ATErr checkOK(); //Helper fn to quickly check that OK has been returned |
Yann | 0:f30e2135b0db | 92 | |
Yann | 0:f30e2135b0db | 93 | private: |
Yann | 0:f30e2135b0db | 94 | int readLine(char* line, int maxLen, int timeout); //Read a single line from serial port |
Yann | 0:f30e2135b0db | 95 | int writeLine(const char* line); //Write a single line to serial port |
Yann | 0:f30e2135b0db | 96 | |
Yann | 0:f30e2135b0db | 97 | int readRaw(char* str, int maxLen, int timeout = 0, int minLen = 0); //Read from serial port in buf |
Yann | 0:f30e2135b0db | 98 | int writeRaw(const char* str); //Write directly to serial port |
Yann | 0:f30e2135b0db | 99 | |
Yann | 0:f30e2135b0db | 100 | volatile int m_readTimeout; |
Yann | 0:f30e2135b0db | 101 | volatile bool m_lineMode; |
Yann | 0:f30e2135b0db | 102 | //bool m_signalsEnable; |
Yann | 0:f30e2135b0db | 103 | bool m_isOpen; |
Yann | 0:f30e2135b0db | 104 | |
Yann | 0:f30e2135b0db | 105 | char* m_tmpBuf; |
Yann | 0:f30e2135b0db | 106 | |
Yann | 0:f30e2135b0db | 107 | #if 0 |
Yann | 0:f30e2135b0db | 108 | class ATSigHandler |
Yann | 0:f30e2135b0db | 109 | { |
Yann | 0:f30e2135b0db | 110 | class CDummy; |
Yann | 0:f30e2135b0db | 111 | public: |
Yann | 0:f30e2135b0db | 112 | ATSigHandler(const char* name, CDummy* cbObj, bool (CDummy::*cbMeth)(ATIf* pIf, bool beg, bool* pMoreData)) : m_cbObj(cbObj), m_cbMeth(cbMeth), m_name(name) |
Yann | 0:f30e2135b0db | 113 | {} |
Yann | 0:f30e2135b0db | 114 | protected: |
Yann | 0:f30e2135b0db | 115 | CDummy* m_cbObj; |
Yann | 0:f30e2135b0db | 116 | bool (CDummy::*m_cbMeth)(ATIf* pIf, bool beg, bool* pMoreData); //*pMoreData set to true if needs to read next line, beg = true if beginning of new code |
Yann | 0:f30e2135b0db | 117 | const char* m_name; |
Yann | 0:f30e2135b0db | 118 | |
Yann | 0:f30e2135b0db | 119 | friend class ATIf; |
Yann | 0:f30e2135b0db | 120 | }; |
Yann | 0:f30e2135b0db | 121 | |
Yann | 0:f30e2135b0db | 122 | volatile ATSigHandler* m_pCurrentSignal; //Signal that asked more data |
Yann | 0:f30e2135b0db | 123 | |
Yann | 0:f30e2135b0db | 124 | bool handleSignal(); //Returns true if signal has been handled |
Yann | 0:f30e2135b0db | 125 | list<ATSigHandler> m_signals; |
Yann | 0:f30e2135b0db | 126 | #endif |
Yann | 0:f30e2135b0db | 127 | |
Yann | 0:f30e2135b0db | 128 | }; |
Yann | 0:f30e2135b0db | 129 | |
Yann | 0:f30e2135b0db | 130 | #endif |