summaryrefslogtreecommitdiffstats
path: root/door_lock_rfm/util.c
diff options
context:
space:
mode:
authorSadeep Madurange <sadeep@asciimx.com>2025-09-07 17:04:34 +0800
committerSadeep Madurange <sadeep@asciimx.com>2025-09-07 17:04:34 +0800
commit276856de6c63bbbf3e56cc08dcca00ba10080b7e (patch)
treec68484bc312cc3a8abcea4fa9f4948a6f1f65cc6 /door_lock_rfm/util.c
parentf4b0b734a595919cf451ab9448b06274c8e609a4 (diff)
downloadsmart-home-276856de6c63bbbf3e56cc08dcca00ba10080b7e.tar.gz
Door lock with MOSFETs and and without RFM.HEADmaster
Diffstat (limited to 'door_lock_rfm/util.c')
-rw-r--r--door_lock_rfm/util.c89
1 files changed, 89 insertions, 0 deletions
diff --git a/door_lock_rfm/util.c b/door_lock_rfm/util.c
new file mode 100644
index 0000000..ec5369e
--- /dev/null
+++ b/door_lock_rfm/util.c
@@ -0,0 +1,89 @@
+#include <avr/io.h>
+#include <avr/interrupt.h>
+#include <util/delay.h>
+
+#include "util.h"
+
+#define LOCK_LED PC3
+#define UNLOCK_LED PC4
+#define BATLOW_LED PC5
+#define LED_DDR DDRC
+#define LED_PORT PORTC
+
+int is_btn_pressed(uint8_t pin, uint8_t btn)
+{
+ if (!((pin >> btn) & 0x01)) {
+ _delay_ms(100);
+ return !((pin >> btn) & 0x01);
+ }
+ return 0;
+}
+
+void xor(const char *k, const char *s, char *d, uint8_t n)
+{
+ int i;
+
+ for (i = 0; i < n; i++)
+ d[i] = s[i] ^ k[i];
+}
+
+/* Measure vcc by measuring known internal 1.1v bandgap
+ * reference voltage against AVCC.
+ * Place a 100nF bypass capacitor on AREF.
+ */
+uint16_t getvcc(void)
+{
+ uint16_t vcc;
+
+ ADMUX |= (1 << REFS0);
+ ADMUX |= (1 << MUX3) | (1 << MUX2) | (1 << MUX1);
+ ADCSRA |= (1 << ADEN) | (1 << ADPS2) | (1 << ADPS0);
+
+ // https://www.sciencetronics.com/greenphotons/?p=1521
+ _delay_us(500);
+
+ ADCSRA |= (1 << ADSC);
+ while (ADCSRA & (1 << ADSC))
+ ;
+ vcc = (1100UL * 1023 / ADC);
+
+ ADCSRA &= ~(1 << ADEN);
+ return vcc;
+}
+
+void led_init(void)
+{
+ LED_DDR |= (1 << LOCK_LED) | (1 << UNLOCK_LED);
+ LED_DDR |= (1 << BATLOW_LED);
+
+ LED_PORT &= ~(1 << LOCK_LED);
+ LED_PORT &= ~(1 << UNLOCK_LED);
+ LED_PORT &= ~(1 << BATLOW_LED);
+}
+
+void led_locked(void)
+{
+ LED_PORT |= (1 << LOCK_LED);
+ _delay_ms(100);
+ LED_PORT &= ~(1 << LOCK_LED);
+ _delay_ms(100);
+ LED_PORT |= (1 << LOCK_LED);
+ _delay_ms(100);
+ LED_PORT &= ~(1 << LOCK_LED);
+}
+
+void led_unlocked(void)
+{
+ LED_PORT |= (1 << UNLOCK_LED);
+ _delay_ms(70);
+ LED_PORT &= ~(1 << UNLOCK_LED);
+ _delay_ms(70);
+ LED_PORT |= (1 << UNLOCK_LED);
+ _delay_ms(70);
+ LED_PORT &= ~(1 << UNLOCK_LED);
+}
+
+void led_bat(void)
+{
+ LED_PORT ^= (1 << BATLOW_LED);
+}