summaryrefslogtreecommitdiffstats
path: root/avr.cpp
blob: 585dc7d4831588a817b87dbd93be9f724cda13aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "avr.h"
#include "adc.h"
#include "global.h"
#include "util.h"

#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) {
  if(i <= 0)  while(getButton() == 0);
  else while(getButton() != CLAMP(1, i, 5));
  
  while(getButton() != 0);
}

void beep(unsigned long freq) {
	const int prescalers[7] = {1, 8, 32, 64, 128, 256, 1024};
	
	if(freq == 0) {
		beepOff();
		return;
	}
        
	
	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;
}

void ledOn() {
  PORTA &= ~0x20;
}

void ledOff() {
  PORTA |= 0x20;
}