Axeda Ready Demo for Freescale FRDM-KL46Z as accident alert system

Dependencies:   FRDM_MMA8451Q KL46Z-USBHost MAG3110 SocketModem TSI mbed FATFileSystem

Fork of AxedaGo-Freescal_FRDM-KL46Z by Axeda Corp

Committer:
AxedaCorp
Date:
Wed Jul 02 19:57:37 2014 +0000
Revision:
2:2f9019c5a9fc
Parent:
0:65004368569c
ip switch

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AxedaCorp 0:65004368569c 1 /* Universal Socket Modem Interface Library
AxedaCorp 0:65004368569c 2 * Copyright (c) 2013 Multi-Tech Systems
AxedaCorp 0:65004368569c 3 *
AxedaCorp 0:65004368569c 4 * Licensed under the Apache License, Version 2.0 (the "License");
AxedaCorp 0:65004368569c 5 * you may not use this file except in compliance with the License.
AxedaCorp 0:65004368569c 6 * You may obtain a copy of the License at
AxedaCorp 0:65004368569c 7 *
AxedaCorp 0:65004368569c 8 * http://www.apache.org/licenses/LICENSE-2.0
AxedaCorp 0:65004368569c 9 *
AxedaCorp 0:65004368569c 10 * Unless required by applicable law or agreed to in writing, software
AxedaCorp 0:65004368569c 11 * distributed under the License is distributed on an "AS IS" BASIS,
AxedaCorp 0:65004368569c 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
AxedaCorp 0:65004368569c 13 * See the License for the specific language governing permissions and
AxedaCorp 0:65004368569c 14 * limitations under the License.
AxedaCorp 0:65004368569c 15 */
AxedaCorp 0:65004368569c 16
AxedaCorp 0:65004368569c 17 #include "MTSCircularBuffer.h"
AxedaCorp 0:65004368569c 18
AxedaCorp 0:65004368569c 19 using namespace mts;
AxedaCorp 0:65004368569c 20
AxedaCorp 0:65004368569c 21 MTSCircularBuffer::MTSCircularBuffer(int bufferSize) : bufferSize(bufferSize), readIndex(0), writeIndex(0), bytes(0), _threshold(-1), _op(Vars::GREATER)
AxedaCorp 0:65004368569c 22 {
AxedaCorp 0:65004368569c 23 buffer = new char[bufferSize];
AxedaCorp 0:65004368569c 24 }
AxedaCorp 0:65004368569c 25
AxedaCorp 0:65004368569c 26 MTSCircularBuffer::~MTSCircularBuffer()
AxedaCorp 0:65004368569c 27 {
AxedaCorp 0:65004368569c 28 delete[] buffer;
AxedaCorp 0:65004368569c 29 }
AxedaCorp 0:65004368569c 30
AxedaCorp 0:65004368569c 31 int MTSCircularBuffer::capacity()
AxedaCorp 0:65004368569c 32 {
AxedaCorp 0:65004368569c 33 return bufferSize;
AxedaCorp 0:65004368569c 34 }
AxedaCorp 0:65004368569c 35
AxedaCorp 0:65004368569c 36 int MTSCircularBuffer::read(char* data, int length)
AxedaCorp 0:65004368569c 37 {
AxedaCorp 0:65004368569c 38 int i = 0;
AxedaCorp 0:65004368569c 39 while ((i < length) && (bytes > 0)) {
AxedaCorp 0:65004368569c 40 if (readIndex == bufferSize) {
AxedaCorp 0:65004368569c 41 readIndex = 0;
AxedaCorp 0:65004368569c 42 }
AxedaCorp 0:65004368569c 43 data[i++] = buffer[readIndex++];
AxedaCorp 0:65004368569c 44 bytes--;
AxedaCorp 0:65004368569c 45 checkThreshold();
AxedaCorp 0:65004368569c 46 }
AxedaCorp 0:65004368569c 47 return i;
AxedaCorp 0:65004368569c 48 }
AxedaCorp 0:65004368569c 49
AxedaCorp 0:65004368569c 50 int MTSCircularBuffer::read(char& data)
AxedaCorp 0:65004368569c 51 {
AxedaCorp 0:65004368569c 52 if (bytes == 0) {
AxedaCorp 0:65004368569c 53 return 0;
AxedaCorp 0:65004368569c 54 }
AxedaCorp 0:65004368569c 55 if (readIndex == bufferSize) {
AxedaCorp 0:65004368569c 56 readIndex = 0;
AxedaCorp 0:65004368569c 57 }
AxedaCorp 0:65004368569c 58 data = buffer[readIndex++];
AxedaCorp 0:65004368569c 59 bytes--;
AxedaCorp 0:65004368569c 60 checkThreshold();
AxedaCorp 0:65004368569c 61 return 1;
AxedaCorp 0:65004368569c 62 }
AxedaCorp 0:65004368569c 63
AxedaCorp 0:65004368569c 64 int MTSCircularBuffer::write(const char* data, int length)
AxedaCorp 0:65004368569c 65 {
AxedaCorp 0:65004368569c 66 int i = 0;
AxedaCorp 0:65004368569c 67 while((i < length) && (bytes < bufferSize)) {
AxedaCorp 0:65004368569c 68 if(writeIndex == bufferSize) {
AxedaCorp 0:65004368569c 69 writeIndex = 0;
AxedaCorp 0:65004368569c 70 }
AxedaCorp 0:65004368569c 71 buffer[writeIndex++] = data[i++];
AxedaCorp 0:65004368569c 72 bytes++;
AxedaCorp 0:65004368569c 73 checkThreshold();
AxedaCorp 0:65004368569c 74 }
AxedaCorp 0:65004368569c 75 return i;
AxedaCorp 0:65004368569c 76 }
AxedaCorp 0:65004368569c 77
AxedaCorp 0:65004368569c 78 int MTSCircularBuffer::write(char data)
AxedaCorp 0:65004368569c 79 {
AxedaCorp 0:65004368569c 80 if (bytes == bufferSize) {
AxedaCorp 0:65004368569c 81 return 0;
AxedaCorp 0:65004368569c 82 }
AxedaCorp 0:65004368569c 83 if(writeIndex == bufferSize) {
AxedaCorp 0:65004368569c 84 writeIndex = 0;
AxedaCorp 0:65004368569c 85 }
AxedaCorp 0:65004368569c 86 buffer[writeIndex++] = data;
AxedaCorp 0:65004368569c 87 bytes++;
AxedaCorp 0:65004368569c 88 checkThreshold();
AxedaCorp 0:65004368569c 89 return 1;
AxedaCorp 0:65004368569c 90 }
AxedaCorp 0:65004368569c 91
AxedaCorp 0:65004368569c 92 int MTSCircularBuffer::remaining()
AxedaCorp 0:65004368569c 93 {
AxedaCorp 0:65004368569c 94 return bufferSize - bytes;
AxedaCorp 0:65004368569c 95 }
AxedaCorp 0:65004368569c 96
AxedaCorp 0:65004368569c 97 int MTSCircularBuffer::size()
AxedaCorp 0:65004368569c 98 {
AxedaCorp 0:65004368569c 99 return bytes;
AxedaCorp 0:65004368569c 100 }
AxedaCorp 0:65004368569c 101
AxedaCorp 0:65004368569c 102 bool MTSCircularBuffer::isFull()
AxedaCorp 0:65004368569c 103 {
AxedaCorp 0:65004368569c 104 if (bytes == bufferSize) {
AxedaCorp 0:65004368569c 105 return true;
AxedaCorp 0:65004368569c 106 } else {
AxedaCorp 0:65004368569c 107 return false;
AxedaCorp 0:65004368569c 108 }
AxedaCorp 0:65004368569c 109 }
AxedaCorp 0:65004368569c 110
AxedaCorp 0:65004368569c 111 bool MTSCircularBuffer::isEmpty()
AxedaCorp 0:65004368569c 112 {
AxedaCorp 0:65004368569c 113 if (bytes == 0) {
AxedaCorp 0:65004368569c 114 return true;
AxedaCorp 0:65004368569c 115 } else {
AxedaCorp 0:65004368569c 116 return false;
AxedaCorp 0:65004368569c 117 }
AxedaCorp 0:65004368569c 118 }
AxedaCorp 0:65004368569c 119
AxedaCorp 0:65004368569c 120 void MTSCircularBuffer::clear()
AxedaCorp 0:65004368569c 121 {
AxedaCorp 0:65004368569c 122 writeIndex = readIndex = bytes = 0;
AxedaCorp 0:65004368569c 123 }
AxedaCorp 0:65004368569c 124
AxedaCorp 0:65004368569c 125 void MTSCircularBuffer::checkThreshold()
AxedaCorp 0:65004368569c 126 {
AxedaCorp 0:65004368569c 127 if (_threshold == -1) {
AxedaCorp 0:65004368569c 128 return;
AxedaCorp 0:65004368569c 129 }
AxedaCorp 0:65004368569c 130 switch (_op) {
AxedaCorp 0:65004368569c 131 case Vars::GREATER:
AxedaCorp 0:65004368569c 132 if (bytes > _threshold) {
AxedaCorp 0:65004368569c 133 notify.call();
AxedaCorp 0:65004368569c 134 }
AxedaCorp 0:65004368569c 135 break;
AxedaCorp 0:65004368569c 136 case Vars::LESS:
AxedaCorp 0:65004368569c 137 if (bytes < _threshold) {
AxedaCorp 0:65004368569c 138 notify.call();
AxedaCorp 0:65004368569c 139 }
AxedaCorp 0:65004368569c 140 break;
AxedaCorp 0:65004368569c 141 case Vars::GREATER_EQUAL:
AxedaCorp 0:65004368569c 142 if (bytes >= _threshold) {
AxedaCorp 0:65004368569c 143 notify.call();
AxedaCorp 0:65004368569c 144 }
AxedaCorp 0:65004368569c 145 break;
AxedaCorp 0:65004368569c 146 case Vars::LESS_EQUAL:
AxedaCorp 0:65004368569c 147 if (bytes <= _threshold) {
AxedaCorp 0:65004368569c 148 notify.call();
AxedaCorp 0:65004368569c 149 }
AxedaCorp 0:65004368569c 150 break;
AxedaCorp 0:65004368569c 151 case Vars::EQUAL:
AxedaCorp 0:65004368569c 152 if (bytes == _threshold) {
AxedaCorp 0:65004368569c 153 notify.call();
AxedaCorp 0:65004368569c 154 }
AxedaCorp 0:65004368569c 155 break;
AxedaCorp 0:65004368569c 156 }
AxedaCorp 0:65004368569c 157 }
AxedaCorp 0:65004368569c 158