-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsp.c
More file actions
106 lines (84 loc) · 2.44 KB
/
sp.c
File metadata and controls
106 lines (84 loc) · 2.44 KB
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
//******************************************************************************
#include "sp.h"
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <assert.h>
#define SP_ADDR "172.23.42.29"
#define SP_PORT 2342
#define CMD_BITMAPLINEAR 0x0012
#define SUBCMD_BITMAP_NORMAL 0x0
typedef struct __attribute__((__packed__))
{
uint16_t command;
uint16_t offset;
uint16_t length;
uint16_t subcommand;
uint16_t reserved;
uint8_t bits[SP_HEIGHT][SP_TILES_HORIZ];
} packet_t;
static int sock = -1;
static struct sockaddr_in sin;
static packet_t packet;
//******************************************************************************
void sp_create()
{
assert(sock < 0);
sin.sin_family = AF_INET;
sin.sin_port = htons(SP_PORT);
sin.sin_addr.s_addr = inet_addr(SP_ADDR);
sock = socket(sin.sin_family, SOCK_DGRAM, IPPROTO_UDP);
assert(sock >= 0);
packet.command = htons(CMD_BITMAPLINEAR);
packet.offset = 0;
packet.length = htons(SP_HEIGHT * SP_TILES_HORIZ);
packet.subcommand = htons(SUBCMD_BITMAP_NORMAL);
packet.reserved = 0;
memset(&packet.bits[0][0], 0, SP_HEIGHT * SP_TILES_HORIZ);
}
//******************************************************************************
void sp_free()
{
assert(sock >= 0);
close(sock);
sock = -1;
}
//******************************************************************************
void sp_send()
{
assert(sock >= 0);
int rc = sendto(sock, &packet, sizeof(packet_t), 0, (struct sockaddr *)&sin, sizeof(sin));
assert(rc >= 0);
}
//******************************************************************************
void sp_clear()
{
assert(sock >= 0);
memset(&packet.bits[0][0], 0, SP_HEIGHT * SP_TILES_HORIZ);
}
//******************************************************************************
void sp_set(int y, int x, bool val)
{
assert(y >= 0 && y < SP_HEIGHT);
assert(x >= 0 && x < SP_WIDTH);
assert(sock >= 0);
int col = x >> 3;
int bit = 7 - (x & 0b111);
if (val)
packet.bits[y][col] |= 1 << bit;
else
packet.bits[y][col] &= ~(1 << bit);
}
//******************************************************************************
uint8_t *get(int y, int col)
{
assert(y >= 0 && y < SP_HEIGHT);
assert(col >= 0 && col < SP_TILES_HORIZ);
assert(sock >= 0);
return &packet.bits[y][col];
}
//******************************************************************************