blackbox/lowlevel.c

Mon, 14 Nov 2011 21:15:11 +0100

author
Malte Bayer <mbayer@neo-soft.org>
date
Mon, 14 Nov 2011 21:15:11 +0100
changeset 2
6c59b4293fa9
parent 0
9b7de464f0ea
child 3
1a0ef40ef458
permissions
-rw-r--r--

finished: rails short circuit check

#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include "stdint.h"
#include "main.h"
#include "driver/rs232.h"

void LED(uint8_t num, uint8_t state) {
    switch (num) {
        case 1: switch (state) {
            case 0: LED1_PORT &= ~_BV(LED1); break;
            case 1: LED1_PORT |= _BV(LED1); break;
            case 2: LED1_PORT ^= _BV(LED1); break;
            } break;

        case 2: switch (state) {
            case 0: LED2_PORT &= ~_BV(LED2); break;
            case 1: LED2_PORT |= _BV(LED2); break;
            case 2: LED2_PORT ^= _BV(LED2); break;
            } break;

        case 3: switch (state) {
            case 0: LED3_PORT &= ~_BV(LED3); break;
            case 1: LED3_PORT |= _BV(LED3); break;
            case 2: LED3_PORT ^= _BV(LED3); break;
            } break;

        case 4: switch (state) {
            case 0: LED4_PORT &= ~_BV(LED4); break;
            case 1: LED4_PORT |= _BV(LED4); break;
            case 2: LED4_PORT ^= _BV(LED4); break;
            } break;

        case 5: switch (state) {
            case 0: LED5_PORT &= ~_BV(LED5); break;
            case 1: LED5_PORT |= _BV(LED5); break;
            case 2: LED5_PORT ^= _BV(LED5); break;
            } break;
    }
}

void check_rails_shortcut(void) {
    // check for short circuit on the rails
    if ((PIN(RAIL_DETECT_PORT) & _BV(RAIL_DETECT)) == 0) {
        _delay_ms(1);
        if ((PIN(RAIL_DETECT_PORT) & _BV(RAIL_DETECT)) == 0) {
            cli(); // disable ALL Interrupts
            RAIL_POWER_PORT &= ~_BV(RAIL_POWER); // disable rails power
            RS232_puts_p(PSTR("!!! SHORT CIRCUIT ON RAILS, POWERED OFF !!!\n"));
            LED(1, 1);
            LED(2, 1);
            LED(3, 0);
            LED(4, 0);
            LED(5, 0);
            while (1) {
                LED(1, 2);
                LED(2, 2);
                LED(4, 2);
                LED(5, 2);
                _delay_ms(100);
                LED(3, 2);
                _delay_ms(100);
                LED(3, 2);
                _delay_ms(100);
                LED(3, 2);
                _delay_ms(100);
                LED(3, 2);
            }
        }
    }
}

mercurial