aboutsummaryrefslogtreecommitdiff
path: root/common.h
blob: 028817c037ba67668e3ca2743bcecddfcb91aa1f (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
#ifndef COMMON_H
#define COMMON_H

/* cpu runs at n mhz */
#define F_CPU 1000000

#define sleepwhile(cond) \
	sleep_enable (); \
	while (cond) { sleep_cpu (); } \
	sleep_disable ();

#define __unused__ __attribute__ ((unused))

/* define lightweight assert (without printf) that halts the cpu */
#include <stdio.h>
#include <avr/sleep.h>
#include <avr/interrupt.h>
#define assert(x) if (!(x)) { \
		puts("assert in " __FILE__ ":" #x); \
		shutdownError (); \
	}

#include <stdbool.h>

/* global wakeup flag, incremented by functions that interact with the main
 * loop (i.e. not pwm) */
extern volatile uint8_t wakeup;

/* wakeup sources */
#define WAKE_ACCEL_HORIZON 0
#define WAKE_ACCEL_SHAKE 1
#define WAKE_GYRO 2
#define WAKE_I2C 3
#define WAKE_TIMER 4

#define shouldWakeup(x) (wakeup & (1 << x))
#define enableWakeup(x) wakeup |= 1 << x;
#include <util/atomic.h>
#define disableWakeup(x) \
	ATOMIC_BLOCK (ATOMIC_FORCEON) { \
		wakeup &= ~(1 << x); \
	}

void shutdownError ();

#define sign(x) ((x < 0) ? -1 : 1)

#endif /* COMMON_H */