VFD modular clock firmware
Dependencies: DipCortex-EEprom RTC flw mbed
menu.cpp@0:f6e68b4ce169, 2015-02-09 (annotated)
- Committer:
- Backstrom
- Date:
- Mon Feb 09 13:40:46 2015 +0000
- Revision:
- 0:f6e68b4ce169
Initial commit
Who changed what in which revision?
User | Revision | Line number | New 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 "menu.h" |
Backstrom | 0:f6e68b4ce169 | 18 | |
Backstrom | 0:f6e68b4ce169 | 19 | char menu_buf[MAX_BUF]; |
Backstrom | 0:f6e68b4ce169 | 20 | |
Backstrom | 0:f6e68b4ce169 | 21 | ///////////////////////////////////////////////////////// |
Backstrom | 0:f6e68b4ce169 | 22 | // MenuItemValue |
Backstrom | 0:f6e68b4ce169 | 23 | |
Backstrom | 0:f6e68b4ce169 | 24 | MenuItemValue::MenuItemValue(int32_t min, int32_t max) |
Backstrom | 0:f6e68b4ce169 | 25 | : m_min(min) |
Backstrom | 0:f6e68b4ce169 | 26 | , m_max(max) |
Backstrom | 0:f6e68b4ce169 | 27 | , m_value(min) |
Backstrom | 0:f6e68b4ce169 | 28 | , m_activated(false) |
Backstrom | 0:f6e68b4ce169 | 29 | { |
Backstrom | 0:f6e68b4ce169 | 30 | } |
Backstrom | 0:f6e68b4ce169 | 31 | |
Backstrom | 0:f6e68b4ce169 | 32 | int32_t MenuItemValue::getValue() |
Backstrom | 0:f6e68b4ce169 | 33 | { |
Backstrom | 0:f6e68b4ce169 | 34 | return m_value; |
Backstrom | 0:f6e68b4ce169 | 35 | } |
Backstrom | 0:f6e68b4ce169 | 36 | |
Backstrom | 0:f6e68b4ce169 | 37 | int32_t MenuItemValue::incrementValue() |
Backstrom | 0:f6e68b4ce169 | 38 | { |
Backstrom | 0:f6e68b4ce169 | 39 | if (m_activated) { |
Backstrom | 0:f6e68b4ce169 | 40 | m_value++; |
Backstrom | 0:f6e68b4ce169 | 41 | if (m_value > m_max) m_value = m_min; |
Backstrom | 0:f6e68b4ce169 | 42 | } |
Backstrom | 0:f6e68b4ce169 | 43 | else { |
Backstrom | 0:f6e68b4ce169 | 44 | m_activated = true; |
Backstrom | 0:f6e68b4ce169 | 45 | } |
Backstrom | 0:f6e68b4ce169 | 46 | |
Backstrom | 0:f6e68b4ce169 | 47 | return m_value; |
Backstrom | 0:f6e68b4ce169 | 48 | } |
Backstrom | 0:f6e68b4ce169 | 49 | |
Backstrom | 0:f6e68b4ce169 | 50 | void MenuItemValue::resetActive() |
Backstrom | 0:f6e68b4ce169 | 51 | { |
Backstrom | 0:f6e68b4ce169 | 52 | m_activated = false; |
Backstrom | 0:f6e68b4ce169 | 53 | } |
Backstrom | 0:f6e68b4ce169 | 54 | |
Backstrom | 0:f6e68b4ce169 | 55 | ///////////////////////////////////////////////////////// |
Backstrom | 0:f6e68b4ce169 | 56 | // MenuItem |
Backstrom | 0:f6e68b4ce169 | 57 | |
Backstrom | 0:f6e68b4ce169 | 58 | MenuItem::MenuItem(const char* shortName, const char* longName, bool onOff, PREFS pref) |
Backstrom | 0:f6e68b4ce169 | 59 | : m_onOff(onOff) |
Backstrom | 0:f6e68b4ce169 | 60 | , m_pref(pref) |
Backstrom | 0:f6e68b4ce169 | 61 | { |
Backstrom | 0:f6e68b4ce169 | 62 | strncpy(m_shortName, shortName, 4); |
Backstrom | 0:f6e68b4ce169 | 63 | strncpy(m_longName, longName, 8); |
Backstrom | 0:f6e68b4ce169 | 64 | m_menuItemValue = new MenuItemValue(0, 1); |
Backstrom | 0:f6e68b4ce169 | 65 | } |
Backstrom | 0:f6e68b4ce169 | 66 | |
Backstrom | 0:f6e68b4ce169 | 67 | MenuItem::MenuItem(const char* shortName, bool onOff, PREFS pref) |
Backstrom | 0:f6e68b4ce169 | 68 | : m_onOff(onOff) |
Backstrom | 0:f6e68b4ce169 | 69 | , m_pref(pref) |
Backstrom | 0:f6e68b4ce169 | 70 | { |
Backstrom | 0:f6e68b4ce169 | 71 | strncpy(m_shortName, shortName, 4); |
Backstrom | 0:f6e68b4ce169 | 72 | m_longName[0] = 0; |
Backstrom | 0:f6e68b4ce169 | 73 | m_menuItemValue = new MenuItemValue(0, 1); |
Backstrom | 0:f6e68b4ce169 | 74 | } |
Backstrom | 0:f6e68b4ce169 | 75 | |
Backstrom | 0:f6e68b4ce169 | 76 | MenuItem::MenuItem(const char* shortName, const char* longName, int32_t min, int32_t max, PREFS pref) |
Backstrom | 0:f6e68b4ce169 | 77 | : m_onOff(false) |
Backstrom | 0:f6e68b4ce169 | 78 | , m_pref(pref) |
Backstrom | 0:f6e68b4ce169 | 79 | { |
Backstrom | 0:f6e68b4ce169 | 80 | strncpy(m_shortName, shortName, 4); |
Backstrom | 0:f6e68b4ce169 | 81 | strncpy(m_longName, longName, 8); |
Backstrom | 0:f6e68b4ce169 | 82 | m_menuItemValue = new MenuItemValue(min, max); |
Backstrom | 0:f6e68b4ce169 | 83 | } |
Backstrom | 0:f6e68b4ce169 | 84 | |
Backstrom | 0:f6e68b4ce169 | 85 | MenuItem::MenuItem(const char* shortName, int32_t min, int32_t max, PREFS pref) |
Backstrom | 0:f6e68b4ce169 | 86 | : m_onOff(false) |
Backstrom | 0:f6e68b4ce169 | 87 | , m_pref(pref) |
Backstrom | 0:f6e68b4ce169 | 88 | { |
Backstrom | 0:f6e68b4ce169 | 89 | strncpy(m_shortName, shortName, 4); |
Backstrom | 0:f6e68b4ce169 | 90 | m_longName[0] = 0; |
Backstrom | 0:f6e68b4ce169 | 91 | m_menuItemValue = new MenuItemValue(min, max); |
Backstrom | 0:f6e68b4ce169 | 92 | } |
Backstrom | 0:f6e68b4ce169 | 93 | |
Backstrom | 0:f6e68b4ce169 | 94 | const char* MenuItem::getName(uint8_t digits) const |
Backstrom | 0:f6e68b4ce169 | 95 | { |
Backstrom | 0:f6e68b4ce169 | 96 | if (m_longName[0] == 0) return m_shortName; |
Backstrom | 0:f6e68b4ce169 | 97 | return digits <= 4 ? m_shortName : m_longName; |
Backstrom | 0:f6e68b4ce169 | 98 | } |
Backstrom | 0:f6e68b4ce169 | 99 | |
Backstrom | 0:f6e68b4ce169 | 100 | extern DigitalOut led; |
Backstrom | 0:f6e68b4ce169 | 101 | |
Backstrom | 0:f6e68b4ce169 | 102 | const char* MenuItem::selectValue(uint8_t digits) const |
Backstrom | 0:f6e68b4ce169 | 103 | { |
Backstrom | 0:f6e68b4ce169 | 104 | uint32_t value = m_menuItemValue->incrementValue(); |
Backstrom | 0:f6e68b4ce169 | 105 | |
Backstrom | 0:f6e68b4ce169 | 106 | // special handling |
Backstrom | 0:f6e68b4ce169 | 107 | if (m_pref == PREF_GPS_TZM) { |
Backstrom | 0:f6e68b4ce169 | 108 | value *= 15; |
Backstrom | 0:f6e68b4ce169 | 109 | } |
Backstrom | 0:f6e68b4ce169 | 110 | |
Backstrom | 0:f6e68b4ce169 | 111 | if (m_onOff) { |
Backstrom | 0:f6e68b4ce169 | 112 | if (value == 1) |
Backstrom | 0:f6e68b4ce169 | 113 | snprintf(menu_buf, MAX_BUF-1, "%s", "on"); |
Backstrom | 0:f6e68b4ce169 | 114 | else |
Backstrom | 0:f6e68b4ce169 | 115 | snprintf(menu_buf, MAX_BUF-1, "%s", "off"); |
Backstrom | 0:f6e68b4ce169 | 116 | } |
Backstrom | 0:f6e68b4ce169 | 117 | else if (m_pref == PREF_FLW) { |
Backstrom | 0:f6e68b4ce169 | 118 | if (value == 0) |
Backstrom | 0:f6e68b4ce169 | 119 | snprintf(menu_buf, MAX_BUF-1, "%s", "off"); |
Backstrom | 0:f6e68b4ce169 | 120 | if (value == 1) |
Backstrom | 0:f6e68b4ce169 | 121 | snprintf(menu_buf, MAX_BUF-1, "%s", "on"); |
Backstrom | 0:f6e68b4ce169 | 122 | else if (value == 2) |
Backstrom | 0:f6e68b4ce169 | 123 | snprintf(menu_buf, MAX_BUF-1, "%s", "full"); |
Backstrom | 0:f6e68b4ce169 | 124 | } |
Backstrom | 0:f6e68b4ce169 | 125 | else { |
Backstrom | 0:f6e68b4ce169 | 126 | // special handling for year/month/day - these values are not stored in the eeprom |
Backstrom | 0:f6e68b4ce169 | 127 | // but go directly to the RTC chip |
Backstrom | 0:f6e68b4ce169 | 128 | if (m_pref == PREF_YEAR) { |
Backstrom | 0:f6e68b4ce169 | 129 | set_year(value); |
Backstrom | 0:f6e68b4ce169 | 130 | } |
Backstrom | 0:f6e68b4ce169 | 131 | else if (m_pref == PREF_MONTH) { |
Backstrom | 0:f6e68b4ce169 | 132 | set_month(value); |
Backstrom | 0:f6e68b4ce169 | 133 | } |
Backstrom | 0:f6e68b4ce169 | 134 | else if (m_pref == PREF_DAY) { |
Backstrom | 0:f6e68b4ce169 | 135 | set_day(value); |
Backstrom | 0:f6e68b4ce169 | 136 | } |
Backstrom | 0:f6e68b4ce169 | 137 | |
Backstrom | 0:f6e68b4ce169 | 138 | snprintf(menu_buf, MAX_BUF-1, "%d", value); |
Backstrom | 0:f6e68b4ce169 | 139 | } |
Backstrom | 0:f6e68b4ce169 | 140 | |
Backstrom | 0:f6e68b4ce169 | 141 | // save to prefs |
Backstrom | 0:f6e68b4ce169 | 142 | if (m_pref != PREF_NULL) { |
Backstrom | 0:f6e68b4ce169 | 143 | set_pref(m_pref, value); |
Backstrom | 0:f6e68b4ce169 | 144 | } |
Backstrom | 0:f6e68b4ce169 | 145 | |
Backstrom | 0:f6e68b4ce169 | 146 | return menu_buf; |
Backstrom | 0:f6e68b4ce169 | 147 | } |
Backstrom | 0:f6e68b4ce169 | 148 | |
Backstrom | 0:f6e68b4ce169 | 149 | void MenuItem::resetActive() const |
Backstrom | 0:f6e68b4ce169 | 150 | { |
Backstrom | 0:f6e68b4ce169 | 151 | m_menuItemValue->resetActive(); |
Backstrom | 0:f6e68b4ce169 | 152 | } |
Backstrom | 0:f6e68b4ce169 | 153 | |
Backstrom | 0:f6e68b4ce169 | 154 | const MenuItem menu1("TIME", "SET TIME", true, PREF_SET_TIME); |
Backstrom | 0:f6e68b4ce169 | 155 | const MenuItem menu2("ALRM", "SET ALR", true, PREF_SET_ALARM); |
Backstrom | 0:f6e68b4ce169 | 156 | const MenuItem menu3("24H", true, PREF_24H); |
Backstrom | 0:f6e68b4ce169 | 157 | const MenuItem menu4("BRIT", "BRITE", 2, 10, PREF_BRIGHTNESS); |
Backstrom | 0:f6e68b4ce169 | 158 | const MenuItem menu5("YEAR", 2014, 2075, PREF_YEAR); |
Backstrom | 0:f6e68b4ce169 | 159 | const MenuItem menu6("MNTH", "MONTH", 1, 12, PREF_MONTH); |
Backstrom | 0:f6e68b4ce169 | 160 | const MenuItem menu7("DAY", 1, 31, PREF_DAY); |
Backstrom | 0:f6e68b4ce169 | 161 | const MenuItem menu8("GPS", true, PREF_GPS); |
Backstrom | 0:f6e68b4ce169 | 162 | const MenuItem menu9("TZH", "GPS TZH", -12, 14, PREF_GPS_TZH); |
Backstrom | 0:f6e68b4ce169 | 163 | const MenuItem menu10("TZM", "GPS TZM", 0, 3, PREF_GPS_TZM); |
Backstrom | 0:f6e68b4ce169 | 164 | const MenuItem menu11("ADTE", "AUTODATE", true, PREF_AUTODATE); |
Backstrom | 0:f6e68b4ce169 | 165 | const MenuItem menu12("FLW", 0, 2, PREF_FLW); |
Backstrom | 0:f6e68b4ce169 | 166 | |
Backstrom | 0:f6e68b4ce169 | 167 | #ifdef HAVE_GPS |
Backstrom | 0:f6e68b4ce169 | 168 | #define ITEMS 12 |
Backstrom | 0:f6e68b4ce169 | 169 | const MenuItem* menuItems[] = { &menu1, &menu2, &menu3, &menu4, &menu5, &menu6, &menu7, &menu8, &menu9, &menu10, &menu11, &menu12 }; |
Backstrom | 0:f6e68b4ce169 | 170 | #else |
Backstrom | 0:f6e68b4ce169 | 171 | #define ITEMS 8 |
Backstrom | 0:f6e68b4ce169 | 172 | const MenuItem* menuItems[] = { &menu1, &menu2, &menu3, &menu4, &menu5, &menu6, &menu7, &menu12 }; |
Backstrom | 0:f6e68b4ce169 | 173 | #endif |
Backstrom | 0:f6e68b4ce169 | 174 | |
Backstrom | 0:f6e68b4ce169 | 175 | Menu::Menu() |
Backstrom | 0:f6e68b4ce169 | 176 | : m_position(0) |
Backstrom | 0:f6e68b4ce169 | 177 | , m_size(ITEMS) |
Backstrom | 0:f6e68b4ce169 | 178 | , m_digits(8) |
Backstrom | 0:f6e68b4ce169 | 179 | { |
Backstrom | 0:f6e68b4ce169 | 180 | } |
Backstrom | 0:f6e68b4ce169 | 181 | |
Backstrom | 0:f6e68b4ce169 | 182 | const char* Menu::reset() |
Backstrom | 0:f6e68b4ce169 | 183 | { |
Backstrom | 0:f6e68b4ce169 | 184 | m_position = 0; |
Backstrom | 0:f6e68b4ce169 | 185 | return menuItems[m_position]->getName(m_digits); |
Backstrom | 0:f6e68b4ce169 | 186 | } |
Backstrom | 0:f6e68b4ce169 | 187 | |
Backstrom | 0:f6e68b4ce169 | 188 | const char* Menu::next() |
Backstrom | 0:f6e68b4ce169 | 189 | { |
Backstrom | 0:f6e68b4ce169 | 190 | menuItems[m_position]->resetActive(); |
Backstrom | 0:f6e68b4ce169 | 191 | |
Backstrom | 0:f6e68b4ce169 | 192 | m_position++; |
Backstrom | 0:f6e68b4ce169 | 193 | if (m_position >= ITEMS) m_position = 0; |
Backstrom | 0:f6e68b4ce169 | 194 | |
Backstrom | 0:f6e68b4ce169 | 195 | return menuItems[m_position]->getName(m_digits); |
Backstrom | 0:f6e68b4ce169 | 196 | } |
Backstrom | 0:f6e68b4ce169 | 197 | |
Backstrom | 0:f6e68b4ce169 | 198 | const char* Menu::select(bool& enterSetTime, bool& enterSetAlarm) |
Backstrom | 0:f6e68b4ce169 | 199 | { |
Backstrom | 0:f6e68b4ce169 | 200 | enterSetTime = enterSetAlarm = false; |
Backstrom | 0:f6e68b4ce169 | 201 | |
Backstrom | 0:f6e68b4ce169 | 202 | if (menuItems[m_position]->isSetTimeTrigger()) |
Backstrom | 0:f6e68b4ce169 | 203 | enterSetTime = true; |
Backstrom | 0:f6e68b4ce169 | 204 | |
Backstrom | 0:f6e68b4ce169 | 205 | if (menuItems[m_position]->isSetAlarmTrigger()) |
Backstrom | 0:f6e68b4ce169 | 206 | enterSetAlarm = true; |
Backstrom | 0:f6e68b4ce169 | 207 | |
Backstrom | 0:f6e68b4ce169 | 208 | return menuItems[m_position]->selectValue(m_digits); |
Backstrom | 0:f6e68b4ce169 | 209 | } |
Backstrom | 0:f6e68b4ce169 | 210 | |
Backstrom | 0:f6e68b4ce169 | 211 | void Menu::leave() |
Backstrom | 0:f6e68b4ce169 | 212 | { |
Backstrom | 0:f6e68b4ce169 | 213 | menuItems[m_position]->resetActive(); |
Backstrom | 0:f6e68b4ce169 | 214 | save_prefs(); |
Backstrom | 0:f6e68b4ce169 | 215 | } |