diff options
Diffstat (limited to 'digital-controller/arduino-uno-controller')
-rw-r--r-- | digital-controller/arduino-uno-controller/firmware/firmware.ino | 86 |
1 files changed, 59 insertions, 27 deletions
diff --git a/digital-controller/arduino-uno-controller/firmware/firmware.ino b/digital-controller/arduino-uno-controller/firmware/firmware.ino index e87db8d..3735c4a 100644 --- a/digital-controller/arduino-uno-controller/firmware/firmware.ino +++ b/digital-controller/arduino-uno-controller/firmware/firmware.ino @@ -1,41 +1,73 @@ #include <Wire.h> -#define I2C_SLAVE_ADDR 0x26 // I2C slave address (38, 0x26) +#define INPUT_SIZE 100 // TODO: make this a reasonable value +#define sep " " +char input[INPUT_SIZE + 1]; -void setup() -{ +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); - // init the Wire object Wire.begin(); - - // wait for slave to finish any init sequence - delay(2000); - Serial.begin(9600); } -void loop() -{ - for( int a=10; a<=255; a++ ){ - //generate buffer containing data to send via I2C - uint8_t buf[1]; - buf[0] = 255-a; // adjusting this byte changes fan speed. Can be adjusted from 0 to 256. - buf[1] = a; // adjusting this byte changes LED intensity. Can be adjusted from 0 to 256. - Serial.println(int(buf)); +void loop() { +} - // send buffer - Wire.beginTransmission(I2C_SLAVE_ADDR); - Wire.write(buf, 2); - Wire.endTransmission(); - - // delay 20 milliseconds to accomodate slave onReceive() - delay(20); +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(); - /////////////////////// WHY DO I NEED THIS PART?////////////////////////// - Wire.requestFrom(I2C_SLAVE_ADDR, 1); + if (error == 0) + { + Serial.print("I2C device found at address "); + Serial.println(address); + } + } - // delay 1 second so user can watch results - delay(50); +} + +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(); } |