Skip to content

Example Application

OpenAcousticDevices edited this page Nov 18, 2017 · 15 revisions

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 simple Time Setter configuration tool.

The application starts by importing the standard Boolean header and the AudioMoth library header.

#include <stdbool.h>

#include "audioMoth.h"

It then provide all the mandatory empty interrupt handlers.

/* Required interrupt handles */

void AudioMoth_handleSwitchInterrupt(void) { };
void AudioMoth_handleMicrophoneInterrupt(int16_t sample) { };
void AudioMoth_handleDirectMemoryAccessInterrupt(bool primaryChannel, int16_t **nextBuffer) { };
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(void) {

    /* 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 will enter a low power state until it is woken by the switch position changing or by the USB being connected.

    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();

    } else {

        /* Flash both LED */

        AudioMoth_setBothLED(true);

        AudioMoth_delay(100);

        AudioMoth_setBothLED(false);

    }

    /* Power down and wake up in one second */

    AudioMoth_powerDownAndWake(1, true);

}
Clone this wiki locally