1
0
Fork 0
i237/src/main.c

131 lines
3.1 KiB
C
Raw Normal View History

#include <stdio.h>
#include <string.h>
2016-11-07 02:30:33 +02:00
#include <time.h>
#include <avr/io.h>
2016-11-07 01:42:18 +02:00
#include <avr/interrupt.h>
2016-10-10 22:24:33 +03:00
#include <avr/pgmspace.h>
#include <util/delay.h>
#include "hmi_msg.h"
2016-11-07 01:42:18 +02:00
#include "../lib/andygock_avr-uart/uart.h"
#include "uart_wrap.h"
#include "print_helper.h"
2016-10-10 22:24:33 +03:00
#include "../lib/hd44780_111/hd44780.h"
2016-11-07 01:42:18 +02:00
#define BAUDRATE 9600
2016-11-07 01:42:18 +02:00
// For configuring arduino mega pin 25
#define LED_INIT DDRA |= _BV(DDA3);
2016-11-07 02:30:33 +02:00
#define LED_TOGGLE PORTA ^= _BV(PORTA3)
static inline void init_system_clock(void)
{
TCCR5A = 0; // Clear control register A
TCCR5B = 0; // Clear control register B
TCCR5B |= _BV(WGM52) | _BV(CS52); // CTC and fCPU/256
OCR5A = 62549; // 1 s
TIMSK5 |= _BV(OCIE5A); // Output Compare A Match Interrupt Enable
}
2016-11-07 01:42:18 +02:00
static inline void init_hw (void)
{
2016-11-07 01:42:18 +02:00
// IO init
/// Set arduino pin 25 as output
LED_INIT;
2016-11-07 02:30:33 +02:00
// System clock
init_system_clock();
2016-11-07 01:42:18 +02:00
// UART init
uart0_init(UART_BAUD_SELECT(BAUDRATE, F_CPU));
uart3_init(UART_BAUD_SELECT(BAUDRATE, F_CPU));
stdout = stdin = &uart0_io;
stderr = &uart3_out;
2016-11-07 01:42:18 +02:00
// LCD init
2016-10-10 22:24:33 +03:00
lcd_init();
lcd_clrscr();
2016-11-07 01:42:18 +02:00
// Enable interupts
sei();
}
static inline void start_ui (void)
{
// Print program and libc versions
2016-11-20 00:51:04 +02:00
fprintf_P(stderr, PSTR(PROG_VERSION "\n"),
2016-10-24 00:01:26 +03:00
PSTR(GIT_DESCR), PSTR(__DATE__), PSTR(__TIME__));
2016-11-20 00:51:04 +02:00
fprintf_P(stderr, PSTR(LIBC_VERSION "\n"), PSTR(__AVR_LIBC_VERSION_STRING__));
2016-10-10 22:24:33 +03:00
2016-11-07 01:42:18 +02:00
// print student name
2016-10-23 22:11:20 +03:00
fprintf_P(stdout, PSTR(STUD_NAME));
2016-11-07 01:42:18 +02:00
fputc('\n', stdout); // Add a new line to the uart printout
2016-10-23 22:11:20 +03:00
lcd_puts_P(PSTR(STUD_NAME));
2016-11-07 01:42:18 +02:00
// ASCII table print
print_ascii_tbl(stdout);
2016-10-23 22:49:49 +03:00
unsigned char ascii[128];
for (unsigned char i = 0; i < sizeof(ascii); i++) {
ascii[i] = i;
}
print_for_human(stdout, ascii, sizeof(ascii));
2016-09-10 00:23:14 +03:00
2016-11-07 02:30:33 +02:00
// Bootstrap search_month message
2016-11-07 01:42:18 +02:00
fprintf_P(stdout, PSTR(GET_MONTH_MSG));
}
static inline void search_month (void)
{
char letter;
fscanf(stdin, "%c", &letter);
fprintf(stdout, "%c\n", letter);
lcd_goto(0x40); // Got to the beginning of the next line
2016-12-01 14:38:58 +02:00
char spaces_to_print = 16;
2016-11-07 01:42:18 +02:00
for (int i = 0; i < 6; i++) {
if (!strncmp_P(&letter, (PGM_P)pgm_read_word(&months[i]), 1)) {
2016-12-01 14:38:58 +02:00
char printed_count;
printed_count = fprintf_P(stdout, (PGM_P)pgm_read_word(&months[i]));
2016-11-07 01:42:18 +02:00
fputc('\n', stdout);
lcd_puts_P((PGM_P)pgm_read_word(&months[i]));
lcd_putc(' ');
2016-12-01 14:38:58 +02:00
spaces_to_print -= (printed_count + 1);
}
2016-11-07 01:42:18 +02:00
}
2016-12-01 14:38:58 +02:00
// Clear the end of the line
for (; spaces_to_print > -1; spaces_to_print--) {
lcd_putc(' ');
}
2016-11-07 01:42:18 +02:00
fprintf_P(stdout, PSTR(GET_MONTH_MSG));
}
2016-11-20 00:51:04 +02:00
static inline void heartbeat (void)
{
2016-11-07 02:30:33 +02:00
static time_t time_prev;
time_t time_cur = time(NULL);
if (time_cur <= time_prev) {
return;
}
time_prev = time_cur;
fprintf_P(stderr, PSTR(UPTIME_MSG "\n"), time_cur);
LED_TOGGLE;
}
2016-11-07 01:42:18 +02:00
int main (void)
{
init_hw();
start_ui();
2016-10-19 22:51:12 +03:00
2016-11-07 01:42:18 +02:00
while (1) {
2016-11-07 02:30:33 +02:00
heartbeat();
2016-11-07 01:42:18 +02:00
if (uart0_available()) {
search_month();
}
}
}
2016-11-07 02:30:33 +02:00
// System clock
ISR(TIMER5_COMPA_vect)
{
system_tick();
}