Skip to content

Commit

Permalink
Add loop and sample Threads. Fix printf gremlins.
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulZC committed May 13, 2024
1 parent bf02085 commit c1ea73c
Show file tree
Hide file tree
Showing 12 changed files with 172 additions and 103 deletions.
16 changes: 7 additions & 9 deletions Firmware/OpenLog_Artemis_Geophone_Logger/Geophone.ino
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,6 @@ void geophone_setup()
g_ui32GeophoneDataBufferPointer = 0; // Reset the pointer

//stimer_init(); // Now start the timer

cancel_handle_sample = the_event_queue.call_every(SAMPLE_INTERVAL, &sampling_interrupt); // Call sampling_interrupt every 2ms (500Hz)

cancel_handle_loop = the_event_queue.call_every(LOOP_INTERVAL, &mainLoop); // Call mainLoop every 250ms

the_event_queue.dispatch();
}

/*
Expand Down Expand Up @@ -169,17 +163,21 @@ bool geophone_loop()
{
for (uint32_t i = 1; i < FREQ_LIMIT; i++)
{
sprintf(tempData, "%.2f,", g_fGeophoneMagnitudes[i]);
char tempStr[16];
olaftoa(g_fGeophoneMagnitudes[i], tempStr, 2, sizeof(tempStr) / sizeof(char));
sprintf(tempData, "%s,", tempStr);
strcat(geophoneData, tempData);
sprintf(tempData, "%.2f\n", g_fGeophoneMagnitudes[i]);
sprintf(tempData, "%s\n", tempStr);
strcat(geophoneDataSerial, tempData);
}

sprintf(tempData, "%d", ui32LoudestFrequency);
strcat(geophoneData, tempData);
strcat(peakFreq, tempData);

sprintf(tempData, ",%.2f", fMaxValue);
char tempStr[16];
olaftoa(fMaxValue, tempStr, 2, sizeof(tempStr) / sizeof(char));
sprintf(tempData, ",%s", tempStr);
strcat(peakFreq, tempData);

if (settings.printMeasurementCount)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
The RTC can be set by a u-blox GNSS module.
Based on v14 of:
Based on v2.8 of:
OpenLog Artemis
By: Nathan Seidle
SparkFun Electronics
Expand Down Expand Up @@ -170,14 +170,12 @@ uint64_t qwiicPowerOnTime = 0; //Used to delay after Qwiic power on to allow sen
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

//Geophone settings
#define TEST_PERIOD_1 20000 // Uncomment to enable sinusoidal test signal (period in micros) (20000 = 50Hz)
#define TEST_AMPLITUDE_1 100 // Amplitude of the sinusoidal test signal
#define TEST_PERIOD_2 30303 // Uncomment to enable second (mixed) cosinusoidal test signal (period in micros) (30303 = 33Hz)
#define TEST_AMPLITUDE_2 50 // Amplitude of the sinusoidal test signal
//#define TEST_PERIOD_1 20000 // Uncomment to enable sinusoidal test signal (period in micros) (20000 = 50Hz)
//#define TEST_AMPLITUDE_1 100 // Amplitude of the sinusoidal test signal
//#define TEST_PERIOD_2 30303 // Uncomment to enable second (mixed) cosinusoidal test signal (period in micros) (30303 = 33Hz)
//#define TEST_AMPLITUDE_2 50 // Amplitude of the sinusoidal test signal
//#define SAMPLE_INTERVAL 6000 // Interval for 500Hz with a 3MHz clock

#define DUMP(varname) {if (settings.serialPlotterMode == false) Serial.printf("%s: %llu\n", #varname, varname)}

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

#include "WDT.h" // WDT support
Expand Down Expand Up @@ -212,16 +210,20 @@ void stopWatchdog()

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

// Use an EventQueue to control the ADC samples
#include "mbed_events.h"
// Use Thread and EventQueue to control the ADC samples
#include "mbed.h"
#include <stdio.h>

events::EventQueue the_event_queue;
rtos::Thread sample_thread;
events::EventQueue sample_event_queue;

int cancel_handle_sample = 0;
//static const std::chrono::milliseconds SAMPLE_INTERVAL = 2ms;
static const uint32_t SAMPLE_INTERVAL = 2;

rtos::Thread loop_thread;
events::EventQueue loop_event_queue;

int cancel_handle_loop = 0;
//static const std::chrono::milliseconds LOOP_INTERVAL = 250ms;
static const uint32_t LOOP_INTERVAL = 250;
Expand Down Expand Up @@ -318,8 +320,6 @@ void setup() {
else
if (settings.serialPlotterMode == false) Serial.println("No Qwiic devices detected");

//Serial.printf("Setup time: %.02f ms\n", (micros() - startTime) / 1000.0);

//Print the helper text
if ((settings.enableTerminalOutput == true) && (settings.serialPlotterMode == false))
{
Expand All @@ -332,15 +332,46 @@ void setup() {

digitalWrite(PIN_STAT_LED, LOW); // Turn the STAT LED off now that everything is configured

geophone_setup(); // Set up the ADC and get ready to start the events
geophone_setup(); // Set up the ADC

loop_thread.start(&startMainLoop);

loop_thread.set_priority(osPriorityNormal1); // Increase loop_thread priority above the actual loop()

sample_thread.start(&startSamples);

sample_thread.set_priority(osPriorityNormal2); // Increase sample_thread priority so it can interrupt loop_thread
}

void loop()
{
yield(); // Nothing to do here...
}

void startSamples()
{
cancel_handle_sample = sample_event_queue.call_every(SAMPLE_INTERVAL, &sampling_interrupt); // Call sampling_interrupt every 2ms (500Hz)

sample_event_queue.dispatch();

while (1)
yield();
}

void startMainLoop()
{
cancel_handle_loop = loop_event_queue.call_every(LOOP_INTERVAL, &mainLoop); // Call mainLoop every 250ms

loop_event_queue.dispatch();

while (1)
yield();
}

void mainLoop()
{
digitalWrite(PIN_LOGIC_DEBUG, !digitalRead(PIN_LOGIC_DEBUG));

if (Serial.available()) // Check if the user pressed a key
{
samplingEnabled = false; // Disable sampling while menu is open
Expand Down
45 changes: 41 additions & 4 deletions Firmware/OpenLog_Artemis_Geophone_Logger/Sensors.ino
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,27 @@ void getDateTime()

if (settings.logDate)
{
char rtcDate[12]; //10/12/2019,
char rtcDate[11]; // 10/12/2019
char rtcDay[3];
char rtcMonth[3];
char rtcYear[5];
if (myRTC.dayOfMonth < 10)
sprintf(rtcDay, "0%d", myRTC.dayOfMonth);
else
sprintf(rtcDay, "%d", myRTC.dayOfMonth);
if (myRTC.month < 10)
sprintf(rtcMonth, "0%d", myRTC.month);
else
sprintf(rtcMonth, "%d", myRTC.month);
if (myRTC.year < 10)
sprintf(rtcYear, "200%d", myRTC.year);
else
sprintf(rtcYear, "20%d", myRTC.year);
if (settings.americanDateStyle == true)
sprintf(rtcDate, "%02d/%02d/20%02d,", myRTC.month, myRTC.dayOfMonth, myRTC.year);
sprintf(rtcDate, "%s/%s/%s,", rtcMonth, rtcDay, rtcYear);
else
sprintf(rtcDate, "%02d/%02d/20%02d,", myRTC.dayOfMonth, myRTC.month, myRTC.year);
sprintf(rtcDate, "%s/%s/%s,", rtcDay, rtcMonth, rtcYear);

strcat(dateTime, rtcDate);
}

Expand All @@ -27,7 +43,28 @@ void getDateTime()
{
if (adjustedHour > 12) adjustedHour -= 12;
}
sprintf(rtcTime, "%02d:%02d:%02d.%02d,", adjustedHour, myRTC.minute, myRTC.seconds, myRTC.hundredths);
char rtcHour[3];
char rtcMin[3];
char rtcSec[3];
char rtcHundredths[3];
if (adjustedHour < 10)
sprintf(rtcHour, "0%d", adjustedHour);
else
sprintf(rtcHour, "%d", adjustedHour);
if (myRTC.minute < 10)
sprintf(rtcMin, "0%d", myRTC.minute);
else
sprintf(rtcMin, "%d", myRTC.minute);
if (myRTC.seconds < 10)
sprintf(rtcSec, "0%d", myRTC.seconds);
else
sprintf(rtcSec, "%d", myRTC.seconds);
if (myRTC.hundredths < 10)
sprintf(rtcHundredths, "0%d", myRTC.hundredths);
else
sprintf(rtcHundredths, "%d", myRTC.hundredths);
sprintf(rtcTime, "%s:%s:%s.%s,", rtcHour, rtcMin, rtcSec, rtcHundredths);

strcat(dateTime, rtcTime);
}
} //end if use RTC for timestamp
Expand Down
2 changes: 1 addition & 1 deletion Firmware/OpenLog_Artemis_Geophone_Logger/autoDetect.ino
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ deviceType_e testDevice(uint8_t i2cAddress)
//Confidence: Medium - reads Configuration Register
SfeADS1219ArdI2C sensor;
if (sensor.begin(qwiic, i2cAddress) == true) //Wire port, Address
return (DEVICE_ADC_ADS122C04);
return (DEVICE_ADC_ADS1219);
}
break;
case 0x42:
Expand Down
13 changes: 12 additions & 1 deletion Firmware/OpenLog_Artemis_Geophone_Logger/logging.ino
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,18 @@ char* findNextAvailableLog(int &newFileNumber, const char *fileLeader)
static char newFileName[40];
while (1)
{
sprintf(newFileName, "%s%05u.TXT", fileLeader, newFileNumber); //Splice the new file number into this file name. Max no. is 99999.
char newFileNumberStr[6];
if (newFileNumber < 10)
sprintf(newFileNumberStr, "0000%d", newFileNumber);
else if (newFileNumber < 100)
sprintf(newFileNumberStr, "000%d", newFileNumber);
else if (newFileNumber < 1000)
sprintf(newFileNumberStr, "00%d", newFileNumber);
else if (newFileNumber < 10000)
sprintf(newFileNumberStr, "0%d", newFileNumber);
else
sprintf(newFileNumberStr, "%d", newFileNumber);
sprintf(newFileName, "%s%s.TXT", fileLeader, newFileNumberStr); //Splice the new file number into this file name. Max no. is 99999.

if (sd.exists(newFileName) == false) break; //File name not found so we will use it.

Expand Down
8 changes: 4 additions & 4 deletions Firmware/OpenLog_Artemis_Geophone_Logger/lowerPower.ino
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ void powerDownOLA(void)
detachInterrupt(PIN_POWER_LOSS);
#endif

the_event_queue.cancel(cancel_handle_sample); // Stop the ADC task
the_event_queue.cancel(cancel_handle_loop); // Stop the loop task
sample_event_queue.cancel(cancel_handle_sample); // Stop the ADC task
loop_event_queue.cancel(cancel_handle_loop); // Stop the loop task

//Prevent stop logging button from waking us from sleep
if (settings.useGPIO32ForStopLogging == true)
Expand Down Expand Up @@ -167,8 +167,8 @@ void resetArtemis(void)

delay(sdPowerDownDelay); // Give the SD card time to finish writing ***** THIS IS CRITICAL *****

the_event_queue.cancel(cancel_handle_sample); // Stop the ADC task
the_event_queue.cancel(cancel_handle_loop); // Stop the loop task
sample_event_queue.cancel(cancel_handle_sample); // Stop the ADC task
loop_event_queue.cancel(cancel_handle_loop); // Stop the loop task

Serial.flush(); //Finish any prints

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,12 @@ void menuAttachedDevices()
temp = temp->next;
}

availableDevices++;
Serial.printf("%d) Configure Qwiic Settings\r\n", availableDevices);
availableDevices++;

Serial.println(F("x) Exit"));

int nodeNumber = getNumber(menuTimeout); //Timeout after x seconds
if (nodeNumber > 0 && nodeNumber < availableDevices - 1)
if (nodeNumber > 0 && nodeNumber < availableDevices)
{
//Lookup the function we need to call based the node number
FunctionPointer functionPointer = getConfigFunctionPtr(nodeNumber - 1);
Expand All @@ -129,10 +127,6 @@ void menuAttachedDevices()

configureDevice(nodeNumber - 1); //Reconfigure this device with the new settings
}
else if (nodeNumber == availableDevices - 1)
{
menuConfigure_QwiicBus();
}
else if (nodeNumber == STATUS_PRESSED_X)
break;
else if (nodeNumber == STATUS_GETNUMBER_TIMEOUT)
Expand Down
1 change: 0 additions & 1 deletion Firmware/OpenLog_Artemis_Geophone_Logger/menuMain.ino
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ void menuMain()

Serial.println("2) Configure Time Stamp");

//Serial.println("3) Detect / Configure Attached Devices");
Serial.println("3) Configure Qwiic Settings");

Serial.println("4) Configure Geophone Threshold");
Expand Down
4 changes: 3 additions & 1 deletion Firmware/OpenLog_Artemis_Geophone_Logger/menuThreshold.ino
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ void menuThreshold()
Serial.println("Menu: Configure Geophone Threshold");

Serial.print("1) Threshold: ");
Serial.printf("%f\n", settings.threshold);
char tempStr[16];
olaftoa(settings.threshold, tempStr, 3, sizeof(tempStr) / sizeof(char));
Serial.printf("%s\n", tempStr);

Serial.println("x) Exit");

Expand Down
46 changes: 41 additions & 5 deletions Firmware/OpenLog_Artemis_Geophone_Logger/menuTimeStamp.ino
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,58 @@ void menuTimeStamp()
Serial.print("Current date/time: ");
myRTC.getTime();

char rtcDate[11]; //10/12/2019
char rtcDate[11]; // 10/12/2019
char rtcDay[3];
char rtcMonth[3];
char rtcYear[5];
if (myRTC.dayOfMonth < 10)
sprintf(rtcDay, "0%d", myRTC.dayOfMonth);
else
sprintf(rtcDay, "%d", myRTC.dayOfMonth);
if (myRTC.month < 10)
sprintf(rtcMonth, "0%d", myRTC.month);
else
sprintf(rtcMonth, "%d", myRTC.month);
if (myRTC.year < 10)
sprintf(rtcYear, "200%d", myRTC.year);
else
sprintf(rtcYear, "20%d", myRTC.year);
if (settings.americanDateStyle == true)
sprintf(rtcDate, "%02d/%02d/20%02d", myRTC.month, myRTC.dayOfMonth, myRTC.year);
sprintf(rtcDate, "%s/%s/%s,", rtcMonth, rtcDay, rtcYear);
else
sprintf(rtcDate, "%02d/%02d/20%02d", myRTC.dayOfMonth, myRTC.month, myRTC.year);
sprintf(rtcDate, "%s/%s/%s,", rtcDay, rtcMonth, rtcYear);

Serial.print(rtcDate);
Serial.print(" ");

char rtcTime[12]; //09:14:37.41
char rtcTime[13]; //09:14:37.41,
int adjustedHour = myRTC.hour;
if (settings.hour24Style == false)
{
if (adjustedHour > 12) adjustedHour -= 12;
}
sprintf(rtcTime, "%02d:%02d:%02d.%02d", adjustedHour, myRTC.minute, myRTC.seconds, myRTC.hundredths);
char rtcHour[3];
char rtcMin[3];
char rtcSec[3];
char rtcHundredths[3];
if (adjustedHour < 10)
sprintf(rtcHour, "0%d", adjustedHour);
else
sprintf(rtcHour, "%d", adjustedHour);
if (myRTC.minute < 10)
sprintf(rtcMin, "0%d", myRTC.minute);
else
sprintf(rtcMin, "%d", myRTC.minute);
if (myRTC.seconds < 10)
sprintf(rtcSec, "0%d", myRTC.seconds);
else
sprintf(rtcSec, "%d", myRTC.seconds);
if (myRTC.hundredths < 10)
sprintf(rtcHundredths, "0%d", myRTC.hundredths);
else
sprintf(rtcHundredths, "%d", myRTC.hundredths);
sprintf(rtcTime, "%s:%s:%s.%s", rtcHour, rtcMin, rtcSec, rtcHundredths);

Serial.println(rtcTime);

Serial.print("1) Log Date: ");
Expand Down
2 changes: 1 addition & 1 deletion Firmware/OpenLog_Artemis_Geophone_Logger/nvm.ino
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void recordSystemSettingsToFile()
settingsFile.println("americanDateStyle=" + (String)settings.americanDateStyle);
settingsFile.println("hour24Style=" + (String)settings.hour24Style);
settingsFile.println("serialTerminalBaudRate=" + (String)settings.serialTerminalBaudRate);
settingsFile.println("localUTCOffset=" + (String)settings.localUTCOffset);
settingsFile.print("localUTCOffset="); settingsFile.println(settings.localUTCOffset);
settingsFile.println("printDebugMessages=" + (String)settings.printDebugMessages);
settingsFile.println("powerDownQwiicBusBetweenReads=" + (String)settings.powerDownQwiicBusBetweenReads);
settingsFile.println("qwiicBusMaxSpeed=" + (String)settings.qwiicBusMaxSpeed);
Expand Down
Loading

0 comments on commit c1ea73c

Please sign in to comment.