Library for using the xbee-compatible Bluetooth Bee Module on an m3pi.

Dependencies:   SensoryTest

Fork of btbee by Nikolas Goldin

Committer:
ngoldin
Date:
Wed Feb 05 11:18:48 2014 +0000
Revision:
4:3cdbefbd2409
Parent:
3:ddeb620fb25e
fixed read_line. hopefully.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ngoldin 0:e7cb710c8900 1 #include "btbee.h"
ngoldin 0:e7cb710c8900 2
ngoldin 0:e7cb710c8900 3 btbee::btbee(PinName respin, PinName tx, PinName rx) :
ngoldin 0:e7cb710c8900 4 Serial(tx, rx) , reset_out(respin)
ngoldin 0:e7cb710c8900 5 {
ngoldin 2:12c38a710982 6 reset_out.write(1);
ngoldin 2:12c38a710982 7 baud(DEFAULT_BAUD);
ngoldin 0:e7cb710c8900 8 }
ngoldin 0:e7cb710c8900 9
ngoldin 0:e7cb710c8900 10 btbee::btbee( ) :
ngoldin 0:e7cb710c8900 11 Serial(p28,p27), reset_out(p26)
ngoldin 0:e7cb710c8900 12 {
ngoldin 0:e7cb710c8900 13 reset_out.write(1);
ngoldin 2:12c38a710982 14 baud(DEFAULT_BAUD);
ngoldin 0:e7cb710c8900 15 }
ngoldin 0:e7cb710c8900 16
ngoldin 0:e7cb710c8900 17 void btbee::reset(void){
ngoldin 0:e7cb710c8900 18 reset_out.write(0);
ngoldin 0:e7cb710c8900 19 wait(0.01);
ngoldin 0:e7cb710c8900 20 reset_out.write(1);
ngoldin 0:e7cb710c8900 21 }
ngoldin 0:e7cb710c8900 22
ngoldin 2:12c38a710982 23 void btbee::at_baud(void){
ngoldin 2:12c38a710982 24 baud(AT_BAUD);
ngoldin 2:12c38a710982 25 }
ngoldin 2:12c38a710982 26
ngoldin 2:12c38a710982 27 void btbee::factory_baud(void){
ngoldin 2:12c38a710982 28 baud(FACTORY_BAUD);
ngoldin 2:12c38a710982 29 }
ngoldin 2:12c38a710982 30
ngoldin 2:12c38a710982 31 void btbee::default_baud(void){
ngoldin 2:12c38a710982 32 baud(DEFAULT_BAUD);
ngoldin 2:12c38a710982 33 }
ngoldin 1:56f437e4d9e0 34
ngoldin 1:56f437e4d9e0 35 /* Read from the serial as long as it is readable.
ngoldin 1:56f437e4d9e0 36 * Params: pointer to char array for the return,
ngoldin 1:56f437e4d9e0 37 * int containing the length of the char array
ngoldin 1:56f437e4d9e0 38 * pointer to int for return of chars read
ngoldin 1:56f437e4d9e0 39 * Return: 1 if ok, 0 if array full but more there to read
ngoldin 1:56f437e4d9e0 40 */
ngoldin 1:56f437e4d9e0 41 int btbee::read_all(char * arr, const int len, int * chars_read){
ngoldin 1:56f437e4d9e0 42 int pos=0;
ngoldin 1:56f437e4d9e0 43 while (readable()){
ngoldin 1:56f437e4d9e0 44 if (pos==len){return 0;}
ngoldin 1:56f437e4d9e0 45 arr[pos]=getc();
ngoldin 1:56f437e4d9e0 46 pos++;
ngoldin 1:56f437e4d9e0 47 *chars_read = pos;
ngoldin 1:56f437e4d9e0 48 }
ngoldin 1:56f437e4d9e0 49 return 1;
ngoldin 1:56f437e4d9e0 50 }
ngoldin 1:56f437e4d9e0 51
ngoldin 3:ddeb620fb25e 52 /* Read from the serial as long as no LF (\n) char has been read.
ngoldin 3:ddeb620fb25e 53 * WARNING: This will block your program if no LF is received.
ngoldin 3:ddeb620fb25e 54 * Params: pointer to char array for the return,
ngoldin 3:ddeb620fb25e 55 * int containing the length of the char array
ngoldin 3:ddeb620fb25e 56 * pointer to int for return of chars read
ngoldin 3:ddeb620fb25e 57 * Return: 1 if ok, 0 if array full but more there to read
ngoldin 3:ddeb620fb25e 58 */
ngoldin 3:ddeb620fb25e 59 int btbee::read_line(char * arr, const int len, int * chars_read){
ngoldin 3:ddeb620fb25e 60 int pos=0;
ngoldin 3:ddeb620fb25e 61 do
ngoldin 3:ddeb620fb25e 62 {
ngoldin 4:3cdbefbd2409 63 while(!readable()){} //wait until readable
ngoldin 4:3cdbefbd2409 64 if (pos==len){return 0;}
ngoldin 3:ddeb620fb25e 65 arr[pos]=getc();
ngoldin 3:ddeb620fb25e 66 pos++;
ngoldin 3:ddeb620fb25e 67 *chars_read = pos;
ngoldin 3:ddeb620fb25e 68 }
ngoldin 4:3cdbefbd2409 69 while (!(arr[pos-1]=='\n'));
ngoldin 3:ddeb620fb25e 70 return 1;
ngoldin 3:ddeb620fb25e 71 }