Skip to content

Instantly share code, notes, and snippets.

@monpetit
Created June 1, 2015 07:16
Show Gist options
  • Save monpetit/cfef6b9d75fb03022723 to your computer and use it in GitHub Desktop.
Save monpetit/cfef6b9d75fb03022723 to your computer and use it in GitHub Desktop.
#include <Wire.h>
#define I2C_SLAVE_ADDRESS 0x19 // the 7-bit address
#define CMD_LED_ON 0xA0
#define CMD_LED_OFF 0xA1
#define CMD_ADD_NUM 0xB0
#define CMD_GET_RESULT 0xB1
void setup()
{
Wire.begin();
Serial.begin(115200);
}
void loop()
{
Wire.beginTransmission(I2C_SLAVE_ADDRESS);
Wire.write(CMD_LED_ON);
Wire.endTransmission();
delay(20);
Wire.beginTransmission(I2C_SLAVE_ADDRESS);
Wire.write(CMD_LED_OFF);
Wire.endTransmission();
delay(1980);
uint16_t x = random(0, 0x7fff);
uint16_t y = random(0, 0x7fff);
uint8_t txbuffer[4];
txbuffer[0] = (x >> 8);
txbuffer[1] = (x & 0xff);
txbuffer[2] = (y >> 8);
txbuffer[3] = (y & 0xff);
Wire.beginTransmission(I2C_SLAVE_ADDRESS);
Wire.write(CMD_ADD_NUM);
for (int i = 0; i < 4; i++) {
Wire.write(txbuffer[i]);
}
Wire.endTransmission();
uint8_t rxbuffer[2];
Wire.beginTransmission(I2C_SLAVE_ADDRESS);
Wire.write(CMD_GET_RESULT);
Wire.endTransmission();
Wire.requestFrom(I2C_SLAVE_ADDRESS, 2);
rxbuffer[0] = Wire.read();
rxbuffer[1] = Wire.read();
uint16_t sum = (rxbuffer[0] << 8) | rxbuffer[1];
Serial.print(x);
Serial.print(" + ");
Serial.print(y);
Serial.print(" = ");
Serial.println(sum);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment