-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
107 lines (83 loc) · 2.39 KB
/
main.c
File metadata and controls
107 lines (83 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "config.h"
#define CPU_CLOCK_FREQUENCY F_CPU
#include <avr/io.h>
#include <util/delay.h>
#include "clock.h"
#include "display.h"
#include "scene.h"
#include "sensor.h"
#include "spi.h"
#include "storage.h"
#include "twi.h"
#include "uart.h"
// IO CONNECTIVITY
// PB0 -> sensor RDY
// PB1 -> CS display
// PB2 -> CS flash chip
// PB3 -> MOSI
// PB4 -> MISO
// PB5 -> CLK
// PC0 -> display reset
// PC1 -> display A0
// PC4 -> TWI SDA
// PC5 -> TWI SCL
// PD2 .. PD5 -> user buttons (4)
// PD6 -> TWI error / Flash storage write LED (red)
// PD7 -> program LED (blue)
// uncomment to set the datetime when flashing.
// must flash a second time with this option turned off again
// #define DO_SET_DATETIME
#define MAIN_LOOP_DELAY 200U
#define DATA_SAVE_INTERVAL 60U // seconds
#define DATA_SAVE_LOOP_COUNT DATA_SAVE_INTERVAL * 1000U / MAIN_LOOP_DELAY
uint16_t data_save_counter = 0;
int main(void) {
SPI_init();
TWI_init();
UART_init();
_delay_ms(100);
SENSOR_init();
CLOCK_init();
DISPLAY_init();
DISPLAY_clear();
STORAGE_init();
DDRD |= (1 << PD7);
// PORTD |= (1 << PD7);
_delay_ms(500);
// STORAGE_print_debug_information();
// STORAGE_block_erase_32k(0);
// scan the flash chip for the last written datapoint
DISPLAY_set_pos(0, 0);
DISPLAY_write("Scanning storage...");
STORAGE_current_location = STORAGE_scan_location();
// dump current database on startup
DISPLAY_set_pos(1, 0);
DISPLAY_write("Dumping data...");
STORAGE_dump_datapoints_to_uart();
// UART_send_string("\r\nstorage location ");
// UART_send_number_hex_32(STORAGE_current_location);
//
// STORAGE_print_page_data(STORAGE_current_location*16-256);
// STORAGE_print_page_data(STORAGE_current_location*16);
// STORAGE_print_page_data(STORAGE_current_location*16+256);
// set date & time
#ifdef DO_SET_DATETIME
DATETIME time = {.seconds = 0, .minutes = 32, .hours = 12, .weekday = 1, .day = 9, .month = 6, .year = 2025};
CLOCK_write_time(time);
#endif
while (1) {
SENSOR_reading reading = SENSOR_read_data();
if (reading.success) {
SENSOR_last_reading = reading;
if (data_save_counter >= DATA_SAVE_LOOP_COUNT) {
STORAGE_save_datapoint(SENSOR_last_reading, CLOCK_read_time());
data_save_counter = 0;
}
}
SCENE_display_current_scene();
SCNENE_advance();
_delay_ms(MAIN_LOOP_DELAY);
data_save_counter++;
}
return 0;
}