pentabug/fw_test/main.c

171 lines
2.8 KiB
C
Raw Normal View History

2012-09-20 20:55:19 +02:00
#include <inttypes.h>
#include <avr/io.h>
#include <avr/interrupt.h>
2012-10-01 21:39:25 +02:00
#define __DELAY_BACKWARD_COMPATIBLE__
2012-09-20 20:55:19 +02:00
#include <util/delay.h>
#include <stdlib.h>
2013-08-15 02:12:41 +02:00
#define ever (;;)
2012-09-20 20:55:19 +02:00
2013-08-29 00:41:44 +02:00
#define follow(active, to_port, to_pin) { if(active) to_port &= ~(1 << to_pin); else to_port |= 1 << to_pin; }
#define not_follow(active, to_port, to_pin) { if(active) to_port |= 1 << to_pin; else to_port &= ~(1 << to_pin); }
2012-09-20 20:55:19 +02:00
2013-08-29 00:41:44 +02:00
#define FLASH(port, pin) { port &= ~(1 << pin); _delay_ms(50); port |= 1 << pin;}
2013-08-15 04:15:23 +02:00
2013-08-29 00:41:44 +02:00
static volatile uint32_t button_state = 0;
static volatile uint8_t next_mode = 0;
static volatile uint8_t ir_active = 0;
enum test_modes {
PHOTONS,
MOTOR,
AUDIO,
SHIELD,
// the end ... wrap
MODE_MAX,
};
ISR(TIMER0_COMPA_vect) {
if(ir_active) {
PORTD ^= 1 << 2;
} else {
PORTD &= ~(1 << 2);
}
2012-09-20 20:55:19 +02:00
2013-08-29 00:41:44 +02:00
if(PINB & (1 << 1)) {
button_state = 0;
} else {
++button_state;
}
2012-09-20 20:55:19 +02:00
2013-08-29 00:41:44 +02:00
if(button_state == (38l * 1000 / 2)) {
next_mode = 1;
}
}
2012-09-20 20:55:19 +02:00
2013-08-29 00:41:44 +02:00
static void reset_hw(void) {
// 0: S1
// 1: S2
// 2: SHIELD
// 7: BUZZR
PORTB = (1 << 0) | (1 << 1) | (1 << 2) | (1 << 7);
DDRB = (1 << 2) | (1 << 7);
// 0: BUZGND
// 2: LED2
// 3: LED2 (+)
// 4: SHIELD
// 5: SHIELD
PORTC = (1 << 2) | (1 << 3) | (1 << 4) | (1 << 5);
DDRC = (1 << 0) | (1 << 2) | (1 << 3) | (1 << 4) | (1 << 5);
// 2: IR
// 4: LED
// 5: SHIELD
// 6: SHIELD
// 7: SHIELD
PORTD = (1 << 4) | (1 << 5) | (1 << 6) | (1 << 7);
DDRD = (1 << 2) | (1 << 4) | (1 << 5) | (1 << 6) | (1 << 7);
2012-09-20 20:55:19 +02:00
2013-08-29 00:41:44 +02:00
}
2012-09-20 20:55:19 +02:00
2013-08-29 00:41:44 +02:00
int main(void) {
uint8_t vib_delay = 0;
2013-08-15 04:15:23 +02:00
2013-08-15 04:41:58 +02:00
// we need to get real fast (8MHz) to handle 38kHz IR frequency ...
2013-08-15 04:15:23 +02:00
2013-08-15 04:41:58 +02:00
CLKPR = 0b10000000;
CLKPR = 0b00000000;
2013-08-15 04:15:23 +02:00
// initialize timer
TIMSK0 |= (1 << OCIE0A);
2013-08-29 00:41:44 +02:00
// calculated and works, but frequency is a little bit off?
2013-08-26 20:40:02 +02:00
OCR0A = 105;
2013-08-15 04:15:23 +02:00
2013-08-29 00:41:44 +02:00
TCCR0A = (1 << WGM01);
TCCR0B = (1 << CS00);
reset_hw();
2013-08-15 04:15:23 +02:00
// no prescaler
2012-09-24 19:45:51 +02:00
2013-08-15 04:15:23 +02:00
sei();
2012-09-24 19:45:51 +02:00
2013-08-15 02:12:41 +02:00
// looping
2012-09-20 20:55:19 +02:00
2013-08-29 00:41:44 +02:00
enum test_modes mode = PHOTONS;
2012-09-24 19:45:51 +02:00
2013-08-29 00:41:44 +02:00
for ever {
// next mode?
if(next_mode) {
// cleanup
switch(mode) {
case PHOTONS:
ir_active = 0;
break;
case MOTOR:
case AUDIO:
case SHIELD:
break;
case MODE_MAX: break;
}
2013-08-29 00:41:44 +02:00
reset_hw();
2012-09-25 20:19:14 +02:00
2013-08-29 00:41:44 +02:00
++mode;
2012-09-25 20:19:14 +02:00
2013-08-29 00:41:44 +02:00
if(mode == MODE_MAX) {
mode = 0;
2013-08-15 02:12:41 +02:00
}
2012-09-20 21:20:51 +02:00
2013-08-29 00:41:44 +02:00
uint32_t i;
for(i = 0; i < 50; ++i) {
PORTB ^= 1 << 7;
_delay_ms(1);
}
2013-08-15 04:15:23 +02:00
2013-08-29 00:41:44 +02:00
next_mode = 0;
2013-08-15 04:15:23 +02:00
}
2013-08-29 00:41:44 +02:00
uint8_t button = !(PINB & (1 << 0));
not_follow(PIND & (1 << 3), PORTC, 2);
switch(mode) {
case PHOTONS:
ir_active = button;
follow(button, PORTD, 4);
break;
case MOTOR:
not_follow(button, PORTB, 6);
break;
case AUDIO:
{
if(button) {
PORTC ^= 1 << 0;
_delay_ms(2);
}
break;
}
case SHIELD:
if(button) {
FLASH(PORTB, 2);
FLASH(PORTD, 5);
FLASH(PORTD, 6);
FLASH(PORTD, 7);
FLASH(PORTC, 5);
FLASH(PORTC, 4);
}
break;
case MODE_MAX: break;
}
2012-09-20 20:55:19 +02:00
}
2013-08-15 02:12:41 +02:00
2012-09-20 20:55:19 +02:00
/* never return 0; */
2012-09-25 07:23:14 +02:00
return 0;
2012-09-20 20:55:19 +02:00
}