VFD modular clock firmware

Dependencies:   DipCortex-EEprom RTC flw mbed

Committer:
Backstrom
Date:
Mon Feb 09 13:40:46 2015 +0000
Revision:
0:f6e68b4ce169
Initial commit

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
Backstrom 0:f6e68b4ce169 18 #include "prefs.h"
Backstrom 0:f6e68b4ce169 19 #include "DipCortex-EEprom.h"
Backstrom 0:f6e68b4ce169 20
Backstrom 0:f6e68b4ce169 21 #include "VFDDisplay.h"
Backstrom 0:f6e68b4ce169 22 #include "IV18Display.h"
Backstrom 0:f6e68b4ce169 23 #include "flw.h"
Backstrom 0:f6e68b4ce169 24
Backstrom 0:f6e68b4ce169 25 const uint8_t sig0 = 0x42;
Backstrom 0:f6e68b4ce169 26 const uint8_t sig1 = 0x66;
Backstrom 0:f6e68b4ce169 27
Backstrom 0:f6e68b4ce169 28 PrefsData prefs_data;
Backstrom 0:f6e68b4ce169 29
Backstrom 0:f6e68b4ce169 30 extern IV18Display display;
Backstrom 0:f6e68b4ce169 31 extern DigitalOut led;
Backstrom 0:f6e68b4ce169 32 extern FourLetterWord flw;
Backstrom 0:f6e68b4ce169 33
Backstrom 0:f6e68b4ce169 34 static int s_pref_year;
Backstrom 0:f6e68b4ce169 35 static int s_pref_month;
Backstrom 0:f6e68b4ce169 36 static int s_pref_day;
Backstrom 0:f6e68b4ce169 37
Backstrom 0:f6e68b4ce169 38 uint8_t crc8(const void *vptr, int len)
Backstrom 0:f6e68b4ce169 39 {
Backstrom 0:f6e68b4ce169 40 const uint8_t *data = static_cast<const uint8_t *>(vptr);
Backstrom 0:f6e68b4ce169 41 unsigned crc = 0;
Backstrom 0:f6e68b4ce169 42 int i, j;
Backstrom 0:f6e68b4ce169 43
Backstrom 0:f6e68b4ce169 44 for (j = len; j; j--, data++) {
Backstrom 0:f6e68b4ce169 45 crc ^= (*data << 8);
Backstrom 0:f6e68b4ce169 46 for(i = 8; i; i--) {
Backstrom 0:f6e68b4ce169 47 if (crc & 0x8000)
Backstrom 0:f6e68b4ce169 48 crc ^= (0x1070 << 3);
Backstrom 0:f6e68b4ce169 49 crc <<= 1;
Backstrom 0:f6e68b4ce169 50 }
Backstrom 0:f6e68b4ce169 51 }
Backstrom 0:f6e68b4ce169 52
Backstrom 0:f6e68b4ce169 53 return (uint8_t)(crc >> 8);
Backstrom 0:f6e68b4ce169 54 }
Backstrom 0:f6e68b4ce169 55
Backstrom 0:f6e68b4ce169 56 void init_prefs()
Backstrom 0:f6e68b4ce169 57 {
Backstrom 0:f6e68b4ce169 58 IAP_Init();
Backstrom 0:f6e68b4ce169 59
Backstrom 0:f6e68b4ce169 60 IAP_Eeprom_Read(0, prefs_data.buf, PREFS_SIZE);
Backstrom 0:f6e68b4ce169 61
Backstrom 0:f6e68b4ce169 62 bool initialized = false;
Backstrom 0:f6e68b4ce169 63
Backstrom 0:f6e68b4ce169 64 // check if eeprom has been initialized
Backstrom 0:f6e68b4ce169 65 if (prefs_data.buf[0] == sig0 && prefs_data.buf[1] == sig1) {
Backstrom 0:f6e68b4ce169 66 // check crc
Backstrom 0:f6e68b4ce169 67 uint8_t calc_crc = crc8(prefs_data.buf, PREFS_SIZE-1);
Backstrom 0:f6e68b4ce169 68
Backstrom 0:f6e68b4ce169 69 if (calc_crc == prefs_data.prefs.crc)
Backstrom 0:f6e68b4ce169 70 initialized = true;
Backstrom 0:f6e68b4ce169 71 }
Backstrom 0:f6e68b4ce169 72
Backstrom 0:f6e68b4ce169 73 if (!initialized) {
Backstrom 0:f6e68b4ce169 74 memset(prefs_data.buf, 0, PREFS_SIZE);
Backstrom 0:f6e68b4ce169 75 prefs_data.prefs.sig0 = sig0;
Backstrom 0:f6e68b4ce169 76 prefs_data.prefs.sig1 = sig1;
Backstrom 0:f6e68b4ce169 77 prefs_data.prefs.disp_24h = 1;
Backstrom 0:f6e68b4ce169 78 prefs_data.prefs.brightness = 10;
Backstrom 0:f6e68b4ce169 79 prefs_data.prefs.dots = 1;
Backstrom 0:f6e68b4ce169 80 prefs_data.prefs.gps = 1;
Backstrom 0:f6e68b4ce169 81 prefs_data.prefs.gps_tzh = 9; // Tokyo, Japan
Backstrom 0:f6e68b4ce169 82 prefs_data.prefs.gps_tzm = 0;
Backstrom 0:f6e68b4ce169 83 prefs_data.prefs.auto_date = 1;
Backstrom 0:f6e68b4ce169 84 prefs_data.prefs.flw = 0;
Backstrom 0:f6e68b4ce169 85 prefs_data.prefs.crc = crc8(prefs_data.buf, PREFS_SIZE-1);
Backstrom 0:f6e68b4ce169 86
Backstrom 0:f6e68b4ce169 87 IAP_Eeprom_Write(0, prefs_data.buf, PREFS_SIZE);
Backstrom 0:f6e68b4ce169 88 }
Backstrom 0:f6e68b4ce169 89 }
Backstrom 0:f6e68b4ce169 90
Backstrom 0:f6e68b4ce169 91 void save_prefs()
Backstrom 0:f6e68b4ce169 92 {
Backstrom 0:f6e68b4ce169 93 uint8_t calc_crc = crc8(prefs_data.buf, PREFS_SIZE-1);
Backstrom 0:f6e68b4ce169 94
Backstrom 0:f6e68b4ce169 95 if (calc_crc != prefs_data.prefs.crc) {
Backstrom 0:f6e68b4ce169 96 prefs_data.prefs.crc = calc_crc;
Backstrom 0:f6e68b4ce169 97 IAP_Eeprom_Write(0, prefs_data.buf, PREFS_SIZE);
Backstrom 0:f6e68b4ce169 98 }
Backstrom 0:f6e68b4ce169 99 }
Backstrom 0:f6e68b4ce169 100
Backstrom 0:f6e68b4ce169 101 PrefsData* get_prefs()
Backstrom 0:f6e68b4ce169 102 {
Backstrom 0:f6e68b4ce169 103 if (prefs_data.prefs.flw == 2)
Backstrom 0:f6e68b4ce169 104 flw.setCensored(false);
Backstrom 0:f6e68b4ce169 105 else
Backstrom 0:f6e68b4ce169 106 flw.setCensored(true);
Backstrom 0:f6e68b4ce169 107
Backstrom 0:f6e68b4ce169 108 return &prefs_data;
Backstrom 0:f6e68b4ce169 109 }
Backstrom 0:f6e68b4ce169 110
Backstrom 0:f6e68b4ce169 111 void set_year(int year)
Backstrom 0:f6e68b4ce169 112 {
Backstrom 0:f6e68b4ce169 113 s_pref_year = year;
Backstrom 0:f6e68b4ce169 114 }
Backstrom 0:f6e68b4ce169 115
Backstrom 0:f6e68b4ce169 116 void set_month(int month)
Backstrom 0:f6e68b4ce169 117 {
Backstrom 0:f6e68b4ce169 118 s_pref_month = month;
Backstrom 0:f6e68b4ce169 119 }
Backstrom 0:f6e68b4ce169 120
Backstrom 0:f6e68b4ce169 121 void set_day(int day)
Backstrom 0:f6e68b4ce169 122 {
Backstrom 0:f6e68b4ce169 123 s_pref_day = day;
Backstrom 0:f6e68b4ce169 124 }
Backstrom 0:f6e68b4ce169 125
Backstrom 0:f6e68b4ce169 126
Backstrom 0:f6e68b4ce169 127 void set_extra_prefs(int year, int month, int day)
Backstrom 0:f6e68b4ce169 128 {
Backstrom 0:f6e68b4ce169 129 s_pref_year = year;
Backstrom 0:f6e68b4ce169 130 s_pref_month = month;
Backstrom 0:f6e68b4ce169 131 s_pref_day = day;
Backstrom 0:f6e68b4ce169 132 }
Backstrom 0:f6e68b4ce169 133
Backstrom 0:f6e68b4ce169 134 void get_extra_prefs(int& year, int& month, int& day)
Backstrom 0:f6e68b4ce169 135 {
Backstrom 0:f6e68b4ce169 136 year = s_pref_year;
Backstrom 0:f6e68b4ce169 137 month = s_pref_month;
Backstrom 0:f6e68b4ce169 138 day = s_pref_day;
Backstrom 0:f6e68b4ce169 139 }
Backstrom 0:f6e68b4ce169 140
Backstrom 0:f6e68b4ce169 141 void set_pref(PREFS pref, uint32_t value)
Backstrom 0:f6e68b4ce169 142 {
Backstrom 0:f6e68b4ce169 143 switch (pref) {
Backstrom 0:f6e68b4ce169 144 case PREF_24H:
Backstrom 0:f6e68b4ce169 145 prefs_data.prefs.disp_24h = value;
Backstrom 0:f6e68b4ce169 146 break;
Backstrom 0:f6e68b4ce169 147 case PREF_BRIGHTNESS:
Backstrom 0:f6e68b4ce169 148 prefs_data.prefs.brightness = value;
Backstrom 0:f6e68b4ce169 149 display.setBrightness(value);
Backstrom 0:f6e68b4ce169 150 break;
Backstrom 0:f6e68b4ce169 151 case PREF_ALARM:
Backstrom 0:f6e68b4ce169 152 prefs_data.prefs.alarm = value;
Backstrom 0:f6e68b4ce169 153 break;
Backstrom 0:f6e68b4ce169 154 case PREF_YEAR:
Backstrom 0:f6e68b4ce169 155 s_pref_year = value;
Backstrom 0:f6e68b4ce169 156 break;
Backstrom 0:f6e68b4ce169 157 case PREF_MONTH:
Backstrom 0:f6e68b4ce169 158 s_pref_month = value;
Backstrom 0:f6e68b4ce169 159 break;
Backstrom 0:f6e68b4ce169 160 case PREF_DAY:
Backstrom 0:f6e68b4ce169 161 s_pref_day = value;
Backstrom 0:f6e68b4ce169 162 break;
Backstrom 0:f6e68b4ce169 163 case PREF_DOTS:
Backstrom 0:f6e68b4ce169 164 prefs_data.prefs.dots = value;
Backstrom 0:f6e68b4ce169 165 break;
Backstrom 0:f6e68b4ce169 166 case PREF_GPS:
Backstrom 0:f6e68b4ce169 167 prefs_data.prefs.gps = value;
Backstrom 0:f6e68b4ce169 168 break;
Backstrom 0:f6e68b4ce169 169 case PREF_GPS_TZH:
Backstrom 0:f6e68b4ce169 170 prefs_data.prefs.gps_tzh = value;
Backstrom 0:f6e68b4ce169 171 break;
Backstrom 0:f6e68b4ce169 172 case PREF_GPS_TZM:
Backstrom 0:f6e68b4ce169 173 prefs_data.prefs.gps_tzm = value;
Backstrom 0:f6e68b4ce169 174 break;
Backstrom 0:f6e68b4ce169 175 case PREF_AUTODATE:
Backstrom 0:f6e68b4ce169 176 prefs_data.prefs.auto_date = value;
Backstrom 0:f6e68b4ce169 177 break;
Backstrom 0:f6e68b4ce169 178 case PREF_FLW:
Backstrom 0:f6e68b4ce169 179 prefs_data.prefs.flw = value;
Backstrom 0:f6e68b4ce169 180 break;
Backstrom 0:f6e68b4ce169 181 default:
Backstrom 0:f6e68b4ce169 182 break;
Backstrom 0:f6e68b4ce169 183 }
Backstrom 0:f6e68b4ce169 184 }
Backstrom 0:f6e68b4ce169 185