workshop nik: play little melody

This commit is contained in:
schneider 2011-08-13 16:01:33 +02:00
parent 0eaf74fa87
commit c974cfc072
1 changed files with 66 additions and 36 deletions

View File

@ -62,6 +62,21 @@ uint8_t lk_in1 = 0x00;
uint16_t lk_ticks = 0x0000; uint16_t lk_ticks = 0x0000;
uint8_t lk_piezo_toggle = 0x00; uint8_t lk_piezo_toggle = 0x00;
struct note {
int freq;
int duration;
};
struct note melody[] = {
{1000, 1000},
{2000, 300},
{3000, 800},
{ 500, 1000},
{ 300, 1000}
};
#define MELODY_LEN (sizeof(melody)/sizeof(struct note))
int melody_index;
int melody_timeout;
static void init_lilakit(void); static void init_lilakit(void);
static void tick_lilakit(void); static void tick_lilakit(void);
@ -75,9 +90,8 @@ void ram(void) {
SCB_SYSAHBCLKCTRL |= (SCB_SYSAHBCLKCTRL_CT32B0); SCB_SYSAHBCLKCTRL |= (SCB_SYSAHBCLKCTRL_CT32B0);
TMR_TMR32B0MR0 = (72E6/5E3)/2; TMR_TMR32B0MR0 = (72E6/5E3)/2;
TMR_TMR32B0MCR = (TMR_TMR32B0MCR_MR0_INT_ENABLED | TMR_TMR32B0MCR_MR0_RESET_ENABLED); TMR_TMR32B0MCR = (TMR_TMR32B0MCR_MR0_INT_ENABLED | TMR_TMR32B0MCR_MR0_RESET_ENABLED);
NVIC_EnableIRQ(TIMER_32_0_IRQn); melody_index = MELODY_LEN;
TMR_TMR32B0TCR = TMR_TMR32B0TCR_COUNTERENABLE_ENABLED;
init_lilakit(); init_lilakit();
mainloop(); mainloop();
@ -92,12 +106,11 @@ void handler(void)
gpioSetValue (LK_PIEZO, time); gpioSetValue (LK_PIEZO, time);
} }
static void mainloop(void) { static void mainloop(void)
{
int dx=0; int dx=0;
int dy=0; int dy=0;
static uint32_t ctr=0;
ctr++;
setExtFont(GLOBAL(nickfont)); setExtFont(GLOBAL(nickfont));
dx=DoString(0,0,GLOBAL(nickname)); dx=DoString(0,0,GLOBAL(nickname));
dx=(RESX-dx)/2; dx=(RESX-dx)/2;
@ -108,20 +121,54 @@ static void mainloop(void) {
lcdClear(); lcdClear();
lcdSetPixel(1,1,1); lcdSetPixel(1,1,1);
DoString(dx,dy,GLOBAL(nickname)); DoString(dx,dy,GLOBAL(nickname));
lcdRefresh(); lcdRefresh();
//lcdClear();
//lcdRefresh();
lk_ticks = 0; lk_ticks = 0;
lk_button_mode = 0x00;
lk_ls0 = 0x00;
lk_ls1 = 0x00;
lk_ls2 = 0x00;
lk_ls3 = 0x00;
lk_in0 = 0x00;
lk_in1 = 0x00;
lk_ticks = 0x0000;
lk_piezo_toggle = 0x00;
while(getInputRaw()==BTN_NONE){ while(getInputRaw()==BTN_NONE){
tick_lilakit(); tick_lilakit();
//delayms_queue_plus(10,0); melody_play();
//delayms_queue(10); //delayms_queue(10); //XXX: this hangs the badge.
delayms(10); delayms(10);
}; }
return; return;
} }
void melody_play(void)
{
int ton, timer;
if( melody_index < MELODY_LEN){
if(melody_timeout == 0){
ton = melody[melody_index].freq;
melody_timeout = melody[melody_index].duration / 10;
timer = (72000000UL/ton/2);
TMR_TMR32B0MR0 = timer;
if (TMR_TMR32B0TC > timer){
//schneller fix wenn ton zurueckgesetzt wird, aber timer weiterlaeuft
TMR_TMR32B0TC=0;
}
if( melody_index == 0 ){
NVIC_EnableIRQ(TIMER_32_0_IRQn);
TMR_TMR32B0TCR = TMR_TMR32B0TCR_COUNTERENABLE_ENABLED;
}
melody_index++;
}
melody_timeout--;
}else{
NVIC_DisableIRQ(TIMER_32_0_IRQn);
TMR_TMR32B0TCR = TMR_TMR32B0TCR_COUNTERENABLE_DISABLED;
}
}
static uint32_t lkSetI2C(uint8_t cr, uint8_t value) { static uint32_t lkSetI2C(uint8_t cr, uint8_t value) {
I2CMasterBuffer[0] = LK_I2C_WRITE; I2CMasterBuffer[0] = LK_I2C_WRITE;
I2CMasterBuffer[1] = cr; I2CMasterBuffer[1] = cr;
@ -157,29 +204,11 @@ static void lkReadI2C() {
lk_in0 = lkGetI2C(LK_I2C_CR_INPUT0); lk_in0 = lkGetI2C(LK_I2C_CR_INPUT0);
lk_in1 = lkGetI2C(LK_I2C_CR_INPUT1); lk_in1 = lkGetI2C(LK_I2C_CR_INPUT1);
} }
static void tick_lilakit(void) static void tick_lilakit(void)
{ // every 10ms { // every 10ms
uint32_t ton, timer;
lk_ticks++; lk_ticks++;
if( (lk_ticks % 100) == 0 ){
//lcdPrintln("a"); lcdRefresh();
ton = 2000;
timer = (72000000UL/ton/2);
TMR_TMR32B0MR0 = timer;
}
if( (lk_ticks % 100) == 50 ){
//lcdPrintln("b"); lcdRefresh();
ton = 5000;
timer = (72000000UL/ton/2);
TMR_TMR32B0MR0 = timer;
}
//return;
if (TMR_TMR32B0TC > timer){ //schneller fix wenn ton zurueckgesetzt wird, aber timer weiterlaeuft
TMR_TMR32B0TC=0;
}
if (lkEnabled == 0) { if (lkEnabled == 0) {
return; return;
} }
@ -189,6 +218,9 @@ static void tick_lilakit(void)
} }
if ((lk_in0 & 0x02) == 0 && lk_button_mode == 0) { if ((lk_in0 & 0x02) == 0 && lk_button_mode == 0) {
melody_index = 0;
melody_timeout = 0;
lk_ticks = 0; lk_ticks = 0;
lk_button_mode = 1; lk_button_mode = 1;
@ -208,9 +240,8 @@ static void tick_lilakit(void)
} }
static void init_lilakit(void) { static void init_lilakit(void)
{
i2cInit(I2CMASTER); // Init I2C
lkEnabled = (lkSetI2C(LK_I2C_CR_LS0, LK_I2C_LS_OFF << 0) == I2CSTATE_ACK); // probe i2c lkEnabled = (lkSetI2C(LK_I2C_CR_LS0, LK_I2C_LS_OFF << 0) == I2CSTATE_ACK); // probe i2c
if (lkEnabled == 0) { if (lkEnabled == 0) {
@ -245,7 +276,6 @@ static void init_lilakit(void) {
gpioSetDir(LK_PIEZO, gpioDirection_Output); gpioSetDir(LK_PIEZO, gpioDirection_Output);
gpioSetValue(LK_PIEZO, 1); gpioSetValue(LK_PIEZO, 1);
// Prepare blinking // Prepare blinking
lkSetI2C(LK_I2C_CR_PSC0, 0x23); lkSetI2C(LK_I2C_CR_PSC0, 0x23);
lkSetI2C(LK_I2C_CR_PWM0, 0x66); lkSetI2C(LK_I2C_CR_PWM0, 0x66);