Created
June 1, 2015 07:13
-
-
Save monpetit/dd59df9ec5c794ed0980 to your computer and use it in GitHub Desktop.
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
#include <TinyWireS.h> | |
#define I2C_SLAVE_ADDRESS 0x19 // the 7-bit address | |
#ifndef TWI_RX_BUFFER_SIZE | |
#define TWI_RX_BUFFER_SIZE (16) | |
#endif | |
#define CMD_LED_ON 0xA0 | |
#define CMD_LED_OFF 0xA1 | |
#define CMD_ADD_NUM 0xB0 | |
#define CMD_GET_RESULT 0xB1 | |
const int led = 3; | |
#define led_on() digitalWrite(led, HIGH) | |
#define led_off() digitalWrite(led, LOW) | |
volatile uint8_t i2c_cmd; | |
void reset_blink(volatile uint32_t count); | |
void receiveEvent(uint8_t howMany); | |
void requestEvent(void); | |
uint8_t buffer[4]; | |
volatile uint16_t sum = 0; | |
uint8_t sumbuffer[2]; | |
uint8_t buffer_index = 0; | |
volatile bool send_flag = false; | |
void setup() | |
{ | |
randomSeed(analogRead(0)); | |
pinMode(3, OUTPUT); | |
reset_blink(30); | |
TinyWireS.begin(I2C_SLAVE_ADDRESS); | |
TinyWireS.onReceive(receiveEvent); | |
TinyWireS.onRequest(requestEvent); | |
} | |
void loop() | |
{ | |
TinyWireS_stop_check(); | |
} | |
void reset_blink(volatile uint32_t count) | |
{ | |
while (count--) { | |
digitalWrite(led, digitalRead(led) ^ 1); | |
delay(20); | |
} | |
} | |
/** | |
* The I2C data received -handler | |
* | |
* This needs to complete before the next incoming transaction | |
* (start, data, restart/stop) on the bus does so be quick, | |
* set flags for long running tasks to be called from the mainloop | |
* instead of running them directly, | |
*/ | |
void receiveEvent(uint8_t howMany) | |
{ | |
if (howMany < 1) { | |
// Sanity-check | |
return; | |
} | |
if (howMany > TWI_RX_BUFFER_SIZE) { | |
// Also insane number | |
return; | |
} | |
i2c_cmd = TinyWireS.receive(); | |
howMany--; | |
if (i2c_cmd == CMD_LED_ON) { | |
led_on(); | |
return; | |
} | |
if (i2c_cmd == CMD_LED_OFF) { | |
led_off(); | |
return; | |
} | |
if (i2c_cmd == CMD_ADD_NUM) { | |
for (int i = 0; i < 4; i++) { | |
buffer[i] = TinyWireS.receive(); | |
} | |
uint16_t x = (buffer[0] << 8) | buffer[1]; | |
uint16_t y = (buffer[2] << 8) | buffer[3]; | |
sum = x + y; | |
sumbuffer[0] = (uint8_t)(sum >> 8); | |
sumbuffer[1] = (uint8_t)(sum & 0xff); | |
} | |
if (i2c_cmd == CMD_GET_RESULT) { | |
send_flag = true; | |
buffer_index = 0; | |
} | |
} | |
/** | |
* This is called for each read request we receive, | |
* never put more than one byte of data (with TinyWireS.send) to the | |
* send-buffer when using this callback | |
*/ | |
void requestEvent(void) | |
{ | |
if (send_flag) { | |
TinyWireS.send(sumbuffer[buffer_index]); | |
buffer_index++; | |
if (buffer_index > 1) { | |
send_flag = false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment