Example #1
0
void Init() {
    JtagDisable();
    Uart.Init(USART2, 256000);
#ifndef TESTING
    // Charging and powering
    PinSetupIn(EXT_PWR_GPIO,  EXT_PWR_PIN,  pudPullDown);
    PinSetupIn(CHARGING_GPIO, CHARGING_PIN, pudPullUp);

    LedsInit();
    VibroInit();
    if(!Iwdg.ResetOccured()) {
        Uart.Printf("Glove1_f100 AHB=%u; APB1=%u; APB2=%u\r", Clk.AHBFreqHz, Clk.APB1FreqHz, Clk.APB2FreqHz);
        Vibro.On(100);
        chThdSleepMilliseconds(99);
        ShowChargeLevel();
        chThdSleepMilliseconds(900);
        SwitchOffEverything();
    }
    else Uart.Printf("W\r");
    AccInit();

    // Application init
    App.Init();
#else
    Uart.Printf("Glove tester\r");
    PinSetupOut(GPIOA, 15, omPushPull);
    PinSet(GPIOA, 15);
    chThdSleepMilliseconds(450);
    Acc[0].SetPortAndPins(GPIOA, 1, 3, 4);
#endif
}
int main(void) {

    // Init PIC24
    InitMain();

    // Init modules
    AudioInInit();
    Uart2Init(UART_BAUD_250000, 0);
    LedsInit();

    // Main loop
    while(1) {
        if(AudioInIsGetReady()) {
            Fixed audioSample = AudioInGet();

            // Update LEDs
            LedsUpdate(audioSample);

            // Print audio sample
            Uart2RxTasks();
            if(Uart2IsPutReady() >= 6) {
                static const char asciiDigits[10] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
                int i = FIXED_TO_INT(audioSample);
                div_t n;
                int print = 0;
                if(i < 0) {
                    Uart2PutChar('-');
                    i = -i;
                }
                if(i >= 10000) {
                    n = div(i, 10000);
                    Uart2PutChar(asciiDigits[n.quot]);
                    i = n.rem;
                    print = 1;
                }
                if(i >= 1000 || print) {
                    n = div(i, 1000);
                    Uart2PutChar(asciiDigits[n.quot]);
                    i = n.rem;
                    print = 1;
                }
                if(i >= 100 || print) {
                    n = div(i, 100);
                    Uart2PutChar(asciiDigits[n.quot]);
                    i = n.rem;
                    print = 1;
                }
                if(i >= 10 || print) {
                    n = div(i, 10);
                    Uart2PutChar(asciiDigits[n.quot]);
                    i = n.rem;
                }
                Uart2PutChar(asciiDigits[i]);
                Uart2PutChar('\r');
            }
        }
    }
}