LPC11U35 ADC Tick & USBSerial

Dependencies:   mbed

Dependents:   SmallDoseMeter_SingleCH_AE_lpc11u35_V1_00

Committer:
H_Tsunemoto
Date:
Mon Feb 19 09:09:52 2018 +0000
Revision:
1:b1a3be5f48ab
Parent:
0:871ab6846b18
test

Who changed what in which revision?

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