From 101f0b998fd5355e2ee3148d6c229d53d18ceb6d Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Fri, 4 Apr 2025 16:04:45 -0700 Subject: [PATCH] starting bcd conversion --- LispLibrary.h | 6 ++++-- ulisp-extensions.ino | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/LispLibrary.h b/LispLibrary.h index b23d6f7..bff5e2a 100644 --- a/LispLibrary.h +++ b/LispLibrary.h @@ -172,19 +172,21 @@ Returns a list with all items for which tst is true removed from lst." Returns a list with all items for which tst is false removed from lst." (mapcan #'(lambda (x) (when (funcall tst x) (list x))) lst)) +(defvar *rtc-port* 1) + (defun rtc-set (hr min) "(rtc-set hr min) Set the time on a DS3231 RTC. Times are in BCD, so use the appropriate reader macro, e.g. (rtc-set #x12 #x34) for 12:34. Assumes seconds are zero." - (with-i2c (str #x68) + (with-i2c (str *rtc-port* #x68) (write-byte 0 str) (write-byte 0 str) (write-byte min str) (write-byte hr str))) (defun rtc-get () - (with-i2c (str #x68) + (with-i2c (str *rtc-port* #x68) (write-byte 0 str) (restart-i2c str 3) (reverse diff --git a/ulisp-extensions.ino b/ulisp-extensions.ino index 4645e38..c3734b9 100644 --- a/ulisp-extensions.ino +++ b/ulisp-extensions.ino @@ -2,6 +2,26 @@ User Extensions */ +// Utility functions +uint8_t +dec_to_bcd(uint8_t n) +{ + uint8_t bcd = 0; + uint8_t tens = n / 10; + + bcd = tens << 4; + tens *= 10; + bcd += (n - tens) & 0x0f; + return bcd; +} + + +uint8_t +bcd_to_dec(uint8_t n) +{ + return ((n>>4) * 10) + (n&0x0f); +} + // Definitions object * fn_now(object *args, object *env)