BERG Cloud / BERGCloud

Dependents:   LittleCounter-Example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BERGCloudMessageBuffer.cpp Source File

BERGCloudMessageBuffer.cpp

00001 /*
00002 
00003 Simple message buffer
00004 
00005 Copyright (c) 2013 BERG Cloud Ltd. http://bergcloud.com/
00006 
00007 Permission is hereby granted, free of charge, to any person obtaining a copy
00008 of this software and associated documentation files (the "Software"), to deal
00009 in the Software without restriction, including without limitation the rights
00010 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00011 copies of the Software, and to permit persons to whom the Software is
00012 furnished to do so, subject to the following conditions:
00013 
00014 The above copyright notice and this permission notice shall be included in
00015 all copies or substantial portions of the Software.
00016 
00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00020 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00021 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00022 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00023 THE SOFTWARE.
00024 
00025 */
00026 
00027 #include "BERGCloudMessageBuffer.h"
00028 
00029 BERGCloudMessageBuffer::BERGCloudMessageBuffer(void)
00030 {
00031   clear();
00032 }
00033 
00034 void BERGCloudMessageBuffer::clear(void)
00035 {
00036   bytesWritten = 0; /* Number of bytes written */
00037   bytesRead = 0;    /* Number of bytes read */
00038 }
00039 
00040 void BERGCloudMessageBuffer::restart(void)
00041 {
00042   /* Restart reading from the beginning */
00043   bytesRead = 0;
00044 }
00045 
00046 uint16_t BERGCloudMessageBuffer::size(void)
00047 {
00048   /* Get total size of the buffer */
00049   return BUFFER_SIZE_BYTES;
00050 }
00051 
00052 uint16_t BERGCloudMessageBuffer::used(void)
00053 {
00054   /* Get mumber of bytes used in the buffer */
00055   return bytesWritten;
00056 }
00057 
00058 void BERGCloudMessageBuffer::used(uint16_t used)
00059 {
00060   /* Set number of bytes used in the buffer */
00061   bytesWritten = used;
00062 }
00063 
00064 uint16_t BERGCloudMessageBuffer::available(void)
00065 {
00066   /* Get space available in the buffer */
00067   return BUFFER_SIZE_BYTES - bytesWritten;
00068 }
00069 
00070 bool BERGCloudMessageBuffer::available(uint16_t required)
00071 {
00072   /* Test if space is available for the number of bytes required */
00073   return (BUFFER_SIZE_BYTES - bytesWritten) >= required;
00074 }
00075 
00076 void BERGCloudMessageBuffer::add(uint8_t data)
00077 {
00078   /* Write a byte to the buffer; no checks */
00079     buffer[bytesWritten++] = data;
00080 }
00081 
00082 bool BERGCloudMessageBuffer::peek(uint8_t *data)
00083 {
00084   /* Peek at the next byte in the buffer */
00085   if (bytesRead >= bytesWritten)
00086   {
00087     /* No more data */
00088     return false;
00089   }
00090 
00091   
00092   *data = buffer[bytesRead]; /* No increment */
00093   return true;
00094 }
00095 
00096 uint8_t BERGCloudMessageBuffer::read(void)
00097 {
00098   /* Read the next byte from the buffer; no checks */
00099   return buffer[bytesRead++];
00100 }
00101 
00102 uint16_t BERGCloudMessageBuffer::remaining(void)
00103 {
00104   /* Returns the number of bytes that have been written but not read */
00105   return bytesWritten - bytesRead;
00106 }
00107 
00108 bool BERGCloudMessageBuffer::remaining(uint16_t required)
00109 {
00110   /* Test if data is remaining for the number of bytes required */
00111   return (bytesWritten - bytesRead) >= required;
00112 }
00113 
00114 uint8_t *BERGCloudMessageBuffer::ptr(void)
00115 {
00116   return buffer;
00117 }
00118