Pulse Oximeter (NONIN) communicates with mbed via Bluetooth dongle and sends Heart Rate and Oxygen Saturation via GPRS module
Dependencies: C12832 GPS GSM mbed
Fork of myBlueUSB_localfix by
btserial.cpp@0:003889bc474f, 2013-12-07 (annotated)
- Committer:
- nobukuma
- Date:
- Sat Dec 07 14:19:00 2013 +0000
- Revision:
- 0:003889bc474f
- Child:
- 3:55a622e3dbb5
http://mbed.org/users/networker/code/myBlueUSB/ rev13??rev12??????????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nobukuma | 0:003889bc474f | 1 | #include "btserial.h" |
nobukuma | 0:003889bc474f | 2 | |
nobukuma | 0:003889bc474f | 3 | btserial::btserial(char ba[6], char ch) { |
nobukuma | 0:003889bc474f | 4 | L2CAPAddr a; |
nobukuma | 0:003889bc474f | 5 | memcpy(&a.bdaddr, ba, 6); |
nobukuma | 0:003889bc474f | 6 | a.psm = ch; |
nobukuma | 0:003889bc474f | 7 | sendptr = 0; |
nobukuma | 0:003889bc474f | 8 | recptrin = 0; |
nobukuma | 0:003889bc474f | 9 | recptrout = bufsize - 1; |
nobukuma | 0:003889bc474f | 10 | free = bufsize; |
nobukuma | 0:003889bc474f | 11 | sock = Socket_Open(SOCKET_RFCOM, &a.hdr, cb, this); |
nobukuma | 0:003889bc474f | 12 | } |
nobukuma | 0:003889bc474f | 13 | |
nobukuma | 0:003889bc474f | 14 | btserial::btserial(char ch) { |
nobukuma | 0:003889bc474f | 15 | sendptr = 0; |
nobukuma | 0:003889bc474f | 16 | recptrin = 0; |
nobukuma | 0:003889bc474f | 17 | recptrout = bufsize - 1; |
nobukuma | 0:003889bc474f | 18 | free = bufsize; |
nobukuma | 0:003889bc474f | 19 | sock = Socket_Listen(SOCKET_RFCOM, ch, cb, this); |
nobukuma | 0:003889bc474f | 20 | } |
nobukuma | 0:003889bc474f | 21 | |
nobukuma | 0:003889bc474f | 22 | void btserial::cb(int socket, SocketState state, const unsigned char *data, int len, void* userData) { |
nobukuma | 0:003889bc474f | 23 | btserial *self = (btserial*)userData; |
nobukuma | 0:003889bc474f | 24 | if (state == SocketState_Open) |
nobukuma | 0:003889bc474f | 25 | if (len > 0) |
nobukuma | 0:003889bc474f | 26 | self->stash(data, len); |
nobukuma | 0:003889bc474f | 27 | else { |
nobukuma | 0:003889bc474f | 28 | //Socket_GetOpt(sock, SO_RECBUF, &recbufsize, sizeof(recbufsize)); |
nobukuma | 0:003889bc474f | 29 | //Socket_GetOpt(sock, SO_SNDBUF, &sndbufsize, sizeof(sndbufsize)); |
nobukuma | 0:003889bc474f | 30 | port_settings ps;//defaults are ok |
nobukuma | 0:003889bc474f | 31 | ps.baud = 3; //9600 baud |
nobukuma | 0:003889bc474f | 32 | ps.bits = 3;//8 bits |
nobukuma | 0:003889bc474f | 33 | ps.stop = 0;//1 bit |
nobukuma | 0:003889bc474f | 34 | ps.par = 0; //no parity |
nobukuma | 0:003889bc474f | 35 | ps.mask = MASK_BITRATE|MASK_DATABITS|MASK_STOPBITS|MASK_PARITYBITS; |
nobukuma | 0:003889bc474f | 36 | //set_remote_port_parameters(sock, &ps); |
nobukuma | 0:003889bc474f | 37 | self->open = true; |
nobukuma | 0:003889bc474f | 38 | } |
nobukuma | 0:003889bc474f | 39 | else if (state == SocketState_Closed) |
nobukuma | 0:003889bc474f | 40 | self->open = false; |
nobukuma | 0:003889bc474f | 41 | } |
nobukuma | 0:003889bc474f | 42 | |
nobukuma | 0:003889bc474f | 43 | void btserial::stash(const unsigned char *data, int len) { |
nobukuma | 0:003889bc474f | 44 | int i = 0; |
nobukuma | 0:003889bc474f | 45 | while (i < len && free>0) { |
nobukuma | 0:003889bc474f | 46 | recbuf[recptrin++] = data[i++]; |
nobukuma | 0:003889bc474f | 47 | if (recptrin == bufsize) recptrin = 0; |
nobukuma | 0:003889bc474f | 48 | free--; |
nobukuma | 0:003889bc474f | 49 | } |
nobukuma | 0:003889bc474f | 50 | } |
nobukuma | 0:003889bc474f | 51 | |
nobukuma | 0:003889bc474f | 52 | int btserial::getc() { |
nobukuma | 0:003889bc474f | 53 | if (free == bufsize || !open) |
nobukuma | 0:003889bc474f | 54 | return -1; |
nobukuma | 0:003889bc474f | 55 | free++; |
nobukuma | 0:003889bc474f | 56 | recptrout++; |
nobukuma | 0:003889bc474f | 57 | if (recptrout == bufsize) recptrout = 0; |
nobukuma | 0:003889bc474f | 58 | return recbuf[recptrout]; |
nobukuma | 0:003889bc474f | 59 | } |
nobukuma | 0:003889bc474f | 60 | |
nobukuma | 0:003889bc474f | 61 | int btserial::putc(int c) { |
nobukuma | 0:003889bc474f | 62 | if (sendptr==bufsize || !open) |
nobukuma | 0:003889bc474f | 63 | return -1; |
nobukuma | 0:003889bc474f | 64 | sendbuf[sendptr++] = c; |
nobukuma | 0:003889bc474f | 65 | if (sendptr==bufsize || c=='\n' || c=='\r') { |
nobukuma | 0:003889bc474f | 66 | Socket_Send(sock, sendbuf, sendptr); |
nobukuma | 0:003889bc474f | 67 | sendptr = 0; |
nobukuma | 0:003889bc474f | 68 | } |
nobukuma | 0:003889bc474f | 69 | return c; |
nobukuma | 0:003889bc474f | 70 | } |
nobukuma | 0:003889bc474f | 71 | |
nobukuma | 0:003889bc474f | 72 | void btserial::baud(int br) { |
nobukuma | 0:003889bc474f | 73 | int rates[] = {2400,4800,7200,9600,19200,38400,57600,115200,230400}; |
nobukuma | 0:003889bc474f | 74 | if (!open) return; |
nobukuma | 0:003889bc474f | 75 | for (int i = 0; i < sizeof(rates)/sizeof(int);i++) |
nobukuma | 0:003889bc474f | 76 | if (rates[i] == br) { |
nobukuma | 0:003889bc474f | 77 | port_settings ps; |
nobukuma | 0:003889bc474f | 78 | ps.baud = i; |
nobukuma | 0:003889bc474f | 79 | ps.mask = MASK_BITRATE; |
nobukuma | 0:003889bc474f | 80 | set_remote_port_parameters(sock, &ps); |
nobukuma | 0:003889bc474f | 81 | return; |
nobukuma | 0:003889bc474f | 82 | } |
nobukuma | 0:003889bc474f | 83 | printf("Illegal baudrate requested %d\n", br); |
nobukuma | 0:003889bc474f | 84 | } |
nobukuma | 0:003889bc474f | 85 | |
nobukuma | 0:003889bc474f | 86 | void btserial::format(int bits, Serial::Parity par, int stop) { |
nobukuma | 0:003889bc474f | 87 | if (!open) return; |
nobukuma | 0:003889bc474f | 88 | port_settings ps; |
nobukuma | 0:003889bc474f | 89 | ps.bits = bits-5; |
nobukuma | 0:003889bc474f | 90 | ps.stop = stop-1; |
nobukuma | 0:003889bc474f | 91 | switch (par) { |
nobukuma | 0:003889bc474f | 92 | case Serial::None: |
nobukuma | 0:003889bc474f | 93 | ps.par = 0; |
nobukuma | 0:003889bc474f | 94 | ps.par_t = 0; |
nobukuma | 0:003889bc474f | 95 | break; |
nobukuma | 0:003889bc474f | 96 | case Serial::Odd: |
nobukuma | 0:003889bc474f | 97 | ps.par = 1; |
nobukuma | 0:003889bc474f | 98 | ps.par_t = 0; |
nobukuma | 0:003889bc474f | 99 | break; |
nobukuma | 0:003889bc474f | 100 | case Serial::Even: |
nobukuma | 0:003889bc474f | 101 | ps.par = 1; |
nobukuma | 0:003889bc474f | 102 | ps.par_t = 1; |
nobukuma | 0:003889bc474f | 103 | break; |
nobukuma | 0:003889bc474f | 104 | case Serial::Forced0: |
nobukuma | 0:003889bc474f | 105 | ps.par = 1; |
nobukuma | 0:003889bc474f | 106 | ps.par_t = 3; |
nobukuma | 0:003889bc474f | 107 | break; |
nobukuma | 0:003889bc474f | 108 | case Serial::Forced1: |
nobukuma | 0:003889bc474f | 109 | ps.par = 1; |
nobukuma | 0:003889bc474f | 110 | ps.par_t = 2; |
nobukuma | 0:003889bc474f | 111 | break; |
nobukuma | 0:003889bc474f | 112 | } |
nobukuma | 0:003889bc474f | 113 | ps.mask = MASK_DATABITS|MASK_STOPBITS|MASK_PARITYBITS|MASK_PARITYTYPE; |
nobukuma | 0:003889bc474f | 114 | set_remote_port_parameters(sock, &ps); |
nobukuma | 0:003889bc474f | 115 | } |
nobukuma | 0:003889bc474f | 116 |