Sensor reporting over USB CDC

Dependencies:   MAG3110 MMA8451Q SLCD- TSI USBDevice mbed

Committer:
wue
Date:
Wed Apr 16 12:20:12 2014 +0000
Revision:
0:7b58cdacf811
Sensor reporting over USB CDC

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wue 0:7b58cdacf811 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
wue 0:7b58cdacf811 2 *
wue 0:7b58cdacf811 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
wue 0:7b58cdacf811 4 * and associated documentation files (the "Software"), to deal in the Software without
wue 0:7b58cdacf811 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
wue 0:7b58cdacf811 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
wue 0:7b58cdacf811 7 * Software is furnished to do so, subject to the following conditions:
wue 0:7b58cdacf811 8 *
wue 0:7b58cdacf811 9 * The above copyright notice and this permission notice shall be included in all copies or
wue 0:7b58cdacf811 10 * substantial portions of the Software.
wue 0:7b58cdacf811 11 *
wue 0:7b58cdacf811 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
wue 0:7b58cdacf811 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
wue 0:7b58cdacf811 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
wue 0:7b58cdacf811 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
wue 0:7b58cdacf811 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
wue 0:7b58cdacf811 17 */
wue 0:7b58cdacf811 18
wue 0:7b58cdacf811 19 #include "stdint.h"
wue 0:7b58cdacf811 20 #include "USBSerial.h"
wue 0:7b58cdacf811 21
wue 0:7b58cdacf811 22 int USBSerial::_putc(int c) {
wue 0:7b58cdacf811 23 if (!terminal_connected)
wue 0:7b58cdacf811 24 return 0;
wue 0:7b58cdacf811 25 send((uint8_t *)&c, 1);
wue 0:7b58cdacf811 26 return 1;
wue 0:7b58cdacf811 27 }
wue 0:7b58cdacf811 28
wue 0:7b58cdacf811 29 int USBSerial::_getc() {
wue 0:7b58cdacf811 30 uint8_t c = 0;
wue 0:7b58cdacf811 31 while (buf.isEmpty());
wue 0:7b58cdacf811 32 buf.dequeue(&c);
wue 0:7b58cdacf811 33 return c;
wue 0:7b58cdacf811 34 }
wue 0:7b58cdacf811 35
wue 0:7b58cdacf811 36
wue 0:7b58cdacf811 37 bool USBSerial::writeBlock(uint8_t * buf, uint16_t size) {
wue 0:7b58cdacf811 38 if(size > MAX_PACKET_SIZE_EPBULK) {
wue 0:7b58cdacf811 39 return false;
wue 0:7b58cdacf811 40 }
wue 0:7b58cdacf811 41 if(!send(buf, size)) {
wue 0:7b58cdacf811 42 return false;
wue 0:7b58cdacf811 43 }
wue 0:7b58cdacf811 44 return true;
wue 0:7b58cdacf811 45 }
wue 0:7b58cdacf811 46
wue 0:7b58cdacf811 47
wue 0:7b58cdacf811 48
wue 0:7b58cdacf811 49 bool USBSerial::EP2_OUT_callback() {
wue 0:7b58cdacf811 50 uint8_t c[65];
wue 0:7b58cdacf811 51 uint32_t size = 0;
wue 0:7b58cdacf811 52
wue 0:7b58cdacf811 53 //we read the packet received and put it on the circular buffer
wue 0:7b58cdacf811 54 readEP(c, &size);
wue 0:7b58cdacf811 55 for (uint32_t i = 0; i < size; i++) {
wue 0:7b58cdacf811 56 buf.queue(c[i]);
wue 0:7b58cdacf811 57 }
wue 0:7b58cdacf811 58
wue 0:7b58cdacf811 59 //call a potential handler
wue 0:7b58cdacf811 60 rx.call();
wue 0:7b58cdacf811 61
wue 0:7b58cdacf811 62 // We reactivate the endpoint to receive next characters
wue 0:7b58cdacf811 63 readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
wue 0:7b58cdacf811 64 return true;
wue 0:7b58cdacf811 65 }
wue 0:7b58cdacf811 66
wue 0:7b58cdacf811 67 uint8_t USBSerial::available() {
wue 0:7b58cdacf811 68 return buf.available();
wue 0:7b58cdacf811 69 }