Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 6:2eefa9ea0bf1, committed 2016-01-21
- Comitter:
- AkinoriHashimoto
- Date:
- Thu Jan 21 06:29:54 2016 +0000
- Parent:
- 5:a05c7662c51f
- Commit message:
- 1st publish.
Changed in this revision
--- a/HC05.cpp Mon Nov 02 07:22:50 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,114 +0,0 @@ -#include "HC05.h" - -HC05::HC05(PinName TX, PinName RX) - : hc05(TX, RX) -{ -} -void HC05::init(int baudrate, int bit, int parity, int stop, bool CRLN) -{ - hc05.baud(baudrate); - - // 力技 - if (parity == SerialBase::Odd) - hc05.format(bit, SerialBase::Odd, stop); - else if (parity == SerialBase::Even) - hc05.format(bit, SerialBase::Even, stop); // 8bit, NonParity, 1stopbit - else// if (parity == SerialBase::None) - hc05.format(bit, SerialBase::None, stop); // 8bit, NonParity, 1stopbit - - // attach rxIrq. because, rx buf of serial equals to 16 Bytes. - this->hc05.attach(this, &HC05::_readIrq, Serial::RxIrq); - - CR= '\r'; - if(CRLN) - CR= "\r\n"; - return; -} - -void HC05::setAttachRx(void (*fptr)(char)) -{ - // Serial.attachから呼ばれる_readIrq()内から、fptrをCall. - fptrRxIrq= fptr; - return; -} - -bool HC05::readable() -{ - return hc05.readable() || !ringBuf.empty(); -} - -string HC05::read() // public: -{ - _read(); - if(ringBuf.empty()) - return ""; - return ringBuf.get(); -} - -string HC05::getLine() -{ - string tmp= this->read(); - if(tmp.empty()) - return ""; - - // tmp is not empty. - int idx= tmp.rfind('\r'); - if(idx == string::npos) { - ringBuf.set(tmp); // 戻す - return ""; - } - - // find \r - if(tmp[++idx] == '\n') - idx++; - - // idxは改行後の文字先頭Indexを示す。 - string rtn= tmp.substr(0, idx); - tmp= tmp.substr(idx); - ringBuf.set(tmp); - return rtn; -} - -void HC05::sendLine(string str, bool addCR) -{ - if(addCR) - str += "\r\n"; - hc05.printf(str.c_str()); - return; -} - -int HC05::_putc(int val) // for printf() -{ - hc05.putc(val); - return val; -} - -int HC05::_getc() // for "Stream" -{ - return -1;//hc05.getc(); -} - -// Internal READ() -void HC05::_readIrq(void) -{ - if(!hc05.readable()) - return; - - char chr= hc05.getc(); - ringBuf.set(chr); - - if(this->fptrRxIrq == NULL) - return; - fptrRxIrq(chr); - - return; -} -void HC05::_read() -{ - // Bufferを吸い尽くす - while(hc05.readable()) - ringBuf.set((char)hc05.getc()); - return; // Bufferになくなった -} - -// EOF \ No newline at end of file
--- a/HC05.h Mon Nov 02 07:22:50 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,87 +0,0 @@ -/** Bluetooth module HC05 control class with using RTOS. - * - */ -/** - * @code -#include "mbed.h" -#include "HC05.h" - -DigitalOut led[]= {LED1, LED2, LED3, LED4}; -HC05 hc05(p9, p10); - -void rcvIrq(char id) -{ - led[0]= !led[0]; - if('2'<=id && id<='4') { // LED2 ~ 4 - id -= '1'; - led[id]= !led[id]; - } - return; -} - -int main() -{ - hc05.init(); - hc05.setAttachRx(rcvIrq); - while(true) { - hc05.sendLine(hc05.read()); -// hc05.sendLine(hc05.getLine()); - wait(10); - } -} -// EOF - * @endcode -*/ - -#pragma once - -#include "mbed.h" -#include <string> -#include "RingBuffer.h" - -/** Bluetooth module HC05 control class. - * - */ -class HC05 : public Stream -{ -public: - /** Create Serial port to HC05. - * @param TX, RX; Serial port. - */ - HC05(PinName tx, PinName rx); - - /** init - * @param baud-rate; Baud rate (bps). 9600, 38,400, 115,200, 230,400 - * @param bit, parity, stop; Default: 1Stopbit, NoneParity, 1StopBit. - * -- parity select; N(None), O(Odd), E(Even). - * @param CRLN; true -> CR&LN (\r\n), false -> CR only (\r). - */ - void init(int baudrate= 115200, int bit=8, int parity=SerialBase::None, int stop=1, bool CRLN=true); - - - void setAttachRx(void (*fptr)(char)); - bool readable(); - string read(); - string getLine(); - - void sendLine(string str, bool addCR=true); - -private: - RawSerial hc05; - string CR; - RingBuffer ringBuf; - - // RxIrq function pointer as out of class. - void (*fptrRxIrq)(char); - - // copy buf of rx to private string. - void _read(); // copy buf of rx to private string. - void _readIrq(); // copy buf of rx to private string. - - // virtual func for printf() in Stream-class. - virtual int _putc(int val); - virtual int _getc(); - -}; - -// EOF \ No newline at end of file
--- a/RingBuffer.lib Mon Nov 02 07:22:50 2015 +0000 +++ b/RingBuffer.lib Thu Jan 21 06:29:54 2016 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/AkinoriHashimoto/code/RingBuffer/#db4675083c8c +https://developer.mbed.org/users/AkinoriHashimoto/code/RingBuffer/#dced590a2d1b
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mySerial.cpp Thu Jan 21 06:29:54 2016 +0000 @@ -0,0 +1,138 @@ +#include "mySerial.h" + +mySerial::mySerial(PinName TX, PinName RX) + : serial(TX, RX) +{ +} +void mySerial::init(int baudrate, int bit, int parity, int stop, bool CRLN) +{ + serial.baud(baudrate); + + // 力技 + if (parity == SerialBase::Odd) + serial.format(bit, SerialBase::Odd, stop); + else if (parity == SerialBase::Even) + serial.format(bit, SerialBase::Even, stop); // 8bit, NonParity, 1stopbit + else// if (parity == SerialBase::None) + serial.format(bit, SerialBase::None, stop); // 8bit, NonParity, 1stopbit + + // attach rxIrq. because, rx buf of serial equals to 16 Bytes. + this->serial.attach(this, &mySerial::_readIrq, Serial::RxIrq); + // Tx Buf empty Interrupt. + this->serial.attach(this, &mySerial::putcIrq, Serial::TxIrq); + + CR= '\r'; + if(CRLN) + CR= "\r\n"; + return; +} + +void mySerial::setAttachRx(void (*fptr)(char)) +{ + // Serial.attachから呼ばれる_readIrq()内から、fptrをCall. + fptrRxIrq= fptr; + return; +} + +bool mySerial::readable() +{ + return serial.readable() || !ringBufRx.empty(); +} + +string mySerial::get() // public: +{ + _read(); + if(ringBufRx.empty()) + return ""; + return ringBufRx.get(); +} + +string mySerial::getLine() +{ + /* + string tmp= this->read(); + if(tmp.empty()) + return ""; + + // tmp is not empty. + int idx= tmp.rfind('\r'); + if(idx == string::npos) { + ringBufRx.set(tmp); // 戻す + return ""; + } + */ + + _read(); + // RingBuf is empty of doesm't find CR. + if(ringBufRx.empty() || !ringBufRx.chkCR()) + return ""; + + return ringBufRx.getLine(); + /* + string tmp= ringBufRx.get(); + // find \r + if(tmp[++idx] == '\n') + idx++; + + // idxは改行後の文字先頭Indexを示す。 + string rtn= tmp.substr(0, idx); + tmp= tmp.substr(idx); + ringBufRx.set(tmp); + return rtn; + */ +} + +void mySerial::sendLine(string str, bool addCR) +{ + if(addCR) + str += "\r\n"; + this->printf(str.c_str()); // _putc()が呼ばれるはず + return; +} + +int mySerial::_putc(int val) // for printf() +{ + ringBufTx.set(val); +// serial.putc(val); + putcIrq(); + return val; +} + +int mySerial::_getc() // for "Stream" +{ + return -1;//mySerial.getc(); +} + +void mySerial::putcIrq(void) +{ + if(!serial.writeable() || ringBufTx.empty()) + return; + + serial.putc(ringBufTx.getc()); + return; +} + +// Internal READ() +void mySerial::_readIrq(void) +{ + if(!serial.readable()) + return; + + char chr= serial.getc(); + ringBufRx.set(chr); + + if(this->fptrRxIrq == NULL) + return; + + fptrRxIrq(chr); + return; +} +void mySerial::_read() +{ + // Bufferを吸い尽くす + while(serial.readable()) + ringBufRx.set((char)serial.getc()); + return; // Bufferになくなった +} + +// EOF \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mySerial.h Thu Jan 21 06:29:54 2016 +0000 @@ -0,0 +1,89 @@ +/** Bluetooth module mySerial control class with using RTOS. + * + */ +/** + * @code +#include "mbed.h" +#include "mySerial.h" + +DigitalOut led[]= {LED1, LED2, LED3, LED4}; +mySerial pc(USBTX, USBRX); + +void rcvIrq(char id) +{ + led[0]= !led[0]; + if('2'<=id && id<='4') { // LED2 ~ 4 + id -= '1'; + led[id]= !led[id]; + } + return; +} + +int main() +{ + pc.init(); + pc.setAttachRx(rcvIrq); + while(true) { + pc.printf("@"); + pc.sendLine(pc.get()); + wait(10); + } +} +// EOF + * @endcode +*/ + +#pragma once + +#include "mbed.h" +#include <string> +#include "RingBuffer.h" + +/** Serial communicate class with Tx & Rx ringbuf. + * + */ +class mySerial : public Stream +{ +public: + /** Create Serial port to mySerial. + * @param TX, RX; Serial port. + */ + mySerial(PinName tx, PinName rx); + + /** init + * @param baud-rate; Baud rate (bps). 9600, 38,400, 115,200, 230,400 + * @param bit, parity, stop; Default: 1Stopbit, NoneParity, 1StopBit. + * -- parity select; N(None), O(Odd), E(Even). + * @param CRLN; true -> CR&LN (\r\n), false -> CR only (\r). + */ + void init(int baudrate= 115200, int bit=8, int parity=SerialBase::None, int stop=1, bool CRLN=true); + + + void setAttachRx(void (*fptr)(char)); + + bool readable(); + string get(); + string getLine(); + + void sendLine(string str, bool addCR=true); + +private: + RawSerial serial; + string CR; + RingBuffer ringBufRx; + RingBuffer ringBufTx; + + // RxIrq function pointer as out of class. + void (*fptrRxIrq)(char); + + // copy buf of rx to private string. + void _read(); // copy buf of rx to private string. + void _readIrq(); // copy buf of rx to private string. + + // virtual func for printf() in Stream-class. + virtual int _putc(int val); + virtual int _getc(); + void putcIrq(); +}; + +// EOF \ No newline at end of file