VFD modular clock firmware

Dependencies:   DipCortex-EEprom RTC flw mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers menu.cpp Source File

menu.cpp

00001 /*
00002  * VFD Modular Clock - mbed
00003  * (C) 2011-14 Akafugu Corporation
00004  *
00005  * This program is free software; you can redistribute it and/or modify it under the
00006  * terms of the GNU General Public License as published by the Free Software
00007  * Foundation; either version 2 of the License, or (at your option) any later
00008  * version.
00009  *
00010  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
00011  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
00012  * PARTICULAR PURPOSE.  See the GNU General Public License for more details.
00013  *
00014  */
00015 
00016 #include "global.h"
00017 #include "menu.h"
00018 
00019 char menu_buf[MAX_BUF];
00020 
00021 /////////////////////////////////////////////////////////
00022 // MenuItemValue
00023 
00024 MenuItemValue::MenuItemValue(int32_t min, int32_t max)
00025 : m_min(min)
00026 , m_max(max)
00027 , m_value(min)
00028 , m_activated(false)
00029 {
00030 }
00031 
00032 int32_t MenuItemValue::getValue()
00033 {
00034     return m_value;
00035 }
00036 
00037 int32_t MenuItemValue::incrementValue()
00038 {
00039     if (m_activated) {
00040         m_value++;
00041         if (m_value > m_max) m_value = m_min;
00042     }
00043     else {
00044         m_activated = true;    
00045     }
00046     
00047     return m_value;
00048 }    
00049 
00050 void MenuItemValue::resetActive()
00051 {
00052     m_activated = false;
00053 }
00054 
00055 /////////////////////////////////////////////////////////
00056 // MenuItem
00057 
00058 MenuItem::MenuItem(const char* shortName, const char* longName, bool onOff, PREFS pref)
00059 : m_onOff(onOff)
00060 , m_pref(pref)
00061 {
00062     strncpy(m_shortName, shortName, 4);
00063     strncpy(m_longName, longName, 8);
00064     m_menuItemValue = new MenuItemValue(0, 1);
00065 }
00066 
00067 MenuItem::MenuItem(const char* shortName, bool onOff, PREFS pref)
00068 : m_onOff(onOff)
00069 , m_pref(pref)
00070 {
00071     strncpy(m_shortName, shortName, 4);
00072     m_longName[0] = 0;
00073     m_menuItemValue = new MenuItemValue(0, 1);
00074 }
00075 
00076 MenuItem::MenuItem(const char* shortName, const char* longName, int32_t min, int32_t max, PREFS pref)
00077 : m_onOff(false)
00078 , m_pref(pref)
00079 {
00080     strncpy(m_shortName, shortName, 4);
00081     strncpy(m_longName, longName, 8);    
00082     m_menuItemValue = new MenuItemValue(min, max);
00083 }
00084 
00085 MenuItem::MenuItem(const char* shortName, int32_t min, int32_t max, PREFS pref)
00086 : m_onOff(false)
00087 , m_pref(pref)
00088 {
00089     strncpy(m_shortName, shortName, 4);
00090     m_longName[0] = 0;
00091     m_menuItemValue = new MenuItemValue(min, max);
00092 }
00093 
00094 const char* MenuItem::getName(uint8_t digits) const
00095 {
00096     if (m_longName[0] == 0) return m_shortName;
00097     return digits <= 4 ? m_shortName : m_longName;
00098 }
00099 
00100 extern DigitalOut led;
00101 
00102 const char* MenuItem::selectValue(uint8_t digits) const
00103 {
00104     uint32_t value = m_menuItemValue->incrementValue();
00105 
00106     // special handling
00107     if (m_pref == PREF_GPS_TZM) {
00108         value *= 15;    
00109     }
00110     
00111     if (m_onOff) {
00112         if (value == 1)
00113             snprintf(menu_buf, MAX_BUF-1, "%s", "on");
00114         else
00115             snprintf(menu_buf, MAX_BUF-1, "%s", "off");
00116     }
00117     else if (m_pref == PREF_FLW) {
00118         if (value == 0)
00119             snprintf(menu_buf, MAX_BUF-1, "%s", "off");
00120         if (value == 1)
00121             snprintf(menu_buf, MAX_BUF-1, "%s", "on");
00122         else if (value == 2)
00123             snprintf(menu_buf, MAX_BUF-1, "%s", "full");            
00124     }
00125     else {
00126         // special handling for year/month/day - these values are not stored in the eeprom
00127         // but go directly to the RTC chip
00128         if (m_pref == PREF_YEAR) {
00129             set_year(value);    
00130         }
00131         else if (m_pref == PREF_MONTH) {
00132             set_month(value);    
00133         }
00134         else if (m_pref == PREF_DAY) {
00135             set_day(value);    
00136         }
00137         
00138         snprintf(menu_buf, MAX_BUF-1, "%d", value);    
00139     }
00140     
00141     // save to prefs
00142     if (m_pref != PREF_NULL) {
00143         set_pref(m_pref, value);
00144     }
00145 
00146     return menu_buf;
00147 }
00148 
00149 void MenuItem::resetActive() const
00150 {
00151     m_menuItemValue->resetActive();
00152 }
00153 
00154 const MenuItem menu1("TIME", "SET TIME", true, PREF_SET_TIME);
00155 const MenuItem menu2("ALRM", "SET ALR", true, PREF_SET_ALARM);
00156 const MenuItem menu3("24H", true, PREF_24H);
00157 const MenuItem menu4("BRIT", "BRITE", 2, 10, PREF_BRIGHTNESS);
00158 const MenuItem menu5("YEAR", 2014, 2075, PREF_YEAR);
00159 const MenuItem menu6("MNTH", "MONTH", 1, 12, PREF_MONTH);
00160 const MenuItem menu7("DAY", 1, 31, PREF_DAY);
00161 const MenuItem menu8("GPS", true, PREF_GPS);
00162 const MenuItem menu9("TZH", "GPS TZH", -12, 14, PREF_GPS_TZH);
00163 const MenuItem menu10("TZM", "GPS TZM", 0, 3, PREF_GPS_TZM);
00164 const MenuItem menu11("ADTE", "AUTODATE", true, PREF_AUTODATE);
00165 const MenuItem menu12("FLW", 0, 2, PREF_FLW);
00166 
00167 #ifdef HAVE_GPS
00168 #define ITEMS 12
00169 const MenuItem* menuItems[] = { &menu1, &menu2, &menu3, &menu4, &menu5, &menu6, &menu7, &menu8, &menu9, &menu10, &menu11, &menu12 };
00170 #else
00171 #define ITEMS 8
00172 const MenuItem* menuItems[] = { &menu1, &menu2, &menu3, &menu4, &menu5, &menu6, &menu7, &menu12 };
00173 #endif
00174 
00175 Menu::Menu()
00176     : m_position(0)
00177     , m_size(ITEMS)
00178     , m_digits(8)
00179 {
00180 }
00181 
00182 const char* Menu::reset()
00183 {
00184     m_position = 0;
00185     return menuItems[m_position]->getName(m_digits);
00186 }
00187 
00188 const char* Menu::next()
00189 {
00190     menuItems[m_position]->resetActive();
00191     
00192     m_position++;
00193     if (m_position >= ITEMS) m_position = 0;
00194     
00195     return menuItems[m_position]->getName(m_digits);
00196 }
00197 
00198 const char* Menu::select(bool& enterSetTime, bool& enterSetAlarm)
00199 {
00200     enterSetTime = enterSetAlarm = false;
00201     
00202     if (menuItems[m_position]->isSetTimeTrigger())
00203         enterSetTime = true;
00204 
00205     if (menuItems[m_position]->isSetAlarmTrigger())
00206         enterSetAlarm = true;
00207     
00208     return menuItems[m_position]->selectValue(m_digits);
00209 }
00210 
00211 void Menu::leave()
00212 {
00213     menuItems[m_position]->resetActive(); 
00214     save_prefs();   
00215 }