109109
110110/* Magnetic switch constants */
111111
112- #define MAGNETIC_SWITCH_FLASH_MODULO 2
112+ #define MAGNETIC_SWITCH_WAIT_INTERVAL 500
113+ #define MAGNETIC_SWITCH_WAIT_MULTIPLIER 2
113114
114115#define MAGNETIC_SWITCH_CHANGE_FLASHES 10
115116
@@ -766,7 +767,7 @@ static uint32_t *recordingErrorHasOccurred = (uint32_t*)(AM_BACKUP_DOMAIN_START_
766767
767768static uint32_t * recordingPreparationPeriod = (uint32_t * )(AM_BACKUP_DOMAIN_START_ADDRESS + 40 );
768769
769- static uint32_t * magneticSwitchWaitingCounter = (uint32_t * )(AM_BACKUP_DOMAIN_START_ADDRESS + 44 );
770+ static uint32_t * magneticSwitchWaitCounter = (uint32_t * )(AM_BACKUP_DOMAIN_START_ADDRESS + 44 );
770771
771772static uint32_t * waitingForMagneticSwitch = (uint32_t * )(AM_BACKUP_DOMAIN_START_ADDRESS + 48 );
772773
@@ -850,7 +851,7 @@ static int16_t secondaryBuffer[MAXIMUM_SAMPLES_IN_DMA_TRANSFER];
850851
851852/* Firmware version and description */
852853
853- static uint8_t firmwareVersion [AM_FIRMWARE_VERSION_LENGTH ] = {1 , 7 , 0 };
854+ static uint8_t firmwareVersion [AM_FIRMWARE_VERSION_LENGTH ] = {1 , 7 , 1 };
854855
855856static uint8_t firmwareDescription [AM_FIRMWARE_DESCRIPTION_LENGTH ] = "AudioMoth-Firmware-Basic" ;
856857
@@ -884,7 +885,7 @@ static void copyToBackupDomain(uint32_t *dst, uint8_t *src, uint32_t length) {
884885 value = (value << BITS_PER_BYTE ) + * (src + length - 1 - i );
885886 }
886887
887- * (dst + length / UINT32_SIZE_IN_BYTES ) = value ;
888+ if ( length % UINT32_SIZE_IN_BYTES ) * (dst + length / UINT32_SIZE_IN_BYTES ) = value ;
888889
889890}
890891
@@ -1004,9 +1005,9 @@ int main(void) {
10041005
10051006 * recordingPreparationPeriod = INITIAL_PREPARATION_PERIOD ;
10061007
1007- /* Initialise magentic switch state variables */
1008+ /* Initialise magnetic switch state variables */
10081009
1009- * magneticSwitchWaitingCounter = 0 ;
1010+ * magneticSwitchWaitCounter = 0 ;
10101011
10111012 * waitingForMagneticSwitch = false;
10121013
@@ -1224,24 +1225,22 @@ int main(void) {
12241225
12251226 if (switchPosition == AM_SWITCH_CUSTOM ) {
12261227
1227- if (configSettings -> enableMagneticSwitch ) {
1228+ * magneticSwitchWaitCounter = 0 ;
1229+
1230+ * waitingForMagneticSwitch = configSettings -> enableMagneticSwitch ;
1231+
1232+ if (configSettings -> enableMagneticSwitch || * shouldSetTimeFromGPS ) {
12281233
12291234 * timeOfNextRecording = UINT32_MAX ;
12301235
12311236 * durationOfNextRecording = UINT32_MAX ;
12321237
12331238 * timeOfNextGPSTimeSetting = UINT32_MAX ;
12341239
1235- * magneticSwitchWaitingCounter = 0 ;
1236-
1237- * waitingForMagneticSwitch = true;
1238-
12391240 } else {
12401241
12411242 scheduleRecording (scheduleTime , timeOfNextRecording , durationOfNextRecording , timeOfNextGPSTimeSetting );
12421243
1243- * waitingForMagneticSwitch = false;
1244-
12451244 }
12461245
12471246 }
@@ -1286,15 +1285,23 @@ int main(void) {
12861285
12871286 bool enableLED = (switchPosition == AM_SWITCH_DEFAULT ) || configSettings -> enableLED ;
12881287
1288+ /* Calculate time until next activity */
1289+
1290+ int64_t timeUntilPreparationStart = (int64_t )* timeOfNextRecording * MILLISECONDS_IN_SECOND - (int64_t )* recordingPreparationPeriod - (int64_t )currentTime * MILLISECONDS_IN_SECOND - (int64_t )currentMilliseconds ;
1291+
1292+ int64_t timeUntilNextGPSTimeSetting = (int64_t )* timeOfNextGPSTimeSetting * MILLISECONDS_IN_SECOND - (int64_t )currentTime * MILLISECONDS_IN_SECOND - (int64_t )currentMilliseconds ;
1293+
12891294 /* If the GPS synchronisation window has passed then cancel it */
12901295
1291- if ( currentTime >= * timeOfNextGPSTimeSetting + GPS_MAX_TIME_SETTING_PERIOD ) * timeOfNextGPSTimeSetting = UINT32_MAX ;
1296+ int64_t timeSinceScheduledGPSTimeSetting = - timeUntilNextGPSTimeSetting ;
12921297
1293- /* Calculate time until next activitiy */
1298+ if ( timeSinceScheduledGPSTimeSetting > GPS_MAX_TIME_SETTING_PERIOD * MILLISECONDS_IN_SECOND ) {
12941299
1295- int64_t timeUntilPreparationStart = ( int64_t ) * timeOfNextRecording * MILLISECONDS_IN_SECOND - ( int64_t ) * recordingPreparationPeriod - ( int64_t ) currentTime * MILLISECONDS_IN_SECOND - ( int64_t ) currentMilliseconds ;
1300+ * timeOfNextGPSTimeSetting = UINT32_MAX ;
12961301
1297- int64_t timeUntilNextGPSTimeSetting = (int64_t )* timeOfNextGPSTimeSetting * MILLISECONDS_IN_SECOND - (int64_t )currentTime * MILLISECONDS_IN_SECOND - (int64_t )currentMilliseconds ;
1302+ timeUntilNextGPSTimeSetting = (int64_t )* timeOfNextGPSTimeSetting * MILLISECONDS_IN_SECOND - (int64_t )currentTime * MILLISECONDS_IN_SECOND - (int64_t )currentMilliseconds ;
1303+
1304+ }
12981305
12991306 /* Decide on the activity this wake up period */
13001307
@@ -1506,9 +1513,19 @@ int main(void) {
15061513
15071514 /* Determine if LED should be shown on this cycle */
15081515
1509- bool showFlash = * waitingForMagneticSwitch == false || (* waitingForMagneticSwitch && * magneticSwitchWaitingCounter % MAGNETIC_SWITCH_FLASH_MODULO == 0 );
1516+ bool showFlash = true;
1517+
1518+ if (magneticSwitchEnabled ) {
1519+
1520+ uint32_t flashModulo = WAITING_LED_FLASH_INTERVAL / MAGNETIC_SWITCH_WAIT_INTERVAL ;
15101521
1511- * magneticSwitchWaitingCounter = (* magneticSwitchWaitingCounter + 1 ) % MAGNETIC_SWITCH_FLASH_MODULO ;
1522+ if (* waitingForMagneticSwitch ) flashModulo *= MAGNETIC_SWITCH_WAIT_MULTIPLIER ;
1523+
1524+ showFlash = * magneticSwitchWaitCounter % flashModulo == 0 ;
1525+
1526+ * magneticSwitchWaitCounter = (* magneticSwitchWaitCounter + 1 ) % flashModulo ;
1527+
1528+ }
15121529
15131530 /* Flash LED to indicate waiting */
15141531
@@ -1580,7 +1597,7 @@ int main(void) {
15801597
15811598 * timeOfNextGPSTimeSetting = UINT32_MAX ;
15821599
1583- * magneticSwitchWaitingCounter = 0 ;
1600+ * magneticSwitchWaitCounter = 0 ;
15841601
15851602 * waitingForMagneticSwitch = true;
15861603
@@ -1602,7 +1619,9 @@ int main(void) {
16021619
16031620 int64_t timeToEarliestEvent = MIN (timeUntilPreparationStart , timeUntilNextGPSTimeSetting ) - EM4_WAKEUP_PERIOD ;
16041621
1605- int64_t timeToWait = MAX (0 , MIN (timeToEarliestEvent , WAITING_LED_FLASH_INTERVAL ));
1622+ uint32_t interval = magneticSwitchEnabled ? MAGNETIC_SWITCH_WAIT_INTERVAL : WAITING_LED_FLASH_INTERVAL ;
1623+
1624+ int64_t timeToWait = MAX (0 , MIN (timeToEarliestEvent , interval ));
16061625
16071626 SAVE_SWITCH_POSITION_AND_POWER_DOWN (timeToWait );
16081627
@@ -1950,7 +1969,7 @@ void AudioConfig_handleAudioConfigurationPacket(uint8_t *receiveBuffer, uint32_t
19501969
19511970 bool isTimePacket = size == (UINT32_SIZE_IN_BYTES + UINT16_SIZE_IN_BYTES );
19521971
1953- bool isDeploymentPacket = size == (UINT32_SIZE_IN_BYTES + UINT16_SIZE_IN_BYTES + DEPLOYMENT_ID_LENGTH );
1972+ bool isDeploymentPacket = size == (UINT32_SIZE_IN_BYTES + UINT16_SIZE_IN_BYTES + DEPLOYMENT_ID_LENGTH );
19541973
19551974 if (isTimePacket || isDeploymentPacket ) {
19561975
0 commit comments