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

spi/AferoCommHndlr.cpp

Committer:
wataloh
Date:
2017-01-25
Revision:
4:b38e09f24711
Parent:
2:dfe671e31221
Child:
6:88cc04eb613a

File content as of revision 4:b38e09f24711:

#include "AferoCommHndlr.h"

Necochan *Necochan::ref = NULL;

Necochan::Necochan()
{
    if(ref==NULL)
    {
        ref = this;
    }
    out = new DigitalOut(PTB8,1);
}

void Necochan::release()
{
    DigitalOut *p = ref->out;
    *p = 1;
}

void Necochan::push()
{
    SERIAL_PRINT_DBG("push meow\n");
    *out = 0;
    timeout.attach(&release,1);
}

static AferoCommHndlr *self = NULL;

AferoCommHndlr::AferoCommHndlr() : flowControlEnabled(false)
{
    necochan = new Necochan();
}

void
AferoCommHndlr::loop()
{
    piafLib->loop();
    todoQ->loop();
}

AferoCommHndlr*
AferoCommHndlr::create(Timer *timer, mbedSPI *spi)
{
     SERIAL_PRINT_DBG("HELLO\n");
     self = new AferoCommHndlr();
     self->timer = timer;
     self->piafLib = iafLib::create((PinName)PINS::ASR_1::SPI::SR,
       AferoCommHndlr::fco_irq_fall,
       AferoCommHndlr::myOnAttributeSet,
       AferoCommHndlr::myOnAttributeSetComplete, spi);
     self->todoQ = ToDoQ::create(AferoCommHndlr::onGetTodo);
     SERIAL_PRINT_DBG("AferoCommHndlr init done\n");
     self->piafLib->setAttribute8(1,1);
     
     return self;
}

void
AferoCommHndlr::myOnAttributeSet(
    const uint8_t requestId,
    const uint16_t attributeId,
    const uint16_t valueLen,
    const uint8_t *value)
{
    self->piafLib->setAttributeComplete(requestId, attributeId, valueLen, value);
}

int32_t
AferoCommHndlr::sanitizeSensingInterval(uint16_t attributeId, int32_t interval)
{
    if(interval < 5)
    {
        interval = 5;
        self->piafLib->setAttribute32(attributeId, interval);
    }
    return interval;
}

void
AferoCommHndlr::myOnAttributeSetComplete(
    const uint8_t requestId,
    const uint16_t attributeId,
    const uint16_t valueLen,
    const uint8_t *value)
{
//    uint8_t *buf = new uint8_t[valueLen+1];
//    memset(buf,'\0',valueLen+1);
//    memcpy(buf, value, valueLen);
//    SERIAL_PRINT_DBG("attr id:%d value:%s\n",attributeId,buf);
//    delete[] buf;
//    buf = NULL;
    
    int32_t sensing_interval;
    char *buf = NULL;
    if(attributeId >= 4 && attributeId <= 8)
    {
        sensing_interval = *((int32_t*)value);
    }
    
    if(attributeId==2)
    {
        PREFERENCES::CRC32.ui32 = *((uint32_t*)value);
    }
    else if(attributeId==4)
    {
        buf = "PREFERENCES::SENSING_INTERVAL[SENSORS::ACCELEROMETER]";
        PREFERENCES::SENSING_INTERVAL[SENSORS::ACCELEROMETER] = self->sanitizeSensingInterval(attributeId,sensing_interval);
    }
    else if(attributeId==5)
    {
        buf = "PREFERENCES::SENSING_INTERVAL[SENSORS::COLOR]";
        PREFERENCES::SENSING_INTERVAL[SENSORS::COLOR] = self->sanitizeSensingInterval(attributeId,sensing_interval);
    }
    else if(attributeId==6)
    {
        buf = "PREFERENCES::SENSING_INTERVAL[SENSORS::PRESSURE]";
        PREFERENCES::SENSING_INTERVAL[SENSORS::PRESSURE] = self->sanitizeSensingInterval(attributeId,sensing_interval);
    }
    else if(attributeId==7)
    {
        buf = "PREFERENCES::SENSING_INTERVAL[SENSORS::CURRENT_TRANS]";
        PREFERENCES::SENSING_INTERVAL[SENSORS::CURRENT_TRANS] = self->sanitizeSensingInterval(attributeId,sensing_interval);
    }
    else if(attributeId==8)
    {
        buf = "PREFERENCES::SENSING_INTERVAL[SENSORS::TEMPERATURE]";
        PREFERENCES::SENSING_INTERVAL[SENSORS::TEMPERATURE] = self->sanitizeSensingInterval(attributeId,sensing_interval);
    }
    else if(attributeId==9)
    {
        SERIAL_PRINT_DBG("time stamp:%ld\n", *((uint32_t*)value));
        RTC_Handler::getInstance()->setUTC(*((uint32_t*)value));
    }
    else if(attributeId==1024)
    {
        self->necochan->push();
    }
    
    if(attributeId >= 4 && attributeId <= 8)
    {
        SERIAL_PRINT_DBG("%s=%ld\n",buf,sensing_interval);
    }
    
}

void
AferoCommHndlr::fco_irq_fall()
{
    if(self->piafLib)
    {
        self->piafLib->mcuISR();
    }
}

void
AferoCommHndlr::onGetTodo(ToDo *todo)
{
    char buf[255];
    memset(buf,'\0',sizeof(buf));
    if(todo!=NULL)
    {
        if(PREFERENCES::FLOW_CONTROL==true)
        {
            PREFERENCES::_crc32 crc32;
            todo->toBASE64(buf,&crc32);
            self->piafLib->setAttribute(ATTR_ID_SENSE_VAL,strlen(buf),buf);
            self->piafLib->setAttribute64(3,crc32.ui32);
        }
        else
        {
            todo->toJSON(buf);
            SERIAL_PRINT_DBG("%s\n",buf);
            self->piafLib->setAttribute(ATTR_ID_SENSE_VAL,strlen(buf),buf);
        }
    }
}