-
Notifications
You must be signed in to change notification settings - Fork 18
Example Application
AudioMoth applications are written by editing the main.c
file. The example included in the repository represents a minimal application that handles default USB messages allowing interaction with the AudioMoth Time App tool.
The application starts by importing the standard Boolean header and the AudioMoth library header.
#include <stdbool.h>
#include "audioMoth.h"
It should include a firmware version and description.
/* Firmware version and description */
static uint8_t firmwareVersion[AM_FIRMWARE_VERSION_LENGTH] = {1, 0, 0};
static uint8_t firmwareDescription[AM_FIRMWARE_DESCRIPTION_LENGTH] = "Example-Firmware";
It should then provide all the mandatory empty timezone, interrupt and USB application packet handlers, and it should populate the firmware USB message handlers.
/* Required time zone handler */
void AudioMoth_timezoneRequested(int8_t *timezoneHours, int8_t *timezoneMinutes) { }
/* Required interrupt handlers */
void AudioMoth_handleSwitchInterrupt() { }
void AudioMoth_handleMicrophoneChangeInterrupt() { }
void AudioMoth_handleMicrophoneInterrupt(int16_t sample) { }
void AudioMoth_handleDirectMemoryAccessInterrupt(bool primaryChannel, int16_t **nextBuffer) { }
/* Required USB message handlers */
void AudioMoth_usbFirmwareVersionRequested(uint8_t **firmwareVersionPtr) {
*firmwareVersionPtr = firmwareVersion;
}
void AudioMoth_usbFirmwareDescriptionRequested(uint8_t **firmwareDescriptionPtr) {
*firmwareDescriptionPtr = firmwareDescription;
}
void AudioMoth_usbApplicationPacketRequested(uint32_t messageType, uint8_t *transmitBuffer, uint32_t size) { }
void AudioMoth_usbApplicationPacketReceived(uint32_t messageType, uint8_t *receiveBuffer, uint8_t *transmitBuffer, uint32_t size) { }
The main
function then starts by initialing the AudioMoth device and checking the switch position.
/* Main function */
int main() {
/* Initialise device */
AudioMoth_initialise();
/* Check the switch position */
AM_switchPosition_t switchPosition = AudioMoth_getSwitchPosition();
If the switch is in the 'USB' position, the code calls AudioMoth_handleUSB
to handle all communication with the host computer. The AudioMoth library automatically handles request to set the time, read the time, read the battery state, and read the unique ID of the device. If the AudioMoth device is not connected to a USB host it will enter a low power state until it is connected. The AudioMoth_handleUSB
function call blocks until the switch position is changed.
if (switchPosition == AM_SWITCH_USB) {
/* Handle the case that the switch is in USB position. Waits in low energy state until USB disconnected or switch moved */
AudioMoth_handleUSB();
If the switch is in either of the other two positions, the AudioMoth device will flash both LED.
} else {
/* Flash both LED */
AudioMoth_setBothLED(true);
AudioMoth_delay(100);
AudioMoth_setBothLED(false);
}
The AudioMoth device will then power down, setting the real time clock so that it wakes up again at the next change of the second.
/* Power down and wake up in one second */
AudioMoth_powerDownAndWake(1, true);
}