summaryrefslogtreecommitdiff
path: root/lib/Arduino_I2C_Port_Expander/src/Arduino_I2C_Port_Expander.cpp
diff options
context:
space:
mode:
authorTorben Egmose <torben.egmose@gmail.com>2021-02-07 22:06:46 +0100
committerTorben Egmose <torben.egmose@gmail.com>2021-02-07 22:06:46 +0100
commitcb38cc24238c70b841e762a9870e46d231761600 (patch)
tree50b8cea71e230f3674277534112332a32430b3e1 /lib/Arduino_I2C_Port_Expander/src/Arduino_I2C_Port_Expander.cpp
init commitHEADmaster
Diffstat (limited to 'lib/Arduino_I2C_Port_Expander/src/Arduino_I2C_Port_Expander.cpp')
-rw-r--r--lib/Arduino_I2C_Port_Expander/src/Arduino_I2C_Port_Expander.cpp92
1 files changed, 92 insertions, 0 deletions
diff --git a/lib/Arduino_I2C_Port_Expander/src/Arduino_I2C_Port_Expander.cpp b/lib/Arduino_I2C_Port_Expander/src/Arduino_I2C_Port_Expander.cpp
new file mode 100644
index 0000000..70470e9
--- /dev/null
+++ b/lib/Arduino_I2C_Port_Expander/src/Arduino_I2C_Port_Expander.cpp
@@ -0,0 +1,92 @@
+#include "Arduino_I2C_Port_Expander.h" //include the declaration for this class
+
+byte io_DataPacket[3];
+byte io_pin;
+byte io_method;
+byte io_value;
+uint8_t _addr;
+
+//<<constructor>>
+EXPAND::EXPAND(uint8_t addr){
+ _addr = addr;
+}
+
+//<<destructor>>
+EXPAND::~EXPAND(){/*nothing to destruct*/}
+
+void EXPAND::digitalWrite(byte pin,byte val){
+ io_DataPacket[0] = 1; // method
+ io_DataPacket[1] = pin; // pin
+ io_DataPacket[2] = val; // val
+ sendDataPacket();
+ int received = receiveResponse();
+}
+
+int EXPAND::digitalRead(byte pin){
+ io_DataPacket[0] = 2; // method
+ io_DataPacket[1] = pin; // pin
+ sendDataPacket();
+ int received = receiveResponse();
+ return received;
+}
+
+int EXPAND::digitalReadPullup(byte pin){
+ io_DataPacket[0] = 3; // method
+ io_DataPacket[1] = pin; // pin
+ sendDataPacket();
+ int received = receiveResponse();
+ return received;
+}
+
+void EXPAND::analogWrite(byte pin,byte val){
+ io_DataPacket[0] = 4; // method
+ io_DataPacket[1] = pin; // pin
+ io_DataPacket[2] = val; // val
+ sendDataPacket();
+ int received = receiveResponse();
+}
+
+int EXPAND::analogRead(byte pin){
+ io_DataPacket[0] = 5; // method
+ io_DataPacket[1] = pin; // pin
+ sendDataPacket();
+ int received = receiveResponse();
+ return received;
+}
+
+void EXPAND::analogWriteSlow(byte pin,byte val){
+ io_DataPacket[0] = 10; // method
+ io_DataPacket[1] = pin; // pin
+ io_DataPacket[2] = val; // val
+ sendDataPacket();
+ int received = receiveResponse();
+}
+
+
+void EXPAND::sendDataPacket(){
+ Wire.beginTransmission(_addr);
+ Wire.write(io_DataPacket, 3);
+ Wire.endTransmission(true);
+ delay(1);
+}
+
+int EXPAND::receiveResponse(){
+ unsigned int receivedValue;
+ int available = Wire.requestFrom(_addr, (byte)1);
+ byte buffer[2];
+ if(available == 1)
+ {
+ buffer[0] = Wire.read();
+ }
+ available = Wire.requestFrom(_addr, (byte)1);
+ if(available == 1)
+ {
+ buffer[1] = Wire.read();
+ }
+ receivedValue = buffer[0] << 8 | buffer[1];
+
+ return receivedValue;
+}
+
+
+