[go: nahoru, domu]

Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
vlad0x00 committed May 18, 2024
1 parent cdd28e0 commit a5e6443
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 89 deletions.
104 changes: 55 additions & 49 deletions bluetooth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,82 +9,63 @@

namespace cvslpr {

static SoftwareSerial bluetooth(BLUETOOTH_RX_PIN, BLUETOOTH_TX_PIN);
volatile bool bluetooth_wakeup = false;
static SoftwareSerial bluetooth(BLUETOOTH_TX_PIN, BLUETOOTH_RX_PIN);
volatile bool bluetooth_switch_interrupt_registered = false;

enum class BluetoothMode
{
DATA,
COMMAND
};

static const char* BLUETOOTH_MODE_NAMES[] = { "data", "command" };

static void
bluetooth_mode(const BluetoothMode mode)
bluetooth_turn_on(const BluetoothMode mode)
{
digitalWrite(BLUETOOTH_ON_PIN, LOW);
digitalWrite(BLUETOOTH_KEY_PIN, mode == BluetoothMode::DATA ? LOW : HIGH);
delay(300);
digitalWrite(BLUETOOTH_ON_PIN, HIGH);
bluetooth.begin(mode == BluetoothMode::DATA ? BLUETOOTH_SERIAL_BAUD_RATE_DATA
: BLUETOOTH_SERIAL_BAUD_RATE_CMD);
msg_print(F("Bluetooth turned on with "));
msg_print(BLUETOOTH_MODE_NAMES[static_cast<int>(mode)]);
msg_print(F(" mode."));
}

static bool
test_bluetooth()
static void
on_bluetooth_switch_interrupt()
{
// Check if the module responds
bluetooth.write(F("AT\r\n"));
bluetooth.flush();

// Let the bluetooth module respond
delay(500);

// Ensure that the response is "OK"
if (!bluetooth.available() || bluetooth.read() != 'O' ||
!bluetooth.available() || bluetooth.read() != 'K') {
return false;
}
// Discard the rest of the response
while (bluetooth.available()) {
bluetooth.read();
}

return true;
msg_println(F("Bluetooth switch interrupt registered."));
bluetooth_switch_interrupt_registered = true;
}

bool
init_bluetooth()
static bool
wait_for_connection()
{
pinMode(BLUETOOTH_KEY_PIN, OUTPUT);
pinMode(BLUETOOTH_ON_PIN, OUTPUT);

if constexpr (BLUETOOTH_TEST) {
if (!test_bluetooth()) {
bluetooth_mode(BluetoothMode::COMMAND);
msg_println(F("Bluetooth module not responding."));
msg_println(F("Waiting for bluetooth connection..."));
bool connected = false;
for (;;) {
if (digitalRead(BLUETOOTH_STATE_PIN) == HIGH) {
return true;
}
if (digitalRead(BLUETOOTH_SWITCH_INTERRUPT_PIN) == HIGH) {
msg_println("Bluetooth switched off while waiting for connection.");
return false;
}
}

bluetooth_mode(BluetoothMode::DATA);

pinMode(BLUETOOTH_INTERRUPT_PIN, INPUT);
attachInterrupt(digitalPinToInterrupt(BLUETOOTH_INTERRUPT_PIN),
on_bluetooth_wakeup,
RISING);

msg_println(F("Bluetooth module initialized."));
return true;
}

void
on_bluetooth_wakeup()
static void
wait_for_off()
{
msg_println(F("Woken up by bluetooth."));
bluetooth_wakeup = true;
msg_println(F("Waiting for bluetooth module to be turned off..."));
while (digitalRead(BLUETOOTH_SWITCH_INTERRUPT_PIN) == HIGH) {
}
// The delay prevents multiple interrupt triggers being handled
delay(200);
}

void
static void
bluetooth_transfer_data()
{
auto logfile = open_log();
Expand Down Expand Up @@ -113,4 +94,29 @@ bluetooth_transfer_data()
logfile.close();
}

bool
init_bluetooth()
{
pinMode(BLUETOOTH_KEY_PIN, OUTPUT);
pinMode(BLUETOOTH_STATE_PIN, INPUT);
pinMode(BLUETOOTH_SWITCH_INTERRUPT_PIN, INPUT);

attachInterrupt(digitalPinToInterrupt(BLUETOOTH_SWITCH_INTERRUPT_PIN),
on_bluetooth_switch_interrupt,
RISING);

msg_println(F("Bluetooth initialized."));
return true;
}

void
bluetooth_handle_transfer()
{
bluetooth_turn_on(BluetoothMode::DATA);
if (wait_for_connection()) {
bluetooth_transfer_data();
wait_for_off();
}
}

} // namespace cvslpr
7 changes: 2 additions & 5 deletions bluetooth.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@

namespace cvslpr {

extern volatile bool bluetooth_wakeup;
extern volatile bool bluetooth_switch_interrupt_registered;

[[nodiscard]] bool
init_bluetooth();

void
on_bluetooth_wakeup();

void
bluetooth_transfer_data();
bluetooth_handle_transfer();

} // namespace cvslpr

Expand Down
15 changes: 8 additions & 7 deletions cave-sleeper.ino
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,19 @@ setup()
go_sleep(true);
}

rtc_wakeup = true;
rtc_interrupt_registered = true;
}

