add reset sketch and reorganize files

This commit is contained in:
r4 2022-04-19 16:44:54 +02:00
parent 4e90b217cb
commit 9420eb8293
9 changed files with 128 additions and 91 deletions

View File

@ -0,0 +1,11 @@
#include "settings.hh"
void setup() {
Serial.begin(9600);
Serial.print("Resetting EEPROM...");
EEPROM_Settings_Class settings;
settings.save();
Serial.println("done.");
}
void loop() {}

1
reset_sketch/settings.hh Symbolic link
View File

@ -0,0 +1 @@
../spybug/settings.hh

26
spybug/aaa_config.hh Normal file
View File

@ -0,0 +1,26 @@
#pragma once
/************************
BEGIN USER CONFIGURATION
************************/
//#define DEBUG_RECORDING
#define PIN_COMPONENT_SWITCH 2 /* Use a digital signal to switch on/off the microphone and SD card for less power draw. */
#define COMPONENT_SWITCH_ON HIGH
#define SAMPLE_MODE_U8
//#define SAMPLE_MODE_S16
//#define ADC_PRESCALE_16 /* Up to ~60kHz. */
//#define ADC_PRESCALE_32 /* Up to ~27kHz. */
#define ADC_PRESCALE_64 /* Up to ~18kHz. */
//#define U8_AMPLIFY_X2 /* (U8 sampling mode only) amplify audio by factor 2. */
#define ADC_CHANNEL AdcChannel0
#define TIMER_COMPARE 1000 /* 16MHz / 1000 = 16kHz. */
#define FLUSH_SAMPLES 64000 /* Flush WAV file every n samples. */
#define PIN_SS 10
/**********************
END USER CONFIGURATION
**********************/

34
spybug/fstr.cpp Normal file
View File

@ -0,0 +1,34 @@
#include "fstr.hh"
size_t fstrlen(const __FlashStringHelper *s) {
PGM_P sp = (PGM_P)s;
size_t len = 0;
while (pgm_read_byte(sp++))
len++;
return len;
}
bool fstreq(const char *a, const __FlashStringHelper *b_fsh) {
PGM_P b = (PGM_P)b_fsh;
while (1) {
if (*a != pgm_read_byte(b))
return false;
if (*a == 0)
return true;
a++; b++;
}
}
int printf(const __FlashStringHelper *fmt, ...) {
size_t len = fstrlen(fmt);
char buf[len + 1];
buf[len] = 0;
memcpy_P(buf, fmt, len + 1);
va_list args;
va_start(args, fmt);
int ret = vprintf(buf, args);
va_end(args);
return ret;
}

9
spybug/fstr.hh Normal file
View File

@ -0,0 +1,9 @@
#pragma once
#include <Arduino.h>
size_t fstrlen(const __FlashStringHelper *s);
bool fstreq(const char *a, const __FlashStringHelper *b_fsh);
int printf(const __FlashStringHelper *fmt, ...);

19
spybug/io.cpp Normal file
View File

@ -0,0 +1,19 @@
#include "io.hh"
static int serial_putch(char c, FILE *f) {
(void)f;
return Serial.write(c) == 1 ? 0 : 1;
}
static int serial_getch(FILE *f) {
(void)f;
while(Serial.available() == 0);
return Serial.read();
}
static FILE serial_in_out;
void io_setup() {
fdev_setup_stream(&serial_in_out, serial_putch, serial_getch, _FDEV_SETUP_RW);
stdout = stdin = stderr = &serial_in_out;
}

5
spybug/io.hh Normal file
View File

@ -0,0 +1,5 @@
#pragma once
#include <Arduino.h>
void io_setup();

12
spybug/settings.hh Normal file
View File

@ -0,0 +1,12 @@
#pragma once
#include <EEPROM.h>
#define EEADDR_SETTINGS 0x00
struct EEPROM_Settings_Class {
unsigned long recording_delay = 0l;
bool serial_log = true;
inline void load() { EEPROM.get(EEADDR_SETTINGS, *this); }
inline void save() { EEPROM.put(EEADDR_SETTINGS, *this); }
};

