Basketball robot mainboard firmware

Dependencies:   USBDevice mbed

Committer:
Reiko
Date:
Mon Sep 10 15:24:08 2018 +0000
Revision:
0:88887cfb2b04
Mainboard firmware for basketball robot

Who changed what in which revision?

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