Implement and document Arduino software

This commit is contained in:
Philipp Matthias Schaefer 2020-12-11 14:18:46 +01:00
parent 28de70e732
commit 001c7cd568
17 changed files with 648 additions and 70 deletions

33
source/arduino/Makefile Normal file
View file

@ -0,0 +1,33 @@
# Copyright (c):
#
# Philipp Matthias Schäfer (philipp.matthias.schaefer@posteo.de), 2020
#
# This file is part of the KrautStatus' Arduino code.
#
# The Clean CommonMark library is free software: you can redistribute it and/or
# modify it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# The Clean CommonMark library is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
# General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License along
# with the Clean CommonMark library. If not, see
# <https://www.gnu.org/licenses/>.
.PHONY: all clean install
all:
arduino-cli compile --fqbn arduino:samd:mkr1000
clean:
rm --recursive build
install:
PORT=$(arduino-cli board list | \
grep arduino:samd:mkr1000 | \
sed -e 's/ .*//g'); \
arduino-cli upload --fqbn arduino:samd:mkr1000 --port $$PORT

View file

@ -0,0 +1,70 @@
/*
Copyright (c):
Philipp Matthias Schäfer (philipp.matthias.schaefer@posteo.de), 2020
This file is part of the KrautStatus' Arduino code.
The Clean CommonMark library is free software: you can redistribute it and/or
modify it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
The Clean CommonMark library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
for more details.
You should have received a copy of the GNU Affero General Public License along
with the Clean CommonMark library. If not, see <https://www.gnu.org/licenses/>.
*/
#include <SPI.h>
#include <uTimerLib.h>
#include "config.h"
#include "matrix.h"
#include "sensor.h"
#include "serial.h"
#include "wifi.h"
const unsigned long int MATRIX_UPDATE_FREQUENCY = 1;
void setup() {
serial_setup();
matrix_setup();
boolean wifi_result = matrix_show_scan_and_run(wifi_setup);
if (!wifi_result) {
matrix_show_failure();
while(true);
}
sensor_setup();
start_matrix_update();
}
void loop() {
int packetSize = Udp.parsePacket();
if (packetSize) {
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(uint8_t(sensor_get_door_state()));
Udp.endPacket();
}
}
void start_matrix_update() {
TimerLib.setInterval_us(matrix_update, 1000000 / MATRIX_UPDATE_FREQUENCY);
}
void matrix_update() {
switch (sensor_get_door_state()) {
OPEN:
matrix_fill(GREEN);
break;
CLOSED:
matrix_fill(BLACK);
break;
}
}

View file

@ -19,14 +19,15 @@ You should have received a copy of the GNU Affero General Public License along
with the Clean CommonMark library. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdint.h>
// SSID and password of the WiFi network to which we broadcast the door lock's
// status.
char SSID[] = "";
char PASSWORD[] = "";
const char SSID[] = "";
const char PASSWORD[] = "";
// Port on which to listen for status requests
unsigned int PORT = 12345;
const unsigned int SERVER_PORT = 12345;
// Pin to which the reed switch is connected
uint8_t REED_PIN = 0;
const uint8_t SENSOR_PIN = 0;

87
source/arduino/matrix.cpp Normal file
View file

