Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Use serial input from your D1M WIFI BLOCK to test the analog and digital pins via I2C to the D1M ARDUPOMI+ BLOCK.
#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
boolean _newData = false;
const byte _numChars = 32;
char _receivedChars[_numChars]; // an array to store the received data
volatile uint8_t sendBuffer[2];
void setup() {
Serial.begin(9600);
Wire.begin(D2, D1);
delay(5000);
}
void loop() {
recvWithEndMarker();
parseSendCommands();
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
while (Serial.available() > 0 && _newData == false) {
rc = Serial.read();
if (rc != endMarker) {
_receivedChars[ndx] = rc;
ndx++;
if (ndx >= _numChars) {
ndx = _numChars - 1;
}
} else {
_receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
_newData = true;
}
}
}
void parseSendCommands() {
if (_newData == true) {
const char delim[2] = " ";
char *token;
token = strtok(_receivedChars, delim);
byte cmd = atoi(token);
byte port = 0;
int value = 0;
int index = 0;
while( token != NULL ) {
index++;
switch (index){
case 1:
token = strtok(NULL, delim);
port = atoi(token);
break;
case 2:
token = strtok(NULL, delim);
value = atoi(token);
break;
default:
token = NULL;
}
}
sendCmd(cmd, port, value);
_newData = false;
}
}
void sendCmd(byte cmd, byte port, int value) {
Serial.println("-----");
Serial.println(cmd);
Serial.println(port);
Serial.println(value);
Serial.println("-----");
Wire.beginTransmission(11); // transmit to device #8
Wire.write(cmd); // sends a char
Wire.write(port); // sends one byte
sendBuffer[0] = value & 0xff;
sendBuffer[1] = value >> 8;
Wire.write((const uint8_t*)sendBuffer,sizeof(sendBuffer));
Wire.endTransmission();
byte byteResponse = 0;
int intResponse = 0;
bool hadResponse = false;
if (cmd == CMD_DIGITAL_READ){
Wire.requestFrom(11,2);
while(Wire.available()) // slave may send less than requested
{
hadResponse = true;
byteResponse = Wire.read(); // receive a byte
}
}else if (cmd == CMD_ANALOG_READ){
Wire.requestFrom(11,2);
while(Wire.available()) // slave may send less than requested
{
hadResponse = true;
intResponse = Wire.read();
intResponse += Wire.read() << 8;
}
}
Serial.println("Sending command:");
Serial.println("\t" + String(cmd) + " " + String(port) + " " + String(value) );
if (cmd == CMD_DIGITAL_READ){
if (hadResponse){
Serial.println("Getting response:");
if (byteResponse == 15){
Serial.println("\tHIGH");
}else{
Serial.println("\tLOW");
}
}else{
Serial.println("No response, check the address/connection");
}
} else if (cmd == CMD_ANALOG_READ){
if (hadResponse){
Serial.println("Getting response:");
int mappedVal = map(intResponse, 3840, 61187, 0, 1023);
Serial.println("\t" + String(intResponse));
Serial.println("\tMapped as " + String(mappedVal));
}else{
Serial.println("No response, check the address/connection");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.