BERG Cloud / BERGCloud

Dependents:   LittleCounter-Example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BERGCloudMbed.cpp Source File

BERGCloudMbed.cpp

00001 /*
00002 
00003 BERGCloud library for mbed
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 <cstdint>
00028 #include <cstddef>
00029 #include <cstdarg>
00030 
00031 #include "BERGCloudMbed.h"
00032 
00033 uint16_t BERGCloudMbed::SPITransaction(uint8_t *dataOut, uint8_t *dataIn, uint16_t dataSize, bool finalCS)
00034 {
00035   uint16_t i;
00036 
00037   if ( (dataOut == NULL) || (dataIn == NULL) || (spi == NULL) )
00038   {
00039     _LOG("Invalid parameter (CBERGCloud::SPITransaction)\r\n");
00040     return 0;
00041   }
00042 
00043   nSSELPin->write(0);
00044 
00045   for (i = 0; i < dataSize; i++)
00046   {
00047     *dataIn++ = spi->write(*dataOut++);
00048   }
00049 
00050   if (finalCS)
00051   {
00052     nSSELPin->write(1);
00053   }
00054 
00055   return dataSize;
00056 }
00057 
00058 void BERGCloudMbed::timerReset(void)
00059 {
00060   timer->reset();
00061 }
00062 
00063 uint32_t BERGCloudMbed::timerRead_mS(void)
00064 {
00065   return timer->read_ms();
00066 }
00067 
00068 void BERGCloudMbed::begin(PinName _MOSIPin, PinName _MISOPin, PinName _SCLKPin, PinName _nSSELPin)
00069 {
00070   /* Call base class method */
00071   BERGCloudBase::begin();
00072 
00073   /* Configure nSSEL control pin */
00074   nSSELPin = new DigitalOut(_nSSELPin);
00075 
00076   if (nSSELPin == NULL)
00077   {
00078     _LOG("nSSELPin is NULL (CBERGCloud::begin)\r\n");
00079     return;
00080   }
00081 
00082   nSSELPin->write(1);
00083 
00084   /* Configure SPI */
00085   spi = new SPI(_MOSIPin, _MISOPin, _SCLKPin);
00086 
00087   if (spi  == NULL)
00088   {
00089     _LOG("spi is NULL (CBERGCloud::begin)\r\n");
00090     delete nSSELPin;
00091     return;
00092   }
00093 
00094   spi->format(8, 0); /* 8-bits; SPI MODE 0 */
00095   spi->frequency(4000000); /* 4MHz */
00096 
00097   /* Configure timer */
00098   timer = new Timer();
00099 
00100   if (timer  == NULL)
00101   {
00102     _LOG("timer is NULL (CBERGCloud::begin)\r\n");
00103     delete nSSELPin;
00104     delete spi;
00105     return;
00106   }
00107 
00108   timer->start();
00109 }
00110 
00111 void BERGCloudMbed::end()
00112 {
00113   if (nSSELPin != NULL)
00114   {
00115     delete nSSELPin;
00116   }
00117 
00118   if (spi != NULL)
00119   {
00120     delete spi;
00121   }
00122 
00123   if (timer != NULL)
00124   {
00125     delete timer;
00126   }
00127 
00128   /* Call base class method */
00129   BERGCloudBase::end();
00130 }
00131 
00132 bool BERGCloudMbed::display(std::string& s)
00133 {
00134   return display(s.c_str());
00135 }
00136 
00137 uint16_t BERGCloudMbed::getHostType(void)
00138 {
00139   return BC_HOST_MBED;
00140 }
00141 
00142 #ifdef BERGCLOUD_PACK_UNPACK
00143 
00144 bool BERGCloudMessage::pack(std::string& s)
00145 {
00146   return pack(s.c_str());
00147 }
00148 
00149 bool BERGCloudMessage::unpack(std::string& s)
00150 {
00151   uint16_t sizeInBytes;
00152 
00153   if (!unpack_raw_header(&sizeInBytes))
00154   {
00155     return false;
00156   }
00157 
00158   if (!remaining(sizeInBytes))
00159   {
00160     _LOG_UNPACK_ERROR_NO_DATA;
00161     return false;
00162   }
00163 
00164   s.clear();
00165   s.append((const char *)&buffer[bytesRead], sizeInBytes);
00166   bytesRead += sizeInBytes;
00167 
00168   return true;
00169 }
00170 
00171 bool BERGCloudMbed::pollForCommand(BERGCloudMessageBuffer& buffer, string &commandName)
00172 {
00173   bool result = false;
00174   char tmp[31 + 1]; /* +1 for null terminator */
00175 
00176   commandName = ""; /* Empty string */
00177   result = pollForCommand(buffer, tmp, sizeof(tmp));
00178 
00179   if (result)
00180   {
00181     commandName = string(tmp);
00182   }
00183 
00184   return result;
00185 }
00186 
00187 #endif // #ifdef BERGCLOUD_PACK_UNPACK
00188