summaryrefslogtreecommitdiff
path: root/src/driver/sharp96.cc
blob: 1e9a2ec1456929ac8c906ff60d62cbe701872dfa (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
#include "driver/sharp96.h"
#include "driver/spi_b.h"
#include "driver/gpio.h"

#ifndef SHARP96_POWER_PIN
#error makeflag sharp96_power_pin required
#endif

#ifndef SHARP96_EN_PIN
#error makeflag sharp96_en_pin required
#endif

#ifndef SHARP96_CS_PIN
#error makeflag sharp96_cs_pin required
#endif

#define CMD_WRITE_LINE 0x80
#define CMD_TOGGLE_VCOM 0x40
#define CMD_CLEAR_SCREEN 0x20

void Sharp96::setup()
{
	gpio.output(SHARP96_POWER_PIN);
	gpio.output(SHARP96_EN_PIN);
	gpio.output(SHARP96_CS_PIN);
}

void Sharp96::powerOn()
{
	gpio.write(SHARP96_POWER_PIN, 1);
	gpio.write(SHARP96_EN_PIN, 1);
}

void Sharp96::powerOff()
{
	gpio.write(SHARP96_EN_PIN, 0);
	gpio.write(SHARP96_POWER_PIN, 0);
}

unsigned char Sharp96::swap_bits(unsigned char byte)
{
	byte = (byte & 0xF0) >> 4 | (byte & 0x0F) << 4;
	byte = (byte & 0xCC) >> 2 | (byte & 0x33) << 2;
	byte = (byte & 0xAA) >> 1 | (byte & 0x55) << 1;
	return byte;
}

void Sharp96::clear()
{
	gpio.write(SHARP96_CS_PIN, 1);
	txbuf[0] = CMD_CLEAR_SCREEN | (CMD_TOGGLE_VCOM * vcom);
	txbuf[1] = 0;
	spi.xmit(2, txbuf, 0, txbuf);
	gpio.write(SHARP96_CS_PIN, 0);
}

void Sharp96::writeLine(unsigned char line_no, unsigned char* data)
{
	txbuf[0] = CMD_WRITE_LINE | (CMD_WRITE_LINE * vcom);
	txbuf[1] = swap_bits(line_no + 1);
	gpio.write(SHARP96_CS_PIN, 1);
	spi.xmit(2, txbuf, 0, txbuf);
	spi.xmit(12, data, 0, txbuf);
	txbuf[0] = 0;
	txbuf[1] = 0;
	spi.xmit(2, txbuf, 0, txbuf);
	gpio.write(SHARP96_CS_PIN, 0);
}

void Sharp96::toggleVCOM()
{
	vcom = !vcom;
	txbuf[0] = CMD_TOGGLE_VCOM * vcom;
	txbuf[1] = 0;
	gpio.write(SHARP96_CS_PIN, 1);
	spi.xmit(2, txbuf, 0, txbuf);
	gpio.write(SHARP96_CS_PIN, 0);
}

Sharp96 sharp96;