Created
January 1, 2018 05:45
-
-
Save IOT-123/05946caa2d4c005431de509875f6936f to your computer and use it in GitHub Desktop.
Code from ESPEasy used for extending DIGITAL/ANALOG pins on D1M WIFI SOC using I2C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/****************************************************************************************************************************\ | |
* Arduino project "ESP Easy" Copyright www.esp8266.nu | |
* | |
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License | |
* as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. | |
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty | |
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | |
* You received a copy of the GNU General Public License along with this program in file 'License.txt'. | |
* | |
* IDE download : https://www.arduino.cc/en/Main/Software | |
* ESP8266 Package : https://github.com/esp8266/Arduino | |
* | |
* Source Code : https://sourceforge.net/projects/espeasy/ | |
* Support : http://www.esp8266.nu | |
* Discussion : http://www.esp8266.nu/forum/ | |
* | |
* Additional information about licensing can be found at : http://www.gnu.org/licenses | |
\*************************************************************************************************************************/ | |
// This file is to be loaded onto an Arduino Pro Mini so it will act as a simple IO extender to the ESP module. | |
// Communication between ESP and Arduino is using the I2C bus, so only two wires needed. | |
// It best to run the Pro Mini on 3V3, although the 16MHz versions do not officially support this voltage level on this frequency. | |
// That way, you can skip levelconverters on I2C. | |
// Arduino Mini Pro uses A4 and A5 for I2C bus. ESP I2C can be configured but they are on GPIO-4 and GPIO-5 by default. | |
#include <Wire.h> | |
#define I2C_MSG_IN_SIZE 4 | |
#define I2C_MSG_OUT_SIZE 4 | |
#define CMD_DIGITAL_WRITE 1 | |
#define CMD_DIGITAL_READ 2 | |
#define CMD_ANALOG_WRITE 3 | |
#define CMD_ANALOG_READ 4 | |
volatile uint8_t sendBuffer[I2C_MSG_OUT_SIZE]; | |
void setup() | |
{ | |
Wire.begin(0x7f); | |
Wire.onReceive(receiveEvent); | |
Wire.onRequest(requestEvent); | |
} | |
void loop() {} | |
void receiveEvent(int count) | |
{ | |
if (count == I2C_MSG_IN_SIZE) | |
{ | |
byte cmd = Wire.read(); | |
byte port = Wire.read(); | |
int value = Wire.read(); | |
value += Wire.read()*256; | |
switch(cmd) | |
{ | |
case CMD_DIGITAL_WRITE: | |
pinMode(port,OUTPUT); | |
digitalWrite(port,value); | |
break; | |
case CMD_DIGITAL_READ: | |
pinMode(port,INPUT_PULLUP); | |
clearSendBuffer(); | |
sendBuffer[0] = digitalRead(port); | |
break; | |
case CMD_ANALOG_WRITE: | |
analogWrite(port,value); | |
break; | |
case CMD_ANALOG_READ: | |
clearSendBuffer(); | |
int valueRead = analogRead(port); | |
sendBuffer[0] = valueRead & 0xff; | |
sendBuffer[1] = valueRead >> 8; | |
break; | |
} | |
} | |
} | |
void clearSendBuffer() | |
{ | |
for(byte x=0; x < sizeof(sendBuffer); x++) | |
sendBuffer[x]=0; | |
} | |
void requestEvent() | |
{ | |
Wire.write((const uint8_t*)sendBuffer,sizeof(sendBuffer)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment