Mac addr added ICRS

Dependencies:   mbed

Fork of Email2Screen by Oliver Mattos

Committer:
je310
Date:
Wed Nov 20 21:22:22 2013 +0000
Revision:
1:b38b745d1ea8
Parent:
0:1619a6b826d7
This is a maced version of olivers code ICRS

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Hello1024 0:1619a6b826d7 1
Hello1024 0:1619a6b826d7 2 /*
Hello1024 0:1619a6b826d7 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
Hello1024 0:1619a6b826d7 4
Hello1024 0:1619a6b826d7 5 Permission is hereby granted, free of charge, to any person obtaining a copy
Hello1024 0:1619a6b826d7 6 of this software and associated documentation files (the "Software"), to deal
Hello1024 0:1619a6b826d7 7 in the Software without restriction, including without limitation the rights
Hello1024 0:1619a6b826d7 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Hello1024 0:1619a6b826d7 9 copies of the Software, and to permit persons to whom the Software is
Hello1024 0:1619a6b826d7 10 furnished to do so, subject to the following conditions:
Hello1024 0:1619a6b826d7 11
Hello1024 0:1619a6b826d7 12 The above copyright notice and this permission notice shall be included in
Hello1024 0:1619a6b826d7 13 all copies or substantial portions of the Software.
Hello1024 0:1619a6b826d7 14
Hello1024 0:1619a6b826d7 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Hello1024 0:1619a6b826d7 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Hello1024 0:1619a6b826d7 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Hello1024 0:1619a6b826d7 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Hello1024 0:1619a6b826d7 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Hello1024 0:1619a6b826d7 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Hello1024 0:1619a6b826d7 21 THE SOFTWARE.
Hello1024 0:1619a6b826d7 22 */
Hello1024 0:1619a6b826d7 23
Hello1024 0:1619a6b826d7 24 #include "SerialBuf.h"
Hello1024 0:1619a6b826d7 25 #include "mbed.h"
Hello1024 0:1619a6b826d7 26
Hello1024 0:1619a6b826d7 27 //#define __DEBUG
Hello1024 0:1619a6b826d7 28 #include "dbg/dbg.h"
Hello1024 0:1619a6b826d7 29
Hello1024 0:1619a6b826d7 30 #include "netCfg.h"
Hello1024 0:1619a6b826d7 31 #if NET_GPRS
Hello1024 0:1619a6b826d7 32
Hello1024 0:1619a6b826d7 33 #if NET_USB_SERIAL
Hello1024 0:1619a6b826d7 34 #define m_pStream( a ) (m_pSerial?m_pSerial->a:m_pUsbSerial->a)
Hello1024 0:1619a6b826d7 35 #else
Hello1024 0:1619a6b826d7 36 #define m_pStream( a ) (m_pSerial->a)
Hello1024 0:1619a6b826d7 37 #endif
Hello1024 0:1619a6b826d7 38
Hello1024 0:1619a6b826d7 39 //Circular buf
Hello1024 0:1619a6b826d7 40
Hello1024 0:1619a6b826d7 41 SerialCircularBuf::SerialCircularBuf(int len) : m_readMode(false)
Hello1024 0:1619a6b826d7 42 {
Hello1024 0:1619a6b826d7 43 m_buf = new char[len];
Hello1024 0:1619a6b826d7 44 m_len = len;
Hello1024 0:1619a6b826d7 45 m_pReadStart = m_pRead = m_buf;
Hello1024 0:1619a6b826d7 46 m_pWrite = m_buf;
Hello1024 0:1619a6b826d7 47 }
Hello1024 0:1619a6b826d7 48
Hello1024 0:1619a6b826d7 49 SerialCircularBuf::~SerialCircularBuf()
Hello1024 0:1619a6b826d7 50 {
Hello1024 0:1619a6b826d7 51 if(m_buf)
Hello1024 0:1619a6b826d7 52 delete[] m_buf;
Hello1024 0:1619a6b826d7 53 }
Hello1024 0:1619a6b826d7 54
Hello1024 0:1619a6b826d7 55 int SerialCircularBuf::room() //Return room available in buf
Hello1024 0:1619a6b826d7 56 {
Hello1024 0:1619a6b826d7 57 //return m_len - len() - 1; //-1 is to avoid loop
Hello1024 0:1619a6b826d7 58 if ( m_pReadStart > m_pWrite )
Hello1024 0:1619a6b826d7 59 return ( m_pReadStart - m_pWrite - 1 );
Hello1024 0:1619a6b826d7 60 else
Hello1024 0:1619a6b826d7 61 return m_len - ( m_pWrite - m_pReadStart ) - 1;
Hello1024 0:1619a6b826d7 62 }
Hello1024 0:1619a6b826d7 63
Hello1024 0:1619a6b826d7 64 int SerialCircularBuf::len() //Return chars length in buf
Hello1024 0:1619a6b826d7 65 {
Hello1024 0:1619a6b826d7 66 if ( m_pWrite >= m_pRead )
Hello1024 0:1619a6b826d7 67 return ( m_pWrite - m_pRead );
Hello1024 0:1619a6b826d7 68 else
Hello1024 0:1619a6b826d7 69 return m_len - ( m_pRead - m_pWrite ); // = ( ( m_buf + m_len) - m_pRead ) + ( m_pWrite - m_buf )
Hello1024 0:1619a6b826d7 70 }
Hello1024 0:1619a6b826d7 71
Hello1024 0:1619a6b826d7 72 void SerialCircularBuf::write(char c)
Hello1024 0:1619a6b826d7 73 {
Hello1024 0:1619a6b826d7 74 #if 0
Hello1024 0:1619a6b826d7 75 if(!room())
Hello1024 0:1619a6b826d7 76 return;
Hello1024 0:1619a6b826d7 77 #endif
Hello1024 0:1619a6b826d7 78 //WARN: Must call room() before
Hello1024 0:1619a6b826d7 79 *m_pWrite = c;
Hello1024 0:1619a6b826d7 80 m_pWrite++;
Hello1024 0:1619a6b826d7 81 if(m_pWrite>=m_buf+m_len)
Hello1024 0:1619a6b826d7 82 m_pWrite=m_buf;
Hello1024 0:1619a6b826d7 83 }
Hello1024 0:1619a6b826d7 84 char SerialCircularBuf::read()
Hello1024 0:1619a6b826d7 85 {
Hello1024 0:1619a6b826d7 86 #if 0
Hello1024 0:1619a6b826d7 87 if(!len())
Hello1024 0:1619a6b826d7 88 return 0;
Hello1024 0:1619a6b826d7 89 #endif
Hello1024 0:1619a6b826d7 90 //WARN: Must call len() before
Hello1024 0:1619a6b826d7 91 char c = *m_pRead;
Hello1024 0:1619a6b826d7 92 m_pRead++;
Hello1024 0:1619a6b826d7 93
Hello1024 0:1619a6b826d7 94 if(m_pRead>=m_buf+m_len)
Hello1024 0:1619a6b826d7 95 m_pRead=m_buf;
Hello1024 0:1619a6b826d7 96
Hello1024 0:1619a6b826d7 97 if(!m_readMode) //If readmode=false, trash this char
Hello1024 0:1619a6b826d7 98 m_pReadStart=m_pRead;
Hello1024 0:1619a6b826d7 99
Hello1024 0:1619a6b826d7 100 return c;
Hello1024 0:1619a6b826d7 101 }
Hello1024 0:1619a6b826d7 102
Hello1024 0:1619a6b826d7 103 void SerialCircularBuf::setReadMode(bool readMode) //If true, keeps chars in buf when read, false by default
Hello1024 0:1619a6b826d7 104 {
Hello1024 0:1619a6b826d7 105 if(m_readMode == true && readMode == false)
Hello1024 0:1619a6b826d7 106 {
Hello1024 0:1619a6b826d7 107 //Trash bytes that have been read
Hello1024 0:1619a6b826d7 108 flushRead();
Hello1024 0:1619a6b826d7 109 }
Hello1024 0:1619a6b826d7 110 m_readMode = readMode;
Hello1024 0:1619a6b826d7 111 }
Hello1024 0:1619a6b826d7 112
Hello1024 0:1619a6b826d7 113 void SerialCircularBuf::flushRead() //Delete chars that have been read & return chars len (only useful with readMode = true)
Hello1024 0:1619a6b826d7 114 {
Hello1024 0:1619a6b826d7 115 m_pReadStart = m_pRead;
Hello1024 0:1619a6b826d7 116 }
Hello1024 0:1619a6b826d7 117
Hello1024 0:1619a6b826d7 118 void SerialCircularBuf::resetRead() //Go back to initial read position & return chars len (only useful with readMode = true)
Hello1024 0:1619a6b826d7 119 {
Hello1024 0:1619a6b826d7 120 m_pRead = m_pReadStart;
Hello1024 0:1619a6b826d7 121 }
Hello1024 0:1619a6b826d7 122
Hello1024 0:1619a6b826d7 123 //SerialBuf
Hello1024 0:1619a6b826d7 124
Hello1024 0:1619a6b826d7 125 SerialBuf::SerialBuf(int len) : m_rxBuf(len), m_txBuf(len), m_pSerial(NULL) //Buffer length
Hello1024 0:1619a6b826d7 126 #if NET_USB_SERIAL
Hello1024 0:1619a6b826d7 127 , m_pUsbSerial(NULL)
Hello1024 0:1619a6b826d7 128 #endif
Hello1024 0:1619a6b826d7 129 {
Hello1024 0:1619a6b826d7 130 DBG("New Serial buf@%p\n", this);
Hello1024 0:1619a6b826d7 131 }
Hello1024 0:1619a6b826d7 132
Hello1024 0:1619a6b826d7 133 SerialBuf::~SerialBuf()
Hello1024 0:1619a6b826d7 134 {
Hello1024 0:1619a6b826d7 135
Hello1024 0:1619a6b826d7 136 }
Hello1024 0:1619a6b826d7 137
Hello1024 0:1619a6b826d7 138 void SerialBuf::attach(Serial* pSerial)
Hello1024 0:1619a6b826d7 139 {
Hello1024 0:1619a6b826d7 140 DBG("Serial buf@%p in attach\n", this);
Hello1024 0:1619a6b826d7 141 m_pSerial = pSerial;
Hello1024 0:1619a6b826d7 142 m_pSerial->attach<SerialBuf>(this, &SerialBuf::onRxInterrupt, Serial::RxIrq);
Hello1024 0:1619a6b826d7 143 m_pSerial->attach<SerialBuf>(this, &SerialBuf::onTxInterrupt, Serial::TxIrq);
Hello1024 0:1619a6b826d7 144 onRxInterrupt(); //Read data
Hello1024 0:1619a6b826d7 145 }
Hello1024 0:1619a6b826d7 146
Hello1024 0:1619a6b826d7 147 void SerialBuf::detach()
Hello1024 0:1619a6b826d7 148 {
Hello1024 0:1619a6b826d7 149 if(m_pSerial)
Hello1024 0:1619a6b826d7 150 {
Hello1024 0:1619a6b826d7 151 m_pSerial->attach<SerialBuf>(NULL, NULL, Serial::RxIrq);
Hello1024 0:1619a6b826d7 152 m_pSerial->attach<SerialBuf>(NULL, NULL, Serial::TxIrq);
Hello1024 0:1619a6b826d7 153 m_pSerial = NULL;
Hello1024 0:1619a6b826d7 154 }
Hello1024 0:1619a6b826d7 155 #if NET_USB_SERIAL
Hello1024 0:1619a6b826d7 156 else if(m_pUsbSerial)
Hello1024 0:1619a6b826d7 157 {
Hello1024 0:1619a6b826d7 158 m_pUsbSerial->attach<SerialBuf>(NULL, NULL, UsbSerial::RxIrq);
Hello1024 0:1619a6b826d7 159 m_pUsbSerial->attach<SerialBuf>(NULL, NULL, UsbSerial::TxIrq);
Hello1024 0:1619a6b826d7 160 m_pUsbSerial = NULL;
Hello1024 0:1619a6b826d7 161 }
Hello1024 0:1619a6b826d7 162 #endif
Hello1024 0:1619a6b826d7 163 }
Hello1024 0:1619a6b826d7 164
Hello1024 0:1619a6b826d7 165 #if NET_USB_SERIAL
Hello1024 0:1619a6b826d7 166 void SerialBuf::attach(UsbSerial* pUsbSerial)
Hello1024 0:1619a6b826d7 167 {
Hello1024 0:1619a6b826d7 168 m_pUsbSerial = pUsbSerial;
Hello1024 0:1619a6b826d7 169 m_pUsbSerial->attach<SerialBuf>(this, &SerialBuf::onRxInterrupt, UsbSerial::RxIrq);
Hello1024 0:1619a6b826d7 170 m_pUsbSerial->attach<SerialBuf>(this, &SerialBuf::onTxInterrupt, UsbSerial::TxIrq);
Hello1024 0:1619a6b826d7 171 onRxInterrupt(); //Read data
Hello1024 0:1619a6b826d7 172 }
Hello1024 0:1619a6b826d7 173 #endif
Hello1024 0:1619a6b826d7 174
Hello1024 0:1619a6b826d7 175 char SerialBuf::getc()
Hello1024 0:1619a6b826d7 176 {
Hello1024 0:1619a6b826d7 177 while(!readable());
Hello1024 0:1619a6b826d7 178 char c = m_rxBuf.read();
Hello1024 0:1619a6b826d7 179 return c;
Hello1024 0:1619a6b826d7 180 }
Hello1024 0:1619a6b826d7 181
Hello1024 0:1619a6b826d7 182 void SerialBuf::putc(char c)
Hello1024 0:1619a6b826d7 183 {
Hello1024 0:1619a6b826d7 184 while(!writeable());
Hello1024 0:1619a6b826d7 185 m_txBuf.write(c);
Hello1024 0:1619a6b826d7 186 onTxInterrupt();
Hello1024 0:1619a6b826d7 187 }
Hello1024 0:1619a6b826d7 188
Hello1024 0:1619a6b826d7 189 bool SerialBuf::readable()
Hello1024 0:1619a6b826d7 190 {
Hello1024 0:1619a6b826d7 191 if( !m_rxBuf.len() ) //Fill buf if possible
Hello1024 0:1619a6b826d7 192 onRxInterrupt();
Hello1024 0:1619a6b826d7 193 return (m_rxBuf.len() > 0);
Hello1024 0:1619a6b826d7 194 }
Hello1024 0:1619a6b826d7 195
Hello1024 0:1619a6b826d7 196 bool SerialBuf::writeable()
Hello1024 0:1619a6b826d7 197 {
Hello1024 0:1619a6b826d7 198 if( !m_txBuf.room() ) //Free buf is possible
Hello1024 0:1619a6b826d7 199 onTxInterrupt();
Hello1024 0:1619a6b826d7 200 return (m_txBuf.room() > 0);
Hello1024 0:1619a6b826d7 201 }
Hello1024 0:1619a6b826d7 202
Hello1024 0:1619a6b826d7 203 void SerialBuf::setReadMode(bool readMode) //If true, keeps chars in buf when read, false by default
Hello1024 0:1619a6b826d7 204 {
Hello1024 0:1619a6b826d7 205 m_rxBuf.setReadMode(readMode);
Hello1024 0:1619a6b826d7 206 }
Hello1024 0:1619a6b826d7 207
Hello1024 0:1619a6b826d7 208 void SerialBuf::flushRead() //Delete chars that have been read & return chars len (only useful with readMode = true)
Hello1024 0:1619a6b826d7 209 {
Hello1024 0:1619a6b826d7 210 m_rxBuf.flushRead();
Hello1024 0:1619a6b826d7 211 }
Hello1024 0:1619a6b826d7 212
Hello1024 0:1619a6b826d7 213 void SerialBuf::resetRead() //Go back to initial read position & return chars len (only useful with readMode = true)
Hello1024 0:1619a6b826d7 214 {
Hello1024 0:1619a6b826d7 215 m_rxBuf.resetRead();
Hello1024 0:1619a6b826d7 216 }
Hello1024 0:1619a6b826d7 217
Hello1024 0:1619a6b826d7 218 void SerialBuf::onRxInterrupt() //Callback from m_pSerial
Hello1024 0:1619a6b826d7 219 {
Hello1024 0:1619a6b826d7 220 while( m_rxBuf.room() && m_pStream(readable()) )
Hello1024 0:1619a6b826d7 221 {
Hello1024 0:1619a6b826d7 222 m_rxBuf.write(m_pStream(getc()));
Hello1024 0:1619a6b826d7 223 }
Hello1024 0:1619a6b826d7 224 }
Hello1024 0:1619a6b826d7 225
Hello1024 0:1619a6b826d7 226 void SerialBuf::onTxInterrupt() //Callback from m_pSerial
Hello1024 0:1619a6b826d7 227 {
Hello1024 0:1619a6b826d7 228 while( m_txBuf.len() && m_pStream(writeable()) )
Hello1024 0:1619a6b826d7 229 {
Hello1024 0:1619a6b826d7 230 m_pStream(putc(m_txBuf.read()));
Hello1024 0:1619a6b826d7 231 }
Hello1024 0:1619a6b826d7 232 }
Hello1024 0:1619a6b826d7 233
Hello1024 0:1619a6b826d7 234
Hello1024 0:1619a6b826d7 235 #endif