#include "project.h" #define NCS (GPIO7) #define NCS_PORT GPIOG #define SCK (GPIO3) #define SCK_PORT GPIOB #define MOSI (GPIO5) #define MOSI_PORT GPIOB #define N_DISPLAYS 3 #define N_DIGITS 8 static uint8_t fb[N_DISPLAYS][N_DIGITS]; static void set (int sck, int ncs, int mosi) { if (sck) SET (SCK); else CLEAR (SCK); if (ncs) SET (NCS); else CLEAR (NCS); if (mosi) SET (MOSI); else CLEAR (MOSI); // delay_us(1); //delay_us(10); } static void spip_send_8 (uint8_t wot) { int i; for (i = 0; i < 8; ++i) { set (0, 0, wot & 0x80); set (1, 0, wot & 0x80); set (0, 0, wot & 0x80); wot <<= 1; } } static int mutex = 0; static int lock (void) { if (__sync_add_and_fetch (&mutex, 1) != 1) { __sync_sub_and_fetch (&mutex, 1); return -1; } return 0; } static void unlock (void) { __sync_sub_and_fetch (&mutex, 1); } static void write_reg (uint8_t reg, uint8_t data) { unsigned i; while (lock()); set (0, 1, 0); set (0, 0, 0); for (i = 0; i < N_DISPLAYS; ++i) { spip_send_8 (reg); spip_send_8 (data); } set (0, 0, 0); set (0, 1, 0); unlock(); } static void cls (void) { unsigned i; for (i = 0; i < N_DISPLAYS; ++i) memset (&fb[i], 0, N_DIGITS); } static void refresh (void) { static uint8_t last_brightness = 255; unsigned i, j; if (last_brightness != pot_brightness) { last_brightness = pot_brightness; write_reg (0xa, last_brightness); } while (lock()); for (j = 0; j < N_DIGITS; ++j) { set (0, 1, 0); set (0, 0, 0); i = N_DISPLAYS - 1; do { spip_send_8 (N_DIGITS - j); spip_send_8 (fb[i][j]); } while (i--); set (0, 0, 0); set (0, 1, 0); } unlock(); } #define SDP 0x80 #define SA 0x40 #define SB 0x20 #define SC 0x10 #define SD 0x08 #define SE 0x04 #define SF 0x02 #define SG 0x01 static uint8_t hex (unsigned v) { switch (v) { case 0: case '0': return SA | SF | SB | SE | SC | SD; case 1: case '1': return SB | SC; case 2: case '2': case 'z': return SA | SB | SG | SE | SD; case 3: case '3': return SA | SB | SG | SC | SD; case 4: case '4': return SF | SG | SB | SC; case 5: case '5': case 's': return SA | SF | SG | SC | SD; case 6: case '6': return SA | SF | SG | SE | SC | SD; case 7: case '7': return SA | SB | SC; case 8: case '8': return SA | SF | SB | SG | SE | SC | SD; case 9: case '9': case 'g': return SA | SF | SB | SG | SC | SD; case 0xa: case 'a': return SA | SF | SB | SG | SE | SC; case 0xb: case 'b': return SF | SG | SE | SC | SD; case 0xc: case 'c': return SG | SE | SD; case 0xd: case 'd': return SB | SG | SE | SC | SD; case 0xe: case 'e': return SA | SF | SG | SE | SD; case 0xf: case 'f': return SA | SF | SG | SE; case 'h': return SF | SG | SE | SC; case 'i': return SE; case 'j': return SB | SC | SD; case 'k': case 'x': return SF | SB | SG | SE | SC; case 'l': return SF | SE | SD; case 'm': case 'n': return SG | SE | SC; case 'o': return SG | SE | SC | SD; case 'p': return SA | SF | SB | SG | SE; case 'q': return SA | SF | SB | SG | SC; case 'r': return SG | SE; case 't': return SF | SG | SE | SD; case 'u': case 'w': return SE | SC | SD; case 'y': return SF | SB | SG | SC | SD; case '-': return SG; case '.': return SDP; } return 0; } static void write_dd (int u, unsigned x, unsigned y) { unsigned t; if (u < 0) return; if (u > 99) return; t = u / 10; u %= 10; fb[y][x] = hex (t); fb[y][x + 1] = hex (u); } static void write_string (const char *s, unsigned x, unsigned y) { for (; (x < N_DIGITS) && *s; s++, x++) { fb[y][x] = hex (*s); if (* (s + 1) == '.') { fb[y][x] |= SDP; s++; } } } static inline void write_dp (unsigned x, unsigned y) { fb[y][x] |= SDP; } void max7219_init (int on) { unsigned i; MAP_OUTPUT_PP (SCK); MAP_OUTPUT_PP (NCS); MAP_OUTPUT_PP (MOSI); set (0, 1, 0); if (on) { write_reg (0xc, 0x1); //Power up write_reg (0xf, 0x0); //normal mode write_reg (0x9, 0x0); //no decode write_reg (0xb, N_DIGITS - 1); //8 digits write_reg (0xa, pot_brightness); for (i = 0; i < N_DIGITS; ++i) write_reg (N_DIGITS - i, 0x0); } else { write_reg (0xc, 0x0); //Power up } } static int have_lock, have_dgps, have_time_lock, time_lock_enabled; void max7219_report_fix (char fix, char fix2) { have_lock = 0; if (fix == 'L') have_lock = 1; if (fix2 == 'D') have_dgps = 1; else have_dgps = 0; if (fix == 'T') { have_lock = 1; have_time_lock = 1; } } void max7219_report_svin (int valid, int active) { time_lock_enabled = 0; if (active || valid) time_lock_enabled = 1; } static const char *day_name[] = {"su", "mo", "tu", "we", "th", "fr", "sa"}; void max7219_dispatch (void) { uint64_t abs = ref_get(); char buf[32]; int wday; EPOCH e; UTC u; UTC gu; ST l; unsigned i; uint64_t wot; cls(); if (gps_initting) write_string ("gps init", 0, 0); else if (!ref_valid) { write_string ("gps acq.", 0, 0); write_dd (gps_sats_searching, 0, 1); write_dp (1, 1); write_dd (gps_sats_inop, 2, 1); write_dp (3, 1); write_dd (gps_sats_locked, 4, 1); write_dp (5, 1); write_dd (gps_sats_with_e, 6, 1); } else { e = ref_decompose (abs); u = time_epoch_to_utc (e); write_dd (u.hour, 0, 0); write_dp (1, 0); write_dd (u.minute, 2, 0); write_dp (3, 0); write_dd (u.second, 4, 0); write_dp (5, 0); write_dd (u.nanosecond / 10000000, 6, 0); l = time_utc_to_lst (u, gps_lon); write_dd (l.hour, 0, 1); write_dp (1, 1); write_dd (l.minute, 2, 1); write_dp (3, 1); write_dd (l.second, 4, 1); write_dp (5, 1); write_dd (l.nanosecond / 10000000, 6, 1); wot = e.s; wot /= 4; wot %= 4; switch (wot) { case 0: if (!have_lock) break; e.s += 86400; e.s += gps_utc_diff; gu = time_epoch_to_utc (e); wday = gps_wday; if (wday < 0) wday = 0; if (wday > 6) wday = 6; write_string (day_name[wday], 0, 2); write_dp (1, 2); write_dd (gu.hour, 2, 2); write_dp (3, 2); write_dd (gu.minute, 4, 2); write_dp (5, 2); write_dd (gu.second, 6, 2); break; case 1: if (!have_lock) break; snprintf (buf, sizeof (buf), "%.8f", gps_lat); buf[sizeof (buf) - 1] = 0; write_string (buf, 0, 2); break; case 2: if (!have_lock) break; snprintf (buf, sizeof (buf), "%.8f", gps_lon); buf[sizeof (buf) - 1] = 0; write_string (buf, 0, 2); break; case 3: for (i = 0; i < 8; ++i) fb[2][i ^ 1] = hex ((if0.ip_addr.addr >> (i << 2)) & 0xf); break; } } if (time_lock_enabled) write_dp (7, 0); if (have_time_lock) write_dp (7, 1); if (have_dgps) write_dp (7, 2); refresh(); }