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

Committer:
wataloh
Date:
Mon May 29 04:06:34 2017 +0000
Revision:
15:2c2c0a7c50c1
Parent:
14:b205267fa5f6
Child:
19:b45b077c88bc
5 sec delay for every data send to ASR-1.; ; upload interval change reflected immediately.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wataloh 0:20bce0dcc921 1 #include "AferoCommHndlr.h"
wataloh 0:20bce0dcc921 2
wataloh 0:20bce0dcc921 3 static AferoCommHndlr *self = NULL;
wataloh 0:20bce0dcc921 4
wataloh 1:b2a9a6f2c30e 5 AferoCommHndlr::AferoCommHndlr() : flowControlEnabled(false)
wataloh 0:20bce0dcc921 6 {
wataloh 6:88cc04eb613a 7 deathWish = new Timeout();
wataloh 14:b205267fa5f6 8 setAttrCmpDelay = new Timeout();
wataloh 14:b205267fa5f6 9 _onGetTodoP = &AferoCommHndlr::_onGetTodo;
wataloh 6:88cc04eb613a 10 }
wataloh 6:88cc04eb613a 11
wataloh 6:88cc04eb613a 12 void
wataloh 6:88cc04eb613a 13 AferoCommHndlr::kick_the_bucket()
wataloh 6:88cc04eb613a 14 {
wataloh 6:88cc04eb613a 15 SERIAL_PRINT_DBG("bucket kicked!!\n");
wataloh 6:88cc04eb613a 16 wtd.kick_the_bucket();
wataloh 0:20bce0dcc921 17 }
wataloh 0:20bce0dcc921 18
wataloh 0:20bce0dcc921 19 void
wataloh 0:20bce0dcc921 20 AferoCommHndlr::loop()
wataloh 0:20bce0dcc921 21 {
wataloh 14:b205267fa5f6 22 piafLib->loop();
wataloh 0:20bce0dcc921 23 todoQ->loop();
wataloh 0:20bce0dcc921 24 }
wataloh 0:20bce0dcc921 25
wataloh 0:20bce0dcc921 26 AferoCommHndlr*
wataloh 0:20bce0dcc921 27 AferoCommHndlr::create(Timer *timer, mbedSPI *spi)
wataloh 0:20bce0dcc921 28 {
wataloh 0:20bce0dcc921 29 SERIAL_PRINT_DBG("HELLO\n");
wataloh 0:20bce0dcc921 30 self = new AferoCommHndlr();
wataloh 0:20bce0dcc921 31 self->timer = timer;
wataloh 1:b2a9a6f2c30e 32 self->piafLib = iafLib::create((PinName)PINS::ASR_1::SPI::SR,
wataloh 0:20bce0dcc921 33 AferoCommHndlr::fco_irq_fall,
wataloh 0:20bce0dcc921 34 AferoCommHndlr::myOnAttributeSet,
wataloh 0:20bce0dcc921 35 AferoCommHndlr::myOnAttributeSetComplete, spi);
wataloh 0:20bce0dcc921 36 self->todoQ = ToDoQ::create(AferoCommHndlr::onGetTodo);
wataloh 0:20bce0dcc921 37 SERIAL_PRINT_DBG("AferoCommHndlr init done\n");
wataloh 6:88cc04eb613a 38 self->piafLib->setAttribute8(1,1) != afSUCCESS ? SERIAL_PRINT_DBG("set attr failed!!\n") : NULL ;
wataloh 1:b2a9a6f2c30e 39
wataloh 0:20bce0dcc921 40 return self;
wataloh 0:20bce0dcc921 41 }
wataloh 0:20bce0dcc921 42
wataloh 0:20bce0dcc921 43 void
wataloh 0:20bce0dcc921 44 AferoCommHndlr::myOnAttributeSet(
wataloh 0:20bce0dcc921 45 const uint8_t requestId,
wataloh 0:20bce0dcc921 46 const uint16_t attributeId,
wataloh 0:20bce0dcc921 47 const uint16_t valueLen,
wataloh 0:20bce0dcc921 48 const uint8_t *value)
wataloh 0:20bce0dcc921 49 {
wataloh 6:88cc04eb613a 50 self->piafLib->setAttributeComplete(requestId, attributeId, valueLen, value) != afSUCCESS ? SERIAL_PRINT_DBG("set attr failed!!\n") : NULL ;
wataloh 0:20bce0dcc921 51 }
wataloh 0:20bce0dcc921 52
wataloh 4:b38e09f24711 53 int32_t
wataloh 4:b38e09f24711 54 AferoCommHndlr::sanitizeSensingInterval(uint16_t attributeId, int32_t interval)
wataloh 4:b38e09f24711 55 {
wataloh 4:b38e09f24711 56 if(interval < 5)
wataloh 4:b38e09f24711 57 {
wataloh 4:b38e09f24711 58 interval = 5;
wataloh 6:88cc04eb613a 59 self->piafLib->setAttribute32(attributeId, interval) != afSUCCESS ? SERIAL_PRINT_DBG("set attr failed!!\n") : NULL ;
wataloh 4:b38e09f24711 60 }
wataloh 4:b38e09f24711 61 return interval;
wataloh 4:b38e09f24711 62 }
wataloh 4:b38e09f24711 63
wataloh 0:20bce0dcc921 64 void
wataloh 0:20bce0dcc921 65 AferoCommHndlr::myOnAttributeSetComplete(
wataloh 0:20bce0dcc921 66 const uint8_t requestId,
wataloh 0:20bce0dcc921 67 const uint16_t attributeId,
wataloh 0:20bce0dcc921 68 const uint16_t valueLen,
wataloh 0:20bce0dcc921 69 const uint8_t *value)
wataloh 0:20bce0dcc921 70 {
wataloh 14:b205267fa5f6 71 self->_myOnAttributeSetComplete(requestId,attributeId,valueLen,value);
wataloh 14:b205267fa5f6 72 }
wataloh 14:b205267fa5f6 73
wataloh 14:b205267fa5f6 74 void
wataloh 14:b205267fa5f6 75 AferoCommHndlr::_myOnAttributeSetComplete(
wataloh 14:b205267fa5f6 76 const uint8_t requestId,
wataloh 14:b205267fa5f6 77 const uint16_t attributeId,
wataloh 14:b205267fa5f6 78 const uint16_t valueLen,
wataloh 14:b205267fa5f6 79 const uint8_t *value)
wataloh 14:b205267fa5f6 80 {
wataloh 4:b38e09f24711 81 int32_t sensing_interval;
wataloh 4:b38e09f24711 82 char *buf = NULL;
wataloh 4:b38e09f24711 83 if(attributeId >= 4 && attributeId <= 8)
wataloh 4:b38e09f24711 84 {
wataloh 4:b38e09f24711 85 sensing_interval = *((int32_t*)value);
wataloh 4:b38e09f24711 86 }
wataloh 4:b38e09f24711 87
wataloh 2:dfe671e31221 88 if(attributeId==2)
wataloh 2:dfe671e31221 89 {
wataloh 2:dfe671e31221 90 PREFERENCES::CRC32.ui32 = *((uint32_t*)value);
wataloh 2:dfe671e31221 91 }
wataloh 2:dfe671e31221 92 else if(attributeId==4)
wataloh 2:dfe671e31221 93 {
wataloh 4:b38e09f24711 94 buf = "PREFERENCES::SENSING_INTERVAL[SENSORS::ACCELEROMETER]";
wataloh 14:b205267fa5f6 95 PREFERENCES::SENSING_INTERVAL[SENSORS::ACCELEROMETER] = sanitizeSensingInterval(attributeId,sensing_interval);
wataloh 2:dfe671e31221 96 }
wataloh 2:dfe671e31221 97 else if(attributeId==5)
wataloh 2:dfe671e31221 98 {
wataloh 4:b38e09f24711 99 buf = "PREFERENCES::SENSING_INTERVAL[SENSORS::COLOR]";
wataloh 14:b205267fa5f6 100 PREFERENCES::SENSING_INTERVAL[SENSORS::COLOR] = sanitizeSensingInterval(attributeId,sensing_interval);
wataloh 2:dfe671e31221 101 }
wataloh 2:dfe671e31221 102 else if(attributeId==6)
wataloh 2:dfe671e31221 103 {
wataloh 4:b38e09f24711 104 buf = "PREFERENCES::SENSING_INTERVAL[SENSORS::PRESSURE]";
wataloh 14:b205267fa5f6 105 PREFERENCES::SENSING_INTERVAL[SENSORS::PRESSURE] = sanitizeSensingInterval(attributeId,sensing_interval);
wataloh 2:dfe671e31221 106 }
wataloh 2:dfe671e31221 107 else if(attributeId==7)
wataloh 2:dfe671e31221 108 {
wataloh 4:b38e09f24711 109 buf = "PREFERENCES::SENSING_INTERVAL[SENSORS::CURRENT_TRANS]";
wataloh 14:b205267fa5f6 110 PREFERENCES::SENSING_INTERVAL[SENSORS::CURRENT_TRANS] = sanitizeSensingInterval(attributeId,sensing_interval);
wataloh 2:dfe671e31221 111 }
wataloh 2:dfe671e31221 112 else if(attributeId==8)
wataloh 2:dfe671e31221 113 {
wataloh 4:b38e09f24711 114 buf = "PREFERENCES::SENSING_INTERVAL[SENSORS::TEMPERATURE]";
wataloh 14:b205267fa5f6 115 PREFERENCES::SENSING_INTERVAL[SENSORS::TEMPERATURE] = sanitizeSensingInterval(attributeId,sensing_interval);
wataloh 2:dfe671e31221 116 }
wataloh 2:dfe671e31221 117 else if(attributeId==9)
wataloh 2:dfe671e31221 118 {
wataloh 2:dfe671e31221 119 SERIAL_PRINT_DBG("time stamp:%ld\n", *((uint32_t*)value));
wataloh 6:88cc04eb613a 120 time_t t;
wataloh 6:88cc04eb613a 121 RTC_GET_UTC(t);
wataloh 6:88cc04eb613a 122 if(*((uint32_t*)value) > ((uint32_t)t))
wataloh 6:88cc04eb613a 123 {
wataloh 6:88cc04eb613a 124 RTC_Handler::getInstance()->setUTC(*((uint32_t*)value));
wataloh 6:88cc04eb613a 125 }
wataloh 2:dfe671e31221 126 }
wataloh 2:dfe671e31221 127 else if(attributeId==1024)
wataloh 1:b2a9a6f2c30e 128 {
wataloh 1:b2a9a6f2c30e 129 }
wataloh 4:b38e09f24711 130
wataloh 4:b38e09f24711 131 if(attributeId >= 4 && attributeId <= 8)
wataloh 4:b38e09f24711 132 {
wataloh 4:b38e09f24711 133 SERIAL_PRINT_DBG("%s=%ld\n",buf,sensing_interval);
wataloh 4:b38e09f24711 134 }
wataloh 14:b205267fa5f6 135 //_onGetTodoP = &AferoCommHndlr::_onGetTodo;
wataloh 15:2c2c0a7c50c1 136 setAttrCmpDelay->attach(&onSetAttrCmpDelay,5);
wataloh 0:20bce0dcc921 137 }
wataloh 0:20bce0dcc921 138
wataloh 0:20bce0dcc921 139 void
wataloh 0:20bce0dcc921 140 AferoCommHndlr::fco_irq_fall()
wataloh 0:20bce0dcc921 141 {
wataloh 0:20bce0dcc921 142 if(self->piafLib)
wataloh 0:20bce0dcc921 143 {
wataloh 0:20bce0dcc921 144 self->piafLib->mcuISR();
wataloh 0:20bce0dcc921 145 }
wataloh 0:20bce0dcc921 146 }
wataloh 0:20bce0dcc921 147
wataloh 14:b205267fa5f6 148 bool
wataloh 14:b205267fa5f6 149 AferoCommHndlr::_onGetTodo(ToDo *todo)
wataloh 0:20bce0dcc921 150 {
wataloh 14:b205267fa5f6 151 _onGetTodoP = &AferoCommHndlr::_onGetTodoFalsy;
wataloh 0:20bce0dcc921 152 char buf[255];
wataloh 0:20bce0dcc921 153 memset(buf,'\0',sizeof(buf));
wataloh 0:20bce0dcc921 154 if(todo!=NULL)
wataloh 0:20bce0dcc921 155 {
wataloh 2:dfe671e31221 156 if(PREFERENCES::FLOW_CONTROL==true)
wataloh 1:b2a9a6f2c30e 157 {
wataloh 2:dfe671e31221 158 PREFERENCES::_crc32 crc32;
wataloh 2:dfe671e31221 159 todo->toBASE64(buf,&crc32);
wataloh 14:b205267fa5f6 160 if(piafLib->setAttribute(ATTR_ID_SENSE_VAL,strlen(buf),buf) != afSUCCESS)
wataloh 14:b205267fa5f6 161 {
wataloh 14:b205267fa5f6 162 SERIAL_PRINT_DBG("set attr failed!!\n");
wataloh 14:b205267fa5f6 163 return false;
wataloh 14:b205267fa5f6 164 }
wataloh 14:b205267fa5f6 165 if(piafLib->setAttribute64(3,crc32.ui32) != afSUCCESS)
wataloh 14:b205267fa5f6 166 {
wataloh 14:b205267fa5f6 167 SERIAL_PRINT_DBG("set attr failed!!\n");
wataloh 14:b205267fa5f6 168 return false;
wataloh 14:b205267fa5f6 169 }
wataloh 1:b2a9a6f2c30e 170 }
wataloh 1:b2a9a6f2c30e 171 else
wataloh 2:dfe671e31221 172 {
wataloh 1:b2a9a6f2c30e 173 todo->toJSON(buf);
wataloh 14:b205267fa5f6 174 int ret = afSUCCESS;
wataloh 14:b205267fa5f6 175 if((ret=piafLib->setAttribute(ATTR_ID_SENSE_VAL,strlen(buf),buf)) != afSUCCESS)
wataloh 14:b205267fa5f6 176 {
wataloh 14:b205267fa5f6 177 SERIAL_PRINT_DBG("set attr failed!! : %d\n", ret);
wataloh 14:b205267fa5f6 178 todo->error_count_inc();
wataloh 14:b205267fa5f6 179 return false;
wataloh 14:b205267fa5f6 180 }
wataloh 14:b205267fa5f6 181 else
wataloh 14:b205267fa5f6 182 {
wataloh 14:b205267fa5f6 183 SERIAL_PRINT_DBG("set attr success!! : %d\n", ret);
wataloh 14:b205267fa5f6 184 SERIAL_PRINT_DBG("%s\n",buf);
wataloh 14:b205267fa5f6 185 todo->error_count_clear();
wataloh 14:b205267fa5f6 186 return true;
wataloh 14:b205267fa5f6 187 }
wataloh 1:b2a9a6f2c30e 188 }
wataloh 0:20bce0dcc921 189 }
wataloh 14:b205267fa5f6 190 return true;
wataloh 0:20bce0dcc921 191 }
wataloh 14:b205267fa5f6 192
wataloh 14:b205267fa5f6 193 bool
wataloh 14:b205267fa5f6 194 AferoCommHndlr::_onGetTodoFalsy(ToDo *todo)
wataloh 14:b205267fa5f6 195 {
wataloh 14:b205267fa5f6 196 return false;
wataloh 14:b205267fa5f6 197 }
wataloh 14:b205267fa5f6 198
wataloh 14:b205267fa5f6 199 bool
wataloh 14:b205267fa5f6 200 AferoCommHndlr::onGetTodo(ToDo *todo)
wataloh 14:b205267fa5f6 201 {
wataloh 14:b205267fa5f6 202 return (self->*(self->_onGetTodoP))(todo);
wataloh 14:b205267fa5f6 203 }
wataloh 14:b205267fa5f6 204
wataloh 14:b205267fa5f6 205 void
wataloh 14:b205267fa5f6 206 AferoCommHndlr::onSetAttrCmpDelay()
wataloh 14:b205267fa5f6 207 {
wataloh 14:b205267fa5f6 208 SERIAL_PRINT_DBG("onSetAttrCmpDelay triggered\n");
wataloh 14:b205267fa5f6 209 self->_onGetTodoP = &AferoCommHndlr::_onGetTodo;
wataloh 14:b205267fa5f6 210 }