Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathdisplay_example.cpp
More file actions
Latest commit
145 lines (123 loc) · 3.52 KB
/
Copy pathdisplay_example.cpp
File metadata and controls
145 lines (123 loc) · 3.52 KB
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
// SPDX-License-Identifier: MIT
/**
Example code to demonstrate the usage of the CX2100 power supply display
Some ASCII control characters are used to control the display behavior:
'\021': switch the backlight on
'\023': switch the backlight off
'\f': clear screen and move cursor to the first character of the first row
'\b': move cursor to previous character
'\n': move cursor to start of next row
'\r': move cursor to first character in row
'\t': move cursor to next character
Copyright (C) 2014 - 2018 Beckhoff Automation GmbH & Co. KG
Author: Patrick Bruenn <p.bruenn@beckhoff.com>
*/
#include<stdio.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
#defineDEVICE_NAME"/dev/cx_display"
/** for delay and multithreading only */
#include<chrono>
#include<thread>
#include<vector>
voidbacklight_example()
{
staticconstchar off[] = "\023\fbacklight is off";
staticconstchar on[] = "\021backlight is on\n";
constint fd = open(DEVICE_NAME, O_WRONLY);
if (-1 == fd) {
perror(NULL);
return;
}
write(fd, off, sizeof(off));
std::this_thread::sleep_for(std::chrono::seconds(2));
write(fd, on, sizeof(on));
std::this_thread::sleep_for(std::chrono::seconds(2));
close(fd);
}
voidsimple_example()
{
constint fd = open(DEVICE_NAME, O_WRONLY);
if (-1 == fd) {
perror(NULL);
return;
}
staticconstchar first[] = "\fHello world!";
write(fd, first, sizeof(first));
std::this_thread::sleep_for(std::chrono::seconds(2));
staticconstchar second[] = "\fI am alive!\nand duallined!";
write(fd, second, sizeof(second));
std::this_thread::sleep_for(std::chrono::seconds(2));
close(fd);
}
voidcomplex_example()
{
constint fd = open(DEVICE_NAME, O_WRONLY);
if (-1 == fd) {
perror(NULL);
return;
}
staticconstchar clearscreen[] = "\fHello there?";
write(fd, clearscreen, sizeof(clearscreen));
std::this_thread::sleep_for(std::chrono::seconds(2));
staticconstchar overwrite_char[] = "\b\b\b\b\b\bworld!";
write(fd, overwrite_char, sizeof(overwrite_char));
std::this_thread::sleep_for(std::chrono::seconds(2));
staticconstchar append[] = "\nI am alive?";
write(fd, append, sizeof(append));
std::this_thread::sleep_for(std::chrono::seconds(2));
staticconstchar overwrite_line[] = "\rIt's\t\t\t\t\t\t!";
write(fd, overwrite_line, sizeof(overwrite_line));
std::this_thread::sleep_for(std::chrono::seconds(2));
close(fd);
}
voidRunAtPos(size_t pos)
{
constint fd = open(DEVICE_NAME, O_WRONLY);
if (-1 == fd) {
perror(NULL);
return;
}
/** move cursor to our position */
for (size_t i = 0; i <= pos; ++i) {
staticconstchar tab = '\t';
write(fd, &tab, 1);
}
/** print some characters */
for (unsignedchar c = '~'; c != ('/' + pos % 10); c--) {
constunsignedchar replace_char[2] = { '\b', c };
write(fd, replace_char, sizeof(replace_char));
/** sleep a little to let the other threads work, too */
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
close(fd);
}
voidmultithreaded_example()
{
std::vector < std::thread > threads(32);
for (size_t i = 0; i < threads.size(); ++i) {
threads[i] = std::thread(&RunAtPos, i);
}
for (auto & t:threads) {
t.join();
}
}
intmain(int argc, char *argv[])
{
/**
* use ASCII code DC1(XON) and DC3(XOFF) to control the backlight
*/
backlight_example();
/**
* Only one thread accesses the display
*/
simple_example();
complex_example();
/**
* Multiple threads writing simultanously to different areas of the display
*/
multithreaded_example();
return0;
}