<commit_before>/** Lost Model Alarm firmware
 * ATtiny25
 *
 * Sounds morse code "W" (warning) after a preset
 * delay, and after a further delay, "SOS" (mayday). 
 * The program utilizes sleep mode to conserve battery
 * power and disables peripherals between beeps.  The 
 * program is originally designed for an ultra-light
 * LMA for ultra micro (UM) models like the Hobbyzone
 * Champ, inspired by losing mine to wind. (I got it
 * back but still...)
 * 
 * In addition, the firmware provides a low battery
 * warning. See adc.c for details.
 *
 * <NAME> - http://www.bot-thoughts.com
 */

#include "config.h"
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <util/delay.h> 
#include <avr/eeprom.h>
#include "battery.h"
#include "buzzer.h"
#include "switch.h"
#include "morse.h"

EEMEM uint8_t cfg_rccal = 0;		 // EEPROM: RC oscillator calibration
EEMEM uint8_t cfg_warn_min = 5; // EEPROM: Delay before warning starts sounding, in minutes.

uint16_t warn_sec;				// Delay (in seconds) before Warning start sounding
uint16_t sos_sec;				// Delay (in seconds) before SOS starts sounding
uint16_t seconds = 1;		// 2^32 = 4294967296 seconds = 7 weeks, way more than enough; int isn't enough
uint8_t pause = PERIOD;	// time to pause between SOS or Warning beeps

void disableWatchdog();
void enableWatchdog();
void slowClock();

int main()
{
	cli();
	disableWatchdog();

	slowClock();
	initBuzzer();
	initSwitch();
	initADC();
	
#ifdef DEBUG
	uint16_t v = getVoltage();
	uint16_t i;

	for (i = 0x8000; i != 0; i >>= 1) {
		if (v & i) {
			dit();
		} else {
			dah();
		}
		_delay_ms(1000);
	}
#endif
	
	// Retrieve the current warning timeout from eeprom
	uint8_t warn_min = eeprom_read_byte(&cfg_warn_min);

	// If switch is depressed (at power up), begin increasing
	// warning time, in 5-minute increments, max 30 minutes
	// SOS time is 2 x warning time.
	//
	while (switchPressed()) {	
		// Increment warning time by 5 minutes
		warn_min += 5;

		// Maximum warning time is 30 minutes
		if (warn_min > 30) warn_min = 5;

		// Save the new warning time
		eeprom_update_byte(&cfg_warn_min, warn_min);

		// Beep out the current warning time
		uint8_t i = warn_min;
		while (i) {
			if (i >= 10) {				// dah for each tens of minutes
				dah();
				i -= 10;
			} else if (i != 0) { // dit indicates 5 minutes
				dit();
				i = 0;
			}
		}

		// pause between increments
		_delay_ms(3000);
	}

	// Compute the number of seconds for warning and SOS
	warn_sec = warn_min * 60;
	sos_sec = warn_sec * 2;

	ok();
	
	/*
	if (checkVoltage()) {
		ok();
	} else {
		sos();
	}
	*/

	enableWatchdog();
 	sei();

	while (1) {
		set_sleep_mode(SLEEP_MODE_PWR_DOWN);
		sleep_mode();
	}
}


ISR(WDT_vect)
{
	seconds++;	// increment 'clock'
<commit_msg>Uncommented startup voltage check<commit_after>/** Lost Model Alarm firmware
 * ATtiny25
 *
 * Sounds morse code "W" (warning) after a preset
 * delay, and after a further delay, "SOS" (mayday). 
 * The program utilizes sleep mode to conserve battery
 * power and disables peripherals between beeps.  The 
 * program is originally designed for an ultra-light
 * LMA for ultra micro (UM) models like the Hobbyzone
 * Champ, inspired by losing mine to wind. (I got it
 * back but still...)
 * 
 * In addition, the firmware provides a low battery
 * warning. See adc.c for details.
 *
 * <NAME> - http://www.bot-thoughts.com
 */

#include "config.h"
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <util/delay.h> 
#include <avr/eeprom.h>
#include "battery.h"
#include "buzzer.h"
#include "switch.h"
#include "morse.h"

EEMEM uint8_t cfg_rccal = 0;		 // EEPROM: RC oscillator calibration
EEMEM uint8_t cfg_warn_min = 5; // EEPROM: Delay before warning starts sounding, in minutes.

uint16_t warn_sec;				// Delay (in seconds) before Warning start sounding
uint16_t sos_sec;				// Delay (in seconds) before SOS starts sounding
uint16_t seconds = 1;		// 2^32 = 4294967296 seconds = 7 weeks, way more than enough; int isn't enough
uint8_t pause = PERIOD;	// time to pause between SOS or Warning beeps

void disableWatchdog();
void enableWatchdog();
void slowClock();

int main()
{
	cli();
	disableWatchdog();

	slowClock();
	initBuzzer();
	initSwitch();
	initADC();
	
//#ifdef DEBUG
	uint16_t v = getVoltage();
	uint16_t i;

	for (i = 0x8000; i != 0; i >>= 1) {
		if (v & i) {
			dit();
		} else {
			dah();
		}
		_delay_ms(1000);
	}
//#endif
	
	// Retrieve the current warning timeout from eeprom
	uint8_t warn_min = eeprom_read_byte(&cfg_warn_min);

	// If switch is depressed (at power up), begin increasing
	// warning time, in 5-minute increments, max 30 minutes
	// SOS time is 2 x warning time.
	//
	while (switchPressed()) {	
		// Increment warning time by 5 minutes
		warn_min += 5;

		// Maximum warning time is 30 minutes
		if (warn_min > 30) warn_min = 5;

		// Save the new warning time
		eeprom_update_byte(&cfg_warn_min, warn_min);

		// Beep out the current warning time
		uint8_t i = warn_min;
		while (i) {
			if (i >= 10) {				// dah for each tens of minutes
				dah();
				i -= 10;
			} else if (i != 0) { // dit indicates 5 minutes
				dit();
				i = 0;
			}
		}

		// pause between increments
		_delay_ms(3000);
	}

	// Compute the number of seconds for warning and SOS
	warn_sec = warn_min * 60;
	sos_sec = warn_sec * 2;

	if (checkVoltage()) {
		ok();
	} else {
		sos();
	}

	enableWatchdog();
 	sei();

	while (1) {
		set_sleep_mode(SLEEP_MODE_PWR_DOWN);
		sleep_mode();
	}
}


ISR(WDT_vect)
{
	seconds++;	// increment 'clock'

{'fraction_non_alphanumeric': 0.12546984070162878, 'fraction_numerical': 0.025416144621442635, 'mean_word_length': 3.2205438066465257, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '453481', 'n_tokens_mistral': 2296, 'n_tokens_neox': 2013, 'n_words': 670}