/** * This function calls ButtonsCheckEvents() once per call and returns which, if any, * of the Morse code events listed in the enum above have been encountered. It checks for BTN4 * events in its input and should be called at 100Hz so that the timing works. The * length that BTN4 needs to be held down for a dot is >= 0.25s and < 0.50s with a dash being a button * down event for >= 0.5s. The button uptime various between dots/dashes (>= .5s), letters * (>= 1s), and words (>= 2s). * * @note This function assumes that the buttons are all unpressed at startup, so that the first * event it will see is a BUTTON_EVENT_*DOWN. * * So pressing the button for 0.1s, releasing it for 0.1s, pressing it for 0.3s, and then waiting * will decode the string '.-' (A). It will trigger the following order of events: * 9 MORSE_EVENT_NONEs, 1 MORSE_EVENT_DOT, 39 MORSE_EVENT_NONEs, a MORSE_EVENT_DASH, 69 * MORSE_EVENT_NONEs, a MORSE_EVENT_END_CHAR, and then MORSE_EVENT_INTER_WORDs. * * @return The MorseEvent that occurred. */ MorseEvent MorseCheckEvents(void) { switch (myState) { case(WAITING): {// if in the state of waiting if (ButtonsCheckEvents() == BUTTON_EVENT_4DOWN) { myState = DOT; myCount = 0; } return MORSE_EVENT_NONE; break; } case(DOT): { // in the state of dot myCount += 1; if (myCount >= MORSE_EVENT_LENGTH_DOWN_DOT) { myState = DASH; } if (ButtonsCheckEvents() == BUTTON_EVENT_4UP) { // Chenge into Inter letter myState = INTER_LETTER; myCount = 0; return MORSE_EVENT_DOT; } return MORSE_EVENT_NONE; break; } case(DASH): { //in Dash state if (ButtonsCheckEvents() == BUTTON_EVENT_4UP) { // change my state to Inter letter myState = INTER_LETTER; myCount = 0; return MORSE_EVENT_DASH; } return MORSE_EVENT_NONE; break; } case(INTER_LETTER): { //In the inter letter state myCount += 1; if (myCount > MORSE_EVENT_LENGTH_UP_INTER_WORD) { //if inter letter time out, we go to myState = WAITING; return MORSE_EVENT_INTER_WORD; } else if (ButtonsCheckEvents() == BUTTON_EVENT_4DOWN) { // if (myCount > MORSE_EVENT_LENGTH_UP_INTER_LETTER) { myCount = 0; myState = DOT; // return inter ltter event return MORSE_EVENT_INTER_LETTER; } myCount = 0; myState = DOT; } return MORSE_EVENT_NONE; break; } default: { //In the deflaut state FATAL_ERROR(); break; } } }
/** * Timer2 interrupt. Checks for button events. */ void __ISR(_TIMER_2_VECTOR, ipl4) TimerInterrupt100Hz(void) { // Clear the interrupt flag. IFS0CLR = 1 << 8; buttonEvents = ButtonsCheckEvents(); }