package main

import (
	"machine"
	"machine/usb/adc/midi"
	"time"
)

func main() {

	// buttons: active LOW (internal pull-up)
	btnA := machine.P1_06
	btnB := machine.P1_04
	btnRot := machine.P0_22 // rotary encoder push button
	btnA.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
	btnB.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
	btnRot.Configure(machine.PinConfig{Mode: machine.PinInputPullup})

	// C major triad: C4, E4, G4
	notes := []midi.Note{midi.C4, midi.E4, midi.G4}
	midichannel := uint8(1)

	note := -1
	oldNote := -1
	for {
		note = -1
		if !btnA.Get() {
			note = 0 // C4
		}
		if !btnB.Get() {
			note = 1 // E4
		}
		if !btnRot.Get() {
			note = 2 // G4
		}

		if note != oldNote {
			if oldNote != -1 {
				midi.Midi.NoteOff(0, midichannel, notes[oldNote], 50)
			}
			if note != -1 {
				midi.Midi.NoteOn(0, midichannel, notes[note], 50)
			}
			oldNote = note
		}

		time.Sleep(100 * time.Millisecond)
	}
}