View File

@ -28,7 +28,6 @@
Out defaults to A0 (AdcChannel0), but can be set manually in ADC_CHANNEL. Out defaults to A0 (AdcChannel0), but can be set manually in ADC_CHANNEL.
*/ */
#include <EEPROM.h>
#include <SD.h> #include <SD.h>
#include <SPI.h> #include <SPI.h>
#include <avr/interrupt.h> #include <avr/interrupt.h>
@ -36,30 +35,10 @@
#include <avr/wdt.h> #include <avr/wdt.h>
#include <avr/sleep.h> #include <avr/sleep.h>
/************************ #include "aaa_config.hh"
BEGIN USER CONFIGURATION #include "fstr.hh"
************************/ #include "io.hh"
//#define DEBUG_RECORDING #include "settings.hh"
//#define PIN_COMPONENT_SWITCH 2 /* Use a digital signal to switch on/off the microphone and SD card for less power draw. */
//#define COMPONENT_SWITCH_ON HIGH
#define SAMPLE_MODE_U8
//#define SAMPLE_MODE_S16
//#define ADC_PRESCALE_16 /* Up to ~60kHz. */
//#define ADC_PRESCALE_32 /* Up to ~27kHz. */
#define ADC_PRESCALE_64 /* Up to ~18kHz. */
//#define U8_AMPLIFY_X2 /* (U8 sampling mode only) amplify audio by factor 2. */
#define ADC_CHANNEL AdcChannel0
#define TIMER_COMPARE 1000 /* 16MHz / 1000 = 16kHz. */
#define FLUSH_SAMPLES 64000 /* Flush WAV file every n samples. */
#define PIN_SS 10
/**********************
END USER CONFIGURATION
**********************/
#if !defined(__AVR_ATmega328P__) || F_CPU != 16000000 #if !defined(__AVR_ATmega328P__) || F_CPU != 16000000
#error "This program only works on ATmega328P devices with a clock frequency of 16MHz!" #error "This program only works on ATmega328P devices with a clock frequency of 16MHz!"
@ -67,56 +46,6 @@
void (*full_reset)() = nullptr; void (*full_reset)() = nullptr;
static int serial_putch(char c, FILE *f) {
(void)f;
return Serial.write(c) == 1 ? 0 : 1;
}
static int serial_getch(FILE *f) {
(void)f;
while(Serial.available() == 0);
return Serial.read();
}
static FILE serial_in_out;
static void setup_serial_in_out() {
fdev_setup_stream(&serial_in_out, serial_putch, serial_getch, _FDEV_SETUP_RW);
stdout = stdin = stderr = &serial_in_out;
}
static size_t fstrlen(const __FlashStringHelper *s) {
PGM_P sp = (PGM_P)s;
size_t len = 0;
while (pgm_read_byte(sp++))
len++;
return len;
}
static int printf(const __FlashStringHelper *fmt, ...) {
size_t len = fstrlen(fmt);
char buf[len + 1];
buf[len] = 0;
memcpy_P(buf, fmt, len + 1);
va_list args;
va_start(args, fmt);
int ret = vprintf(buf, args);
va_end(args);
return ret;
}
static bool fstreq(const char *a, const __FlashStringHelper *b_fsh) {
PGM_P b = (PGM_P)b_fsh;
while (1) {
if (*a != pgm_read_byte(b))
return false;
if (*a == 0)
return true;
a++; b++;
}
}
#define print_special(x) { Serial.print(x); } #define print_special(x) { Serial.print(x); }
#define die(fmt, ...) { disable_recording_interrupts(); if (settings.serial_log) { printf(F("Fatal: ")); printf(fmt, ##__VA_ARGS__); Serial.flush(); } while(1); } #define die(fmt, ...) { disable_recording_interrupts(); if (settings.serial_log) { printf(F("Fatal: ")); printf(fmt, ##__VA_ARGS__); Serial.flush(); } while(1); }
#define dbg(fmt, ...) { printf(F("Debug: ")); printf(fmt, ##__VA_ARGS__); } #define dbg(fmt, ...) { printf(F("Debug: ")); printf(fmt, ##__VA_ARGS__); }
@ -177,6 +106,7 @@ enum AdcChannel : uint8_t {
AdcChannelGnd = 15, AdcChannelGnd = 15,
}; };
EEPROM_Settings_Class settings;
File file; File file;
#if defined(SAMPLE_MODE_U8) #if defined(SAMPLE_MODE_U8)
#define SAMPLE_BUF_SIZE 256 #define SAMPLE_BUF_SIZE 256
@ -199,16 +129,6 @@ volatile int16_t dbg_min = 32767;
volatile int16_t dbg_max = -32768; volatile int16_t dbg_max = -32768;
#endif #endif
#define EEADDR_SETTINGS 0x00
struct EEPROM_Settings {
unsigned long recording_delay;
bool serial_log;
};
EEPROM_Settings settings;
#define load_settings() EEPROM.get(EEADDR_SETTINGS, settings)
#define save_settings() EEPROM.put(EEADDR_SETTINGS, settings)
ISR(TIMER1_COMPA_vect) { ISR(TIMER1_COMPA_vect) {
/* Only write to file, if one of the buffers is full (meaning no access conflicts). */ /* Only write to file, if one of the buffers is full (meaning no access conflicts). */
if (samples_in_buffer[!which_buffer] == SAMPLE_BUF_SIZE) { if (samples_in_buffer[!which_buffer] == SAMPLE_BUF_SIZE) {
@ -346,18 +266,18 @@ void command_loop() {
else else
mins = n; mins = n;
settings.recording_delay = mins; settings.recording_delay = mins;
save_settings(); settings.save();
printf(F("Set waiting time to %lu minutes or "), settings.recording_delay); printf(F("Set waiting time to %lu minutes or "), settings.recording_delay);
print_special((float)settings.recording_delay / 60.f); print_special((float)settings.recording_delay / 60.f);
printf(F(" hours.\n")); printf(F(" hours.\n"));
} else if (n_args == 3 && fstreq(args[1], F("serial"))) { } else if (n_args == 3 && fstreq(args[1], F("serial"))) {
if (fstreq(args[2], F("on"))) { if (fstreq(args[2], F("on"))) {
settings.serial_log = true; settings.serial_log = true;
save_settings(); settings.save();
printf(F("Serial log enabled.\n")); printf(F("Serial log enabled.\n"));
} else if (fstreq(args[2], F("off"))) { } else if (fstreq(args[2], F("off"))) {
settings.serial_log = false; settings.serial_log = false;
save_settings(); settings.save();
printf(F("Serial log disabled.\n")); printf(F("Serial log disabled.\n"));
} else { } else {
printf(F("Usage: 'set serial [on|off]'.\n")); printf(F("Usage: 'set serial [on|off]'.\n"));
@ -392,9 +312,9 @@ void command_loop() {
void setup() { void setup() {
// Serial Setup // Serial Setup
Serial.begin(9600); /* Set baud rate. */ Serial.begin(9600); /* Set baud rate. */
setup_serial_in_out(); /* Add printf support. */ io_setup(); /* Add printf support. */
// Load EEPROM Data // Load EEPROM Data
load_settings(); settings.load();
// Handle Commands // Handle Commands
info(F("Type anything in the next 4s to enter command mode.\n")); info(F("Type anything in the next 4s to enter command mode.\n"));
for (size_t i = 0; i < 4 * 4; i++) { for (size_t i = 0; i < 4 * 4; i++) {
@ -423,7 +343,7 @@ void setup() {
low_power_sleep_minutes(settings.recording_delay); low_power_sleep_minutes(settings.recording_delay);
/* Reset wait time. */ /* Reset wait time. */
settings.recording_delay = 0; settings.recording_delay = 0;
save_settings(); settings.save();
} }
// Activate Components // Activate Components
#ifdef PIN_COMPONENT_SWITCH #ifdef PIN_COMPONENT_SWITCH