-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsleep.c
103 lines (84 loc) · 2.8 KB
/
sleep.c
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
//
// main.c
//
// Copyright (C) 2016 by Eric Dey. All rights reserved.
//
#include "sleep.h"
// This implements a millisecond delay function that takes an unsigned long
// input. This function is designed to be good enough for general purposes
// that include human-perception scales. It is not designed for high accuracy.
// When the remaining time is less than 0.125ms the delay can be ended early.
void delay_ms(unsigned long ms) {
unsigned long cycles; // total count of remaining cycles to wait
unsigned long cutoff; // cutoff limit below which no more delay is done
cycles = (ms)*(_XTAL_FREQ/4000);
cutoff = (_XTAL_FREQ/4000) / 8; // 0.125ms lower limit
// Handle 10,000's of cycles -- avoid calling _delay(n) with n > 10,000
while (cycles >= 20000) {
cycles -= 20000;
_delay(10000);
_delay(10000);
}
if (cycles >= 10000) {
cycles -= 10000;
_delay(10000);
}
if (cycles <= cutoff) return; // Exit as soon as we drop below the cutoff
// Handle 1000's of cycles
while (cycles >= 2000) {
cycles -= 2000;
_delay(2000);
}
if (cycles >= 1000) {
cycles -= 1000;
_delay(1000);
}
if (cycles <= cutoff) return; // Exit as soon as we drop below the cutoff
// Handle 100's of cycles
while (cycles >= 200) {
cycles -= 200;
_delay(200);
}
if (cycles >= 100) {
cycles -= 100;
_delay(100);
}
if (cycles <= cutoff) return; // Exit as soon as we drop below the cutoff
// Handle 10's of cycles
while (cycles >= 20) {
cycles -= 20;
_delay(20);
}
if (cycles >= 10) {
cycles -= 10;
_delay(10);
}
if (cycles <= cutoff) return; // Exit as soon as we drop below the cutoff
// At this point, there are less than 10 cycles remaining and, unless
// the oscillator is really slow, execution will already have been ended.
// This is code is followed through to the end mostly out of a need for
// completion.
// Handle remaining cycles of 3
while (cycles >= 2) {
cycles -= 3;
_delay3(1);
}
if (cycles == 0) return;
// Handle last 1 or 2 cycles -- accuracy is highly questionable by now
_delay(1);
if (cycles == 2) _delay(1);
}
// This implements a Python-style sleep that takes a floating point number
// of seconds. This goes down to 1ms resolution.
void sleep(float seconds) {
unsigned long min, ms;
// Do this 60,000ms (1 min) at a time to avoid overflowing size of long
min = (unsigned long) (seconds / 60.0);
for (; min > 0; min--) {
delay_ms(60000);
seconds -= 60.0;
}
// Remainder of time is less than seconds; handle in one request
ms = (unsigned long) (1000.0 * seconds);
delay_ms(ms);
}