2012-07-17 01:05:27 +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-07-17 01:05:27 +02:00
|
|
|
#include <util/delay.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "main.h"
|
2012-10-01 23:36:37 +02:00
|
|
|
|
2012-09-02 21:53:11 +02:00
|
|
|
#include "lib/usart.h"
|
2012-09-02 23:49:57 +02:00
|
|
|
#include "lib/bughal.h"
|
2012-10-01 21:39:25 +02:00
|
|
|
#include "lib/util.h"
|
2012-10-03 23:41:15 +02:00
|
|
|
#include "lib/music.h"
|
2012-07-26 01:34:49 +02:00
|
|
|
|
2012-10-03 23:41:15 +02:00
|
|
|
//operartion modes
|
|
|
|
#define MODE0 0
|
|
|
|
#define MODE1 1
|
|
|
|
#define MODE2 2
|
|
|
|
#define MODE3 3
|
2012-10-05 21:12:26 +02:00
|
|
|
#define MODE4 4
|
2012-10-05 20:35:33 +02:00
|
|
|
#define NUM_MODES 5
|
2012-10-03 23:41:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2012-10-04 01:03:54 +02:00
|
|
|
uint8_t OpMode = MODE0; //Operation mode
|
2012-10-03 23:41:15 +02:00
|
|
|
bool ModeChanged = true;
|
|
|
|
|
|
|
|
void modeswitch_poll(void){
|
|
|
|
if (btn_state(BTNST_LUP,BTN_LEFT)){
|
|
|
|
//opmode -
|
|
|
|
OpMode = (0 == OpMode) ? (NUM_MODES-1) : (OpMode-1);
|
|
|
|
ModeChanged = true;
|
|
|
|
button_clear(BTN_LEFT);
|
|
|
|
};
|
|
|
|
if (btn_state(BTNST_LUP,BTN_RIGHT)){
|
|
|
|
//opmode +
|
|
|
|
ModeChanged = true;
|
|
|
|
OpMode = ((NUM_MODES-1) == OpMode) ? 0 : (OpMode+1);
|
|
|
|
button_clear(BTN_RIGHT);
|
|
|
|
};
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
void do_mode0(void){
|
2012-10-04 00:26:53 +02:00
|
|
|
static timer_t mytimer;
|
2012-10-05 21:12:26 +02:00
|
|
|
static uint16_t maxval;
|
2012-10-04 00:26:53 +02:00
|
|
|
if (ModeChanged){
|
2012-10-05 21:12:26 +02:00
|
|
|
timer_set(&mytimer, 10);
|
|
|
|
maxval=0;
|
2012-10-03 23:41:15 +02:00
|
|
|
ModeChanged = false;
|
2012-10-05 21:12:26 +02:00
|
|
|
buzzr_up();
|
2012-10-04 00:26:53 +02:00
|
|
|
};
|
|
|
|
if(timer_expired(&mytimer)){
|
2012-10-05 21:12:26 +02:00
|
|
|
//USART0_put_uint16(maxval);
|
|
|
|
//USART0_crlf();
|
|
|
|
timer_set(&mytimer, 10);
|
|
|
|
maxval=0;
|
|
|
|
buzzr_inv();
|
|
|
|
}; //end if timer_expired
|
|
|
|
maxval++;
|
2012-10-04 00:26:53 +02:00
|
|
|
|
2012-10-05 21:12:26 +02:00
|
|
|
|
2012-10-04 00:26:53 +02:00
|
|
|
|
2012-10-05 21:12:26 +02:00
|
|
|
}; //end do_mode0
|
2012-10-03 23:41:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
void do_mode1(void){
|
2012-10-04 00:26:53 +02:00
|
|
|
static uint16_t tone;
|
2012-10-03 23:41:15 +02:00
|
|
|
if (ModeChanged){
|
|
|
|
ModeChanged = false;
|
2012-10-04 00:26:53 +02:00
|
|
|
tone = 1000;
|
|
|
|
music_setNote(tone,0);
|
|
|
|
led_off(LED_L|LED_R);
|
|
|
|
};
|
|
|
|
if (btn_state(BTNST_SUP,BTN_LEFT)) {
|
|
|
|
button_clear(BTN_LEFT);
|
|
|
|
tone += 10;
|
|
|
|
music_setNote(tone,0);
|
|
|
|
};
|
|
|
|
if (btn_state(BTNST_SUP,BTN_RIGHT)) {
|
|
|
|
button_clear(BTN_RIGHT);
|
|
|
|
tone -= 10;
|
|
|
|
music_setNote(tone,0);
|
2012-10-03 23:41:15 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
void do_mode2(void){
|
2012-10-04 01:03:54 +02:00
|
|
|
static timer_t mytimer;
|
|
|
|
uint8_t max = 50;
|
|
|
|
uint8_t min = 5;
|
|
|
|
uint8_t maxfreq = 6000;
|
|
|
|
uint8_t minfreq = 2000;
|
|
|
|
|
2012-10-03 23:41:15 +02:00
|
|
|
if (ModeChanged){
|
|
|
|
ModeChanged = false;
|
2012-10-04 01:03:54 +02:00
|
|
|
music_setNote(NOTE_PAUSE,4); //mute
|
|
|
|
timer_set(&mytimer, 10);
|
|
|
|
led_off(LED_L|LED_R);
|
|
|
|
set_motor(MOTOR_OFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(timer_expired(&mytimer)){
|
|
|
|
set_motor(MOTOR_OFF);
|
|
|
|
music_setNote(NOTE_PAUSE,0); //mute
|
|
|
|
// set random led
|
|
|
|
switch(rand() % 4) {
|
|
|
|
case 0 : led_on(LED_L); break;
|
|
|
|
case 1 : led_on(LED_R); break;
|
|
|
|
case 2 : led_on(LED_L|LED_R); break;
|
|
|
|
default : led_off(LED_L|LED_R);
|
|
|
|
};
|
|
|
|
// decide if to switch motor on (40% chance)
|
|
|
|
if (rand()%5>2) set_motor(MOTOR_ON);
|
|
|
|
|
|
|
|
//decide if to play sound (70% chance)
|
|
|
|
if (rand()%10>2) {
|
|
|
|
music_setNote((rand() % (maxfreq-minfreq)) + minfreq,0);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
timer_set(&mytimer, (rand() % (max-min)) + min);
|
|
|
|
}; //end if timer_expired
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-10-03 23:41:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
void do_mode3(void){
|
2012-10-04 01:03:54 +02:00
|
|
|
static timer_t mytimer;
|
|
|
|
static bool blink;
|
2012-10-03 23:41:15 +02:00
|
|
|
if (ModeChanged){
|
|
|
|
ModeChanged = false;
|
2012-10-04 01:03:54 +02:00
|
|
|
music_setNote(NOTE_PAUSE,4); //mute
|
|
|
|
set_motor(MOTOR_OFF);
|
|
|
|
timer_set(&mytimer, 10);
|
|
|
|
blink = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
if(timer_expired(&mytimer)){
|
|
|
|
if (!blink) {
|
|
|
|
//lets blink
|
|
|
|
led_on(LED_L|LED_R);
|
|
|
|
timer_set(&mytimer, 1);
|
|
|
|
blink=true;
|
|
|
|
} else {
|
|
|
|
//stop blink
|
|
|
|
led_off(LED_L|LED_R);
|
|
|
|
timer_set(&mytimer, 123);
|
|
|
|
blink=false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} //end if timer_expired
|
|
|
|
|
|
|
|
if (btn_state(BTNST_SUP,BTN_LEFT)) {
|
|
|
|
button_clear(BTN_LEFT);
|
|
|
|
set_motor(MOTOR_OFF);
|
|
|
|
|
2012-10-03 23:41:15 +02:00
|
|
|
};
|
2012-10-04 01:03:54 +02:00
|
|
|
if (btn_state(BTNST_SUP,BTN_RIGHT)) {
|
|
|
|
button_clear(BTN_RIGHT);
|
|
|
|
set_motor(MOTOR_ON); };
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-10-05 21:12:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
void do_mode4(void){
|
|
|
|
uint8_t max = 200;
|
|
|
|
uint8_t min = 10;
|
|
|
|
|
|
|
|
static timer_t mytimer;
|
|
|
|
static bool blink;
|
|
|
|
if (ModeChanged){
|
|
|
|
music_setNote(NOTE_PAUSE,0);
|
|
|
|
ModeChanged = false;
|
|
|
|
timer_set(&mytimer, 10);
|
|
|
|
blink = false;
|
|
|
|
};
|
|
|
|
if(timer_expired(&mytimer)){
|
|
|
|
if (!blink) {
|
|
|
|
//lets blink
|
|
|
|
int i = (rand() % 3);
|
|
|
|
switch(i) {
|
|
|
|
case 0 : led_on(LED_L); break;
|
|
|
|
case 1 : led_on(LED_R); break;
|
|
|
|
default : led_on(LED_L|LED_R);
|
|
|
|
};
|
|
|
|
if (rand()%10>8) set_motor(MOTOR_ON);
|
|
|
|
music_setNote(NOTE_C,5);
|
|
|
|
timer_set(&mytimer, 2);
|
|
|
|
blink=true;
|
|
|
|
} else {
|
|
|
|
//stop blink
|
|
|
|
led_off(LED_L|LED_R);
|
|
|
|
set_motor(MOTOR_OFF);
|
|
|
|
music_setNote(NOTE_PAUSE,0);
|
|
|
|
timer_set(&mytimer, (rand() % (max-min)) + min);
|
|
|
|
|
|
|
|
blink=false;
|
|
|
|
}
|
|
|
|
|
|
|
|
} //end if timer_expired
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-10-03 23:41:15 +02:00
|
|
|
};
|
2012-08-12 01:35:00 +02:00
|
|
|
|
2012-08-27 09:43:42 +02:00
|
|
|
|
|
|
|
void __attribute__((noreturn))
|
|
|
|
main(void)
|
|
|
|
{
|
|
|
|
/* hardware initialisation: */
|
2012-09-02 23:49:57 +02:00
|
|
|
init_leds();
|
2012-09-06 23:09:20 +02:00
|
|
|
init_buzzr();
|
2012-10-01 21:39:25 +02:00
|
|
|
init_switch();
|
|
|
|
USART0_Init();
|
|
|
|
init_motor();
|
|
|
|
/* software initialisation */
|
|
|
|
timer_init();
|
2012-10-03 23:41:15 +02:00
|
|
|
music_init();
|
2012-08-27 09:43:42 +02:00
|
|
|
|
|
|
|
/* here the show begins:*/
|
2012-10-01 21:39:25 +02:00
|
|
|
sei();
|
|
|
|
timer_t t;
|
2012-10-01 23:36:37 +02:00
|
|
|
uint8_t ledstate_l =0;
|
|
|
|
uint8_t ledstate_r =0;
|
|
|
|
uint8_t motorstate =0;
|
2012-10-01 21:39:25 +02:00
|
|
|
timer_set(&t, 100);
|
2012-08-07 20:08:57 +02:00
|
|
|
for(;;) /* ever */ {
|
2012-09-06 23:09:20 +02:00
|
|
|
//do something
|
2012-10-01 21:39:25 +02:00
|
|
|
//main polling loop;
|
2012-10-03 23:41:15 +02:00
|
|
|
button_poll();
|
|
|
|
modeswitch_poll();
|
|
|
|
switch(OpMode){
|
|
|
|
case MODE1 : do_mode1(); break;
|
|
|
|
case MODE2 : do_mode2(); break;
|
|
|
|
case MODE3 : do_mode3(); break;
|
2012-10-05 20:35:33 +02:00
|
|
|
case MODE4 : do_mode4(); break;
|
2012-10-03 23:41:15 +02:00
|
|
|
default : do_mode0();
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
|
2012-10-01 21:39:25 +02:00
|
|
|
if (timer_expired(&t)){
|
2012-10-01 23:36:37 +02:00
|
|
|
//while left button is pressed switch left led on
|
|
|
|
if (btn_state(BTNST_SDN,BTN_LEFT)|btn_state(BTNST_LDN,BTN_LEFT)){
|
|
|
|
led_on(LED_L);
|
|
|
|
} else {
|
|
|
|
led_off(LED_L);
|
|
|
|
};
|
2012-10-03 23:41:15 +02:00
|
|
|
if (btn_state(BTNST_SUP,BTN_LEFT)){
|
|
|
|
music_setNote(NOTE_PAUSE,0); //mute
|
|
|
|
button_clear(BTN_LEFT);
|
|
|
|
};
|
|
|
|
|
2012-10-01 23:36:37 +02:00
|
|
|
//when right button has been pressed short, toggle right led
|
|
|
|
if (btn_state(BTNST_SUP,BTN_RIGHT)){
|
|
|
|
if (ledstate_r ==0){
|
|
|
|
ledstate_r = 1;
|
|
|
|
led_on(LED_R);
|
|
|
|
} else {
|
|
|
|
ledstate_r = 0;
|
|
|
|
led_off(LED_R);
|
|
|
|
};
|
2012-10-03 23:41:15 +02:00
|
|
|
music_setNote(500,0); //50Hz sound
|
2012-10-01 23:36:37 +02:00
|
|
|
button_clear(BTN_RIGHT); //this is important, to show the event has been processed
|
|
|
|
};
|
|
|
|
//when right button has been pressed long, toggle motor
|
|
|
|
if (btn_state(BTNST_LUP,BTN_RIGHT)){
|
|
|
|
if (motorstate ==0){
|
|
|
|
motorstate = 1;
|
|
|
|
set_motor(MOTOR_ON);
|
|
|
|
} else {
|
|
|
|
motorstate = 0;
|
|
|
|
set_motor(MOTOR_OFF);
|
|
|
|
};
|
2012-10-03 23:41:15 +02:00
|
|
|
music_setNote(NOTE_B,5); //1000Hz sound
|
2012-10-01 23:36:37 +02:00
|
|
|
button_clear(BTN_RIGHT); //this is important, to show the event has been processed
|
|
|
|
};
|
|
|
|
|
2012-10-01 22:32:52 +02:00
|
|
|
timer_set(&t, 5);
|
2012-10-01 21:39:25 +02:00
|
|
|
}; //end if timer expired
|
2012-08-12 01:35:00 +02:00
|
|
|
//USART0_put_uint16(0xA09F);
|
|
|
|
//USART0_crlf();
|
2012-10-03 23:41:15 +02:00
|
|
|
*/
|
2012-10-01 21:39:25 +02:00
|
|
|
|
2012-08-14 22:53:25 +02:00
|
|
|
}
|
2012-07-18 15:55:20 +02:00
|
|
|
|
2012-08-14 22:53:25 +02:00
|
|
|
/* never return 0; */
|
2012-07-18 15:55:20 +02:00
|
|
|
}
|
2012-08-05 22:13:43 +02:00
|
|
|
|
|
|
|
|
2012-08-12 01:35:00 +02:00
|
|
|
|