ワークショップ用のサンプルプログラムです。

Dependencies:   DHT mbed

Committer:
jksoft
Date:
Fri Apr 29 21:07:24 2016 +0000
Revision:
0:a967c8649563
??

Who changed what in which revision?

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