-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.ic
136 lines (119 loc) · 2.68 KB
/
util.ic
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
* @file util.ic
* @brief Utility functions
* @author Andrew Krieger
* @date 4/25/2011
*/
#use "defines.ic"
/**
* @brief Print half a byte (4 bits) in hex.
* @param b The data to print. Only the low 4 bits are used.
*/
void print_hex_half(int b) {
//Select the proper character from the string below
printf("%c", "0123456789ABCDEF"[b & 0x0F]);
}
/**
* @brief Print a byte in hex.
* @param b The byte to print. Only the low 8 are used.
*/
void print_hex_byte(int b) {
//Print the high nibble
print_hex_half(b>>4);
//Print the low nibble
print_hex_half(b);
}
/**
* @brief Block until the start button is pressed. The stop button is ignored.
*/
void press_start(void) {
//Wait for the user to push start down
while(!start_button())
;
//Wait for the user to let go.
while(start_button())
;
}
/**
* @brief Block until the stop button is pressed. The start button is ignored.
*/
void press_stop(void) {
//Wait for the user to push start down
while(!stop_button())
;
//Wait for the user to let go.
while(stop_button())
;
}
/**
* @brief Block until the start or the stop button is pressed.
* @return @ref BUTTON_START or @ref BUTTON_STOP
*/
int press_button(void) {
//Wait for a button to be pressed down
for(;;) {
if(stop_button()) {
//They pressed stop; wait for them to let go
while(stop_button())
;
//Return STOP
return BUTTON_STOP;
}
if(start_button()) {
//They pressed start; wait for them to let go
while(start_button())
;
//Return start
return BUTTON_START;
}
}
}
/**
* @brief Integer absolute value
* @param x The integer whose absolute value is be found
* @return The absolute value of x
*/
int abs(int x) {
if(x < 0)
return -x;
else
return x;
}
/**
* @brief Floating point absolute value
* @param x The number whose absolute value is be found
* @return The absolute value of x
*/
float fabs(float x) {
if(x < 0.0)
return -x;
else
return x;
}
/**
* @brief Round x to the nearest integer
* @param x the number to round
* @return x rounded using standard rounding rules (half away from 0)
*/
int round(float x) {
float rem = x - (float)(int)x;
if(x >= 0.0) {
if(rem >= 0.5)
return (int)x+1;
else
return (int)x;
} else {
if(rem <= -0.50)
return (int)x-1;
else
return (int)x;
}
}
int myanalog(int sensor)
{
int a,b;
a=analog(sensor);
msleep(1L);
b=analog(sensor);
return (a+b)/2;
}