Outher...Miura Souta

Dependencies:   mbed MCP23017

Revision:
1:5b0303768126
Parent:
0:db8d4af513c0
Child:
2:32d2cd7d744b
--- a/MCP/MCP.cpp	Mon Jan 20 08:46:24 2020 +0000
+++ b/MCP/MCP.cpp	Tue Jan 21 11:10:33 2020 +0000
@@ -1,14 +1,50 @@
 #include "MCP.h"
 #include "mbed.h"
+#include "MCP23017.h"
 
 MCP::MCP(PinName sda, PinName scl, uint8_t device_address)
-    :i2c(sda, scl)
+    :i2c(sda, scl), mcp(i2c, device_address) 
 {
-    _write_opcode = device_address & 0xFE; // low order bit = 0 for write
-    _read_opcode  = device_address | 0x01; // low order bit = 1 for read;
+    _iodir_data.all = 0xffff;
+    _pull_data.all  = 0x0000;
+    _read_data.all  = 0x0000;
+    _write_data.all = 0x0000;
+}
+
+void MCP::PinMode(uint8_t pin, pin_mode mode)
+{
+    if(mode == OUTPUT) {
+        _iodir_data.all &= ~(0x0001 << pin);
+    } else if(mode == INPUT) {
+        _iodir_data.all |= (0x0001 << pin);
+    } else if(mode == INPUT_PULLUP) {
+        _iodir_data.all |= (0x0001 << pin);
+        _pull_data.all |= (0x0001 << pin);
+    }
 }
 
-void MCP::PinMode(uint8_t pin, uint8_t mode)
+void MCP::Write(uint8_t pin, bool signal)
+{
+    if(signal == 1) {
+        _write_data.all |= (0x0001 << pin);
+    } else {
+        _write_data.all &= ~(0x0001 << pin);
+    }
+}
+
+bool MCP::Read(uint8_t pin)
 {
-    //_pull_data
-}
\ No newline at end of file
+    return (_read_data.all >> pin) & 0x01;
+}
+        
+void MCP::Update(void)
+{
+    mcp.direction(PORT_A, _iodir_data.port.port_A);
+    mcp.direction(PORT_B, _iodir_data.port.port_B);
+    mcp.configurePullUps(PORT_A, _pull_data.port.port_A);
+    mcp.configurePullUps(PORT_B, _pull_data.port.port_B);
+    _read_data.port.port_A = mcp.read(PORT_A);
+    _read_data.port.port_B = mcp.read(PORT_B);
+   // mcp.write(PORT_A, _write_data.port_A);
+   // mcp.write(PORT_B, _write_data.port_B);
+}