pentabug/fw_test/main.c

155 lines
2.3 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>
#include <util/delay.h>
#include <stdlib.h>
#define LED_L (1 << PORTC0)
#define LED_R (1 << PORTC2)
#define MOTOR_ON (0<<PORTB1)
#define MOTOR_OFF (1<<PORTB1)
void init_leds(void){
//enable LED channels as output
DDRC |= (1 << PORTC0) | (1 << PORTC1) | (1 << PORTC2) | (1 << PORTC3);
// both LEDs off
PORTC &= ~((1 << PORTC0) | (1 << PORTC1) | (1 << PORTC2) | (1 << PORTC3));
2012-09-20 21:20:51 +02:00
return;
2012-09-20 20:55:19 +02:00
};
void inline led_on(int leds){
PORTC |= leds;
};
void inline led_off(int leds){
PORTC &= ~leds;
};
void init_buzzr(){
//its on B2 and C5, for reasons
DDRC |= (1 << PORTC5);
DDRB |= (1 << PORTB2);
//switch it off
buzzr_off();
return;
};
void buzzr_up(){
PORTC &= ~(1 << PORTC5);
PORTB |= (1 << PORTB2);
return;
};
void buzzr_down(){
PORTC |= (1 << PORTC5);
PORTB &= ~(1 << PORTB2);
};
void inline buzzr_off(){
PORTC &= ~(1 << PORTC5);
PORTB &= ~(1 << PORTB2);
};
void buzzr_inv(){
PORTC ^= (1 << PORTC5);
PORTB ^= (1 << PORTB2);
};
void init_motor(void)
{
/* vibration motor on B1, initially off: */
DDRB |= (1 << PORTB1);
PORTB &= ~( 1<<PORTB1);
return;
}
void set_motor(int val){
PORTB = ~(val);
PORTB = PORTB;
return;
};
2012-09-24 19:11:00 +02:00
void init_switch(void){
2012-09-20 21:20:51 +02:00
return;
};
2012-09-24 19:11:00 +02:00
void blinkrattlebeep(void){
2012-09-20 21:20:51 +02:00
led_off(LED_L|LED_R);
_delay_ms(100);
led_on(LED_L);
_delay_ms(100);
led_off(LED_L);
led_on(LED_R);
_delay_ms(100);
led_off(LED_R);
set_motor(MOTOR_ON);
_delay_ms(200);
set_motor(MOTOR_OFF);
buzzr_up();
for (int i=0; i<100; i++){
_delay_ms(2);
buzzr_inv();
};
buzzr_off();
};
2012-09-24 19:11:00 +02:00
void mode_motortest(void)
{
set_motor(MOTOR_ON);
_delay_ms(400);
set_motor(MOTOR_OFF);
_delay_ms(500);
return;
}
void mode_blinktest(void)
{
led_off(LED_L|LED_R);
_delay_ms(100);
led_on(LED_L);
_delay_ms(100);
led_off(LED_L);
led_on(LED_R);
_delay_ms(100);
led_off(LED_R);
return
}
2012-09-20 21:20:51 +02:00
2012-09-20 20:55:19 +02:00
void __attribute__((noreturn))
main(void)
{
2012-09-20 21:20:51 +02:00
/* hardware initialisation: */
2012-09-20 20:55:19 +02:00
init_motor();
init_buzzr();
init_leds();
2012-09-20 21:20:51 +02:00
uint8_t mode = 0;
2012-09-20 20:55:19 +02:00
for(;;) /* ever */ {
//do something
2012-09-20 21:20:51 +02:00
//TODO: check switches and change mode
switch(mode){
case 1 : led_on(LED_L|LED_R); _delay_ms(500); led_off(LED_L|LED_R); break;
case 2 : break;
default : blinkrattlebeep();
2012-09-20 20:55:19 +02:00
};
2012-09-20 21:20:51 +02:00
2012-09-20 20:55:19 +02:00
}
/* never return 0; */
}