void timer_event(GR_EVENT_TIMER *ev, nxeyes_state *state) { if(ev->tid != state->tid) return; /* Must free timer - even though it's fired, the handle and data structure live on. */ GrDestroyTimer(state->tid); state->tid = 0; if(state->eyes_closed) { state->eyes_closed = 0; start_blink_timer(state); } else { state->eyes_closed = 1; start_blink_timer(state); } draw_eyes(state, 1); }
nxeyes_state *init(void) { nxeyes_state *state; GR_REGION_ID rid1, rid2; GR_SCREEN_INFO si; GR_WM_PROPERTIES props; if(!(state = malloc(sizeof(nxeyes_state)))) return NULL; state->oldx = state->oldy = state->x = state->y = 0; state->button_down = state->eyes_closed = state->quit = 0; state->olx = state->orx = EYEMASK_WIDTH / 2; state->oly = state->ory = EYEMASK_HEIGHT / 2; GrGetScreenInfo(&si); state->mousex = si.xpos; state->mousey = si.ypos; state->mouse_moved = 1; state->gc = GrNewGC(); GrSetGCForeground(state->gc, GR_COLOR_WHITE); GrSetGCBackground(state->gc, GR_COLOR_BLACK); state->wid = GrNewWindow(GR_ROOT_WINDOW_ID, 0, 0, (EYEFG_HEIGHT * 2) + EYE_SPACING, EYEFG_HEIGHT, 0, GR_COLOR_WHITE, 0); rid1 = GrNewBitmapRegion(eyemask_bits, EYEMASK_WIDTH, EYEMASK_HEIGHT); rid2 = GrNewBitmapRegion(eyemask_bits, EYEMASK_WIDTH, EYEMASK_HEIGHT); GrOffsetRegion(rid2, EYEMASK_WIDTH + EYE_SPACING, 0); GrUnionRegion(rid1, rid1, rid2); GrSetWindowRegion(state->wid, rid1, GR_WINDOW_BOUNDING_MASK); GrDestroyRegion(rid1); GrDestroyRegion(rid2); props.flags = GR_WM_FLAGS_PROPS; props.props = GR_WM_PROPS_NODECORATE; GrSetWMProperties(state->wid, &props); GrSelectEvents(state->wid, GR_EVENT_MASK_CLOSE_REQ | GR_EVENT_MASK_MOUSE_POSITION | GR_EVENT_MASK_BUTTON_UP | GR_EVENT_MASK_BUTTON_DOWN | GR_EVENT_MASK_EXPOSURE | GR_EVENT_MASK_TIMER); GrSelectEvents(GR_ROOT_WINDOW_ID, GR_EVENT_MASK_MOUSE_POSITION); GrMapWindow(state->wid); srand(123); #if MW_FEATURE_TIMERS start_blink_timer(state); #endif return state; }
/****************************************************************************** * File: main.c * Author: Kevin Day * Date: February, 2005 * Description: * Main program for audi radio interface * * Copyright (c) 2005 Kevin Day * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * *******************************************************************************/ #include <avr/interrupt.h> #include <avr/pgmspace.h> #include <avr/io.h> #include <avr/wdt.h> #include "types.h" #include "tasks.h" #include "comms_generic.h" #include "timers.h" #include "adc.h" #include "persist.h" #include "hcms.h" #include "gpsavr.h" #include "hud.h" #include "spimaster.h" #include "avrcan.h" #include "avrms2.h" #include "swuart.h" #include "hwi2c.h" #include "adcgauges.h" #include "miscgpio.h" #include "sensors.h" #define LED_PORT PORTG #define LED_DIR DDRG #define LED_PIN PING #define LED_BIT 0 #define ANT_PORT PORTB #define ANT_DIR DDRB #define ANT_PIN PINB #define ANT_BIT 2 void debug_led(u8 val); static inline void start_blink_timer(); void bufferpool_init(); task_t* comms_task_create(); task_t *radio_input_task_create(); #define STACK_CANARY 0xC5 void StackPaint(void) __attribute__ ((naked)) __attribute__ ((section (".init1"))); void StackPaint(void) { #if 0 uint8_t *p = &_end; while(p <= &__stack) { *p = STACK_CANARY; p++; } #else __asm volatile (" ldi r30,lo8(_end)\n" " ldi r31,hi8(_end)\n" " ldi r24,lo8(0xc5)\n" /* STACK_CANARY = 0xc5 */ " ldi r25,hi8(__stack)\n" " rjmp .cmp\n" ".loop:\n" " st Z+,r24\n" ".cmp:\n" " cpi r30,lo8(__stack)\n" " cpc r31,r25\n" " brlo .loop\n" " breq .loop"::); #endif } extern uint8_t __heap_start; /* not _end because of .bufferpool */ extern uint8_t __stack; uint16_t StackCount(void) { const uint8_t *p = &__heap_start; uint16_t c = 0; while(*p == STACK_CANARY && p <= &__stack) { p++; c++; } return c; } #define ADC_CONTEXTS 8 adc_context_t adc_context[ADC_CONTEXTS]; u8 num_adc; u16 stack_high; #define DISPAVR_RESET_PIN 4 /* on port B */ int main() { u8 mcusr_rst = MCUSR; MCUSR = 0; wdt_disable(); wdt_enable(WDTO_2S); /* Disable JTAG so the ADC pins are available. * Don't do this if the JTAG reset flag is set -- * presumably the jtag pod is connected in that * case. */ if (! (mcusr_rst & (1<<JTRF))) { MCUCR |= (1<<JTD); } /* Assert remote AVR reset */ PORTB &= ~(1<<DISPAVR_RESET_PIN); DDRB |= (1<<DISPAVR_RESET_PIN); bufferpool_init(); init_persist_data(); systimer_init(); //fuel_gauge_init(&adc_context[num_adc++]); num_adc = sensors_init(&adc_context[num_adc], num_adc); adc_init_adc(ADC_DIV128, num_adc, adc_context); /* Deassert remote AVR reset */ PORTB |= (1<<DISPAVR_RESET_PIN); spimaster_init(); init_avrms2(); // swuart_init(); /* Enable interrupts */ sei(); /* Populate the tasklist in priority order */ tasklist[num_tasks++] = comms_task_create(); tasklist[num_tasks++] = gps_task_create(); tasklist[num_tasks++] = can_task_create(); tasklist[num_tasks++] = hud_task_create(); // tasklist[num_tasks++] = i2c_task_create(); tasklist[num_tasks++] = gpio_task_create(); // start_blink_timer(); /* non-preemptive static priority scheduler */ while(1) { wdt_reset(); u8 taskidx; u8 r; for(taskidx=0; taskidx<num_tasks; taskidx++) { r = tasklist[taskidx]->taskfunc(); if (r) break; } if (r == 0) { stack_high = StackCount(); /* only run this after all tasks run */ } } }