VFD modular clock firmware

Dependencies:   DipCortex-EEprom RTC flw mbed

Committer:
Backstr?m
Date:
Tue Feb 24 23:01:40 2015 +0900
Revision:
12:dfb422107412
Parent:
0:f6e68b4ce169
Added tag v1.0.2 for changeset 34b344fdec98

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Backstrom 0:f6e68b4ce169 1 /*
Backstrom 0:f6e68b4ce169 2 * VFD Modular Clock - mbed
Backstrom 0:f6e68b4ce169 3 * (C) 2011-14 Akafugu Corporation
Backstrom 0:f6e68b4ce169 4 *
Backstrom 0:f6e68b4ce169 5 * This program is free software; you can redistribute it and/or modify it under the
Backstrom 0:f6e68b4ce169 6 * terms of the GNU General Public License as published by the Free Software
Backstrom 0:f6e68b4ce169 7 * Foundation; either version 2 of the License, or (at your option) any later
Backstrom 0:f6e68b4ce169 8 * version.
Backstrom 0:f6e68b4ce169 9 *
Backstrom 0:f6e68b4ce169 10 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
Backstrom 0:f6e68b4ce169 11 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
Backstrom 0:f6e68b4ce169 12 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
Backstrom 0:f6e68b4ce169 13 *
Backstrom 0:f6e68b4ce169 14 */
Backstrom 0:f6e68b4ce169 15
Backstrom 0:f6e68b4ce169 16 #include "global.h"
Backstrom 0:f6e68b4ce169 17 #include "button.h"
Backstrom 0:f6e68b4ce169 18
Backstrom 0:f6e68b4ce169 19 DigitalIn button1(PinMap::button1);
Backstrom 0:f6e68b4ce169 20 DigitalIn button2(PinMap::button2);
Backstrom 0:f6e68b4ce169 21 DigitalIn button3(PinMap::button3);
Backstrom 0:f6e68b4ce169 22
Backstrom 0:f6e68b4ce169 23 uint8_t button_count = 3;
Backstrom 0:f6e68b4ce169 24
Backstrom 0:f6e68b4ce169 25 uint8_t saved_keystatus = 0;
Backstrom 0:f6e68b4ce169 26 uint8_t keydown_keys = 0;
Backstrom 0:f6e68b4ce169 27 uint8_t keyup_keys = 0;
Backstrom 0:f6e68b4ce169 28 uint8_t keyrepeat_keys = 0;
Backstrom 0:f6e68b4ce169 29
Backstrom 0:f6e68b4ce169 30 uint16_t keyboard_counter[3] = {0, 0, 0};
Backstrom 0:f6e68b4ce169 31 uint8_t button_bit[3] = { 1, 2, 4};
Backstrom 0:f6e68b4ce169 32
Backstrom 0:f6e68b4ce169 33 #define REPEAT_SPEED 35
Backstrom 0:f6e68b4ce169 34 volatile uint8_t repeat_speed = REPEAT_SPEED;
Backstrom 0:f6e68b4ce169 35
Backstrom 0:f6e68b4ce169 36 void initialize_buttons()
Backstrom 0:f6e68b4ce169 37 {
Backstrom 0:f6e68b4ce169 38 // enable pullups
Backstrom 0:f6e68b4ce169 39 button1.mode(PullUp);
Backstrom 0:f6e68b4ce169 40 button2.mode(PullUp);
Backstrom 0:f6e68b4ce169 41 button3.mode(PullUp);
Backstrom 0:f6e68b4ce169 42 }
Backstrom 0:f6e68b4ce169 43
Backstrom 0:f6e68b4ce169 44 uint8_t get_keystatus() {
Backstrom 0:f6e68b4ce169 45 return (!button1) | (!button2 << 1) | (!button3 << 2);
Backstrom 0:f6e68b4ce169 46 }
Backstrom 0:f6e68b4ce169 47
Backstrom 0:f6e68b4ce169 48 void button_tick()
Backstrom 0:f6e68b4ce169 49 {
Backstrom 0:f6e68b4ce169 50 uint8_t keystatus = (!button1) | (!button2 << 1) | (!button3 << 2);
Backstrom 0:f6e68b4ce169 51
Backstrom 0:f6e68b4ce169 52 keydown_keys |= (uint8_t)(keystatus & ~(saved_keystatus));
Backstrom 0:f6e68b4ce169 53 keyup_keys |= (uint8_t)(~(keystatus) & saved_keystatus);
Backstrom 0:f6e68b4ce169 54 saved_keystatus = keystatus;
Backstrom 0:f6e68b4ce169 55
Backstrom 0:f6e68b4ce169 56 for(uint8_t i = 0; i < button_count; i++) {
Backstrom 0:f6e68b4ce169 57 if(~(keydown_keys)&button_bit[i])
Backstrom 0:f6e68b4ce169 58 ; // Do nothing, no keyrepeat is needed
Backstrom 0:f6e68b4ce169 59 else if(keyup_keys&button_bit[i])
Backstrom 0:f6e68b4ce169 60 keyboard_counter[i] = 0;
Backstrom 0:f6e68b4ce169 61 else {
Backstrom 0:f6e68b4ce169 62 if(keyboard_counter[i] >= repeat_speed) {
Backstrom 0:f6e68b4ce169 63 keyrepeat_keys |= button_bit[i];
Backstrom 0:f6e68b4ce169 64 keyboard_counter[i] = 0;
Backstrom 0:f6e68b4ce169 65 }
Backstrom 0:f6e68b4ce169 66
Backstrom 0:f6e68b4ce169 67 keyboard_counter[i]++;
Backstrom 0:f6e68b4ce169 68 }
Backstrom 0:f6e68b4ce169 69 }
Backstrom 0:f6e68b4ce169 70 }
Backstrom 0:f6e68b4ce169 71
Backstrom 0:f6e68b4ce169 72 void get_button_state(struct BUTTON_STATE* buttons)
Backstrom 0:f6e68b4ce169 73 {
Backstrom 0:f6e68b4ce169 74 buttons->b1_keydown = keydown_keys&(button_bit[0]);
Backstrom 0:f6e68b4ce169 75 buttons->b1_keyup = keyup_keys&(button_bit[0]);
Backstrom 0:f6e68b4ce169 76 buttons->b1_repeat = keyrepeat_keys&(button_bit[0]);
Backstrom 0:f6e68b4ce169 77
Backstrom 0:f6e68b4ce169 78 if (keyrepeat_keys&(button_bit[0]))
Backstrom 0:f6e68b4ce169 79 keyrepeat_keys &= ~(button_bit[0]);
Backstrom 0:f6e68b4ce169 80
Backstrom 0:f6e68b4ce169 81 // Reset if we got keyup
Backstrom 0:f6e68b4ce169 82 if(keyup_keys&(button_bit[0])) {
Backstrom 0:f6e68b4ce169 83 keydown_keys &= ~(button_bit[0]);
Backstrom 0:f6e68b4ce169 84 keyup_keys &= ~(button_bit[0]);
Backstrom 0:f6e68b4ce169 85 keyrepeat_keys &= ~(button_bit[0]);
Backstrom 0:f6e68b4ce169 86 keyboard_counter[0] = 0;
Backstrom 0:f6e68b4ce169 87 }
Backstrom 0:f6e68b4ce169 88
Backstrom 0:f6e68b4ce169 89 buttons->b2_keydown = keydown_keys&(button_bit[1]);
Backstrom 0:f6e68b4ce169 90 buttons->b2_keyup = keyup_keys&(button_bit[1]);
Backstrom 0:f6e68b4ce169 91 buttons->b2_repeat = keyrepeat_keys&(button_bit[1]);
Backstrom 0:f6e68b4ce169 92
Backstrom 0:f6e68b4ce169 93 if (keyrepeat_keys&(button_bit[1]))
Backstrom 0:f6e68b4ce169 94 keyrepeat_keys &= ~(button_bit[1]);
Backstrom 0:f6e68b4ce169 95
Backstrom 0:f6e68b4ce169 96 // Reset if we got keyup
Backstrom 0:f6e68b4ce169 97 if(keyup_keys&(button_bit[1])) {
Backstrom 0:f6e68b4ce169 98 keydown_keys &= ~(button_bit[1]);
Backstrom 0:f6e68b4ce169 99 keyup_keys &= ~(button_bit[1]);
Backstrom 0:f6e68b4ce169 100 keyrepeat_keys &= ~(button_bit[1]);
Backstrom 0:f6e68b4ce169 101 keyboard_counter[1] = 0;
Backstrom 0:f6e68b4ce169 102 }
Backstrom 0:f6e68b4ce169 103
Backstrom 0:f6e68b4ce169 104 buttons->b3_keydown = keydown_keys&(button_bit[2]);
Backstrom 0:f6e68b4ce169 105 buttons->b3_keyup = keyup_keys&(button_bit[2]);
Backstrom 0:f6e68b4ce169 106 buttons->b3_repeat = keyrepeat_keys&(button_bit[2]);
Backstrom 0:f6e68b4ce169 107
Backstrom 0:f6e68b4ce169 108 if (keyrepeat_keys&(button_bit[2]))
Backstrom 0:f6e68b4ce169 109 keyrepeat_keys &= ~(button_bit[2]);
Backstrom 0:f6e68b4ce169 110
Backstrom 0:f6e68b4ce169 111 // Reset if we got keyup
Backstrom 0:f6e68b4ce169 112 if(keyup_keys&(button_bit[2])) {
Backstrom 0:f6e68b4ce169 113 keydown_keys &= ~(button_bit[2]);
Backstrom 0:f6e68b4ce169 114 keyup_keys &= ~(button_bit[2]);
Backstrom 0:f6e68b4ce169 115 keyrepeat_keys &= ~(button_bit[2]);
Backstrom 0:f6e68b4ce169 116 keyboard_counter[2] = 0;
Backstrom 0:f6e68b4ce169 117 }
Backstrom 0:f6e68b4ce169 118
Backstrom 0:f6e68b4ce169 119 buttons->both_held = (keydown_keys&(button_bit[0])) && (keydown_keys&(button_bit[1]));
Backstrom 0:f6e68b4ce169 120 buttons->none_held = ~(saved_keystatus)&(button_bit[0]) && ~(saved_keystatus)&((button_bit[1]));
Backstrom 0:f6e68b4ce169 121 }
Backstrom 0:f6e68b4ce169 122