2 * Copyright 2017-2019 Jonathan McDowell <noodles@earth.li>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <user_interface.h>
22 #include "project_config.h"
29 struct station_config wificfg;
30 static os_timer_t update_timer;
31 static os_timer_t ntp_timer;
33 static const struct fontchar clocknums[] = {
35 .bitmap = { 0x0e, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x0e } },
37 .bitmap = { 0x02, 0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0x07 } },
39 .bitmap = { 0x0e, 0x11, 0x10, 0x10, 0x08, 0x04, 0x02, 0x1f } },
41 .bitmap = { 0x0e, 0x11, 0x10, 0x0c, 0x10, 0x10, 0x11, 0x0e } },
43 .bitmap = { 0x10, 0x18, 0x14, 0x12, 0x11, 0x3f, 0x10, 0x10 } },
45 .bitmap = { 0x1f, 0x01, 0x01, 0x0f, 0x10, 0x10, 0x11, 0x0e } },
47 .bitmap = { 0x0e, 0x11, 0x01, 0x0f, 0x11, 0x11, 0x11, 0x0e } },
49 .bitmap = { 0x1f, 0x10, 0x10, 0x08, 0x04, 0x02, 0x02, 0x02 } },
51 .bitmap = { 0x0e, 0x11, 0x11, 0x0e, 0x11, 0x11, 0x11, 0x0e } },
53 .bitmap = { 0x0e, 0x11, 0x11, 0x11, 0x1e, 0x10, 0x11, 0x0e } }
56 void ICACHE_FLASH_ATTR update_func(void *arg)
58 static bool ind = false;
60 uint8_t digits[4], position[4];
66 /* Draw the middle : */
68 max7219_set_pixel(15, 1, true);
69 max7219_set_pixel(15, 2, true);
70 max7219_set_pixel(15, 5, true);
71 max7219_set_pixel(15, 6, true);
72 max7219_set_pixel(16, 1, true);
73 max7219_set_pixel(16, 2, true);
74 max7219_set_pixel(16, 5, true);
75 max7219_set_pixel(16, 6, true);
78 breakdown_time(get_time(), &curtime);
79 mins = curtime.tm_min;
80 hour = curtime.tm_hour;
82 digits[0] = hour / 10;
83 digits[1] = hour % 10;
84 digits[2] = mins / 10;
85 digits[3] = mins % 10;
88 * We want our numbers to use as much of the LED matrix as possible,
89 * and the displayed time to be centred on the display, so we do our
90 * own positioning and blitting instead of using max7219_print.
92 position[1] = 14 - clocknums[digits[1]].width;
93 position[0] = position[1] - clocknums[digits[0]].width - 1;
95 position[3] = position[2] + clocknums[digits[2]].width + 1;
97 max7219_blit(position[0], 0, clocknums[digits[0]].bitmap,
98 clocknums[digits[0]].width, 8);
99 max7219_blit(position[1], 0, clocknums[digits[1]].bitmap,
100 clocknums[digits[1]].width, 8);
101 max7219_blit(position[2], 0, clocknums[digits[2]].bitmap,
102 clocknums[digits[2]].width, 8);
103 max7219_blit(position[3], 0, clocknums[digits[3]].bitmap,
104 clocknums[digits[3]].width, 8);
109 void ICACHE_FLASH_ATTR ntp_func(void *arg)
114 void ICACHE_FLASH_ATTR wifi_callback(System_Event_t *evt)
116 switch (evt->event) {
117 case EVENT_STAMODE_CONNECTED:
118 case EVENT_STAMODE_DISCONNECTED:
119 os_timer_disarm(&ntp_timer);
121 case EVENT_STAMODE_GOT_IP:
124 os_timer_disarm(&ntp_timer);
125 os_timer_setfn(&ntp_timer, ntp_func, NULL);
126 os_timer_arm(&ntp_timer, 3600 * 1000 /* Hourly */, 1);
132 void ICACHE_FLASH_ATTR wifi_init(void)
134 wificfg.bssid_set = 0;
135 os_memcpy(&wificfg.ssid, CFG_WIFI_SSID,
136 os_strlen(CFG_WIFI_SSID));
137 os_memcpy(&wificfg.password, CFG_WIFI_PASSWORD,
138 os_strlen(CFG_WIFI_PASSWORD));
140 wifi_station_set_hostname("esp8266-clock");
141 wifi_set_opmode(STATION_MODE);
142 wifi_station_set_config(&wificfg);
144 wifi_set_event_handler_cb(wifi_callback);
149 /* Fix up UART0 baud rate */
150 uart_div_modify(0, UART_CLK_FREQ / 115200);
151 os_printf("Starting up.");
157 max7219_init(BIT12); /* GPIO12 is CS */
158 max7219_print("Booting");
163 os_timer_setfn(&update_timer, update_func, NULL);
164 os_timer_arm(&update_timer, 10000 /* 10s */, 1);