-
Notifications
You must be signed in to change notification settings - Fork 6
/
ex-valentine.c
76 lines (65 loc) · 1.36 KB
/
ex-valentine.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
/* ex-valentine.c */
/* Copyright (c) 2020 J. M. Spivey */
#include "hardware.h"
#include "microbian.h"
#include "lib.h"
/* heart -- filled-in heart image */
const unsigned heart[] =
IMAGE(0,1,0,1,0,
1,1,1,1,1,
1,1,1,1,1,
0,1,1,1,0,
0,0,1,0,0);
/* small -- small heart image */
const unsigned small[] =
IMAGE(0,0,0,0,0,
0,1,0,1,0,
0,1,1,1,0,
0,0,1,0,0,
0,0,0,0,0);
/* show -- display a picture for a specified time */
void show(const unsigned *img, int t)
{
display_show(img);
timer_delay(t);
}
/* heart_task -- show beating heart */
void heart_task(int n)
{
priority(P_HIGH);
while (1) {
show(heart, 1050);
show(small, 150);
show(heart, 150);
show(small, 150);
}
}
/* prime -- test for primality */
int prime(int n)
{
for (int k = 2; k * k <= n; k++) {
if (n % k == 0)
return 0;
}
return 1;
}
/* prime_task -- print primes on the serial port */
void prime_task(int arg)
{
int n = 2, count = 0;
while (1) {
if (prime(n)) {
count++;
printf("prime(%d) = %d\n", count, n);
}
n++;
}
}
void init(void)
{
serial_init();
timer_init();
display_init();
start("Heart", heart_task, 0, STACK);
start("Prime", prime_task, 0, STACK);
}