3 #define INT_SLAVE1_PIN 3
4 #define INT_SLAVE2_PIN 2
6 #define SLAVE1_ADDRESS 0x09
7 #define SLAVE2_ADDRESS 0x0A
9 size_t i2c_write(uint8_t address, void *data, size_t size);
10 size_t i2c_read(uint8_t address, void *data, size_t size);
11 void slave1_interrupt(void);
12 void slave2_interrupt(void);
14 volatile bool new_data1 = false;
15 volatile bool new_data2 = false;
21 pinMode(INT_SLAVE1_PIN, INPUT);
22 pinMode(INT_SLAVE2_PIN, INPUT);
23 attachInterrupt(digitalPinToInterrupt(INT_SLAVE1_PIN), slave1_interrupt, RISING);
24 attachInterrupt(digitalPinToInterrupt(INT_SLAVE2_PIN), slave2_interrupt, RISING);
33 i2c_read(SLAVE1_ADDRESS, &dip_sw, sizeof(dip_sw));
34 Serial.print("From slave 1: ");
35 Serial.println(dip_sw);
40 i2c_read(SLAVE2_ADDRESS, &dip_sw, sizeof(dip_sw));
41 Serial.print("From slave 2: ");
42 Serial.println(dip_sw);
47 size_t i2c_write(uint8_t address, void *data, size_t size)
52 Wire.beginTransmission(address);
53 bytes_written = Wire.write((uint8_t *) data, size);
54 error_code = Wire.endTransmission();
56 return error_code ? 0: bytes_written;
59 size_t i2c_read(uint8_t address, void *data, size_t size)
61 size_t bytes_received = Wire.requestFrom(address, size);
63 if (bytes_received == size)
65 for (size_t i = 0; i < size; i++)
67 ((uint8_t *) data)[i] = Wire.read();
71 return bytes_received;
74 void slave1_interrupt(void)
79 void slave2_interrupt(void)