aboutsummaryrefslogtreecommitdiff
path: root/digital-controller/arduino-uno-controller/firmware/firmware.ino
blob: 3735c4a413062d869290da67ec9f94aa1bd58d01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <Wire.h>

#define INPUT_SIZE 100  // TODO: make this a reasonable value
#define sep " "
char input[INPUT_SIZE + 1];

char c_address = '0';
char c_led = '0';
char c_fan = '0';
int address = 0;
int led = 0;
int fan = 0;

void setup() {
  pinMode(A4, OUTPUT);
  pinMode(A5, OUTPUT);
  Wire.begin(); 
  Serial.begin(9600);
}

void loop() {
}

void scan() {
  Serial.println("scanning!");
  byte error, address;

  for(address = 1; address < 127; address++ ) 
  {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    error = Wire.endTransmission();

    if (error == 0)
    {
      Serial.print("I2C device found at address ");
      Serial.println(address);
    } 
  }
  
}

void serialEvent() {  // occurs whenever new data comes in the hardware serial RX
  // read serial into input char array
  byte size = Serial.readBytesUntil('\n', input, INPUT_SIZE);
  input[size] = 0;
  // parse input
  char *c_address = strtok(input, sep);
  char *c_led = strtok(0, sep);
  char *c_fan = strtok(0, sep);
  // convert input
  address = atoi(c_address);
  led = atoi(c_led);
  fan = atoi(c_fan);
  // scan if address is less than zero
  if (address < 0) {
    scan();
  }
  // write to peripheral
  uint8_t buf[1];
  buf[0] = fan;
  buf[1] = led;
  Wire.beginTransmission(address);
  Wire.write(buf, 2);
  Wire.endTransmission();
}

void serialEventRun(void) {
  // this runs inside of the main loop, essentially
  if (Serial.available()) serialEvent();
}