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 "HTTPFile.h"
Yann 0:f30e2135b0db 25
Yann 0:f30e2135b0db 26 //#define __DEBUG
Yann 0:f30e2135b0db 27 #include "dbg/dbg.h"
Yann 0:f30e2135b0db 28
Yann 0:f30e2135b0db 29 HTTPFile::HTTPFile(const char* path) : HTTPData(), m_fp(NULL), m_path(path), m_len(0), m_chunked(false)
Yann 0:f30e2135b0db 30 {
Yann 0:f30e2135b0db 31
Yann 0:f30e2135b0db 32 }
Yann 0:f30e2135b0db 33
Yann 0:f30e2135b0db 34 HTTPFile::~HTTPFile()
Yann 0:f30e2135b0db 35 {
Yann 0:f30e2135b0db 36 closeFile();
Yann 0:f30e2135b0db 37 }
Yann 0:f30e2135b0db 38
Yann 0:f30e2135b0db 39 void HTTPFile::clear()
Yann 0:f30e2135b0db 40 {
Yann 0:f30e2135b0db 41 closeFile();
Yann 0:f30e2135b0db 42 //Force reopening
Yann 0:f30e2135b0db 43 }
Yann 0:f30e2135b0db 44
Yann 0:f30e2135b0db 45 int HTTPFile::read(char* buf, int len)
Yann 0:f30e2135b0db 46 {
Yann 0:f30e2135b0db 47 if(!openFile("r")) //File does not exist, or I/O error...
Yann 0:f30e2135b0db 48 return 0;
Yann 0:f30e2135b0db 49 len = fread(buf, 1, len, m_fp);
Yann 0:f30e2135b0db 50 if( feof(m_fp) )
Yann 0:f30e2135b0db 51 {
Yann 0:f30e2135b0db 52 //File read completely, we can close it
Yann 0:f30e2135b0db 53 closeFile();
Yann 0:f30e2135b0db 54 }
Yann 0:f30e2135b0db 55 return len;
Yann 0:f30e2135b0db 56 }
Yann 0:f30e2135b0db 57
Yann 0:f30e2135b0db 58 int HTTPFile::write(const char* buf, int len)
Yann 0:f30e2135b0db 59 {
Yann 0:f30e2135b0db 60 if(!openFile("w")) //File does not exist, or I/O error...
Yann 0:f30e2135b0db 61 return 0;
Yann 0:f30e2135b0db 62 len = fwrite(buf, 1, len, m_fp);
Yann 0:f30e2135b0db 63 DBG("Written %d bytes in %d\n", len, m_fp);
Yann 0:f30e2135b0db 64 if( (!m_chunked && (ftell(m_fp) >= m_len)) ||
Yann 0:f30e2135b0db 65 (m_chunked && !len) )
Yann 0:f30e2135b0db 66 {
Yann 0:f30e2135b0db 67 //File received completely, we can close it
Yann 0:f30e2135b0db 68 closeFile();
Yann 0:f30e2135b0db 69 }
Yann 0:f30e2135b0db 70 return len;
Yann 0:f30e2135b0db 71 }
Yann 0:f30e2135b0db 72
Yann 0:f30e2135b0db 73 string HTTPFile::getDataType() //Internet media type for Content-Type header
Yann 0:f30e2135b0db 74 {
Yann 0:f30e2135b0db 75 return ""; //Unknown
Yann 0:f30e2135b0db 76 }
Yann 0:f30e2135b0db 77
Yann 0:f30e2135b0db 78 void HTTPFile::setDataType(const string& type) //Internet media type from Content-Type header
Yann 0:f30e2135b0db 79 {
Yann 0:f30e2135b0db 80 //Do not really care here
Yann 0:f30e2135b0db 81 }
Yann 0:f30e2135b0db 82
Yann 0:f30e2135b0db 83 bool HTTPFile::getIsChunked() //For Transfer-Encoding header
Yann 0:f30e2135b0db 84 {
Yann 0:f30e2135b0db 85 return false;
Yann 0:f30e2135b0db 86 }
Yann 0:f30e2135b0db 87
Yann 0:f30e2135b0db 88 void HTTPFile::setIsChunked(bool chunked) //From Transfer-Encoding header
Yann 0:f30e2135b0db 89 {
Yann 0:f30e2135b0db 90 m_chunked = chunked;
Yann 0:f30e2135b0db 91 }
Yann 0:f30e2135b0db 92
Yann 0:f30e2135b0db 93 int HTTPFile::getDataLen() //For Content-Length header
Yann 0:f30e2135b0db 94 {
Yann 0:f30e2135b0db 95 return m_len;
Yann 0:f30e2135b0db 96 }
Yann 0:f30e2135b0db 97
Yann 0:f30e2135b0db 98 void HTTPFile::setDataLen(int len) //From Content-Length header, or if the transfer is chunked, next chunk length
Yann 0:f30e2135b0db 99 {
Yann 0:f30e2135b0db 100 if(!m_chunked)
Yann 0:f30e2135b0db 101 m_len = len; //Useful so that we can close file when last byte is written
Yann 0:f30e2135b0db 102 }
Yann 0:f30e2135b0db 103
Yann 0:f30e2135b0db 104 bool HTTPFile::openFile(const char* mode) //true on success, false otherwise
Yann 0:f30e2135b0db 105 {
Yann 0:f30e2135b0db 106 if(m_fp)
Yann 0:f30e2135b0db 107 return true;
Yann 0:f30e2135b0db 108
Yann 0:f30e2135b0db 109 m_fp = fopen(m_path.c_str(), mode);
Yann 0:f30e2135b0db 110 if(m_fp && mode[0]=='r')
Yann 0:f30e2135b0db 111 {
Yann 0:f30e2135b0db 112 //Seek EOF to get length
Yann 0:f30e2135b0db 113 fseek(m_fp, 0, SEEK_END);
Yann 0:f30e2135b0db 114 m_len = ftell(m_fp);
Yann 0:f30e2135b0db 115 fseek(m_fp, 0, SEEK_SET); //Goto SOF
Yann 0:f30e2135b0db 116 }
Yann 0:f30e2135b0db 117
Yann 0:f30e2135b0db 118 DBG("fd = %d\n", m_fp);
Yann 0:f30e2135b0db 119
Yann 0:f30e2135b0db 120 if(!m_fp)
Yann 0:f30e2135b0db 121 return false;
Yann 0:f30e2135b0db 122
Yann 0:f30e2135b0db 123 return true;
Yann 0:f30e2135b0db 124 }
Yann 0:f30e2135b0db 125
Yann 0:f30e2135b0db 126 void HTTPFile::closeFile()
Yann 0:f30e2135b0db 127 {
Yann 0:f30e2135b0db 128 if(m_fp)
Yann 0:f30e2135b0db 129 fclose(m_fp);
Yann 0:f30e2135b0db 130 }