/*
  Custom chip playground (PCF8574)

  Instructions to RUN:

  1. Open the Custom Chip Playground, go to the code editor,
    press "F1" and select "Create a custom TypeScript chip (alpha)".
    this will create a new file called wokwi-custom-chip.ts

  2. Copy all code from wokwi-custom-chip.txt to wokwi-custom-chip.ts

  3. Copy all settings from wokwi-custom-chip-json.txt to wokwi-custom-chip.json

  4. Run the simulator

  5. Click on rotary encoder & Enjoy PacMan! ;)

  See for more info: https://link.wokwi.com/custom-chips-alpha/
*/

#include <PCF8574.h>

PCF8574 pcf20(0x20);

void setup() {
  Serial.begin(115200);

  Serial.print("PCF8574_LIB_VERSION:\t");
  Serial.println(PCF8574_LIB_VERSION);

  if (!pcf20.begin()) {
    Serial.println("Chip not responding. Did you create wokwi-custom-chip.c.temp/.ts?");
  }

  if (!pcf20.isConnected()) {
    Serial.println("could not initialize... => not connected");
    while (1);
  }

  uint8_t value = pcf20.read8();

  Serial.print("0x20 read: ");
  Serial.println(value, HEX);
}

void loop() {

  for (int i = 0; i < 255; i++) {
    pcf20.write8(i);
    delay(10);
  }

  pcf20.write(0, 1);

  for (int i = 0; i < 7; i++) {
    pcf20.shiftLeft();
    delay(100);
  }

  for (int i = 0; i < 7; i++) {
    pcf20.shiftRight();
    delay(100);
  }

  for (int i = 0; i < 8; i++) {
    pcf20.write(i, 1);
    delay(100);
    pcf20.write(i, 0);
    delay(100);
  }

  for (int i = 0; i < 8; i++) {
    pcf20.toggle(i);
    delay(100);
    pcf20.toggle(i);
    delay(100);
  }

  // Echo the state of the lines on the other PCF
  uint8_t value = pcf20.read8();
  pcf20.write8(value);
  delay(100);
}