Skip to content

Commit

Permalink
Merge pull request #9 from hakierspejs/feature/bugfixes-and-improvements
Browse files Browse the repository at this point in the history
Feature/bugfixes and improvements
  • Loading branch information
cb4b1fd915 committed Jun 30, 2024
2 parents 1833667 + e73502c commit ad38b62
Show file tree
Hide file tree
Showing 27 changed files with 1,133 additions and 42 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
with:
submodules: 'recursive'
- uses: arduino/[email protected]
- name: Install platform and compile
run: |
Expand Down
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ ARG UID=1000
ARG GID=1000

RUN apt-get update && apt-get install -y build-essential
RUN useradd -m ${USER} --uid=${UID}
RUN groupadd --gid=${GID} g${GID} && useradd -m ${USER} --uid=${UID} -g${GID} -G dialout

USER ${UID}:${GID}

WORKDIR /usr/src/app

COPY requirements.txt ./

RUN pip install --upgrade pip && pip install --no-cache-dir -r requirements.txt

USER ${UID}:${GID}

CMD [ "echo", "./gen_qr_codes.py" ]
7 changes: 5 additions & 2 deletions Dockerfile.arduino
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ARG USER=docker
ARG UID=1000
ARG GID=1000

RUN groupadd --gid=${GID} g${GID} && useradd -m ${USER} --uid=${UID} -g${GID} -G dialout
RUN useradd --create-home $USER -u $UID

RUN cd /tmp/ \
&& wget https://github.com/arduino/arduino-cli/releases/download/0.18.3/arduino-cli_0.18.3_Linux_64bit.tar.gz \
Expand All @@ -16,10 +16,13 @@ RUN cd /tmp/ \
&& cp ./arduino-cli /usr/bin/arduino-cli \
&& chmod +x /usr/bin/arduino-cli

RUN usermod -a -G dialout ${USER}

RUN arduino-cli core update-index && arduino-cli core install arduino:avr

USER ${UID}:${GID}

WORKDIR /usr/src/app

RUN arduino-cli core update-index && arduino-cli core install arduino:avr

CMD [ "arduino-cli" ]
29 changes: 26 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
DEVICE_SERIAL ?= /dev/ttyUSB0
KEY_NUM ?= 99
DOCKER_ARDUINO_CLI = docker run --device=$(DEVICE_SERIAL) --mount src="$(shell pwd)",target=/usr/src/app,type=bind -it --rm arduino-cli
DOCKER_PYTHON_CLI = docker run --device=$(DEVICE_SERIAL) --mount src="$(shell pwd)",target=/usr/src/app,type=bind -it --rm hs-ldz-totp-lock
DOCKER_ARDUINO_CLI = docker run --device=$(DEVICE_SERIAL) --mount src="$(shell pwd)",target=/usr/src/app,type=bind -it -u root --rm arduino-cli
DOCKER_PYTHON_CLI = docker run --device=$(DEVICE_SERIAL) --mount src="$(shell pwd)",target=/usr/src/app,type=bind -it -u root --rm hs-ldz-totp-lock

decrypt:
openssl aes-256-cbc -d -a -md sha512 -pbkdf2 -iter 100000 -salt -pass env:HSLOCK_SECRET -in backup/rawdata.out.enc -out backup/rawdata.out
Expand Down Expand Up @@ -39,7 +39,7 @@ uno-configure-ds3231: ac-build build
$(DOCKER_PYTHON_CLI) python ./configure_DS3231/configure_time.py $(DEVICE_SERIAL)
$(DOCKER_PYTHON_CLI) python ./configure_DS3231/dump_eeprom.py $(DEVICE_SERIAL)
# Do not uncomment this one - it will reset all locked keys unless the Git copy is up to date.
#$(DOCKER_PYTHON_CLI) python ./configure_DS3231/load_eeprom.py $(DEVICE_SERIAL) backup/rawdata.out
$(DOCKER_PYTHON_CLI) python ./configure_DS3231/load_eeprom.py $(DEVICE_SERIAL) backup/rawdata.out

