import GPIO from "./pins"

console.log(`kill -9 ${process.pid}`)

const gpio = new GPIO()

using led = gpio.output(21)
using button = gpio.input(20, { pull: "up", debounce: 10 })
using switchInput = gpio.input(16, { pull: "up", debounce: 10 })

led.value = button.value

button.onChange((event) => {
  led.value = event.value
})

const switchEvent = new Promise<void>(async (resolve) => {
  await switchInput.waitForValue(0)
  console.log("Switch pressed!")
  resolve()
})

process.on("SIGINT", () => {
  button.close()
  switchInput.close()
  led.close()
  process.exit(0)
})

process.on("SIGTERM", () => {
  button.close()
  switchInput.close()
  led.close()
  process.exit(0)
})

await switchEvent

console.log(`👋 Goodbye!`)