-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello.c
executable file
·91 lines (76 loc) · 1.56 KB
/
hello.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
/*
* Userspace program that communicates with the led_vga device driver
* primarily through ioctls
*
* Stephen A. Edwards
* Columbia University
*/
#include <stdio.h>
#include "vga_led.h"
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
void write_position(int, int);
int vga_led_fd;
int main()
{
vga_led_arg_t vla;
int i;
static const char filename[] = "/dev/vga_led";
int x_pos, x_vel, y_pos, y_vel;
/* Initial Position and Velocity Vectors */
x_pos = 666;
y_pos = 269;
x_vel = 1;
y_vel = 1;
printf("VGA LED Userspace program started\n");
if ((vga_led_fd = open(filename, O_RDWR)) == -1)
{
fprintf(stderr, "could not open %s\n", filename);
return -1;
}
while (1)
{
x_pos = x_pos + x_vel;
y_pos = y_pos + y_vel;
if (x_pos > X_MAX)
{
x_pos = X_MAX;
x_vel = -x_vel;
}
else if (x_pos < X_MIN)
{
x_pos = X_MIN;
x_vel = -x_vel;
}
if (y_pos > Y_MAX)
{
y_pos = Y_MAX;
y_vel = -y_vel;
}
else if (y_pos < Y_MIN)
{
y_pos = Y_MIN;
y_vel = -y_vel;
}
write_position(x_pos, y_pos);
usleep(10000);
}
printf("VGA LED Userspace program terminating\n");
return 0;
}
void write_position(int x, int y)
{
vga_led_arg_t vla;
printf("BALL POSITION (%d, %d)\n", x, y);
vla.x = (unsigned int) x;
vla.y = (unsigned int) y;
if (ioctl(vga_led_fd, VGA_LED_DRAW_BALL, &vla))
{
perror("ioctl(VGA_LED_DRAW_BALL) failed");
return;
}
}