Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: LittleCounter-Example
BERGCloudBase.h@5:2e04a8b3fc25, 2013-11-26 (annotated)
- Committer:
- nickludlam
- Date:
- Tue Nov 26 17:45:12 2013 +0000
- Revision:
- 5:2e04a8b3fc25
- Parent:
- 4:5a04c00b5b6f
- Child:
- 6:79100379d4b4
Update to v1.1.; ; This fixes an issue with unpacking data which required a promotion of container size. It also includes a template for threading, and a fresh implementation of the CRC16 code.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| nickludlam | 0:b4ccb530b9eb | 1 | /* |
| nickludlam | 0:b4ccb530b9eb | 2 | |
| nickludlam | 0:b4ccb530b9eb | 3 | BERGCloud library common API |
| nickludlam | 0:b4ccb530b9eb | 4 | |
| nickludlam | 0:b4ccb530b9eb | 5 | Copyright (c) 2013 BERG Cloud Ltd. http://bergcloud.com/ |
| nickludlam | 0:b4ccb530b9eb | 6 | |
| nickludlam | 0:b4ccb530b9eb | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy |
| nickludlam | 0:b4ccb530b9eb | 8 | of this software and associated documentation files (the "Software"), to deal |
| nickludlam | 0:b4ccb530b9eb | 9 | in the Software without restriction, including without limitation the rights |
| nickludlam | 0:b4ccb530b9eb | 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| nickludlam | 0:b4ccb530b9eb | 11 | copies of the Software, and to permit persons to whom the Software is |
| nickludlam | 0:b4ccb530b9eb | 12 | furnished to do so, subject to the following conditions: |
| nickludlam | 0:b4ccb530b9eb | 13 | |
| nickludlam | 0:b4ccb530b9eb | 14 | The above copyright notice and this permission notice shall be included in |
| nickludlam | 0:b4ccb530b9eb | 15 | all copies or substantial portions of the Software. |
| nickludlam | 0:b4ccb530b9eb | 16 | |
| nickludlam | 0:b4ccb530b9eb | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| nickludlam | 0:b4ccb530b9eb | 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| nickludlam | 0:b4ccb530b9eb | 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| nickludlam | 0:b4ccb530b9eb | 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| nickludlam | 0:b4ccb530b9eb | 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| nickludlam | 0:b4ccb530b9eb | 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| nickludlam | 0:b4ccb530b9eb | 23 | THE SOFTWARE. |
| nickludlam | 0:b4ccb530b9eb | 24 | |
| nickludlam | 0:b4ccb530b9eb | 25 | */ |
| nickludlam | 0:b4ccb530b9eb | 26 | |
| nickludlam | 0:b4ccb530b9eb | 27 | |
| nickludlam | 0:b4ccb530b9eb | 28 | #ifndef BERGCLOUDBASE_H |
| nickludlam | 0:b4ccb530b9eb | 29 | #define BERGCLOUDBASE_H |
| nickludlam | 0:b4ccb530b9eb | 30 | |
| nickludlam | 0:b4ccb530b9eb | 31 | #include "BERGCloudConfig.h" |
| nickludlam | 0:b4ccb530b9eb | 32 | #include "BERGCloudConst.h" |
| nickludlam | 0:b4ccb530b9eb | 33 | #include "BERGCloudLogPrint.h" |
| nickludlam | 0:b4ccb530b9eb | 34 | |
| nickludlam | 0:b4ccb530b9eb | 35 | #ifdef BERGCLOUD_PACK_UNPACK |
| nickludlam | 0:b4ccb530b9eb | 36 | #include "BERGCloudMessageBuffer.h" |
| nickludlam | 0:b4ccb530b9eb | 37 | #endif |
| nickludlam | 0:b4ccb530b9eb | 38 | |
| nickludlam | 0:b4ccb530b9eb | 39 | #define BERGCLOUD_LIB_VERSION (0x0100) |
| nickludlam | 0:b4ccb530b9eb | 40 | |
| nickludlam | 0:b4ccb530b9eb | 41 | #define _TX_GROUPS (2) |
| nickludlam | 0:b4ccb530b9eb | 42 | #define _RX_GROUPS (2) |
| nickludlam | 0:b4ccb530b9eb | 43 | |
| nickludlam | 0:b4ccb530b9eb | 44 | typedef struct { |
| nickludlam | 0:b4ccb530b9eb | 45 | uint8_t *buffer; |
| nickludlam | 0:b4ccb530b9eb | 46 | uint16_t dataSize; |
| nickludlam | 0:b4ccb530b9eb | 47 | } _BC_TX_GROUP; |
| nickludlam | 0:b4ccb530b9eb | 48 | |
| nickludlam | 0:b4ccb530b9eb | 49 | typedef struct { |
| nickludlam | 0:b4ccb530b9eb | 50 | uint8_t *buffer; |
| nickludlam | 0:b4ccb530b9eb | 51 | uint16_t bufferSize; |
| nickludlam | 0:b4ccb530b9eb | 52 | uint16_t *dataSize; |
| nickludlam | 0:b4ccb530b9eb | 53 | } _BC_RX_GROUP; |
| nickludlam | 0:b4ccb530b9eb | 54 | |
| nickludlam | 0:b4ccb530b9eb | 55 | typedef struct { |
| nickludlam | 0:b4ccb530b9eb | 56 | uint8_t command; |
| nickludlam | 0:b4ccb530b9eb | 57 | _BC_TX_GROUP tx[_TX_GROUPS]; |
| nickludlam | 0:b4ccb530b9eb | 58 | _BC_RX_GROUP rx[_RX_GROUPS]; |
| nickludlam | 0:b4ccb530b9eb | 59 | } _BC_SPI_TRANSACTION; |
| nickludlam | 0:b4ccb530b9eb | 60 | |
| nickludlam | 0:b4ccb530b9eb | 61 | class BERGCloudBase |
| nickludlam | 0:b4ccb530b9eb | 62 | { |
| nickludlam | 0:b4ccb530b9eb | 63 | public: |
| nickludlam | 0:b4ccb530b9eb | 64 | /* Check for a command */ |
| nickludlam | 0:b4ccb530b9eb | 65 | bool pollForCommand(uint8_t *commandBuffer, uint16_t commandBufferSize, uint16_t& commandSize, uint8_t& commandID); |
| nickludlam | 0:b4ccb530b9eb | 66 | #ifdef BERGCLOUD_PACK_UNPACK |
| nickludlam | 0:b4ccb530b9eb | 67 | bool pollForCommand(BERGCloudMessageBuffer& buffer, uint8_t& commandID); |
| nickludlam | 0:b4ccb530b9eb | 68 | #endif |
| nickludlam | 0:b4ccb530b9eb | 69 | /* Send an event */ |
| nickludlam | 4:5a04c00b5b6f | 70 | bool sendEvent(uint8_t eventCode, uint8_t *eventBuffer, uint16_t eventSize, bool packed = false); |
| nickludlam | 0:b4ccb530b9eb | 71 | #ifdef BERGCLOUD_PACK_UNPACK |
| nickludlam | 0:b4ccb530b9eb | 72 | bool sendEvent(uint8_t eventCode, BERGCloudMessageBuffer& buffer); |
| nickludlam | 0:b4ccb530b9eb | 73 | #endif |
| nickludlam | 0:b4ccb530b9eb | 74 | /* Get the connection state */ |
| nickludlam | 0:b4ccb530b9eb | 75 | bool getConnectionState(uint8_t& state); |
| nickludlam | 0:b4ccb530b9eb | 76 | /* Get the last-hop signal quality */ |
| nickludlam | 0:b4ccb530b9eb | 77 | bool getSignalQuality(int8_t& rssi, uint8_t& lqi); |
| nickludlam | 0:b4ccb530b9eb | 78 | /* Connect */ |
| nickludlam | 4:5a04c00b5b6f | 79 | virtual bool connect(const uint8_t (&key)[BC_KEY_SIZE_BYTES] = nullKey, uint16_t version = 0, bool waitForConnected = false); |
| nickludlam | 0:b4ccb530b9eb | 80 | /* Check if the device has been claimed */ |
| nickludlam | 0:b4ccb530b9eb | 81 | bool getClaimingState(uint8_t& state); |
| nickludlam | 0:b4ccb530b9eb | 82 | /* Get the current claimcode */ |
| nickludlam | 4:5a04c00b5b6f | 83 | virtual bool getClaimcode(const char (&claimcode)[BC_CLAIMCODE_SIZE_BYTES]); |
| nickludlam | 0:b4ccb530b9eb | 84 | /* Get the EUI64 identifier for this node, its parent or the network coordinator */ |
| nickludlam | 4:5a04c00b5b6f | 85 | virtual bool getEUI64(uint8_t type, uint8_t (&eui64)[BC_EUI64_SIZE_BYTES]); |
| nickludlam | 0:b4ccb530b9eb | 86 | /* Get the Device Address */ |
| nickludlam | 4:5a04c00b5b6f | 87 | virtual bool getDeviceAddress(uint8_t (&address)[BC_ADDRESS_SIZE_BYTES]); |
| nickludlam | 0:b4ccb530b9eb | 88 | /* Set the display style for the OLED display. This also clears the display. */ |
| nickludlam | 0:b4ccb530b9eb | 89 | bool setDisplayStyle(uint8_t style); |
| nickludlam | 0:b4ccb530b9eb | 90 | /* Clear the OLED display */ |
| nickludlam | 0:b4ccb530b9eb | 91 | bool clearDisplay(void); |
| nickludlam | 0:b4ccb530b9eb | 92 | /* Display a line of text on the OLED display */ |
| nickludlam | 0:b4ccb530b9eb | 93 | bool display(const char *text); |
| nickludlam | 0:b4ccb530b9eb | 94 | |
| nickludlam | 0:b4ccb530b9eb | 95 | /* Internal methods */ |
| nickludlam | 0:b4ccb530b9eb | 96 | public: |
| nickludlam | 0:b4ccb530b9eb | 97 | uint8_t lastResponse; |
| nickludlam | 4:5a04c00b5b6f | 98 | static uint8_t nullKey[BC_KEY_SIZE_BYTES]; |
| nickludlam | 0:b4ccb530b9eb | 99 | protected: |
| nickludlam | 0:b4ccb530b9eb | 100 | void begin(void); |
| nickludlam | 0:b4ccb530b9eb | 101 | void end(void); |
| nickludlam | 0:b4ccb530b9eb | 102 | uint16_t Crc16(uint8_t data, uint16_t crc); |
| nickludlam | 0:b4ccb530b9eb | 103 | virtual uint16_t SPITransaction(uint8_t *dataOut, uint8_t *dataIn, uint16_t dataSize, bool finalCS) = 0; |
| nickludlam | 0:b4ccb530b9eb | 104 | virtual void timerReset(void) = 0; |
| nickludlam | 0:b4ccb530b9eb | 105 | virtual uint32_t timerRead_mS(void) = 0; |
| nickludlam | 0:b4ccb530b9eb | 106 | virtual uint16_t getHostType(void) = 0; |
| nickludlam | 0:b4ccb530b9eb | 107 | private: |
| nickludlam | 0:b4ccb530b9eb | 108 | uint8_t SPITransaction(uint8_t data, bool finalCS); |
| nickludlam | 0:b4ccb530b9eb | 109 | void initTransaction(_BC_SPI_TRANSACTION *tr); |
| nickludlam | 5:2e04a8b3fc25 | 110 | bool _transaction(_BC_SPI_TRANSACTION *tr); |
| nickludlam | 0:b4ccb530b9eb | 111 | bool transaction(_BC_SPI_TRANSACTION *tr); |
| nickludlam | 0:b4ccb530b9eb | 112 | bool _sendEvent(uint8_t eventCode, uint8_t *eventBuffer, uint16_t eventSize, uint8_t command); |
| nickludlam | 5:2e04a8b3fc25 | 113 | void lockTake(void); |
| nickludlam | 5:2e04a8b3fc25 | 114 | void lockRelease(void); |
| nickludlam | 0:b4ccb530b9eb | 115 | bool synced; |
| nickludlam | 0:b4ccb530b9eb | 116 | }; |
| nickludlam | 0:b4ccb530b9eb | 117 | |
| nickludlam | 0:b4ccb530b9eb | 118 | #endif // #ifndef BERGCLOUDBASE_H |
| nickludlam | 0:b4ccb530b9eb | 119 |

