PS/2

Dependents:   Synth Lab3Translator PS2_Keyboard CLI ... more

Revision:
0:7ee6afa15d51
Child:
1:823c2798e398
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PS2.h	Tue Aug 31 11:25:34 2010 +0000
@@ -0,0 +1,88 @@
+/**
+ * PS/2 interface control class (Version 0.0.1)
+ *
+ * Copyright (C) 2010 Shinichiro Nakamura (CuBeatSystems)
+ * http://shinta.main.jp/
+ */
+
+#ifndef _PS2_H_
+#define _PS2_H_
+
+#include "mbed.h"
+#include "Semaphore.h"
+
+/**
+ * PS/2 interface control class.
+ */
+class PS2 {
+public:
+    /**
+     * Create.
+     *
+     * @param clkinpin Input pin for clock.
+     * @param datinpin Input pin for data.
+     */
+    PS2(PinName clkin_pin, PinName datin_pin, PinName clkout_pin = NC, PinName datout_pin = NC);
+
+    /**
+     * Destory.
+     */
+    ~PS2();
+
+    /**
+     * Check exists a data.
+     *
+     * @return true if a data exists.
+     */
+    bool exists(void);
+
+    /**
+     * Get a data into a buffer.
+     *
+     * @param buf A pointer to a buffer.
+     * @param bufsiz A size of the buffer.
+     *
+     * @return Number of a byte size.
+     */
+    int getData(uint8_t *buf, size_t bufsiz);
+
+private:
+    InterruptIn clkin;
+    DigitalIn datin;
+    DigitalOut clkout;
+    DigitalOut datout;
+    Timeout timeout;
+    Semaphore sem;
+    int writepoint;
+    int readpoint;
+    static const int TIMEOUT_US = 10 * 1000;
+    static const int RINGBUFSIZ = 16;
+    static const int DATABUFSIZ = 32;
+
+    typedef enum {
+        Idle,
+        Reading,
+        Writing
+    } State;
+
+    typedef struct {
+        State state;
+        int bitcnt;
+        int bytecnt;
+        int errcnt;
+        uint8_t buffer[DATABUFSIZ];
+    } work_t;
+    work_t work;
+
+    typedef struct {
+        int bytecnt;
+        uint8_t buffer[DATABUFSIZ];
+    } data_t;
+    data_t ringbuffer[RINGBUFSIZ];
+
+    void func_timeout(void);
+    void func_fall(void);
+    void init_work(void);
+};
+
+#endif