-
Notifications
You must be signed in to change notification settings - Fork 0
/
painter.c
85 lines (71 loc) · 2.59 KB
/
painter.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
#include "helpers.h"
// Given a x, y coordinate, set that pixel ON on the board
void pixon(const BYTE x, const BYTE y)
{
if (x < SCREEN_X_MIN || x > SCREEN_X_MAX || y < SCREEN_Y_MIN || y > SCREEN_Y_MAX)
return;
// Determine which row the bit is (there are 8 bits in each row so we divide by 8)
BYTE row = y >> 3;
// Multiply row by 128 (<< 7) to push it in the correct place Y position in the buffer (now at (0,Y)). Add X to push it to the correct column (X,Y)
int buf_coord = (row << 7) + x;
// We want to know which bit of that int we want to modify, so we get the last three bits (0-7) which tell us the bit offset aka modulo 8
row = y & 7;
// We push a one with that many steps forward in that int in the buffer and or it to make it 1 at the specific bit
display_buffer[buf_coord] |= 1 << row;
}
// Given a x, y coordinate, set that pixel OFF on the board
void pixoff(const BYTE x, const BYTE y)
{
if (x < SCREEN_X_MIN || x > SCREEN_X_MAX || y < SCREEN_Y_MIN || y > SCREEN_Y_MAX)
return;
// Determine which row the bit is (there are 8 bits in each row so we divide by 8)
BYTE row = y >> 3;
// Multiply row by 128 (<< 7) to push it in the correct place Y position in the buffer (now at (0,Y)). Add X to push it to the correct column (X,Y)
int buf_coord = (row << 7) + x;
// We want to know which bit of that int we want to modify, so we get the last three bits (0-7) which tell us the bit offset aka modulo 8
row = y & 7;
// We push a one with that many steps forward in that int in the buffer and and it with the inverse of that int to make it 0 at the specific bit
display_buffer[buf_coord] &= ~(1 << row);
}
/**
* Draws a line between the points (x0, y0) and (x1, y1) using Bresenham's line algorithm.
*/
void draw_line(int x0, int y0, const int x1, const int y1)
{
int dx = x1 - x0,
dy = y1 - y0,
sx = x0 < x1 ? 1 : -1,
sy = y0 < y1 ? 1 : -1;
dx = dx > 0 ? dx : -dx;
dy = dy > 0 ? -dy : dy;
int error = dx + dy,
e2;
while (1)
{
pixon(x0, y0);
if (x0 == x1 && y0 == y1)
break;
e2 = 2 * error;
if (e2 >= dy)
{
if (x0 == x1)
break;
error = error + dy;
x0 = x0 + sx;
}
if (e2 <= dx)
{
if (y0 == y1)
break;
error = error + dx;
y0 = y0 + sy;
}
}
}
// Clears the buffer by making all its values to 0
void clear_buf()
{
int i;
for (i = 0; i < OLED_BUF_SIZE; i++)
display_buffer[i] = 0;
}