Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: Adafruit_GFX_128x64 DS3231 PinDetect SDFileSystem USBDevice mbed RealtimeMath MODSERIAL
main.cpp
- Committer:
- ellingjp
- Date:
- 2014-06-05
- Revision:
- 20:294eaeaf2ebb
- Parent:
- 19:4f4f7bc4a3fb
- Child:
- 22:9350752f5414
File content as of revision 20:294eaeaf2ebb:
#include "main.h"
#include "mbed.h"
#include "PinDetect.h"
#include "USBSerial.h"
#include "Adafruit_SSD1306.h"
#include "SDFileSystem.h"
#include "receive_data.h"
#include "log_data.h"
#include "process_data.h"
#include "debug.h"
#include "SystemTime.h"
#include "sync.h"
#include "pins.h"
//#include "DS3231.h"
// Capture button stuff
#define PIN_DETECT_SAMPLE_PERIOD_uS 20000 // 20 ms sample period
#define SYNC_HELD_SAMPLES (SYNC_HOLD_TIME_MS * 1000 / PIN_DETECT_SAMPLE_PERIOD_uS)
#ifdef USE_OLED
// Display
SPI spi0(P0_9, NC, P0_10); // mosi, miso, sclk
Adafruit_SSD1306 oled(spi0, P0_11, P0_12, P0_13); // DC, RST, CS
#endif
// DS3231 rtc(I2C_SDA, I2C_SCL);
// Mode button
PinDetect captureButton(P0_16, PullUp);
// State
enum state {IDLE, CAPTURE, SYNC};
enum state State;
// This is used to ensure the rising edge after a hold is ignored
bool ignore_edge = false;
void buttonHeld() {
if (State == IDLE)
State = SYNC;
else
State = IDLE;
// Button was held down, so don't count the next assert
ignore_edge = true;
}
void buttonPressed() {
// Don't ignore subsequent edges
if (ignore_edge) {
ignore_edge = false;
return;
}
if (State == IDLE)
State = CAPTURE;
else if (State == CAPTURE)
State = IDLE;
}
int main(void)
{
SystemTime::start();
State = IDLE;
// After button is held, the next rising edge will call buttonPressed.
// Because of this, inside the callbacks need to ensure that edge
// does not do anything unintended.
captureButton.attach_deasserted_held(buttonHeld);
captureButton.attach_asserted(buttonPressed);
captureButton.setSampleFrequency();
captureButton.setSamplesTillHeld(SYNC_HELD_SAMPLES);
VectorInt16 *data;
while (true) {
if (State == IDLE){
OLED_CLEAR();
OLED_PRINTP("Idling...", 0, 0);
PC_PRINTLN("Idling...");
} else if (State == CAPTURE) {
OLED_PRINTP("Starting capture...", 0, 0);
OLED_PRINTP("Init SD card...", 0, 10);
log_init();
OLED_PRINTP("Init peak detect...", 0, 10);
process_init();
OLED_PRINTP("Init data receipt...", 0, 10);
receive_init();
OLED_CLEAR();
OLED_PRINTP("Capturing data...", 0, 0);
while (State == CAPTURE) {
data = receive_data();
log_data(data);
int split;
if (process_data(data->y, &split)) {
PC_PRINTLNF("Split time: %d", split);
int min = split / 60000;
int sec = (split / 1000) % 60;
int hund = (split / 10) % 100;
OLED_PRINTPF("%1d", min, 0, 40);
OLED_PRINTPF("%02d", sec, 5, 40);
OLED_DRAWPIXEL(14, 44, 0x1);
OLED_PRINTPF("%02d", hund, 15, 40);
log_data(split);
}
}
receive_close();
process_close();
log_close();
} else if (State == SYNC) {
OLED_PRINTP("Ready to sync...", 0, 0);
sync_init();
sync();
}
}
}