uno-upload-lock: ac-build
$(DOCKER_ARDUINO_CLI) arduino-cli board list
Expand All @@ -52,4 +52,27 @@ uno-upload-isp: ac-build
$(DOCKER_ARDUINO_CLI) bash -c "cd /usr/src/app/ArduinoISP && arduino-cli compile --fqbn arduino:avr:uno --libraries=../vendor/librares/ && arduino-cli upload --verbose --verify -p $(DEVICE_SERIAL) --fqbn arduino:avr:uno"

pro-mini-upload-lock: ac-build
echo RESET pin is 10
echo see https://docs.arduino.cc/built-in-examples/arduino-isp/ArduinoISP/
$(DOCKER_ARDUINO_CLI) bash -c "cd /usr/src/app/hsldz_totp_lock && arduino-cli compile --fqbn arduino:avr:uno --libraries=../vendor/librares/ && arduino-cli upload --verbose --verify -p $(DEVICE_SERIAL) --fqbn arduino:avr:mini --programmer arduinoasisp"



nano-configure-ds3231: ac-build build
$(DOCKER_ARDUINO_CLI) arduino-cli board list
$(DOCKER_ARDUINO_CLI) bash -c "cd /usr/src/app/configure_DS3231 && arduino-cli compile --fqbn arduino:avr:nano --libraries=../vendor/librares/ && arduino-cli upload -p $(DEVICE_SERIAL) --fqbn arduino:avr:nano"
$(DOCKER_PYTHON_CLI) python ./configure_DS3231/configure_time.py $(DEVICE_SERIAL)
$(DOCKER_PYTHON_CLI) python ./configure_DS3231/dump_eeprom.py $(DEVICE_SERIAL)
# Do not uncomment this one - it will reset all locked keys unless the Git copy is up to date.
$(DOCKER_PYTHON_CLI) python ./configure_DS3231/load_eeprom.py $(DEVICE_SERIAL) backup/rawdata.out

nano-configure-time: ac-build build
$(DOCKER_ARDUINO_CLI) arduino-cli board list
$(DOCKER_ARDUINO_CLI) bash -c "cd /usr/src/app/configure_DS3231 && arduino-cli compile --fqbn arduino:avr:nano --libraries=../vendor/librares/ && arduino-cli upload -p $(DEVICE_SERIAL) --fqbn arduino:avr:nano"
$(DOCKER_PYTHON_CLI) python ./configure_DS3231/configure_time.py $(DEVICE_SERIAL)


nano-upload-lock: ac-build
$(DOCKER_ARDUINO_CLI) arduino-cli board list
$(DOCKER_ARDUINO_CLI) bash -c "cd /usr/src/app/hsldz_totp_lock && arduino-cli compile --fqbn arduino:avr:nano --libraries=../vendor/librares/ && arduino-cli upload -p $(DEVICE_SERIAL) --fqbn arduino:avr:nano"

43 changes: 12 additions & 31 deletions hsldz_totp_lock/hsldz_totp_lock.ino
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

#define BUZZER_PIN 10
#define LOCK_PIN 12
#define BUTTON_OPEN_PIN 13

#define SOUND_TIME_BUTTON_PRESS 50
#define SOUND_TIME_OPEN 3000
Expand All @@ -21,9 +20,9 @@
#define FREQ_OPEN 2500
#define FREQ_ERROR 800

#define MORSE_SOUND_TIME 100
#define MORSE_PAUSE 15
#define MORSE_FREQ 500
#define MORSE_SOUND_TIME 60
#define MORSE_PAUSE 5
#define MORSE_FREQ 880

