summaryrefslogtreecommitdiff
path: root/wsim.c
blob: cfaa08902881036b33a5cf9c47830d842c916956 (plain)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include<stdio.h>
#include<string.h>
#include<stdint.h>
/* map holding the pixels for a 200x200 pixel imagebuffer, [x][y] 
 * bits are in braille order within each byte
 */
uint8_t map[100][50];
/* takes char storing the on/off positions of 8 points, in braille encoding
 * order, and prints the corresponding braille char
 */

/* global state bit switch */
uint8_t state = 1;
#define STATE_LOOP 0x01
void printbraille(uint8_t points) {
	/* outputs braille characters, the encoding for braile is:
	 * 1 4
	 * 2 5
	 * 3 6
	 * 7 8
	 * with the block being in U+2800 to U+28FF
	 * in utf8 that's encoded
	 * 1110xxxx 10xxxxxx 10xxxxxx
	 * [1110](0010) [10](1000)87 [10]654321
	 */
	printf("%c%c%c", 
		0b11100010, 
		0b10100000 | (points >> 6),
		0b10000000 | (points & 0b00111111));
}
/* prints the map, clears output before aswell*/
void printmap() {
	uint8_t xi = 0;
	uint8_t yi = 0;
	/*terminal escape code to move the cursor to beginning of screen or 1,1*/
	printf("\033[1;1H");
	while (yi < 50) {
		xi = 0;
		while (xi < 100) {
			printbraille(map[xi][yi]);
			xi++;
		}
		printf("\n");
		yi++;
	}
}
/* sets a pixel in the map, converts x,y coordinates to braille order
 * if set is 0, then set it to 0
 * if 1, set to 1
 * if > 1, flip
 * does not throw errors
 */
void setpixel(uint8_t x, uint8_t y,uint8_t set) {
	/* anchor x and y coordinates, eg the index in map*/
	uint8_t ax;
	uint8_t ay;
	/*bitmask in braille order of the byte in mask*/
	uint8_t shift = 0x01;
	/* check if out of bounds, since the arguments are unsigned, don't have
	 * to check below 0, doesn't throw errors
	 */
	if ( (x >= 200 ) || (y >= 200) ) {
		return;
	}
	/* integer division rounds down, therefore dividing will always get the
	 * index
	 */
	ax = x/2;
	ay = y/4;
	/*
	 * maps to braille order based off of x and y offsets
	 * 1 4
	 * 2 5
	 * 3 6
	 * 7 8
	 */
	switch(x % 2) {
		case 0:
			switch (y % 4) {
				case 0:
					shift = shift << 0;
				break;
				case 1:
					shift = shift << 1;
				break;
				case 2:
					shift = shift << 2;
				break;
				case 3:
					shift = shift << 6;
				break;
			}
		break;
		case 1:
			switch (y % 4) {
				case 0:
					shift = shift << 3;
				break;
				case 1:
					shift = shift << 4;
				break;
				case 2:
					shift = shift << 5;
				break;
				case 3:
					shift = shift << 7;
				break;
			}
		break;
	}
	/* sets the bit based on set*/
	switch (set) {
		case 0:
			map[ax][ay] = map[ax][ay] & (~shift);
		break;
		case 1:
			map[ax][ay] = map[ax][ay] | shift;
		break;
		default:
			map[ax][ay] = ((~map[ax][ay]) & shift) | 
					((~shift) & map[ax][ay]);
		break;
	}
	return;
}
/*takes two points and draws a line between them, dot specifies that every nth x
 * point be drawn, if dot != 1, then a dotted line is drawn
 */
void drawline(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, uint8_t dot) {
	/*temporary x and y for switching x1,y1 and x2,y1 if x1>x2*/
	uint8_t tv;
	/*number of x values to translate*/
	uint8_t dx;
	/*number of y values to translate*/
	uint8_t dy;
	/* for whether dy should be positive or negative, 1 pos, 0 neg*/
	uint8_t dir;
	/*drawin index*/
	uint8_t xi = 0;
	/*makes sure that x1 < x2, otherwise switch them arround*/
	if (x1 > x2) {
		tv = x1;
		x1 = x2;
		x2 = tv;
		tv = y1;
		y1 = y2;
		y2 = tv;
	}
	dx = x2 - x1;
	/*find absolute
	 * dir is used since i'm planning to port to a platform that may not
	 * have signed ints.
	 */
	if (y1 > y2) {
		dir = 0;
		dy = y1 - y2;
	} else {
		dir = 1;
		dy = y2 - y1;
	}
	/*
	 *For vertical Lines
	 * Uses xi as a y index now
	 */
	if (x1 == x2) {
		while (xi <= dy) {
			if (dir) {
				setpixel(x1, y1 + xi, 1);
			} else {
				setpixel(x1, y1 - xi, 1);
			}
			xi++;
		}
	}
	/*
	 * loop through x and draws y in line, dots done with modulus.
	 */
	while (xi <= (dx)) {
		if ((xi % dot) == 0) {
			if (dir) {
				setpixel(x1 + xi, y1 + ((dy/dx * xi)),1);
			} else {
				setpixel(x1 + xi, y1 - ((dy/dx * xi)) ,1);
			}
		}
		xi++;
	}
	
	return;	
}
int main (int argc, char * argv[]) {
	/*holds keypress*/
	char input;
	/*zeroing map*/
	memset(map, 0, sizeof(map[0][0]) * 100 * 50);
	printmap();
	while (state & STATE_LOOP) {
		setpixel(10,20,2);
		drawline(0,0,20,20,1);
		drawline(0,40,10,20,1);
		drawline(10,50,0,40,2);
		drawline(10,0,10,200,1);
		drawline(10,100,100,100,4);
		printmap();
		
		/* checks input*/
		input = getc(stdin);
		/*haven't set the terminal to raw mode so will still need
 		 * space
		 */
		if (input == 'q') {
			state = state & (~STATE_LOOP);
		}
	}
	return 0;
}