This repository has been archived by the owner on Jul 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
u8g2_hal_rpi.cpp
135 lines (125 loc) · 3.99 KB
/
u8g2_hal_rpi.cpp
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
//
// Created by raffy on 6/27/18.
//
#include <wiringPi.h>
#include <iostream>
#include "spi.h"
#include "u8g2_hal_rpi.h"
#define U8G2_HAL_SPI_SPEED 500000
#define U8G2_HAL_SPI_MODE SPI_DEV_MODE_0
#define U8G2_HAL_SPI_CHANNEL 0
using namespace std;
static u8g2_rpi_hal_t u8g2_rpi_hal;
void u8g2_rpi_hal_init(u8g2_rpi_hal_t param) {
u8g2_rpi_hal = param;
}
uint8_t cb_byte_spi_hw(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
switch (msg) {
case U8X8_MSG_BYTE_SET_DC:
u8x8_gpio_SetDC(u8x8, arg_int);
break;
case U8X8_MSG_BYTE_SEND: {
if (spi_write(U8G2_HAL_SPI_CHANNEL, (uint8_t *) arg_ptr, arg_int) < 1)
cerr << "Unable to send data" << endl;
break;
}
case U8X8_MSG_BYTE_INIT: {
//disable chip-select
u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);
//initialize spi device
int fd = spi_setup(U8G2_HAL_SPI_CHANNEL, U8G2_HAL_SPI_SPEED, U8G2_HAL_SPI_MODE);
if (fd < 0) {
cerr << "Problem initializing SPI on RPI" << endl;
return 0;
}
//IMPORTANT: Make sure we reset the pin modes
// to activate the SPI hardware features!!!!
pinModeAlt(u8g2_rpi_hal.mosi, 0b100);
pinModeAlt(u8g2_rpi_hal.clk, 0b100);
pinModeAlt(u8g2_rpi_hal.cs, 0b100);
break;
}
case U8X8_MSG_BYTE_START_TRANSFER: {
u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_enable_level);
u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->post_chip_enable_wait_ns, nullptr);
break;
}
case U8X8_MSG_BYTE_END_TRANSFER: {
u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->pre_chip_disable_wait_ns, nullptr);
u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);
break;
}
default:
return 0;
}
return 1;
}
uint8_t cb_gpio_delay_rpi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, U8X8_UNUSED void *arg_ptr) {
switch (msg) {
case U8X8_MSG_GPIO_AND_DELAY_INIT: {
pinMode(u8g2_rpi_hal.mosi, OUTPUT);
pinMode(u8g2_rpi_hal.clk, OUTPUT);
pinMode(u8g2_rpi_hal.cs, OUTPUT);
pinMode(u8g2_rpi_hal.dc, OUTPUT);
pinMode(u8g2_rpi_hal.reset, OUTPUT);
break;
}
case U8X8_MSG_DELAY_MILLI: {
delay(arg_int);
break;
}
case U8X8_MSG_DELAY_NANO: {
//this is important. Removing this will cause garbage data to be displayed.
delayMicroseconds(arg_int == 0 ? 0 : 1);
break;
}
case U8X8_MSG_GPIO_SPI_CLOCK: {
digitalWrite(u8g2_rpi_hal.clk, arg_int);
break;
}
case U8X8_MSG_GPIO_SPI_DATA: {
digitalWrite(u8g2_rpi_hal.mosi, arg_int);
break;
}
case U8X8_MSG_GPIO_CS: {
digitalWrite(u8g2_rpi_hal.cs, arg_int);
break;
}
case U8X8_MSG_GPIO_DC: {
digitalWrite(u8g2_rpi_hal.dc, arg_int);
break;
}
case U8X8_MSG_GPIO_RESET: {
digitalWrite(u8g2_rpi_hal.reset, arg_int);
break;
}
default: {
return 0;
}
}
return 1;
}
uint8_t cb_byte_i2c_hw(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
switch (msg) {
case U8X8_MSG_BYTE_INIT: {
//disable chip-select
//TODO: Add implementation
break;
}
case U8X8_MSG_BYTE_SEND: {
//TODO: Add implementation
break;
}
case U8X8_MSG_BYTE_START_TRANSFER: {
//TODO: Add implementation
break;
}
case U8X8_MSG_BYTE_END_TRANSFER: {
//TODO: Add implementation
break;
}
default:
return 0;
}
return 1;
}