#define EEPROM_ADDRESS 0x57
#define EEPROM_CODE 32
Expand Down Expand Up @@ -98,7 +97,6 @@ void setup(){
Serial.begin(9600);
Wire.begin();
eeprom.initialize();
pinMode(BUTTON_OPEN_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LOCK_PIN, OUTPUT);
watchdog.enable(Watchdog::TIMEOUT_8S);
Expand Down Expand Up @@ -223,16 +221,21 @@ bool isTOTPCodeValid(String userInput) {
};
DateTime now = RTC.now();
unsigned long currentUnixTimestamp = now.unixtime();
int timeDeviations[5] = {-60, -30, 0, 30, 60};
int timeDeviations[7] = {0, -30, 30, 60, -60, -90, 90};
String userCode = userInput.substring(2, 8);
Serial.print("Time: "); Serial.println(currentUnixTimestamp);
TOTP totp = TOTP(keyBytes, hmacKeySize);
for (int i = 0; i < 5; i++) {
for (int i = 0; i < 7; i++) {
int delta = timeDeviations[i];
char* correctCode = totp.getCode(currentUnixTimestamp + delta);
// Serial.println(userCode);
// Serial.println(correctCode);
if (userCode == correctCode) {
if (i > 4) {
DateTime now = RTC.now();
unsigned long currentUnixTimestamp = now.unixtime();
echo_morse_reversed_int(currentUnixTimestamp);
}
// Serial.println("+++++++++++++++++++++++");
return userCode == correctCode;
}
Expand Down Expand Up @@ -356,33 +359,11 @@ void makeMaintenance(String userInputPrev) {

void loop(){
watchdog.reset();
digitalWrite(LOCK_PIN, LOW);
boolean openButtonIsDown = digitalRead(BUTTON_OPEN_PIN);
if (openButtonIsDown) {
// Serial.println(__TIMESTAMP__);
int timeout = 30; // ms
int iterations = 15;
int limit = 7;
int press_counter = 0;
for (int i = 0; i < iterations; i++) {
watchdog.reset();
tone(BUZZER_PIN, FREQ_OPEN_BUTTON_PRESS, timeout - 10);
delay(timeout);
if (digitalRead(BUTTON_OPEN_PIN) == HIGH) {
press_counter += 1;
};
};
if (press_counter > limit) {
unlockTheDoor();
} else {
press_counter = 0;
};
};

digitalWrite(LOCK_PIN, LOW);
char customKey = customKeypad.getKey();
if (customKey){
watchdog.reset();
tone(BUZZER_PIN, FREQ_BUTTON_PRESS, SOUND_TIME_BUTTON_PRESS);
watchdog.reset();
if (customKey == '*') {
userInput = "";
userInputPrev = "";
Expand Down
1 change: 0 additions & 1 deletion vendor/librares/Watchdog
Submodule Watchdog deleted from 7cbbb1
32 changes: 32 additions & 0 deletions vendor/librares/Watchdog/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
License Agreement
(3-clause BSD License)
Janelia Research Campus Software Copyright 1.1

Copyright (c) 2021, Howard Hughes Medical Institute
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the name of the Howard Hughes Medical Institute nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written
permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 changes: 41 additions & 0 deletions vendor/librares/Watchdog/README.org
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#+TITLE: Watchdog
#+AUTHOR: Peter Polidoro
#+EMAIL: [email protected]

* Library Information
- Name :: Watchdog
- Version :: 3.0.2
- License :: BSD
- URL :: https://github.com/janelia-arduino/Watchdog
- Author :: Peter Polidoro, Antonio Brewer, Steve Sawtelle
- Email :: [email protected]

The watchdog monitors the operation of the system by expecting periodic
communication from the software, generally known as servicing or refreshing the
watchdog. If this periodic refreshing does not occur, the watchdog resets the
system. Works with avr, megaavr, and teensy processors.

When the watchdog timer is enabled, a call to the reset instruction is
required before the timer expires after the timeout duration, otherwise a
watchdog-initiated device reset will occur.

** Timeout Durations

Possible timeout durations are 15ms, 30ms, 60ms, 120ms, 250ms, 500ms, 1s, 2s, 4s, and 8s.

Not all devices allow timeout durations.
- Teensy 4.x does not allow timeout durations below 1s.
- Other processors do not allow timeout durations of 4s or 8s.

** Teensy 4

The watchdog code for the Teensy 4 was written by Antonio Brewer:

https://github.com/tonton81/WDT_T4

** Tripped Method

The tripped method was written by Steve Sawtelle.

Unfortunately this method does not work with certain microprocessors and
boards, such as the Arduino Mega 2560, Teensy 2.0, and Teensy++ 2.0.
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// ----------------------------------------------------------------------------
// WatchdogTester.ino
//
// Authors:
// Peter Polidoro [email protected]
// ----------------------------------------------------------------------------

// This sample program shows how to use the watchdog timer.

// When the device first powers on, it turns the LED off for 1 seconds. It then
// turns the LED on, and pauses for 4 seconds letting you know it is ready to
// start. Watchdog is initialized and the loop starts. It will flash the LED and
// reset watchdog for the first 6 secs. After that, it stops resetting watchdog.
// This causes the watchdog to reboot the device. When the device powers on
// after a watchdog device reset, it blinks the LED twice for 1.5 seconds before
// flashing the LED to let you know the watchdog had been previously tripped.

#include <Watchdog.h>


const unsigned long SETUP_LED_OFF_DURATION = 1000;
const unsigned long SETUP_LED_ON_IF_NOT_TRIPPED_DURATION = 4000;
const unsigned long SETUP_LED_ON_IF_TRIPPED_DURATION = 1500;
const unsigned long TIMEOUT_DURATION = 6000;
const unsigned long BLINK_HALF_PERIOD = 100;
const unsigned long RESET_DURATION = 500;

Watchdog watchdog;
unsigned long enabled_time;
unsigned long blink_time;
unsigned long reset_time;

void setLedOff()
{
digitalWrite(LED_BUILTIN,LOW);
delay(SETUP_LED_OFF_DURATION);
}

void setLedOn(unsigned long duration)
{
digitalWrite(LED_BUILTIN,HIGH);
delay(duration);
}

void toggleLed()
{
digitalWrite(LED_BUILTIN,!digitalRead(LED_BUILTIN));
}

void setup()
{
pinMode(LED_BUILTIN,OUTPUT);

// Indicate we are starting over by holding led off
setLedOff();

// Indicate we are in setup by holding LED on
if (!watchdog.tripped())
{
// blink once to indicate power cycle reset
setLedOn(SETUP_LED_ON_IF_NOT_TRIPPED_DURATION);
}
else
{
// blink twice to indicate watchdog tripped reset
setLedOn(SETUP_LED_ON_IF_TRIPPED_DURATION);
setLedOff();
setLedOn(SETUP_LED_ON_IF_TRIPPED_DURATION);
}

// Setup watchdog
watchdog.enable(Watchdog::TIMEOUT_1S);

enabled_time = millis();
blink_time = enabled_time;
reset_time = enabled_time;
}

void loop()
{
const unsigned long time = millis();

// Toggle led
if ((time - blink_time) >= BLINK_HALF_PERIOD)
{
blink_time = time;
toggleLed();
}

// Stop resetting watchdog after timeout
if (((time - enabled_time) <= TIMEOUT_DURATION) &&
((time - reset_time) >= RESET_DURATION))
{
reset_time = time;
watchdog.reset();
}
}
9 changes: 9 additions & 0 deletions vendor/librares/Watchdog/library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name=Watchdog
version=3.0.2
author=Peter Polidoro <[email protected]>, Antonio Brewer, Steve Sawtelle
maintainer=Peter Polidoro <[email protected]>
sentence=Watchdog resets the device if the timer expires before the watchdog is reset.
paragraph=Like this project? Please star it on GitHub!
category=Device Control
url=https://github.com/janelia-arduino/Watchdog.git
architectures=avr,megaavr,teensy
Loading

0 comments on commit ad38b62

Please sign in to comment.