  *
  * History
  * 2/23/20 Initial creation
+ * 11/3/20 Modify for STB board
  */
 
-#define ADDRESS  0x40  // high jumper set (address 64)
+#define ADDRESS  1 // low jumper set (address 1)
 
 #include "Particle.h"
 

 *
 * To update Photon:
 *   1. Edit this code
 *   2. "particle flash RearPanel"
 *
 * Hardware
 * 1. Photon
 * 2. ControlEverything.com NCD8Relay Photon Controller
 * 3. NCD8PWM board connected via I2C to NCD8Relay board
 * NCD 8-channel PWM OC board with adress switches set per ADDRESS
 * LED connected to channel 0
 * 

    } while( status != 0 && retries-- > 0);

    if(status == 0) {
      Serial.println("Status = 0, setting Mode1 and Mode2");
        Wire.beginTransmission(ADDRESS);
        Wire.write(0);      // Control register - No AI, point to reg 0 Mode1
        Wire.write(0);      // Mode1 register: Osc on (no sleep), diable AI, subaddrs, and allcall
        Wire.endTransmission();

        Wire.beginTransmission(ADDRESS);
        Wire.write(1);      // Mode2 register
        Wire.write(0x04);   // Dimming, Not inverted, totem-pole (this is the default except D0)
        Wire.endTransmission();

    } else {
        Serial.println("Transmission failed");
    }

    Serial.println("Turning ON all LEDS");
    Wire.beginTransmission(ADDRESS);
	  Wire.write(0x8c); // AI + LEDOUT0
	  Wire.write(0x55); // LEDOUT0 All 4 LEDS ON
	  Wire.write(0x55); // LEDOUT1 All 4 LEDS ON
	  status = Wire.endTransmission();
    if(status != 0){
      Serial.println("Write failed");
    }else{
      Serial.println("Write succeeded");
    }    

    delay(2000);

    Serial.println("Turning OFF all LEDS");
    Wire.beginTransmission(ADDRESS);
	  Wire.write(0x8c); // AI + LEDOUT0
	  Wire.write(0x00); // LEDOUT0 All 4 LEDS OFF
	  Wire.write(0xaa); // LEDOUT1 All 4 LEDS Dimming
	  status = Wire.endTransmission();
    if(status != 0){
      Serial.println("Write failed");
    }else{
      Serial.println("Write succeeded");
    }    

//    delay(2000);

    // Serial.println("Setting LED 2 = 255");
    // Wire.beginTransmission(ADDRESS);
	  // Wire.write(3);    // 2 + led #
	  // Wire.write(255);  // Full brightness
	  // status = Wire.endTransmission();
    // if(status != 0){
    //   Serial.println("Write failed");
    // }else{
    //   Serial.println("Write succeeded");
    // }    

    // delay(1000);

    // Serial.println("Setting LED 2 = 0");
    // Wire.beginTransmission(ADDRESS);
	  // Wire.write(3);    // 2 + led #
	  // Wire.write(0);    // Off
	  // status = Wire.endTransmission();
    // if(status != 0){
    //   Serial.println("Write failed");
    // }else{
    //   Serial.println("Write succeeded");
    // }    

    // Repeat
    // TODO:
}

// From TravisE_NCD_Technica
unsigned long scanInterval = 10000;
unsigned long lastScan;
byte dimming = 0;

void loop() {
    byte status;
    // if(millis() > lastScan + scanInterval){
    //     lastScan = millis();

    //     bool devicesFound = false;
    //     String newDevices = "Devices at: ";
    //     //Step through all 127 possible I2C addresses to scan for devices on the I2C bus.
    //     for(int i = 1; i < 128; i++){
    //         //Make a general call to device at the current address to see if anything responds.
    //         Wire.beginTransmission(i);
    //         byte status = Wire.endTransmission();
    //         if(status == 0){
    //             //Device found so append it to our device list event string
    //             newDevices.concat(i);
    //             newDevices.concat(", ");
    //             devicesFound = true;
    //         }
        
    //     }
    //     if(devicesFound){
    //         Serial.println(newDevices);
    //     }else{
    //         Serial.println("No Devices Found");
    //     }
    // }

    Serial.println("Turning ON LEDS 0-3");
    Wire.beginTransmission(ADDRESS);
	  Wire.write(0x8c); // AI + LEDOUT0
	  Wire.write(0x55); // LEDOUT0 All 4 LEDS ON
	  //Wire.write(0x55); // LEDOUT1 All 4 LEDS ON
	  status = Wire.endTransmission();
    if(status != 0){
      Serial.println("Write failed");
    }else{
      Serial.println("Write succeeded");
    }    

    Serial.println("Setting LEDs 4-7 = 255");
    Wire.beginTransmission(ADDRESS);
	  Wire.write(0x86); // AI + 2 + led # 4
	  Wire.write(dimming);  // Full brightness
	  Wire.write(dimming);  // Full brightness
	  Wire.write(dimming);  // Full brightness
	  Wire.write(dimming);  // Full brightness
	  status = Wire.endTransmission();
    if(status != 0){
      Serial.println("Write failed");
    }else{
      Serial.println("Write succeeded");
    }    

    delay(1000);
    dimming += 16;

    Serial.println("Turning OFF LEDS 0-3");
    Wire.beginTransmission(ADDRESS);
	  Wire.write(0x8c); // AI + LEDOUT0
	  Wire.write(0x00); // LEDOUT0 All 4 LEDS OFF
	  //Wire.write(0x00); // LEDOUT1 All 4 LEDS OFF
	  status = Wire.endTransmission();
    if(status != 0){
      Serial.println("Write failed");
    }else{
      Serial.println("Write succeeded");
    }    

    Serial.println("Setting LEDs 4-7 = 0");
    Wire.beginTransmission(ADDRESS);
	  Wire.write(0x86);    // 2 + led # 4
	  Wire.write(dimming);
	  Wire.write(dimming);
	  Wire.write(dimming);
	  Wire.write(dimming);
	  status = Wire.endTransmission();
    if(status != 0){
      Serial.println("Write failed");
    }else{
      Serial.println("Write succeeded");
    }    

    delay(1000);
    dimming += 16;

}
