summaryrefslogtreecommitdiff
path: root/lib
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
init commitHEADmaster
Diffstat (limited to 'lib')
-rw-r--r--lib/Arduino_I2C_Port_Expander/src/Arduino_I2C_Port_Expander.cpp92
-rw-r--r--lib/Arduino_I2C_Port_Expander/src/Arduino_I2C_Port_Expander.h31
-rw-r--r--lib/README46
3 files changed, 169 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;
+}
+
+
+
diff --git a/lib/Arduino_I2C_Port_Expander/src/Arduino_I2C_Port_Expander.h b/lib/Arduino_I2C_Port_Expander/src/Arduino_I2C_Port_Expander.h
new file mode 100644
index 0000000..9d9c2b2
--- /dev/null
+++ b/lib/Arduino_I2C_Port_Expander/src/Arduino_I2C_Port_Expander.h
@@ -0,0 +1,31 @@
+#ifndef EXPANDUINO_H
+#define EXPANDUINO_H
+
+#if ARDUINO >= 100
+ #include "Arduino.h"
+#else
+ #include "WProgram.h"
+#endif
+#include <Wire.h>
+
+class EXPAND {
+ public:
+ EXPAND(uint8_t addr);
+ ~EXPAND();
+ void digitalWrite(byte pin,byte val);
+ int digitalRead(byte pin);
+ int digitalReadPullup(byte pin);
+ void analogWrite(byte pin,byte val);
+ int analogRead(byte pin);
+ void sendDataPacket();
+ int receiveResponse();
+
+ void analogWriteSlow(byte pin,byte val);
+ private:
+ uint8_t _addr;
+};
+
+
+#endif
+
+
diff --git a/lib/README b/lib/README
new file mode 100644
index 0000000..6debab1
--- /dev/null
+++ b/lib/README
@@ -0,0 +1,46 @@
+
+This directory is intended for project specific (private) libraries.
+PlatformIO will compile them to static libraries and link into executable file.
+
+The source code of each library should be placed in a an own separate directory
+("lib/your_library_name/[here are source files]").
+
+For example, see a structure of the following two libraries `Foo` and `Bar`:
+
+|--lib
+| |
+| |--Bar
+| | |--docs
+| | |--examples
+| | |--src
+| | |- Bar.c
+| | |- Bar.h
+| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
+| |
+| |--Foo
+| | |- Foo.c
+| | |- Foo.h
+| |
+| |- README --> THIS FILE
+|
+|- platformio.ini
+|--src
+ |- main.c
+
+and a contents of `src/main.c`:
+```
+#include <Foo.h>
+#include <Bar.h>
+
+int main (void)
+{
+ ...
+}
+
+```
+
+PlatformIO Library Dependency Finder will find automatically dependent
+libraries scanning project source files.
+
+More information about PlatformIO Library Dependency Finder
+- https://docs.platformio.org/page/librarymanager/ldf.html