A Composite device with USBSerial and USBKeyboard

Dependents:   USBSerialKeyboard_Example

Committer:
p3p
Date:
Sat Mar 30 12:27:31 2013 +0000
Revision:
1:9367d07d0707
Parent:
0:93ff83b03fd8
Initial Release

Who changed what in which revision?

UserRevisionLine numberNew contents of line
p3p 0:93ff83b03fd8 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
p3p 0:93ff83b03fd8 2 *
p3p 0:93ff83b03fd8 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
p3p 0:93ff83b03fd8 4 * and associated documentation files (the "Software"), to deal in the Software without
p3p 0:93ff83b03fd8 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
p3p 0:93ff83b03fd8 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
p3p 0:93ff83b03fd8 7 * Software is furnished to do so, subject to the following conditions:
p3p 0:93ff83b03fd8 8 *
p3p 0:93ff83b03fd8 9 * The above copyright notice and this permission notice shall be included in all copies or
p3p 0:93ff83b03fd8 10 * substantial portions of the Software.
p3p 0:93ff83b03fd8 11 *
p3p 0:93ff83b03fd8 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
p3p 0:93ff83b03fd8 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
p3p 0:93ff83b03fd8 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
p3p 0:93ff83b03fd8 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
p3p 0:93ff83b03fd8 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
p3p 0:93ff83b03fd8 17 */
p3p 0:93ff83b03fd8 18
p3p 0:93ff83b03fd8 19 #ifndef CIRCBUFFER_H
p3p 0:93ff83b03fd8 20 #define CIRCBUFFER_H
p3p 0:93ff83b03fd8 21
p3p 0:93ff83b03fd8 22 template <class T>
p3p 0:93ff83b03fd8 23 class CircBuffer {
p3p 0:93ff83b03fd8 24 public:
p3p 0:93ff83b03fd8 25 CircBuffer(int length) {
p3p 0:93ff83b03fd8 26 write = 0;
p3p 0:93ff83b03fd8 27 read = 0;
p3p 0:93ff83b03fd8 28 size = length + 1;
p3p 0:93ff83b03fd8 29 buf = (T *)malloc(size * sizeof(T));
p3p 0:93ff83b03fd8 30 };
p3p 0:93ff83b03fd8 31
p3p 0:93ff83b03fd8 32 bool isFull() {
p3p 0:93ff83b03fd8 33 return ((write + 1) % size == read);
p3p 0:93ff83b03fd8 34 };
p3p 0:93ff83b03fd8 35
p3p 0:93ff83b03fd8 36 bool isEmpty() {
p3p 0:93ff83b03fd8 37 return (read == write);
p3p 0:93ff83b03fd8 38 };
p3p 0:93ff83b03fd8 39
p3p 0:93ff83b03fd8 40 void queue(T k) {
p3p 0:93ff83b03fd8 41 if (isFull()) {
p3p 0:93ff83b03fd8 42 read++;
p3p 0:93ff83b03fd8 43 read %= size;
p3p 0:93ff83b03fd8 44 }
p3p 0:93ff83b03fd8 45 buf[write++] = k;
p3p 0:93ff83b03fd8 46 write %= size;
p3p 0:93ff83b03fd8 47 }
p3p 0:93ff83b03fd8 48
p3p 0:93ff83b03fd8 49 uint16_t available() {
p3p 0:93ff83b03fd8 50 return (write >= read) ? write - read : size - read + write;
p3p 0:93ff83b03fd8 51 };
p3p 0:93ff83b03fd8 52
p3p 0:93ff83b03fd8 53 bool dequeue(T * c) {
p3p 0:93ff83b03fd8 54 bool empty = isEmpty();
p3p 0:93ff83b03fd8 55 if (!empty) {
p3p 0:93ff83b03fd8 56 *c = buf[read++];
p3p 0:93ff83b03fd8 57 read %= size;
p3p 0:93ff83b03fd8 58 }
p3p 0:93ff83b03fd8 59 return(!empty);
p3p 0:93ff83b03fd8 60 };
p3p 0:93ff83b03fd8 61
p3p 0:93ff83b03fd8 62 private:
p3p 0:93ff83b03fd8 63 volatile uint16_t write;
p3p 0:93ff83b03fd8 64 volatile uint16_t read;
p3p 0:93ff83b03fd8 65 uint16_t size;
p3p 0:93ff83b03fd8 66 T * buf;
p3p 0:93ff83b03fd8 67 };
p3p 0:93ff83b03fd8 68
p3p 0:93ff83b03fd8 69 #endif