/
sineblinker.cpp
44 lines (41 loc) · 923 Bytes
/
sineblinker.cpp
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
#include <stdio.h>
#include <fcntl.h>
#include <time.h>
#include <math.h>
#include <unistd.h>
#define PERIOD 10000000
#define PULSE_BASE 1000
#define SLEEP 10000
#define PI 3.14159265358979323846
#define DEBUG 0
int main()
{
timespec ts;
int led = open(
"/sys/devices/platform/leds-gpio/leds/beaglebone::usr3/brightness", O_WRONLY);
while (true) {
clock_gettime(CLOCK_REALTIME, &ts);
unsigned long long nsec = ts.tv_nsec;
unsigned long long mod = nsec % PERIOD;
long double pct = mod / (long double)PERIOD;
long double radians = pct * 2 * PI;
long double sine = sinl(radians) + 1.0;
if (DEBUG) {
int stars = sine * 40;
int i = 0;
while (i < stars) {
printf("*");
i++;
}
printf("\n");
}
long on_nsec = (long)(sine * PULSE_BASE);
write(led, "1", 1);
ts.tv_sec = 0;
ts.tv_nsec = on_nsec;
nanosleep(&ts, NULL);
write(led, "0", 1);
ts.tv_nsec = SLEEP;
nanosleep(&ts, NULL);
}
}