Summary Files Commits Branches @commit @diff @stats
path: root / Year 3 / Internet of things / goam / main / display.c
blob: a1f3cc25b0ab4c698816f17a1ce53fdcf738956a (plain) (blame)
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
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include <string.h>

#include "driver/i2c.h"
#include "display.h"

unsigned int min(unsigned int a, unsigned int b) {
	return (a < b) ? a : b;
}

static const char* TAG = "display";

#define LAST_PAGE (CONFIG_DISPLAY_PAGES - 1)
#define LAST_COL  (CONFIG_DISPLAY_COLUMNS - 1)

/*
 * I2C settings
 */

#define MASTER_FREQUENCY CONFIG_I2C_MASTER_FREQUENCY

static void i2c_init() {
	i2c_config_t conf = {
		.mode = I2C_MODE_MASTER,
		.sda_io_num = CONFIG_DISPLAY_SDA_PIN,
		.scl_io_num = CONFIG_DISPLAY_SCL_PIN,
		.sda_pullup_en = GPIO_PULLUP_ENABLE,
		.scl_pullup_en = GPIO_PULLUP_ENABLE,
		.master.clk_speed = 400000, // 400kHz; has to be at least 385kHz
	};
	ESP_ERROR_CHECK(i2c_param_config(I2C_NUM_0, &conf));
	ESP_ERROR_CHECK(i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0));
	/* i2c_set_start_timing(I2C_NUM_0, 10, 10); */
}

/*
 * I2C commands
 */

enum DATA_TYPE {
	CMD_STREAM  = 0x00,
	DATA_STREAM = 0x40,
};

static void send(enum DATA_TYPE type, const unsigned char* data, size_t size) {
	if (size == 0) return;

	i2c_cmd_handle_t i2c_handle = i2c_cmd_link_create();
	ESP_ERROR_CHECK(i2c_master_start(i2c_handle));

	// Slave address
	ESP_ERROR_CHECK(i2c_master_write_byte(i2c_handle, 0b01111000, true));
	ESP_ERROR_CHECK(i2c_master_write_byte(i2c_handle, type, true));
	// Control/data bytes
	ESP_ERROR_CHECK(i2c_master_write(i2c_handle, data, size, true));

	// "The write mode will be finished when a stop condition is applied"
	ESP_ERROR_CHECK(i2c_master_stop(i2c_handle));
	ESP_ERROR_CHECK(i2c_master_cmd_begin(I2C_NUM_0, i2c_handle, 1000));
	i2c_cmd_link_delete(i2c_handle);
}

/*
 * Static interface
 */

static unsigned char start_page = 0;
#define PAGE_INC ((start_page + 1) % (LAST_PAGE + 1))

static unsigned char start_col = 0;

static void display_output_borders(unsigned char column_start, unsigned char column_end, unsigned char page_start, unsigned char page_end) {
	start_col    = column_start;
	start_page = page_start;
	unsigned char command[] = { 0x21, column_start, column_end, 0x22, page_start, page_end };
	send(CMD_STREAM, command, sizeof(command));
}

static void display_output_beginning() {
	display_output_borders(0, LAST_COL, 0, LAST_PAGE);
}

/*
 * Public interface
 */

static const unsigned char startup[] = {
	0x8D, 0x14, 0xAF, // pump charge and enable
	0x20, 0b00, // vertical address mode
};

void display_init() {
	i2c_init();
	send(CMD_STREAM, startup, sizeof(startup));
	display_output_beginning();
	display_println(LINE_CLEAR LINE_CLEAR LINE_CLEAR LINE_CLEAR);
	ESP_LOGI(TAG, "Initialized display");
}

void display_invertion(enum Inversion inv_byte) {
	send(CMD_STREAM, (const unsigned char*)&inv_byte, 1);
}

// This is better than an include, because we can define our font
// with compile flags
extern const unsigned bitpixel_char_height;
extern const unsigned bitpixel_char_width;
extern const uint8_t bitpixel_chars[256][16];

static unsigned char out_page_row[LAST_COL + 1];

void display_println(const char* str) {
	do {
		int column, character = '\0';
		for (int linepage = 0; linepage <= 1; linepage++) {
			for (column = 0; column <= LAST_COL; column++) {
				character = (int)str[column / bitpixel_char_width];
				if (character == '\0') {
					break;
				}
				// The font currently only has capital letters, this converts lowercase to capital
				if ('a' <= character && character <= 'z') {
					character -= 'a' - 'A';
				}
				out_page_row[column] = bitpixel_chars[character]
													 [(column * 2 + linepage) % bitpixel_char_height];
			}

			send(DATA_STREAM, out_page_row, column);

			if (character == '\0') {
				display_output_borders(start_col, LAST_COL, PAGE_INC, LAST_PAGE);
			}

			start_page = PAGE_INC;
		}
		str += column / bitpixel_char_width + (character == '\0');
	} while (*str != '\0' && start_page > 0);
}


void display_printat(const char* str, unsigned char line, unsigned char column) {
	if (line < 1) line = 1;
	if (line > 4) line = 4;
	if (column < 1) column = 1;
	if (column > 16) column = 16;
	unsigned char endColumn = min(LAST_COL, (column - 1 + strlen(str)) * bitpixel_char_width - 1);
	display_output_borders((column - 1) * bitpixel_char_width, endColumn, (line - 1) * 2, LAST_PAGE);
	display_println(str);
	display_output_beginning();
}

void display_printon(const char* str, unsigned char line) {
	display_printat(str, line, 1);
}