#include "Config.h"
#include "Sensors.h"
#include "SystemTrigger.h"
#include "Utils.h"

#include "../lib/SparkFun_SX1509_Arduino_Library/src/SparkFunSX1509.h"

#include <Adafruit_NeoPixel.h>

// SX1509 I2C address (set by ADDR1 and ADDR0 (00 by default):
const byte SX1509_ADDRESS = 0x3E;  // SX1509 I2C address
SX1509 io; // Create an SX1509 object to be used throughout

// create a pixel strand with 1 pixel on PIN_NEOPIXEL
#define PIN_NEOPIXEL 11

Adafruit_NeoPixel pixels(1, PIN_NEOPIXEL);

int ledCounter = 0;

void setup() {
    pinMode(12, OUTPUT);
    digitalWrite(12, HIGH);
    pinMode(0, OUTPUT);
    digitalWrite(12, LOW);
    pixels.begin();  // initialize the pixel

    // Setup the port expander
    if (!io.begin(SX1509_ADDRESS))
    {
        DEBUGPORT.println("Could not init SX1509.");
        while(true){};
    }
    else {
        // set all pins to enabled
        for (int i=0;i < 15; i++) {
            io.pinMode(i, OUTPUT);
            io.digitalWrite(i,LOW);
        }
    }
}

void loop() {

    // set the first pixel #0 to red
    pixels.setPixelColor(0, pixels.Color(255, 0, 0));
    // and write the data
    pixels.show();

    io.digitalWrite(ledCounter % 8,HIGH);
    digitalWrite(0, HIGH);

    delay(100);

    io.digitalWrite(ledCounter % 8,LOW);
    digitalWrite(0, LOW);
    
    ledCounter++;

    // turn off the pixel
    pixels.clear();
    pixels.show();
  
    delay(100);
}
