pwm period is now 200us instead of the default 20ms veml6040 config is now AF_BIT | TRIG_BIT

Dependencies:   mbed MMA8451Q USBDevice WakeUp vt100

Fork of afero_node_suntory_2017_06_15 by Orefatoi

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers afLib.h Source File

afLib.h

00001 /**
00002  * Copyright 2015 Afero, Inc.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef AFLIB_H__
00018 #define AFLIB_H__
00019 
00020 #include "mbed.h"
00021 #include "iafLib.h"
00022 #include "SPI.h"
00023 #include "Command.h"
00024 #include "StatusCommand.h"
00025 #include "afSPI.h"
00026 #include "WatchDogWrapper.hpp"
00027 
00028 #define STATE_IDLE                          0
00029 #define STATE_STATUS_SYNC                   1
00030 #define STATE_STATUS_ACK                    3
00031 #define STATE_SEND_BYTES                    4
00032 #define STATE_RECV_BYTES                    5
00033 #define STATE_CMD_COMPLETE                  6
00034 
00035 #define SPI_FRAME_LEN                       16
00036 
00037 #define REQUEST_QUEUE_SIZE                  10
00038 
00039 typedef struct {
00040     uint8_t     messageType;
00041     uint16_t    attrId;
00042     uint8_t     requestId;
00043     uint16_t    valueLen;
00044     uint8_t     *p_value;
00045 } request_t;
00046 
00047 class afLib : public iafLib {
00048 public:
00049     afLib(PinName mcuInterrupt, isr isrWrapper,
00050           onAttributeSet attrSet, onAttributeSetComplete attrSetComplete, afSPI *theSPI);
00051 //wsugi 20161128
00052     ~afLib();
00053 //wsugi 20161128
00054     virtual void loop(void);
00055 
00056     virtual int getAttribute(const uint16_t attrId);
00057 
00058     virtual int setAttributeBool(const uint16_t attrId, const bool value);
00059 
00060     virtual int setAttribute8(const uint16_t attrId, const int8_t value);
00061 
00062     virtual int setAttribute16(const uint16_t attrId, const int16_t value);
00063 
00064     virtual int setAttribute32(const uint16_t attrId, const int32_t value);
00065 
00066     virtual int setAttribute64(const uint16_t attrId, const int64_t value);
00067 
00068     virtual int setAttribute(const uint16_t attrId, const string &value);
00069 
00070     virtual int setAttribute(const uint16_t attrId, const uint16_t valueLen, const char *value);
00071 
00072     virtual int setAttribute(const uint16_t attrId, const uint16_t valueLen, const uint8_t *value);
00073 
00074     virtual int setAttributeComplete(uint8_t requestId, const uint16_t attrId, const uint16_t valueLen, const uint8_t *value);
00075 
00076     virtual bool isIdle();
00077 
00078     virtual void mcuISR();
00079 
00080 private:
00081     Timeout deathWish;
00082     Timer *checkLastSync;
00083     static void kick_the_bucket();
00084     afSPI *_theSPI;
00085 
00086     //SPISettings _spiSettings;
00087     volatile int _interrupts_pending;
00088     int _state;
00089     uint16_t _bytesToSend;
00090     uint16_t _bytesToRecv;
00091     uint8_t _requestId;
00092     uint16_t _outstandingSetGetAttrId;
00093 
00094     // Application Callbacks.
00095     onAttributeSet _onAttrSet;
00096     onAttributeSetComplete _onAttrSetComplete;
00097 
00098     Command *_writeCmd;
00099     uint16_t _writeBufferLen;
00100     uint8_t *_writeBuffer;
00101 
00102     Command *_readCmd;
00103     uint16_t _readBufferLen;
00104     uint8_t *_readBuffer;
00105 
00106     int _writeCmdOffset;
00107     int _readCmdOffset;
00108 
00109     StatusCommand *_txStatus;
00110     StatusCommand *_rxStatus;
00111 
00112     request_t _request;
00113     request_t _requestQueue[REQUEST_QUEUE_SIZE];
00114 
00115 #ifdef ATTRIBUTE_CLI
00116     int parseCommand(const char *cmd);
00117 #endif
00118 
00119     void sendCommand(void);
00120 
00121     void runStateMachine(void);
00122 
00123     void printState(int state);
00124 
00125     //void beginSPI();
00126 
00127     //void endSPI();
00128 
00129     int exchangeStatus(StatusCommand *tx, StatusCommand *rx);
00130 
00131     bool inSync(StatusCommand *tx, StatusCommand *rx);
00132 
00133     int writeStatus(StatusCommand *c);
00134 
00135     void sendBytes();
00136 
00137     void recvBytes();
00138 
00139     void dumpBytes(char *label, int len, uint8_t *bytes);
00140 
00141     void updateIntsPending(int amount);
00142 
00143     void queueInit(void);
00144 
00145     int queuePut(uint8_t messageType, uint8_t requestId, const uint16_t attributeId, uint16_t valueLen, const uint8_t *value);
00146 
00147     int queueGet(uint8_t *messageType, uint8_t *requestId, uint16_t *attributeId, uint16_t *valueLen, uint8_t **value);
00148 
00149     int doGetAttribute(uint8_t requestId, uint16_t attrId);
00150 
00151     int doSetAttribute(uint8_t requestId, uint16_t attrId, uint16_t valueLen, uint8_t *value);
00152 
00153     int doUpdateAttribute(uint8_t requestId, uint16_t attrId, uint8_t status, uint16_t valueLen, uint8_t *value);
00154 
00155     void onStateIdle(void);
00156     void onStateSync(void);
00157     void onStateAck(void);
00158     void onStateSendBytes(void);
00159     void onStateRecvBytes(void);
00160     void onStateCmdComplete(void);
00161     void printTransaction(uint8_t *rbytes, int len);
00162     InterruptIn fco;
00163 };
00164 
00165 #endif // AFLIB_H__