Modified for compatibility with Rev.E. hardware

Fork of AkmSensor by AKM Development Platform

akmsensor.h

Committer:
masahikofukasawa
Date:
2017-05-24
Revision:
37:c76d2edf3426
Parent:
34:1ea3357c8d9a
Child:
40:42e48427e4b7

File content as of revision 37:c76d2edf3426:

#ifndef AKMSENSOR_H
#define AKMSENSOR_H

#include "mbed.h"
#include "Message.h"
//#include "debug.h"

// SPI Pin Number Definitions
#define SPI_SCK                          P0_8       // SPI, SCK 
#define SPI_MISO                         P0_9       // SPI, MISO
#define SPI_MOSI                         P0_10      //SPI, MOSI
#define SPI_CS                           P0_11      //SPI, CS
#define SPI_DRDY                         P0_7       //SPI, DRDY

// I2C Pin Number Definitions
#define I2C_SCL                          P0_8       //I2C, SCL
#define I2C_SDA                          P0_10      //I2C, SDA

// Digital Port Pin Number Definitions
#define DIGITAL_D0                       P0_11      
#define DIGITAL_D1                       P0_9

// DRDY Port Pin Number Definitions
#define I2C_DRDY                         P0_11

// Analog In Pin Number Definitions
#define ANALOG_IN_PIN                    P0_5       //P05

// I2C Speed Standards
#define I2C_SPEED_100KHZ                100000
#define I2C_SPEED_400KHZ                400000

// Sampling rate for ADC
#define SENSOR_SAMPLING_RATE            0.1         // 10Hz

/**
 * Abstract base class for general AKM sensors.
 */
class AkmSensor
{

public:
    /**
     * Error type for debugging purposes.
     */
    typedef enum {

        SUCCESS = 0,            /**< Successful termination (0) */
        ERROR,                  /**< Error during execution (1) */
    } Status;
    
    /**
     * Primary IDs for the major categories of sensors.
     */
    typedef enum {
        AKM_PRIMARY_ID_AKD_SPI              = 0x0,      /**< AKD SPI Devices */
        AKM_PRIMARY_ID_UNIPOLAR             = 0x1,      /**< Unipolar Switches */
        AKM_PRIMARY_ID_OMNIPOLAR            = 0x2,      /**< Omnipolar Switches */
        AKM_PRIMARY_ID_LATCH                = 0x3,      /**< Bipolar Latches */
        AKM_PRIMARY_ID_DUAL_OUTPUT          = 0x4,      /**< Dual Output Switches */
        AKM_PRIMARY_ID_ONECHIP_ENCODER      = 0x5,      /**< One-Chip Encoders */
        AKM_PRIMARY_ID_TBD1                 = 0x6,      /**< Undefined */
        AKM_PRIMARY_ID_CURRENT_SENSOR_3V    = 0x7,      /**< Current Sensor 3V Output */
        AKM_PRIMARY_ID_DEMO                 = 0x8,      /**< Demo Sensors */
        AKM_PRIMARY_ID_CURRENT_SENSOR_5V    = 0x9,      /**< Current Sensors 5V Output */
        AKM_PRIMARY_ID_MISC_ANALOG          = 0xA,      /**< Analog Devices */
        AKM_PRIMARY_ID_LINEAR_SENSOR        = 0xB,      /**< Linear Sensors */
        AKM_PRIMARY_ID_MOTOR_DRIVER         = 0xC,      /**< Motor Drivers */
        AKM_PRIMARY_ID_IR_SENSOR            = 0xD,      /**< IR Sensors */
        AKM_PRIMARY_ID_ANGLE_SENSOR         = 0xE,      /**< Angle Sensors */
        AKM_PRIMARY_ID_AKD_I2C              = 0xF,      /**< AKD I2C Devices */
    } SensorPrimaryId;


    virtual ~AkmSensor(){};

    /**
     * Process for intializing the selected sensor.
     *
     * @return Termination status type for debugging purposes.
     */
    virtual Status init(const uint8_t id, const uint8_t subid) = 0;
    
    /**
     * Process abstraction for starting sensor operation.
     *
     * @return Termination status type for debugging purposes.
     */
    virtual Status startSensor() = 0;
    
    /**
     * Process abstraction for starting sensor operation.
     *
     * @param sec Number of seconds of operation.
     * @return Termination status type for debugging purposes.
     */
    virtual Status startSensor(const float sec) = 0;
    
    /**
     * Process abstraction for stopping sensor operation.
     *
     * @return Termination status type for debugging purposes.
     */
    virtual Status stopSensor() = 0;
    
    /**
     * Process abstraction for reading data from the sensor.
     *
     * @param msg Message object that will hold the sensor data.
     * @return Termination status type for debugging purposes.
     */
    virtual Status readSensorData(Message* msg) = 0;
    
    /**
     * Primary process for interfacing a sensor with the AKDP.  When implemented
     * in sensor class, it will transfer commands between the the sensor control
     * class and AkmSensorManager. 
     *
     * @param in Command message to be processed by sensor.
     * @param out Message returned from sensor.
     * @return Termination status type for debugging purposes.
     */
    virtual Status requestCommand(Message* in, Message* out) = 0;
    
    /**
     * Set event flag.
     */
    virtual void setEvent(){
//        MSG("#setEvent called.\r\n");
        event = true;
    }
    
    /**
     * Clear event flag.
     */
    void clearEvent(){
//        MSG("#clearEvent called.\r\n");
        event = false;
    }
    
    /**
     * Checks if an event has occurred.
     *
     * @return TRUE if event has occurred, FALSE otherwise.
     */
    bool isEvent(){
        return event;
    }
    
    /**
     * Retrieve the name of the sensor.
     *
     * @return Name of sensor as a character array.
     */
    char* getSensorName(){
        return sensorName;
    };
    
    /**
     * Retrieve the primary ID of the sensor.
     *
     * @return Primary ID as an integer.
     */
    int getPrimaryId(){
        return primaryId;
    };
    
    /**
     * Retrieve the Sub-ID of the sensor.
     *
     * @return Sub-ID as an integer.
     */
    int getSecondaryId(){
        return subId;
    };

private:
    bool            event;

protected:
    uint8_t         primaryId;
    uint8_t         subId;
    char*           sensorName;
    
    AkmSensor(){
        event = false;
        primaryId = 0;
        subId = 0;
        sensorName = "";
    };
};

#endif