#!/bin/bash
PATH=$PATH:/usr/local/bin
MAJOR=0
MINOR=1

# Load I2C module
sudo modprobe i2c-dev

# Write firmware to controller
sudo pymcuprog write --erase -d attiny402 -t uart -u /dev/ttyAMA0 -f /usr/share/testtools/firmware/studiodac8x-${MAJOR}.${MINOR}.hex
sleep 2

# Read back version from Microcontroller to check if the firmware was installed correctly
MAJOR_HEX=$(i2cget -y 1 0x17 1 b 2>/dev/null | sed 's/0x//')
MINOR_HEX=$(i2cget -y 1 0x17 2 b 2>/dev/null | sed 's/0x//')

MAJOR_READ=$((16#$MAJOR_HEX))
MINOR_READ=$((16#$MINOR_HEX))

# Check if major and minor versions are correct
if [[ $MAJOR_READ -ne $MAJOR || $MINOR_READ -ne $MINOR ]]; then
    figlet "FAIL"
    echo "Version mismatch: Expected ${MAJOR}.${MINOR}, but read ${MAJOR_READ}.${MINOR_READ}"
    exit 1
fi

echo "Controller firmware $MAJOR.$MINOR installed correctly"

# Write HAT EEPROM
sudo hateeprom --type=24c32 flash /opt/hifiberry/eeprom/studiodac8x.eep
if [ "$?" != 0 ]; then
    figlet "FAIL"
    echo "Failed to write HAT EEPROM"
    exit 1
fi

figlet "OK"

# Play a test tone
play -q -n -c 8 synth 20 sine 1000

reset
figlet "Studio DAC8x OK"

