AKM Development Platform. This is the D7.014 version.

Dependencies:   AK09970 AK099XX AK7401 AK7451 AK8963X AK9750 AK9752 AkmSensor BLE_API I2CNano MCP342x SerialNano SpiNano TCA9554A mbed nRF51822

Fork of AKDP by Masahiko Fukasawa

Revision:
0:c240899240e7
Child:
1:0914af311974
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Message.h	Thu Apr 28 21:24:22 2016 +0000
@@ -0,0 +1,162 @@
+#ifndef __MESSAGE_H__
+#define __MESSAGE_H__
+
+/**
+ * Message is sent from the host. A message consists of a command and arguments.
+ */
+class Message {
+public:
+    typedef enum {
+        CMD_GET_FW_VERSION = 0x01,                     /**< Gets FW version.                */
+        CMD_GET_MAG_PART = 0x02,                       /**< Gets magnetic sensor ID         */
+        CMD_GET_THRESHOLD = 0x80,                      /**< Gets threshold of AK0997x       */
+        CMD_SET_THRESHOLD_FROM_HOST = 0x81,            /**< Sets threshold of AK0997x from host. */
+        CMD_SET_MEASUREMENT_FREQUENCY = 0x82,          /**< Sets measurement frequency.     */
+        CMD_SEND_SWITCH_MODE_MEASUREMENT_DATA = 0x83,  /**< Sends measurement data on/off   */
+        CMD_SWITCH_MODE_MEASUREMENT_DATA = 0x84,       /**< Measurement data in switch mode */
+        CMD_GET_POLARITY = 0x85,                       /**< Gets polarity of AK0997x        */
+        CMD_SET_POLARITY = 0x86,                       /**< Sets polarity of AK0997x        */
+
+        CMD_IR_GET_THRESHOLD = 0x90,                   /**< Gets threshold of AK9750.       */
+        CMD_IR_SET_THRESHOLD = 0x91,                   /**< Sets threshold of AK9750.       */
+        CMD_IR_GET_HYSTERESIS = 0x92,                  /**< Gets hysteresis setting of AK9750 */
+        CMD_IR_SET_HYSTERESIS = 0x93,                  /**< Sets hysteresis to AK9750       */
+
+        CMD_DOOR_STATE_CHANGED = 0xA0,                 /**< Door state changed.             */
+        CMD_UPDATE_MAG_VECTOR = 0xA1,                  /**< Update magnetic vector state.   */
+        CMD_THRESHOLD_UPDATED = 0xA2,                  /**< Threshold updated.              */
+        CMD_GET_CURRENT_DOOR_STATE = 0xA3,             /**< Gets the current door state.    */
+        CMD_IR_STATE_CHANGED = 0xA5,                   /**< IR state changed.               */
+        CMD_UPDATE_IR_STATE = 0xA6,                    /**< Update IR state 1 or 2          */
+        CMD_GET_CURRENT_HUMAN_PRESENCE_STATE = 0xA7,   /**< Gets the current human presence state. */
+        CMD_IR_THRESHOLD_UPDATED = 0xA8,               /**< IR threshold updated.           */
+
+//        CMD_MOTION_DETECTED = 0xB1,                    /**< Motion detected.                */
+//        CMD_GET_MOTION_THRESHOLD = 0xB2,               /**< Get motion threshold            */
+//        CMD_SET_MOTION_THRESHOLD = 0xB3,               /**< Set motion threshold            */
+        CMD_UNKNOWN = 0x00,                            /**< Unknown command.                */
+
+        CMD_SET_SERIAL_TARGET = 0x0A,                  /**< Set serial output target        */
+        CMD_REG_WRITE = 0x11,                          /**< Register write 1 byte           */
+        CMD_REG_WRITEN = 0x12,                         /**< Register write N bytes          */
+        CMD_REG_READ = 0x13,                           /**< Register read 1 byte            */
+        CMD_REG_READN = 0x14,                          /**< Register read N bytes           */
+        CMD_GET_ID = 0xB0,                             /**< Get connected sensor ID         */
+        CMD_STOP_MEASUREMENT = 0xBA,                   /**< Stop Measurement                */
+        CMD_START_MEASUREMENT = 0xBB,                  /**< Start Measurement               */
+        CMD_ANGLE_ZERO_RESET = 0xBC,                   /**< Angle sensor zero reset         */
+    } Command;
+    
+    typedef enum {
+        ARG_MAG_FREQ_1HZ = 0x00,                           /**< Frequency setting 1 Hz */
+        ARG_MAG_FREQ_10HZ = 0x01,                          /**< Frequency setting 10 Hz */
+        ARG_MAG_FREQ_20HZ = 0x02,                          /**< Frequency setting 20 Hz */
+        ARG_MAG_FREQ_50HZ = 0x03,                          /**< Frequency setting 50 Hz */
+        ARG_MAG_FREQ_100HZ = 0x04,                         /**< Frequency setting 100 Hz */
+        ARG_MAG_FREQ_POWER_DOWN = 0x05,                    /**< Frequency setting 0 Hz (Power-down) */
+        ARG_MAG_FREQ_0P5HZ = 0x06,                         /**< Frequency setting 0.5 Hz */
+        ARG_MAG_FREQ_0P25HZ = 0x07,                        /**< Frequency setting 0.25 Hz */
+    } MagFrequency;
+    
+    typedef enum{
+        SW_OFF = 0x00,
+        SW_ON = 0x01,
+    } SwitchState;
+    
+    typedef enum {
+        SUCCESS = 0,
+        END_OF_STR,
+        ERROR_ILLEGAL_CHAR,
+        ERROR_INDEX_OUT_OF_BOUNDS,
+    } Status;
+    
+    /**
+     * Constructor.
+     */
+    Message() : cmd(CMD_UNKNOWN), num_arg(0){
+    };
+    
+    /**
+     * Convert an ascii into one byte value.
+     * @param out Pointer to the buffer stores result value
+     * @param in Pointer to a hex format ascii value. The value must be expressed by two-byte ascii chars. For example, the value 5 must be expressed as "05", not "5".
+     */   
+    static Status asciiToChar(char *out, const char *in);
+    
+    /**
+     * Convert a byte value into a hex format ascii value. The result value have two-byte length. If the input value is 5, the result string is "05", not "5".
+     * @param out Pointer to the buffer stores result ascii. The buffer must have two byte length.
+     * @param in Pointer to a byte value to be converted.
+     */
+    static void charToAscii(char *out, const char *in);
+
+    /**
+     * Parses given string and construct a message object.
+     * @param msg Pointer to an message object.
+     * @param str Pointer to an string to be parsed.
+     * @return Returns SUCCESS if parsed successfully, otherwise returns other code.
+     */
+    static Status parse(Message *msg, const char *str);
+    
+    /**
+     * Sets command of this message.
+     * @param cmd Command to be set.
+     */
+    void setCommand(Command cmd);
+    
+    /**
+     * Sets argument(s) to this message.
+     * @param arg pointer to the argument(s) to be stored.
+     * @param len length of the argument in bytes
+     */
+    void setArguments(const char *arg, int len);
+    
+    /**
+     * Sets argument at the specified index.
+     * @param arg value of the argument
+     */
+    void setArgument(int index, char arg);
+    
+    /**
+     * Gets the command contained in this message.
+     * @return The command contained in this message.
+     */
+    Command getCommand() const;
+    
+    /**
+     * Gets n th argument of the message.
+     * @param index argument to be gotten. Index must be less than the number obtained from getArgNum().
+     * @return Returns value of the argument specified by index. If the value larger than argsize is specified, returns 0.
+     */
+    char getArgument(int index) const;
+    
+    /**
+     * Gets number of arguments.
+     * @return Returns number of arguments included in this message.
+     */
+    int getArgNum() const;
+
+    /**
+     * Gets maximum message length in byte unit.
+     * @return maximum length of message in byte.
+     */
+    static int getMaxMessageLength();
+    
+    /**
+     * Gets arguments.
+     * @param buf
+     * @param maxNum maximum number of arguments to be get
+     */
+    void getArguments(char *buf, int maxNum) const;
+
+        
+private:
+    const static int NUM_MAX_ARG = 16;                           /**< Maximum number of arguments in message. */
+    const static int MAX_LEN = 1 + 2 + 2*NUM_MAX_ARG + 1 + 1;    /**< Maximum length of message in byte unit. $ + command(2bytes) + data + \r + \n */
+
+    Command cmd;                                       /**< Holds command. */
+    char arg[NUM_MAX_ARG];                             /**< Holds arguments. */
+    int num_arg;                                       /**< Holds number of arguments. */
+};
+
+#endif // __MESSAGE_H__