void
loop()
{
if constexpr (!INIT_RTC_TIME || INIT_RTC_TIME_AND_RUN) {
if (status_good) {
if (bluetooth_wakeup) {
bluetooth_transfer_data();
bluetooth_wakeup = false;
if (bluetooth_switch_interrupt_registered) {
bluetooth_handle_transfer();
bluetooth_switch_interrupt_registered = false;
}
if (rtc_wakeup) {
if (rtc_interrupt_registered) {
const auto now = get_current_time();
const auto readout = measure();
log(readout, now);
Expand All @@ -75,10 +75,11 @@ loop()
msg_println(F("Setting alarm failed."));
led_signal_error_perpetual();
}
rtc_wakeup = false;
rtc_interrupt_registered = false;
}

go_sleep(!bluetooth_wakeup && !rtc_wakeup);
go_sleep(!bluetooth_switch_interrupt_registered &&
!rtc_interrupt_registered);
}
}
}
Expand Down
37 changes: 15 additions & 22 deletions config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,32 @@ namespace cvslpr {
// to the following pin.
constexpr inline int RTC_INTERRUPT_PIN = 2;

// The bluetooth module should send a rising edge interrupt when it
// connects to a device to the following pin. The pin is likely named
// "STATE" on the bluetooth module.
constexpr inline int BLUETOOTH_INTERRUPT_PIN = 3;

// The SD card chip select pin.
constexpr inline int SD_CS_PIN = 4;

// Pin to receive data on from the bluetooth module. This should be
// connected to the TX pin of the module.
constexpr inline int BLUETOOTH_RX_PIN = 6;
// The pin to which a physical switch should be attached to, which
// should send a rising edge when turned on. This will indicate
// to the microcontroller that the bluetooth module should be
// turned on and ready to connect to the user's mobile device.
// The physical switch should also enable power on bluetooth module.
constexpr inline int BLUETOOTH_SWITCH_INTERRUPT_PIN = 3;

// Pin on which bluetooth RX is connected on.
constexpr inline int BLUETOOTH_RX_PIN = 5;

// Pin to send data from the bluetooth module. This should be connected
// to the RX pin of the module.
constexpr inline int BLUETOOTH_TX_PIN = 5;
// Pin on which bluetooth TX is connected on.
constexpr inline int BLUETOOTH_TX_PIN = 6;

// The following pin might be named "EN" on the bluetooth module.
constexpr inline int BLUETOOTH_KEY_PIN = 7;

// If true, a test is run for whether the bluetooth module is
// working properly. If false, the bluetooth module is assumed
// to be working properly. Requires BLUETOOTH_ON_PIN to be used.
constexpr inline bool BLUETOOTH_TEST = false;

// The microcontroller will set this pin to HIGH when it wants to
// power on the bluetooth module and LOW when it wants to power it off.
// This requires some sort of setup (e.g. a transistor) to actually
// power the bluetooth module.
constexpr inline int BLUETOOTH_ON_PIN = 8;
// The pin which indicates whether or not the bluetooth module is
// connected.
constexpr inline int BLUETOOTH_STATE_PIN = 9;

// The microcontroller will set this pin to HIGH when it wants to
// turn on the LED.
constexpr inline int LED_PIN = 13;
constexpr inline int LED_PIN = 8;

constexpr inline long BLUETOOTH_SERIAL_BAUD_RATE_CMD = 38400;
constexpr inline long BLUETOOTH_SERIAL_BAUD_RATE_DATA = 9600;
Expand Down
10 changes: 5 additions & 5 deletions rtc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace cvslpr {

static RTC_DS3231 rtc;

volatile bool rtc_wakeup = false;
volatile bool rtc_interrupt_registered = false;

FormattedTime
format_time(const DateTime& dt)
Expand All @@ -27,10 +27,10 @@ format_time(const DateTime& dt)
}

static void
on_rtc_wakeup()
on_rtc_interrupt()
{
msg_println(F("Woken up by RTC."));
rtc_wakeup = true;
msg_println(F("RTC interrupt registered."));
rtc_interrupt_registered = true;
}

bool
Expand All @@ -56,7 +56,7 @@ init_rtc()

pinMode(RTC_INTERRUPT_PIN, INPUT_PULLUP);
attachInterrupt(
digitalPinToInterrupt(RTC_INTERRUPT_PIN), on_rtc_wakeup, FALLING);
digitalPinToInterrupt(RTC_INTERRUPT_PIN), on_rtc_interrupt, FALLING);

msg_println(F("RTC module initialized."));
return true;
Expand Down
2 changes: 1 addition & 1 deletion rtc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace cvslpr {

extern volatile bool rtc_wakeup;
extern volatile bool rtc_interrupt_registered;

struct FormattedTime
{
Expand Down

0 comments on commit a5e6443

Please sign in to comment.