Modified for compatibility with Rev.E. hardware

Fork of AkmSensor by AKM Development Platform

ap1017ctrl.cpp

Committer:
tkstreet
Date:
2017-04-18
Revision:
31:8635be9b2e35
Parent:
26:4e436b0cbaf8
Child:
32:42c6b8fb1922

File content as of revision 31:8635be9b2e35:

#include "ap1017ctrl.h"
#include "debug.h"

Ap1017Ctrl::Ap1017Ctrl()
{
    ap1017 = NULL;
    event = false;
    ticker = NULL;
    sensorName = "";
}

Ap1017Ctrl::~Ap1017Ctrl()
{
    if(ap1017)
        delete ap1017;
}

AkmSensor::Status Ap1017Ctrl::init(const uint8_t id, const uint8_t subid)
{
    primaryId = id;
    subId = subid;

    switch (subId) {                // template for multiple drivers
        case SUB_ID_AP1017:
            ap1017 = new AP1017();
            sensorName = "AP1017";
            break;
        default:
            return AkmSensor::ERROR;
    }
    
    // TODO: Initialize ap1017

    return AkmSensor::SUCCESS;
}

bool Ap1017Ctrl::isEvent()
{
    return event;       // No feedback: always false
}

AkmSensor::Status Ap1017Ctrl::startSensor()
{
    ticker.attach(callback(this, &Ap1017Ctrl::timerCallback), interval);
    return AkmSensor::SUCCESS;
}

AkmSensor::Status Ap1017Ctrl::startSensor(const float sec)
{
    interval = sec;
    ticker.attach(callback(this, &Ap1017Ctrl::timerCallback), interval);
    return AkmSensor::SUCCESS;
}

AkmSensor::Status Ap1017Ctrl::stopSensor()
{
    return AkmSensor::SUCCESS;
}

AkmSensor::Status Ap1017Ctrl::readSensorData(Message* msg)
{
    return AkmSensor::SUCCESS;    
}

AkmSensor::Status Ap1017Ctrl::requestCommand(Message* in, Message* out)
{
    AkmSensor::Status status = AkmSensor::SUCCESS;
    
    Message::Command cmd = in->getCommand();        // Store command
    out->setCommand(cmd);                           // Load command into output
    
    switch(cmd)
    {
        case Message::CMD_MOTOR_START_MOTOR:
        {
            if(ap1017->startMotor() != AP1017::SUCCESS)
            {
                MSG("#AP1017: Failed to start motor.\r\n");
                return AkmSensor::ERROR;
            }
            break;
        }
        case Message::CMD_MOTOR_STOP_MOTOR:
        {
            if(ap1017->stopMotor() != AP1017::SUCCESS)
            {
                MSG("#AP1017: Failed to stop motor.\r\n");
            }
            break;
        }
        case Message::CMD_MOTOR_SET_DIRECTION:
        {
            if(ap1017->setDirection(in->getArgument(0)) != AP1017::SUCCESS)
            {
                MSG("#AP1017: Failed to set motor direction.\r\n");
            }
            break;
        }
        case Message::CMD_MOTOR_SET_DUTY_CYCLE:
        {
            if(ap1017->setDutyCycle(in->getArgument(0)) != AP1017::SUCCESS)
            {
                MSG("#AP1017: Failed to set the duty cycle.\r\n");
            }
            break;
        }
        default:
        {
            MSG("AP1017: Invalid or unimplemented command.\r\n");
            status = AkmSensor::ERROR;
        }
    }
        
    return status;
}

char* Ap1017Ctrl::getSensorName()
{
    return sensorName;
}

void Ap1017Ctrl::timerCallback()
{
    // Timer interrupt handling    
}