@ -0,0 +1,87 @@
/*
Copyright (c):
Philipp Matthias Schäfer (philipp.matthias.schaefer@posteo.de), 2020
This file is part of the KrautStatus' Arduino code.
The Clean CommonMark library is free software: you can redistribute it and/or
modify it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
The Clean CommonMark library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
for more details.
You should have received a copy of the GNU Affero General Public License along
with the Clean CommonMark library. If not, see <https://www.gnu.org/licenses/>.
*/
#include <ArduinoGraphics.h>
#include <Arduino_MKRRGB.h>
#include <uTimerLib.h>
#include "matrix.h"
const uint32_t RED = COLOR(255, 0, 0);
const uint32_t YELLOW = COLOR(255, 255, 0);
const unsigned long int SCAN_UPDATE_FREQUENCY = 25;
void matrix_setup() {
MATRIX.begin();
}
void matrix_show_failure() {
MATRIX.stroke(RED);
MATRIX.beginDraw();
MATRIX.clear();
MATRIX.line(0, 0, MATRIX.width() - 1, MATRIX.height() - 1);
MATRIX.line(0, MATRIX.height() - 1, MATRIX.width() - 1, 0);
MATRIX.endDraw();
}
uint8_t scan_timer = 0;
void matrix_paint_scan() {
// We paint a vertical line that oscillates between the left and right border
// of the matrix. Going back and forth one pixel at a time gives us as period
// of
// 2 * width - 2
// because we do not want to remain at the borders for one tick.
scan_timer += 1;
scan_timer %= 2 * MATRIX.width() - 2;
uint8_t position = scan_timer;
if(position >= MATRIX.width())
position = 2 * MATRIX.width() - position - 2;
MATRIX.beginDraw();
MATRIX.clear();
MATRIX.line(position, 0, position, 6);
MATRIX.endDraw();
}
bool matrix_show_scan_and_run(bool(*thunk)()) {
MATRIX.stroke(YELLOW);
TimerLib.setInterval_us(matrix_paint_scan, 1000000 / SCAN_UPDATE_FREQUENCY);
bool result = thunk();
TimerLib.clearTimer();
MATRIX.beginDraw();
MATRIX.clear();
MATRIX.endDraw();
return result;
}
void matrix_fill(uint32_t color) {
MATRIX.fill(color);
MATRIX.beginDraw();
MATRIX.clear();
MATRIX.endDraw();
}

33
source/arduino/matrix.h Normal file
View file

@ -0,0 +1,33 @@
/*
Copyright (c):
Philipp Matthias Schäfer (philipp.matthias.schaefer@posteo.de), 2020
This file is part of the KrautStatus' Arduino code.
The Clean CommonMark library is free software: you can redistribute it and/or
modify it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
The Clean CommonMark library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
for more details.
You should have received a copy of the GNU Affero General Public License along
with the Clean CommonMark library. If not, see <https://www.gnu.org/licenses/>.
*/
#define COLOR(r, g, b) (r << 16 | g << 8 | b)
const uint32_t BLACK = COLOR( 0, 0, 0);
const uint32_t GREEN = COLOR( 0, 255, 0);
void matrix_setup();
void matrix_show_failure();
bool matrix_show_scan_and_run(bool(*)());
void matrix_fill(uint32_t color);

35
source/arduino/sensor.cpp Normal file
View file

@ -0,0 +1,35 @@
/*
Copyright (c):
Philipp Matthias Schäfer (philipp.matthias.schaefer@posteo.de), 2020
This file is part of the KrautStatus' Arduino code.
The Clean CommonMark library is free software: you can redistribute it and/or
modify it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
The Clean CommonMark library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
for more details.
You should have received a copy of the GNU Affero General Public License along
with the Clean CommonMark library. If not, see <https://www.gnu.org/licenses/>.
*/
#include <Arduino.h>
#include "sensor.h"
#include "config.h"
void sensor_setup() {
pinMode(SENSOR_PIN, INPUT_PULLUP);
}
door_state sensor_get_door_state() {
if (digitalRead(SENSOR_PIN) == HIGH)
return OPEN;
return CLOSED;
}

29
source/arduino/sensor.h Normal file
View file

@ -0,0 +1,29 @@
/*
Copyright (c):
Philipp Matthias Schäfer (philipp.matthias.schaefer@posteo.de), 2020
This file is part of the KrautStatus' Arduino code.
The Clean CommonMark library is free software: you can redistribute it and/or
modify it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
The Clean CommonMark library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
for more details.
You should have received a copy of the GNU Affero General Public License along
with the Clean CommonMark library. If not, see <https://www.gnu.org/licenses/>.
*/
typedef enum {
CLOSED = 0,
OPEN = 1
} door_state;
void sensor_setup();
door_state sensor_get_door_state();

26
source/arduino/serial.cpp Normal file
View file

