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