<commit_before>#include <avr/sleep.h>
#include <avr/power.h>
#include <avr/wdt.h>

const unsigned int BUZZER_PIN = PB0;
const unsigned int MOTOR_PIN = PB1;
const unsigned int BUTTON_PIN = PB2;
const unsigned int ACCELEROMETER_IN = PB3;
const unsigned int ACCELEROMETER_POWER = PB4;
const unsigned long DEBOUNCE_TIME = 50;

volatile bool watchDogBarked = false;
bool watchDogEnabled = false;

volatile bool buttonPressed = false;

enum WatchDogTimeout {
  WDT_16ms,
  WDT_32ms,
  WDT_64ms,
  WDT_128ms,
  WDT_250ms,
  WDT_500ms,
  WDT_1sec,
  WDT_2sec,
  WDT_4sec,
  WDT_8sec
};

enum PowerState {
  SLEEP,
  GAME
};

PowerState currentState = SLEEP; // Start with sleep as the initial state

void setupWatchDogTimeoutOneshot(WatchDogTimeout wdt) {
  // Adopted from InsideGadgets (www.insidegadgets.com)
  byte timeoutVal = wdt & 7;
  if (wdt > 7) {
    timeoutVal |= (1 << 5);
  }
  timeoutVal |= (1 << WDCE);

  MCUSR &= ~(1 << WDRF);
  WDTCR |= (1 << WDCE) | (1 << WDE); // Start timed sequence
  WDTCR = timeoutVal;
  WDTCR |= _BV(WDIE);
  wdt_reset();  // pat the dog
}

/**
   Disables the accelerometer by setting the pin that powers it up to LOW
*/
void turnAccelerometerOff() {
  digitalWrite(ACCELEROMETER_POWER, LOW);
}

/**
   Enables the accelerometer by setting the pin that powers it up to HIGH
*/
void turnAccelerometerOn() {
  digitalWrite(ACCELEROMETER_POWER, HIGH);
}

/**
  Watchdog interrupt routine to be triggered when watchdog times out
*/
ISR(WDT_vect) {
  watchDogBarked = true;
  wdt_disable(); // disable watchdog
}

/**
   Change interrupt routine (e.g. when button presses occur)
*/
ISR (PCINT0_vect) {
  // TO-DO: Investigate ways to debounce the button
  if (digitalRead(BUTTON_PIN) == HIGH) {
    buttonPressed = true;
  }
}

/**
   Sets up pin change interrupt for PB2
*/
void setupChangeInterrupt() {
  PCMSK |= bit (PCINT2); // want pin D2 (PB2)
  GIFR |= bit (PCIF); // clear any outstanding interrupts
  GIMSK |= bit (PCIE); // enable pin change interrupts
}

/**
   Sets the MCU into a deep sleep state until a change interrupt is triggered
*/
void goToDeepSleep() {
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  ADCSRA = 0; // turn off ADC
  power_all_disable (); // power off ADC, Timer 0 and 1, serial interface
  sleep_enable();
  sleep_cpu(); // Sleep here and wait for the interrupt
  sleep_disable();
  power_all_enable(); // power everything back on
}

void setup() {
  pinMode(BUZZER_PIN, OUTPUT);
  pinMode(MOTOR_PIN, OUTPUT);
  pinMode(BUTTON_PIN, INPUT);<commit_msg>Register button pressed on button up<commit_after>#include <avr/sleep.h>
#include <avr/power.h>
#include <avr/wdt.h>

const unsigned int BUZZER_PIN = PB0;
const unsigned int MOTOR_PIN = PB1;
const unsigned int BUTTON_PIN = PB2;
const unsigned int ACCELEROMETER_IN = PB3;
const unsigned int ACCELEROMETER_POWER = PB4;
const unsigned long DEBOUNCE_TIME = 50;

volatile bool watchDogBarked = false;
bool watchDogEnabled = false;

volatile bool buttonPressed = false;

enum WatchDogTimeout {
  WDT_16ms,
  WDT_32ms,
  WDT_64ms,
  WDT_128ms,
  WDT_250ms,
  WDT_500ms,
  WDT_1sec,
  WDT_2sec,
  WDT_4sec,
  WDT_8sec
};

enum PowerState {
  SLEEP,
  GAME
};

PowerState currentState = SLEEP; // Start with sleep as the initial state

void setupWatchDogTimeoutOneshot(WatchDogTimeout wdt) {
  // Adopted from InsideGadgets (www.insidegadgets.com)
  byte timeoutVal = wdt & 7;
  if (wdt > 7) {
    timeoutVal |= (1 << 5);
  }
  timeoutVal |= (1 << WDCE);

  MCUSR &= ~(1 << WDRF);
  WDTCR |= (1 << WDCE) | (1 << WDE); // Start timed sequence
  WDTCR = timeoutVal;
  WDTCR |= _BV(WDIE);
  wdt_reset();  // pat the dog
}

/**
   Disables the accelerometer by setting the pin that powers it up to LOW
*/
void turnAccelerometerOff() {
  digitalWrite(ACCELEROMETER_POWER, LOW);
}

/**
   Enables the accelerometer by setting the pin that powers it up to HIGH
*/
void turnAccelerometerOn() {
  digitalWrite(ACCELEROMETER_POWER, HIGH);
}

/**
  Watchdog interrupt routine to be triggered when watchdog times out
*/
ISR(WDT_vect) {
  watchDogBarked = true;
  wdt_disable(); // disable watchdog
}

/**
   Change interrupt routine (e.g. when button presses occur)
*/
ISR (PCINT0_vect) {
  // TO-DO: Investigate ways to debounce the button
  if (digitalRead(BUTTON_PIN) == LOW) {
    buttonPressed = true;
  }
}

/**
   Sets up pin change interrupt for PB2
*/
void setupChangeInterrupt() {
  PCMSK |= bit (PCINT2); // want pin D2 (PB2)
  GIFR |= bit (PCIF); // clear any outstanding interrupts
  GIMSK |= bit (PCIE); // enable pin change interrupts
}

/**
   Sets the MCU into a deep sleep state until a change interrupt is triggered
*/
void goToDeepSleep() {
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  ADCSRA = 0; // turn off ADC
  power_all_disable (); // power off ADC, Timer 0 and 1, serial interface
  sleep_enable();
  sleep_cpu(); // Sleep here and wait for the interrupt
  sleep_disable();
  power_all_enable(); // power everything back on
}

void setup() {
  pinMode(BUZZER_PIN, OUTPUT);
  pinMode(MOTOR_PIN, OUTPUT);
  pinMode(BUTTON_PIN, INPUT);
{'fraction_non_alphanumeric': 0.10978993261989695, 'fraction_numerical': 0.016646848989298454, 'mean_word_length': 3.797528517110266, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 11, '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': '1860726', 'n_tokens_mistral': 1926, 'n_tokens_neox': 1841, 'n_words': 542}