forked from MiSTer-devel/Main_MiSTer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
brightness.cpp
252 lines (219 loc) · 5.69 KB
/
brightness.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
* brightness.c
*
* Copyright 2016 rricharz
* MiSTer port. Copyright 2018 Sorgelig
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>
#include "brightness.h"
#define MAXCOUNT 10 // maximum number of spi transfer attemps
#define LIDBITMASK 0x04
#define SCREENOFFMASK 0x02
#define PARITYMASK 0x80
#define BRIGHTNESSMASK 0x78
#define SHUTDOWNMASK 0X01
///////////////////////////////////////////////////
static uint32_t spiSpeed;
static int spiFd = -1;
static const uint8_t spiBPW = 8;
static const uint16_t spiDelay = 0;
static int spi_rw(unsigned char *data, int len)
{
struct spi_ioc_transfer spi;
memset (&spi, 0, sizeof (spi));
spi.tx_buf = (unsigned long)data;
spi.rx_buf = (unsigned long)data;
spi.len = len;
spi.delay_usecs = spiDelay;
spi.speed_hz = spiSpeed;
spi.bits_per_word = spiBPW;
return ioctl (spiFd, SPI_IOC_MESSAGE(1), &spi);
}
static int spi_open(int speed, int mode)
{
int fd ;
mode &= 3; // Mode is 0, 1, 2 or 3
if ((fd = open("/dev/spidev1.0", O_RDWR | O_CLOEXEC)) < 0)
{
printf("Unable to open SPI device: %s\n", strerror (errno));
return -1;
}
if (ioctl (fd, SPI_IOC_WR_MODE, &mode) >= 0)
{
if (ioctl (fd, SPI_IOC_WR_BITS_PER_WORD, &spiBPW) >= 0)
{
if (ioctl (fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) >= 0)
{
spiSpeed = speed;
spiFd = fd;
return fd;
}
else
{
printf ("SPI Speed Change failure: %s\n", strerror (errno));
}
}
else
{
printf ("SPI BPW Change failure: %s\n", strerror (errno));
}
}
else
{
printf ("SPI Mode Change failure: %s\n", strerror (errno));
}
close(fd);
fd = -1;
return -1;
}
static void spi_close()
{
if(spiFd >= 0) close(spiFd);
spiFd = -1;
}
///////////////////////////////////////////////////
static int lidbit;
static int screenoffbit;
static int parity;
static int brightness = 6;
static int shutdown;
//////////////////////////////
// Calclate the parity of bits 0 - 6
static int parity7(unsigned char data)
{
int i;
int p = 0;
for (i = 0; i < 7; i++) {
if (data & 1) p = !p;
data = data >> 1;
}
return p;
}
///////////////////////////////
// analyze data byte and set global variables
// return 1 of parity is ok
// Sending: bit 8 = parity of bit 1-7
static int analyze(unsigned char data)
{
lidbit = (data & LIDBITMASK) != 0;
screenoffbit = (data & SCREENOFFMASK) != 0;
parity = (data & PARITYMASK) != 0;
brightness = (data & BRIGHTNESSMASK) >> 3;
shutdown = (data & SHUTDOWNMASK) != 0;
// printf("lid = %d, screen = %d, parity = %d, shutdown = %d, brightness = %d\n", lidbit, screenoffbit, parity, shutdown, brightness);
return (parity7(data) == parity);
}
///////////////
// Calculate data byte using global variables
// Set brightness and status parity bits
// Receiving: bit 8 = brightness parity, bit 3 = status parity
static int calculate()
{
int data = brightness << 3;
if (parity7(brightness)) data += PARITYMASK;
if (shutdown) data += SHUTDOWNMASK;
if (screenoffbit) data += SCREENOFFMASK;
if (parity7(data & 3)) data += LIDBITMASK; // parity ofthe two state bits
return data;
}
void setBrightness(int cmd, int val)
{
unsigned char data, new_data;
int count, ok;
if (spi_open(9600, 0) < 0)
{
printf("Cannot initialize spi driver\n");
return;
}
// send 0xFF and receive current status of pi-top-hub
count = 0;
data = 0xff;
printf("Brighntess sending: 0x%X\n", data);
do
{
data = 0xff;
ok = spi_rw(&data, 1);
if (ok) ok = analyze(data);
}
while ((!ok) && (count++ < MAXCOUNT));
// printf("count = %d\n", count);
if (ok)
{
printf("Brighntess receiving: 0x%X - ", data);
printf("Current brightness = %d, ", brightness);
//force to 0 as set to 1 if rebooted while in screenbitoff=0
//the state is stored on pi-top-hub, but isn't reinitialised on reboot
screenoffbit=0;
if(cmd == BRIGHTNESS_UP)
{
brightness++;
}
else if (cmd == BRIGHTNESS_DOWN)
{
brightness--;
}
else if (cmd == BRIGHTNESS_SET)
{
if(!val) screenoffbit=1;
else screenoffbit = 0;
brightness = val;
}
if (brightness < 1) brightness = 1;
if (brightness > 10) brightness = 10;
printf("Requested brightness = %d, ", brightness);
printf("Requested off = %d\n", screenoffbit);
// calculate data to send
shutdown = 0;
new_data = calculate();
// send new data until accepted
count = 0;
data = new_data;
printf("Brighntess sending: 0x%X\n", data);
do
{
data = new_data;
ok = spi_rw(&data, 1);
if (ok) ok = (data & BRIGHTNESSMASK) == (new_data & BRIGHTNESSMASK);
}
while ((!ok) && (count++ < MAXCOUNT));
// printf("count = %d\n", count);
if (ok)
{
printf("Brighntess receiving: 0x%X - ", data);
printf("New brightness = %d\n", brightness);
}
}
else printf("Reading current brightness not successful\n");
spi_close();
}
int getBrightness()
{
if (screenoffbit) return 0;
return brightness;
}