]> the.earth.li Git - esp8266-clock.git/blob - user_main.c
Update Makefile to link 2 separate ROMs
[esp8266-clock.git] / user_main.c
1 /*
2  * Copyright 2017 Jonathan McDowell <noodles@earth.li>
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 #include <ets_sys.h>
18 #include <osapi.h>
19 #include <os_type.h>
20 #include <user_interface.h>
21
22 #include "espmissingincludes.h"
23
24 #include "project_config.h"
25
26 #include "clock.h"
27 #include "max7219.h"
28 #include "spi.h"
29
30 struct station_config wificfg;
31 static os_timer_t update_timer;
32
33 static const struct fontchar clocknums[] = {
34         { .width = 5,
35           .bitmap = { 0x0e, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x0e } },
36         { .width = 3,
37           .bitmap = { 0x02, 0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0x07 } },
38         { .width = 5,
39           .bitmap = { 0x0e, 0x11, 0x10, 0x10, 0x08, 0x04, 0x02, 0x1f } },
40         { .width = 5,
41           .bitmap = { 0x0e, 0x11, 0x10, 0x0c, 0x10, 0x10, 0x11, 0x0e } },
42         { .width = 6,
43           .bitmap = { 0x10, 0x18, 0x14, 0x12, 0x11, 0x3f, 0x10, 0x10 } },
44         { .width = 5,
45           .bitmap = { 0x1f, 0x01, 0x01, 0x0f, 0x10, 0x10, 0x11, 0x0e } },
46         { .width = 5,
47           .bitmap = { 0x0e, 0x11, 0x01, 0x0f, 0x11, 0x11, 0x11, 0x0e } },
48         { .width = 5,
49           .bitmap = { 0x1f, 0x10, 0x10, 0x08, 0x04, 0x02, 0x02, 0x02 } },
50         { .width = 5,
51           .bitmap = { 0x0e, 0x11, 0x11, 0x0e, 0x11, 0x11, 0x11, 0x0e } },
52         { .width = 5,
53           .bitmap = { 0x0e, 0x11, 0x11, 0x11, 0x1e, 0x10, 0x11, 0x0e } }
54 };
55
56 void ICACHE_FLASH_ATTR update_func(void *arg)
57 {
58         static bool ind = false;
59         uint8_t hour, mins;
60         uint8_t digits[4], position[4];
61         struct tm curtime;
62
63         max7219_clear();
64         ind = !ind;
65
66         /* Draw the middle : */
67         if (ind) {
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);
76         }
77
78         breakdown_time(get_time(), &curtime);
79         mins = curtime.tm_min;
80         hour = curtime.tm_hour;
81
82         digits[0] = hour / 10;
83         digits[1] = hour % 10;
84         digits[2] = mins / 10;
85         digits[3] = mins % 10;
86
87         /*
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.
91          */
92         position[1] = 14 - clocknums[digits[1]].width;
93         position[0] = position[1] - clocknums[digits[0]].width - 1;
94         position[2] = 18;
95         position[3] = position[2] + clocknums[digits[2]].width + 1;
96
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);
105
106         max7219_show();
107 }
108
109 void ICACHE_FLASH_ATTR wifi_callback(System_Event_t *evt)
110 {
111         switch (evt->event) {
112         case EVENT_STAMODE_CONNECTED:
113         case EVENT_STAMODE_DISCONNECTED:
114                 break;
115         case EVENT_STAMODE_GOT_IP:
116                 ntp_get_time();
117         default:
118                 break;
119         }
120 }
121
122 void ICACHE_FLASH_ATTR wifi_init(void)
123 {
124         wificfg.bssid_set = 0;
125         os_memcpy(&wificfg.ssid, CFG_WIFI_SSID,
126                 os_strlen(CFG_WIFI_SSID));
127         os_memcpy(&wificfg.password, CFG_WIFI_PASSWORD,
128                 os_strlen(CFG_WIFI_PASSWORD));
129
130         wifi_station_set_hostname("esp8266-clock");
131         wifi_set_opmode(STATION_MODE);
132         wifi_station_set_config(&wificfg);
133
134         wifi_set_event_handler_cb(wifi_callback);
135 }
136
137 void user_init(void)
138 {
139         /* Fix up UART0 baud rate */
140         uart_div_modify(0, UART_CLK_FREQ / 115200);
141         os_printf("Starting up.");
142
143         rtc_init();
144         gpio_init();
145
146         spi_init();
147         max7219_init(BIT12);            /* GPIO12 is CS */
148         max7219_print("Booting");
149         max7219_show();
150
151         wifi_init();
152
153         os_timer_setfn(&update_timer, update_func, NULL);
154         os_timer_arm(&update_timer, 10000 /* 10s */, 1);
155 }