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:
Wed Jan 25 08:20:47 2017 +0000
Revision:
4:b38e09f24711
Parent:
2:dfe671e31221
Child:
6:88cc04eb613a
update

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wataloh 0:20bce0dcc921 1 #include "AferoCommHndlr.h"
wataloh 0:20bce0dcc921 2
wataloh 1:b2a9a6f2c30e 3 Necochan *Necochan::ref = NULL;
wataloh 1:b2a9a6f2c30e 4
wataloh 1:b2a9a6f2c30e 5 Necochan::Necochan()
wataloh 1:b2a9a6f2c30e 6 {
wataloh 1:b2a9a6f2c30e 7 if(ref==NULL)
wataloh 1:b2a9a6f2c30e 8 {
wataloh 1:b2a9a6f2c30e 9 ref = this;
wataloh 1:b2a9a6f2c30e 10 }
wataloh 1:b2a9a6f2c30e 11 out = new DigitalOut(PTB8,1);
wataloh 1:b2a9a6f2c30e 12 }
wataloh 1:b2a9a6f2c30e 13
wataloh 1:b2a9a6f2c30e 14 void Necochan::release()
wataloh 1:b2a9a6f2c30e 15 {
wataloh 1:b2a9a6f2c30e 16 DigitalOut *p = ref->out;
wataloh 1:b2a9a6f2c30e 17 *p = 1;
wataloh 1:b2a9a6f2c30e 18 }
wataloh 1:b2a9a6f2c30e 19
wataloh 1:b2a9a6f2c30e 20 void Necochan::push()
wataloh 1:b2a9a6f2c30e 21 {
wataloh 1:b2a9a6f2c30e 22 SERIAL_PRINT_DBG("push meow\n");
wataloh 1:b2a9a6f2c30e 23 *out = 0;
wataloh 1:b2a9a6f2c30e 24 timeout.attach(&release,1);
wataloh 1:b2a9a6f2c30e 25 }
wataloh 1:b2a9a6f2c30e 26
wataloh 0:20bce0dcc921 27 static AferoCommHndlr *self = NULL;
wataloh 0:20bce0dcc921 28
wataloh 1:b2a9a6f2c30e 29 AferoCommHndlr::AferoCommHndlr() : flowControlEnabled(false)
wataloh 0:20bce0dcc921 30 {
wataloh 1:b2a9a6f2c30e 31 necochan = new Necochan();
wataloh 0:20bce0dcc921 32 }
wataloh 0:20bce0dcc921 33
wataloh 0:20bce0dcc921 34 void
wataloh 0:20bce0dcc921 35 AferoCommHndlr::loop()
wataloh 0:20bce0dcc921 36 {
wataloh 0:20bce0dcc921 37 piafLib->loop();
wataloh 0:20bce0dcc921 38 todoQ->loop();
wataloh 0:20bce0dcc921 39 }
wataloh 0:20bce0dcc921 40
wataloh 0:20bce0dcc921 41 AferoCommHndlr*
wataloh 0:20bce0dcc921 42 AferoCommHndlr::create(Timer *timer, mbedSPI *spi)
wataloh 0:20bce0dcc921 43 {
wataloh 0:20bce0dcc921 44 SERIAL_PRINT_DBG("HELLO\n");
wataloh 0:20bce0dcc921 45 self = new AferoCommHndlr();
wataloh 0:20bce0dcc921 46 self->timer = timer;
wataloh 1:b2a9a6f2c30e 47 self->piafLib = iafLib::create((PinName)PINS::ASR_1::SPI::SR,
wataloh 0:20bce0dcc921 48 AferoCommHndlr::fco_irq_fall,
wataloh 0:20bce0dcc921 49 AferoCommHndlr::myOnAttributeSet,
wataloh 0:20bce0dcc921 50 AferoCommHndlr::myOnAttributeSetComplete, spi);
wataloh 0:20bce0dcc921 51 self->todoQ = ToDoQ::create(AferoCommHndlr::onGetTodo);
wataloh 0:20bce0dcc921 52 SERIAL_PRINT_DBG("AferoCommHndlr init done\n");
wataloh 1:b2a9a6f2c30e 53 self->piafLib->setAttribute8(1,1);
wataloh 1:b2a9a6f2c30e 54
wataloh 0:20bce0dcc921 55 return self;
wataloh 0:20bce0dcc921 56 }
wataloh 0:20bce0dcc921 57
wataloh 0:20bce0dcc921 58 void
wataloh 0:20bce0dcc921 59 AferoCommHndlr::myOnAttributeSet(
wataloh 0:20bce0dcc921 60 const uint8_t requestId,
wataloh 0:20bce0dcc921 61 const uint16_t attributeId,
wataloh 0:20bce0dcc921 62 const uint16_t valueLen,
wataloh 0:20bce0dcc921 63 const uint8_t *value)
wataloh 0:20bce0dcc921 64 {
wataloh 0:20bce0dcc921 65 self->piafLib->setAttributeComplete(requestId, attributeId, valueLen, value);
wataloh 0:20bce0dcc921 66 }
wataloh 0:20bce0dcc921 67
wataloh 4:b38e09f24711 68 int32_t
wataloh 4:b38e09f24711 69 AferoCommHndlr::sanitizeSensingInterval(uint16_t attributeId, int32_t interval)
wataloh 4:b38e09f24711 70 {
wataloh 4:b38e09f24711 71 if(interval < 5)
wataloh 4:b38e09f24711 72 {
wataloh 4:b38e09f24711 73 interval = 5;
wataloh 4:b38e09f24711 74 self->piafLib->setAttribute32(attributeId, interval);
wataloh 4:b38e09f24711 75 }
wataloh 4:b38e09f24711 76 return interval;
wataloh 4:b38e09f24711 77 }
wataloh 4:b38e09f24711 78
wataloh 0:20bce0dcc921 79 void
wataloh 0:20bce0dcc921 80 AferoCommHndlr::myOnAttributeSetComplete(
wataloh 0:20bce0dcc921 81 const uint8_t requestId,
wataloh 0:20bce0dcc921 82 const uint16_t attributeId,
wataloh 0:20bce0dcc921 83 const uint16_t valueLen,
wataloh 0:20bce0dcc921 84 const uint8_t *value)
wataloh 0:20bce0dcc921 85 {
wataloh 2:dfe671e31221 86 // uint8_t *buf = new uint8_t[valueLen+1];
wataloh 2:dfe671e31221 87 // memset(buf,'\0',valueLen+1);
wataloh 2:dfe671e31221 88 // memcpy(buf, value, valueLen);
wataloh 2:dfe671e31221 89 // SERIAL_PRINT_DBG("attr id:%d value:%s\n",attributeId,buf);
wataloh 2:dfe671e31221 90 // delete[] buf;
wataloh 2:dfe671e31221 91 // buf = NULL;
wataloh 1:b2a9a6f2c30e 92
wataloh 4:b38e09f24711 93 int32_t sensing_interval;
wataloh 4:b38e09f24711 94 char *buf = NULL;
wataloh 4:b38e09f24711 95 if(attributeId >= 4 && attributeId <= 8)
wataloh 4:b38e09f24711 96 {
wataloh 4:b38e09f24711 97 sensing_interval = *((int32_t*)value);
wataloh 4:b38e09f24711 98 }
wataloh 4:b38e09f24711 99
wataloh 2:dfe671e31221 100 if(attributeId==2)
wataloh 2:dfe671e31221 101 {
wataloh 2:dfe671e31221 102 PREFERENCES::CRC32.ui32 = *((uint32_t*)value);
wataloh 2:dfe671e31221 103 }
wataloh 2:dfe671e31221 104 else if(attributeId==4)
wataloh 2:dfe671e31221 105 {
wataloh 4:b38e09f24711 106 buf = "PREFERENCES::SENSING_INTERVAL[SENSORS::ACCELEROMETER]";
wataloh 4:b38e09f24711 107 PREFERENCES::SENSING_INTERVAL[SENSORS::ACCELEROMETER] = self->sanitizeSensingInterval(attributeId,sensing_interval);
wataloh 2:dfe671e31221 108 }
wataloh 2:dfe671e31221 109 else if(attributeId==5)
wataloh 2:dfe671e31221 110 {
wataloh 4:b38e09f24711 111 buf = "PREFERENCES::SENSING_INTERVAL[SENSORS::COLOR]";
wataloh 4:b38e09f24711 112 PREFERENCES::SENSING_INTERVAL[SENSORS::COLOR] = self->sanitizeSensingInterval(attributeId,sensing_interval);
wataloh 2:dfe671e31221 113 }
wataloh 2:dfe671e31221 114 else if(attributeId==6)
wataloh 2:dfe671e31221 115 {
wataloh 4:b38e09f24711 116 buf = "PREFERENCES::SENSING_INTERVAL[SENSORS::PRESSURE]";
wataloh 4:b38e09f24711 117 PREFERENCES::SENSING_INTERVAL[SENSORS::PRESSURE] = self->sanitizeSensingInterval(attributeId,sensing_interval);
wataloh 2:dfe671e31221 118 }
wataloh 2:dfe671e31221 119 else if(attributeId==7)
wataloh 2:dfe671e31221 120 {
wataloh 4:b38e09f24711 121 buf = "PREFERENCES::SENSING_INTERVAL[SENSORS::CURRENT_TRANS]";
wataloh 4:b38e09f24711 122 PREFERENCES::SENSING_INTERVAL[SENSORS::CURRENT_TRANS] = self->sanitizeSensingInterval(attributeId,sensing_interval);
wataloh 2:dfe671e31221 123 }
wataloh 2:dfe671e31221 124 else if(attributeId==8)
wataloh 2:dfe671e31221 125 {
wataloh 4:b38e09f24711 126 buf = "PREFERENCES::SENSING_INTERVAL[SENSORS::TEMPERATURE]";
wataloh 4:b38e09f24711 127 PREFERENCES::SENSING_INTERVAL[SENSORS::TEMPERATURE] = self->sanitizeSensingInterval(attributeId,sensing_interval);
wataloh 2:dfe671e31221 128 }
wataloh 2:dfe671e31221 129 else if(attributeId==9)
wataloh 2:dfe671e31221 130 {
wataloh 2:dfe671e31221 131 SERIAL_PRINT_DBG("time stamp:%ld\n", *((uint32_t*)value));
wataloh 2:dfe671e31221 132 RTC_Handler::getInstance()->setUTC(*((uint32_t*)value));
wataloh 2:dfe671e31221 133 }
wataloh 2:dfe671e31221 134 else if(attributeId==1024)
wataloh 1:b2a9a6f2c30e 135 {
wataloh 1:b2a9a6f2c30e 136 self->necochan->push();
wataloh 1:b2a9a6f2c30e 137 }
wataloh 4:b38e09f24711 138
wataloh 4:b38e09f24711 139 if(attributeId >= 4 && attributeId <= 8)
wataloh 4:b38e09f24711 140 {
wataloh 4:b38e09f24711 141 SERIAL_PRINT_DBG("%s=%ld\n",buf,sensing_interval);
wataloh 4:b38e09f24711 142 }
wataloh 4:b38e09f24711 143
wataloh 0:20bce0dcc921 144 }
wataloh 0:20bce0dcc921 145
wataloh 0:20bce0dcc921 146 void
wataloh 0:20bce0dcc921 147 AferoCommHndlr::fco_irq_fall()
wataloh 0:20bce0dcc921 148 {
wataloh 0:20bce0dcc921 149 if(self->piafLib)
wataloh 0:20bce0dcc921 150 {
wataloh 0:20bce0dcc921 151 self->piafLib->mcuISR();
wataloh 0:20bce0dcc921 152 }
wataloh 0:20bce0dcc921 153 }
wataloh 0:20bce0dcc921 154
wataloh 0:20bce0dcc921 155 void
wataloh 0:20bce0dcc921 156 AferoCommHndlr::onGetTodo(ToDo *todo)
wataloh 0:20bce0dcc921 157 {
wataloh 0:20bce0dcc921 158 char buf[255];
wataloh 0:20bce0dcc921 159 memset(buf,'\0',sizeof(buf));
wataloh 0:20bce0dcc921 160 if(todo!=NULL)
wataloh 0:20bce0dcc921 161 {
wataloh 2:dfe671e31221 162 if(PREFERENCES::FLOW_CONTROL==true)
wataloh 1:b2a9a6f2c30e 163 {
wataloh 2:dfe671e31221 164 PREFERENCES::_crc32 crc32;
wataloh 2:dfe671e31221 165 todo->toBASE64(buf,&crc32);
wataloh 2:dfe671e31221 166 self->piafLib->setAttribute(ATTR_ID_SENSE_VAL,strlen(buf),buf);
wataloh 2:dfe671e31221 167 self->piafLib->setAttribute64(3,crc32.ui32);
wataloh 1:b2a9a6f2c30e 168 }
wataloh 1:b2a9a6f2c30e 169 else
wataloh 2:dfe671e31221 170 {
wataloh 1:b2a9a6f2c30e 171 todo->toJSON(buf);
wataloh 1:b2a9a6f2c30e 172 SERIAL_PRINT_DBG("%s\n",buf);
wataloh 1:b2a9a6f2c30e 173 self->piafLib->setAttribute(ATTR_ID_SENSE_VAL,strlen(buf),buf);
wataloh 1:b2a9a6f2c30e 174 }
wataloh 0:20bce0dcc921 175 }
wataloh 0:20bce0dcc921 176 }