/
native.go
161 lines (135 loc) · 3.47 KB
/
native.go
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
// raspui project raspui.go
package raspui
/*
#cgo LDFLAGS: -lbcm2835
#include <bcm2835.h>
#include "RAIO8870.h"
#include "tft.h"
uint8_t getTouchState() {
RAIO_gettouch();
if(my_touch.state == pressed || my_touch.state == down) {
return 1;
}
return 0;
}
uint32_t getTouchX() {
return my_touch.touch_x;
}
uint32_t getTouchY() {
return my_touch.touch_y;
}
*/
import "C"
import (
"bytes"
"github.com/paulrosania/go-charset/charset"
_ "github.com/paulrosania/go-charset/data"
"fmt"
"image"
"unsafe"
)
const (
RED = int(C.COLOR_RED)
BLUE = int(C.COLOR_BLUE)
GREEN = int(C.COLOR_GREEN)
BLACK = int(C.COLOR_BLACK)
WHITE = int(C.COLOR_WHITE)
CYAN = int(C.COLOR_CYAN)
YELLOW = int(C.COLOR_YELLOW)
MAGENTA = int(C.COLOR_MAGENTA)
DARKGREEN = int(C.COLOR_DARK_GREEN)
DARKBLUE = 0x01
LIGHTBLUE = 0xB7
GRAY = 0x92
)
const (
PRESSED = 1
NOTOUCH = 3
)
func getTouchState() int {
if int(C.getTouchState()) == 1 {
return PRESSED
}
return NOTOUCH
}
func getTouchXY() *image.Point {
x := int(C.getTouchX())
y := int(C.getTouchY())
return &image.Point{x, y}
}
func clearDisplay(color int) {
C.Text_Background_Color (C.uint8_t(color))
C.RAIO_clear_screen()
}
func drawLine(x1, y1, x2, y2, color int) {
C.Text_Foreground_Color (C.uint8_t(color))
C.Set_Geometric_Coordinate(C.uint16_t(x1), C.uint16_t(y1), C.uint16_t(x2), C.uint16_t(y2))
C.RAIO_StartDrawing(C.LINE)
}
func drawBox(x1, y1, x2, y2, color int) {
C.Text_Foreground_Color (C.uint8_t(color))
C.Set_Geometric_Coordinate(C.uint16_t(x1), C.uint16_t(y1), C.uint16_t(x2), C.uint16_t(y2))
C.RAIO_StartDrawing(C.SQUARE_NONFILL)
}
func drawFilledBox(x1, y1, x2, y2, color int) {
C.Text_Foreground_Color (C.uint8_t(color))
C.Set_Geometric_Coordinate(C.uint16_t(x1), C.uint16_t(y1), C.uint16_t(x2), C.uint16_t(y2))
C.RAIO_StartDrawing(C.SQUARE_FILL)
}
func drawCircle(x, y, rad, color int) {
C.Text_Foreground_Color (C.uint8_t(color))
C.Set_Geometric_Coordinate_circle(C.uint16_t(x), C.uint16_t(y), C.uint8_t(rad))
C.RAIO_StartDrawing(C.CIRCLE_NONFILL)
}
func drawFilledCircle(x, y, rad, color int) {
C.Text_Foreground_Color (C.uint8_t(color))
C.Set_Geometric_Coordinate_circle(C.uint16_t(x), C.uint16_t(y), C.uint8_t(rad))
C.RAIO_StartDrawing(C.CIRCLE_FILL)
}
func drawText(x, y, size int, text string, fg, bg int) error {
//RAIO_print_text( (DISPLAY_WIDTH/4)-45, (DISPLAY_HEIGHT/4)*3-20, "Button 3 is", COLOR_GREEN, COLOR_BLACK );
buf := new(bytes.Buffer)
w, err := charset.NewWriter("latin1", buf)
if err != nil {
return err
}
fmt.Fprintf(w, text)
w.Close()
b := []byte(buf.String())
b = append(b, 0)
ctext := (*C.uchar)(unsafe.Pointer(&b[0]))
C.RAIO_SetFontSizeFactor(C.uint8_t(size))
C.RAIO_print_text(C.uint16_t(x), C.uint16_t(y), ctext, C.uint8_t(bg), C.uint8_t(fg))
return nil
}
func initDisplay() {
C.bcm2835_init()
C.TFT_init_board()
C.TFT_hard_reset()
C.RAIO_init()
C.RAIO_SetBacklightPWMValue(255)
}
func closeGpio() {
C.TFT_hard_reset()
C.RAIO_SetBacklightPWMValue(0)
C.bcm2835_close()
}
func setBacklightPwmValue(brightness int) {
C.RAIO_SetBacklightPWMValue(C.uint8_t(brightness))
}
func SetGpioOutput(pin int) {
C.bcm2835_gpio_fsel(C.uint8_t(pin), C.BCM2835_GPIO_FSEL_OUTP)
}
func SetGpioInput(pin int) {
C.bcm2835_gpio_fsel(C.uint8_t(pin), C.BCM2835_GPIO_FSEL_INPT)
}
func SetGpio(pin, level int) {
if level == 0 {
C.bcm2835_gpio_clr(C.uint8_t(pin))
} else if level == 1 {
C.bcm2835_gpio_set(C.uint8_t(pin))
}
}
func GetGpio(pin int) int {
return int(C.bcm2835_gpio_lev(C.uint8_t(pin)))
}