This repository has been archived on 2025-03-02. You can view files and clone it, but cannot push or open issues or pull requests.
rc2007-rescue/avr.cpp

44 lines
709 B
C++
Raw Normal View History

2007-04-03 12:48:00 +00:00
#include "avr.h"
#include "adc.h"
2007-04-17 22:10:05 +00:00
#include "global.h"
2007-04-03 12:48:00 +00:00
#include "util.h"
2007-04-15 22:01:04 +00:00
#include <stdint.h>
int getButton() {
uint16_t val = getADCValue(7);
if(val < 144) return 5;
if(val < 228) return 4;
if(val < 304) return 3;
if(val < 376) return 2;
if(val < 600) return 1;
return 0;
}
void waitForButton(int i) {
while(getButton() != CLAMP(0, i, 5));
while(getButton() != 0);
}
2007-04-17 22:10:05 +00:00
void beep(unsigned long freq) {
const int prescalers[7] = {1, 8, 32, 64, 128, 256, 1024};
for(int i = 0; i < 7; i++) {
if(F_CPU/freq/2/prescalers[i] <= 256) {
TCCR2 = (TCCR2&~0x07)|(i+1);
OCR2 = F_CPU/freq/2/prescalers[i] - 1;
return;
}
}
beepOff();
}
void beepOff() {
TCCR2 &= ~0x07;
}