@ -0,0 +1,26 @@
/*
Copyright (c):
Philipp Matthias Schäfer (philipp.matthias.schaefer@posteo.de), 2020
This file is part of the KrautStatus' Arduino code.
The Clean CommonMark library is free software: you can redistribute it and/or
modify it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
The Clean CommonMark library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
for more details.
You should have received a copy of the GNU Affero General Public License along
with the Clean CommonMark library. If not, see <https://www.gnu.org/licenses/>.
*/
#include <SPI.h>
void serial_setup() {
Serial.begin(9600);
}

22
source/arduino/serial.h Normal file
View file

@ -0,0 +1,22 @@
/*
Copyright (c):
Philipp Matthias Schäfer (philipp.matthias.schaefer@posteo.de), 2020
This file is part of the KrautStatus' Arduino code.
The Clean CommonMark library is free software: you can redistribute it and/or
modify it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
The Clean CommonMark library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
for more details.
You should have received a copy of the GNU Affero General Public License along
with the Clean CommonMark library. If not, see <https://www.gnu.org/licenses/>.
*/
void serial_setup();

View file

@ -25,82 +25,43 @@ with the Clean CommonMark library. If not, see <https://www.gnu.org/licenses/>.
#include "config.h"
uint8_t OPEN = 1;
uint8_t CLOSED = 0;
WiFiUDP Udp;
void setup() {
pinMode(REED_PIN, INPUT_PULLUP);
Serial.begin(9600);
while (!Serial);
void print_mac_address() {
uint8_t mac[6];
WiFi.macAddress(mac);
Serial.print("MAC: ");
for(unsigned int i = 5; i > 0; --i) {
Serial.print(mac[i], HEX);
Serial.print(":");
}
Serial.println(mac[0], HEX);
}
boolean wifi_setup() {
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("No WiFI shield present");
// TODO: Create noShieldLoop with visual indication.
while (true);
return false;
}
print_mac_address();
while (true) {
// TODO: Visually indicate that we are trying to connect
Serial.print("Connecting to SSID: ");
Serial.println(SSID);
status = WiFi.begin(SSID, PASSWORD);
if (status == WL_CONNECTED) {
if (WiFi.begin(SSID, PASSWORD) == WL_CONNECTED) {
break;
}
// TODO: Visually indicate that we waiting for trying to connect again
delay(10000);
}
printNetworkInfo();
}
void loop() {
int reed_state = digitalRead(REED_PIN);
int packetSize = Udp.readPacket();
if (packetSize) {
Udp.beginPacket(udp.remoteIP(), Udp.remotePort());
if (reed_state == HIGH) {
Udp.write(CLOSED);
} else {
Udp.write(OPEN);
}
Udp.endPacket();
}
// TODO: Visually indicate open/closed state.
}
void printNetworkInfo() {
Serial.print("Connect to WiFi");
IPAddress ip = WiFi.localIP();
Serial.println("Connect to WiFi");
Serial.print("IP Address: ");
Serial.println(ip);
Serial.println(WiFi.localIP());
byte mac[6];
WiFi.macAddress(mac);
Serial.print("MAC address: ");
for (int i = 5; i >= 0; --i) {
if (mac[i] < 16) {
Serial.print("0");
}
Serial.print(mac[i], HEX);
if (i > 0) {
Serial.print(":");
}
}
Serial.println();
Udp.begin(SERVER_PORT);
return true;
}

27
source/arduino/wifi.h Normal file
View file

@ -0,0 +1,27 @@
/*
Copyright (c):
Philipp Matthias Schäfer (philipp.matthias.schaefer@posteo.de), 2020
This file is part of the KrautStatus' Arduino code.
The Clean CommonMark library is free software: you can redistribute it and/or
modify it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
The Clean CommonMark library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
for more details.
You should have received a copy of the GNU Affero General Public License along
with the Clean CommonMark library. If not, see <https://www.gnu.org/licenses/>.
*/
#include <WiFi101.h>
#include <WiFiUdp.h>
extern WiFiUDP Udp;
bool wifi_setup();