#include <Arduino.h>
#include "TeensyThreads.h"
#include <i2c_t3.h>
int count = 0;
const int LED = 13;
Threads::Mutex i2c_lock;
Threads::Mutex serial_lock;
void setup() {
Wire.begin(I2C_MASTER, 0x00, I2C_PINS_18_19, I2C_PULLUP_EXT, 100000);
Wire.setDefaultTimeout(200000); // 200ms
pinMode(LED, OUTPUT);
auto blinky = threads.addThread(blinkthread);
// threads.setMicroTimer(10);
threads.setTimeSlice(blinky, 1);
}
void loop() {
byte x, y;
{
Threads::Scope locker(i2c_lock);
Wire.beginTransmission(32); Wire.write(0x0C); Wire.write(0xFF); Wire.write(0xFF); Wire.endTransmission();
Wire.beginTransmission(32); Wire.write(0x12); Wire.endTransmission(); Wire.requestFrom(32, 2);
x = Wire.read();
y = Wire.read();
}
{
Threads::Scope locker(serial_lock);
if ( x == 191 && y == 127 ) {
Serial.print("Wire Register Read Thread1: ");
Serial.print(x);
Serial.print(" : ");
Serial.print(y);
Serial.println(" GOOD!");
}
else {
Serial.print("Wire Register Read Thread1: ");
Serial.print(x);
Serial.print(" : ");
Serial.print(y);
Serial.println(" FAIL!");
}
}
delay(1);
}
void blinkthread() {
while (1) {
byte x, y;
{
Threads::Scope locker(i2c_lock);
Wire.beginTransmission(32); Wire.write(0x0C); Wire.write(0xFF); Wire.write(0xFF); Wire.endTransmission();
Wire.beginTransmission(32); Wire.write(0x13); Wire.endTransmission(); Wire.requestFrom(32, 1);
x = Wire.read();
Wire.beginTransmission(32); Wire.write(0x12); Wire.endTransmission(); Wire.requestFrom(32, 1);
y = Wire.read();
}
{
Threads::Scope locker(serial_lock);
if ( x == 127 && y == 191 ) {
Serial.print("Wire Register Read Thread2: ");
Serial.print(x);
Serial.print(" : ");
Serial.print(y);
Serial.println(" GOOD!");
}
else {
Serial.print("Wire Register Read Thread2: ");
Serial.print(x);
Serial.print(" : ");
Serial.print(y);
Serial.println(" FAIL!");
}
}
}
}