-
Notifications
You must be signed in to change notification settings - Fork 0
/
paperlike.c
231 lines (172 loc) · 7.27 KB
/
paperlike.c
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
#include <stdio.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/i2c/IOI2CInterface.h>
#include <ApplicationServices/ApplicationServices.h>
#include <IOKit/graphics/IOGraphicsLib.h>
#define VERBOSE true
#define DASUNG_VENDOR_ID 4707
typedef struct SignalData {
union _SignalDataTypeHead {
unsigned char value;
struct _Head {
unsigned char uSignalDataType : 4;
unsigned char Reserved : 4;
} Head;
} SignalDataTypeHead;
union _SignalDataType {
unsigned char value;
struct _Beat {
unsigned char uBeatSign : 1;
unsigned char uBeatSignOff : 1;
unsigned char Reserved : 6;
} Beat;
struct _ScreenSaver {
unsigned char ucScreenSaver : 1;
unsigned char Reserved : 7;
} ScreenSaver;
struct _TurnOffMonitor {
unsigned char ucTurnOffMonitor : 1;
unsigned char Reserved : 7;
} TurnOffMonitor;
struct _CleanMonitor {
unsigned char uCleanMonitorSoft : 1;
unsigned char uCleanMonitorHard : 1;
unsigned char Reserved : 6;
} CleanMonitor;
} SignalDataType;
}__attribute__((packed)) SignalData;
int getDSFramebuffer() {
if (VERBOSE) printf("--- getDSFramebuffer\n");
io_iterator_t iter;
io_service_t serv, foundService = 0;
kern_return_t err = IOServiceGetMatchingServices(kIOMasterPortDefault, IOServiceMatching(IOFRAMEBUFFER_CONFORMSTO), &iter);
if (err != KERN_SUCCESS) return 0;
while((serv = IOIteratorNext(iter)) != MACH_PORT_NULL) {
io_name_t name;
IORegistryEntryGetName(serv, name);
CFDictionaryRef info = IODisplayCreateInfoDictionary(serv, kIODisplayOnlyPreferredName);
CFIndex vendorID = 0;
CFNumberRef vendorIDRef;
CFDictionaryGetValueIfPresent(info, CFSTR(kDisplayVendorID), (const void**)&vendorIDRef) &&
CFNumberGetValue(vendorIDRef, kCFNumberCFIndexType, &vendorID);
CFRelease(info);
if (vendorID == DASUNG_VENDOR_ID) { foundService = serv; break; }
}
if (!foundService) printf("Can't find Dasung framebuffer\n");
if (VERBOSE && foundService) printf("Dasung framebuffer: %i\n", foundService);
return foundService;
}
int sendDDCWrite(int framebuffer, unsigned char data1, unsigned char data2) {
if (VERBOSE) printf("--- sendDDCWrite\n");
kern_return_t result;
IOI2CConnectRef connect;
IOItemCount busCount;
result = IOFBGetI2CInterfaceCount(framebuffer, &busCount);
if (result != KERN_SUCCESS) { printf("No buses"); return 0; }
for(IOOptionBits bus = 0; bus < busCount; bus++) {
io_service_t interface;
result = IOFBCopyI2CInterfaceForBus(framebuffer, bus, &interface);
if (result != KERN_SUCCESS) { printf("no copy"); return 0; }
result = IOI2CInterfaceOpen(interface, kNilOptions, &connect);
if (result != KERN_SUCCESS) { printf("Not opened %i", result); return 0; }
}
if (VERBOSE) printf("Opened connection\n");
IOI2CRequest request;
UInt8 data[128];
memset((void *)&data, 0, 128);
request.commFlags = 0;
request.sendAddress = 0x6E;
request.sendTransactionType = kIOI2CSimpleTransactionType;
request.sendBuffer= (vm_address_t) &data[0];
request.sendBytes = 7;
request.replyTransactionType = kIOI2CNoTransactionType;
request.replyBytes = 0;
data[0] = 0x51; // magic header
data[1] = 0x84; // 0x80 + buffer size
// buffer start
data[2] = 0x03; // DDCCI_COMMAND_WRITE
data[3] = 0x08; // VCPCODE
data[4] = data1;
data[5] = data2;
// buffer end
data[6] = 0x6E ^ data[0] ^ data[1] ^ data[2] ^ data[3] ^ data[4] ^ data[5];
if (VERBOSE) {
printf("DDCCI Command (%i):", request.sendBytes);
for(int i=0; i<request.sendBytes; i++) { printf(" %02x", data[i]); }
printf("\n");
}
result = IOI2CSendRequest(connect, kNilOptions, &request);
if (result != KERN_SUCCESS) { printf("Send error %i %i\n", request.result, result); }
IOI2CInterfaceClose(connect, kNilOptions );
return 0;
};
int sendClean(int framebuffer) {
if (VERBOSE) printf("--- sendClean\n");
SignalData SD;
memset((void *)&SD, 0, sizeof(SignalData));
SD.SignalDataTypeHead.Head.uSignalDataType = 6; // DS_PACKET_TYPE_CLEANMONITOR;
SD.SignalDataType.CleanMonitor.uCleanMonitorHard = 1;
// SD.SignalDataType.CleanMonitor.uCleanMonitorSoft = 1;
if (VERBOSE) printf("Dasung SignalData (2): %02x %02x\n", SD.SignalDataTypeHead.value, SD.SignalDataType.value);
return sendDDCWrite(framebuffer, SD.SignalDataTypeHead.value, SD.SignalDataType.value);
}
int sendOff(int framebuffer) {
if (VERBOSE) printf("--- sendOff\n");
SignalData SD;
memset((void *)&SD, 0, sizeof(SignalData));
SD.SignalDataTypeHead.Head.uSignalDataType = 3; // DS_PACKET_TYPE_TURNOFFMONITOR;
SD.SignalDataType.TurnOffMonitor.ucTurnOffMonitor = 1;
if (VERBOSE) printf("Dasung SignalData (2): %02x %02x\n", SD.SignalDataTypeHead.value, SD.SignalDataType.value);
return sendDDCWrite(framebuffer, SD.SignalDataTypeHead.value, SD.SignalDataType.value);
}
int sendScreenSaver(int framebuffer) {
if (VERBOSE) printf("--- sendScreenSaver\n");
SignalData SD;
memset((void *)&SD, 0, sizeof(SignalData));
SD.SignalDataTypeHead.Head.uSignalDataType = 2; // DS_PACKET_TYPE_SCREENSAVER;
SD.SignalDataType.ScreenSaver.ucScreenSaver = 1;
if (VERBOSE) printf("Dasung SignalData (2): %02x %02x\n", SD.SignalDataTypeHead.value, SD.SignalDataType.value);
return sendDDCWrite(framebuffer, SD.SignalDataTypeHead.value, SD.SignalDataType.value);
}
int sendScreenSaverOff(int framebuffer) {
if (VERBOSE) printf("--- sendScreenSaverOff\n");
SignalData SD;
memset((void *)&SD, 0, sizeof(SignalData));
SD.SignalDataTypeHead.Head.uSignalDataType = 2; // DS_PACKET_TYPE_SCREENSAVER;
SD.SignalDataType.ScreenSaver.ucScreenSaver = 0;
if (VERBOSE) printf("Dasung SignalData (2): %02x %02x\n", SD.SignalDataTypeHead.value, SD.SignalDataType.value);
return sendDDCWrite(framebuffer, SD.SignalDataTypeHead.value, SD.SignalDataType.value);
}
int sendBeat(int framebuffer) {
if (VERBOSE) printf("--- sendBeat\n");
SignalData SD;
memset((void *)&SD, 0, sizeof(SignalData));
SD.SignalDataTypeHead.Head.uSignalDataType = 1; // DS_PACKET_TYPE_BEAT;
SD.SignalDataType.Beat.uBeatSign = 1;
SD.SignalDataType.Beat.uBeatSignOff = 0;
if (VERBOSE) printf("Dasung SignalData (2): %02x %02x\n", SD.SignalDataTypeHead.value, SD.SignalDataType.value);
return sendDDCWrite(framebuffer, SD.SignalDataTypeHead.value, SD.SignalDataType.value);
}
int main(int argc, char *argv[]) {
if (argc == 2 && strcmp("beat", argv[1]) == 0) {
int framebuffer = getDSFramebuffer();
if (framebuffer) sendBeat(framebuffer);
} else if (argc == 2 && strcmp("screensaver", argv[1]) == 0) {
int framebuffer = getDSFramebuffer();
if (framebuffer) sendScreenSaver(framebuffer);
} else if (argc == 2 && strcmp("screensaveroff", argv[1]) == 0) {
int framebuffer = getDSFramebuffer();
if (framebuffer) sendScreenSaverOff(framebuffer);
} else if (argc == 2 && strcmp("off", argv[1]) == 0) {
int framebuffer = getDSFramebuffer();
if (framebuffer) sendOff(framebuffer);
} else if (argc == 2 && strcmp("clean", argv[1]) == 0) {
int framebuffer = getDSFramebuffer();
if (framebuffer) sendClean(framebuffer);
} else {
printf("\nPaperlike is alternative command line tool for Dasung Paperlike eInk display ");
printf("for MacOS (https://github.com/dzirtusss/paperlike-mac). ");
printf("Usage: 'paperlike clean'\n\n");
}
return 0;
}