#include #include #include // SET UP LCD #define DISPLAY_USEPORTC #define LCDWIDTH 16 #ifdef DISPLAY_USEPORTA #define DISPLAY_PORT PORTA #define DISPLAY_PIN PINA #define DISPLAY_DDR DDRA #asm(".equ __lcd_port=0x1B") #elif defined DISPLAY_USEPORTB #define DISPLAY_PORT PORTB #define DISPLAY_PIN PINB #define DISPLAY_DDR DDRB #asm(".equ __lcd_port=0x18") #elif defined DISPLAY_USEPORTC #define DISPLAY_PORT PORTC #define DISPLAY_PIN PINC #define DISPLAY_DDR DDRC #asm(".equ __lcd_port=0x15") #elif defined DISPLAY_USEPORTD #define DISPLAY_PORT PORTD #define DISPLAY_PIN PIND #define DISPLAY_DDR DDRD #asm(".equ __lcd_port=0x12") #else #error Please specify which port to use for the display. #endif //*************************************************** // SET UP WIRELESS & MAGNETIC SENSOR // using PIN0 ONLY #define WIRELESS_SENSOR_PORT PORTD #define WIRELESS_SENSOR_PIN PIND #define WIRELESS_SENSOR_DDR DDRD //*************************************************** // SET UP BUTTON #define BUTTON_PORT PORTB #define BUTTON_PIN PINB #define BUTTON_DDR DDRB //*************************************************** // SET UP SOUND #define SOUND_PORT PORTA #define SOUND_PIN PINA #define SOUND_DDR DDRA //*************************************************** // Testing definitions for buttons #define SEAT 0b00000010 // P2 #define UP 0b00001000 // P3 #define DOWN 0b00010000 // P4 #define RIGHT 0b00100000 // P5 #define LEFT 0b01000000 // P6 #define OK 0b10000000 // P7 //*************************************************** // Task times, 1 ms resolution #define BOUNCETIME 50 #define UPDATETIME 100 #define BLINKCOUNT 1000 #define FIVESECONDS 5000 #define TENSECONDS 10000 #define BEEPTIME 200 #define FASTBEEPTIME 100 //*************************************************** // State machine states enum debouncerState {NOPUSH, MAYBEPUSH, PUSHED, MAYBENOPUSH}; #define NUMSTATES 8 enum state {LOCKED, DISPLAYTIME, DISPLAYSPEED, DISPLAYDIST, DISPLAYCALORIES, DISPLAYWEIGHT, DISPLAYPASSWORD, ACTIVATELOCK}; enum action {VIEW, MODIFY}; //*************************************************** #define MAXDIGITS 4 typedef struct user { signed char password[MAXDIGITS]; signed char weight[MAXDIGITS]; } User; //*************************************************** // GLOBAL VARIABLES #define CIRCUMFERENCE 81.75 // inches // LCD related char lcd_buffer[17]; // Debouncer related unsigned char PushFlag; // message indicating a button push unsigned char PushedButton; // indicates which button(s) are pushed unsigned char PushState; // state of the debouncer unsigned char Unchecked; // indicates the button has not been checked // Task timers int bouncetime, updatetime, revtime, wirelesstime, beeptime; // The state of the machine signed char curState, curAction; float curVelocity, curMaxVelocity, curDistance, curCalories; signed char curDigit; // The digit to update 0 <= curDigit < MAXDIGITS unsigned char blink; signed char password_try[MAXDIGITS]; // current password try User u; // Magnetic sensor related char curSensor, prevSensor, locked_sensor_state; float velocityMultiplierMPH = CIRCUMFERENCE*3600*1000/63360; float velocityMultiplierMS = CIRCUMFERENCE*25.4; // conversion from inches to meters (with ms to s conversion factor) float calorieMultiplier = 0.239005736 / 4.4 / 1000; // has conversion for joules to calories, lbs to kg float circInMiles = CIRCUMFERENCE/63360; char ready; // Alarm related char alarm, alarm_output, numBeepToggles, fastbeep, alarmOn; // Blink related char chartoblink; int blinkcount; // Timer related char timerOn, hours, minutes, seconds, tenths; //*************************************************** // FUNCTION PROTOTYPES /** * Checks the buttons by looking at the state of the debouncer * as well as the variable PushedButton. * This function is called every UPDATETIME ms. **/ void update(void); /** * This function is called every BEEPTIME ms. * This toggles the alarm, depending on numBeepToggles. * Number of actual beeps = (numBeepToggles / 2) + 1 */ void beep(void); /** * Verifies that the button has been pressed. */ char verifyButton(char b); /** * Updates the state of the global state machine. * Called if a button has been pushed to change state * and the state is not LOCKED. * See state diagram for the state transitions. */ void updateState(void); // Converts an array of length 4 into an int for printing int arrayToInt(char[]); /** * Updates the array passed to it. * Should only be called if the button pressed is not OK. */ void updateDigit(char[]); // The debouncer state machine, run every DEBOUNCETIME ms. void debounce(void); // Prints the current state void printState(void); // Resets the timer. void resetTimer(void); // General initialization routine void initialize(void);