#include "states.h"

    void init_spi(void) {
        SET_PIN_OUT(DDRC, DDC1);  // SCK1
        SET_PIN_OUT(DDRE, DDE3);  // MOSI1
        SET_PIN_IN(DDRC, DDC0);   // MISO1 (driven by the slave; no pull-up)

        // SS1 must be an output before SPE is set. If it is left as an input and
        // reads low, the hardware clears MSTR and the port silently stops being
        // a master.
        SET_PIN_OUT(DDRE, DDE2);
        SET_PIN_HIGH(PORTE, PE2);

        SPCR1 = (1 << SPE1) | (1 << MSTR1);  // Enable, Master, SPR1:0 = 00 -> f_osc/4
    }
    
    
    void rfm69_reset_state(bool state) {
        SET_PIN_OUT(DDRC, DDC2);
        if (state) {
            SET_PIN_HIGH(PORTC, PC2);
        } else {
            SET_PIN_LOW(PORTC, PC2);
        }
    }

    void led_1_set_state(bool state) {
        SET_PIN_OUT(DDRD, DDD4);
        if (state) {
            SET_PIN_HIGH(PORTD, PD4);
        } else {
            SET_PIN_LOW(PORTD, PD4);
        }
    }

    void led_2_set_state(bool state) {
        SET_PIN_OUT(DDRD, DDD6);
        if (state) {
            SET_PIN_HIGH(PORTD, PD6);
        } else {
            SET_PIN_LOW(PORTD, PD6);
        }
    }

    void led_3_set_state(bool state) {
        SET_PIN_OUT(DDRD, DDD7);
        if (state) {
            SET_PIN_HIGH(PORTD, PD7);
        } else {
            SET_PIN_LOW(PORTD, PD7);
        }
    }

    void ldo_set_state(bool state) {
        SET_PIN_OUT(DDRC, DDC3);
        if (state) {
            SET_PIN_HIGH(PORTC, PC3);
        } else {
            SET_PIN_LOW(PORTC, PC3);
        }
    }
