Popular New Releases in Arduino
Tasmota
Tasmota v11.1.0 Ostara
Arduino
Release 3.0.2
Arduino
Release 1.8.19 Security hotfix release
johnny-five
Marlin
Marlin 2.0.9.1
Popular Libraries in Arduino
by arendst c
17398 GPL-3.0
Alternative firmware for ESP8266 with easy configuration using webUI, OTA updates, automation using timers or rules, expandability and entirely local control over MQTT, HTTP, Serial or KNX. Full documentation at
by esp8266 c++
13604 LGPL-2.1
ESP8266 core for Arduino
by arduino java
12500 NOASSERTION
open-source electronics platform
by rwaldron javascript
12386 NOASSERTION
JavaScript Robotics and IoT programming framework, developed at Bocoup.
by qmk c
12112 NOASSERTION
Open-source keyboard firmware for Atmel AVR and Arm USB families
by MarlinFirmware c++
11790 GPL-3.0
Marlin is an optimized firmware for RepRap 3D printers based on the Arduino platform. | Many commercial 3D printers come with Marlin installed. Check with your vendor if you need source code for your specific machine.
by SpacehuhnTech c
8891 NOASSERTION
Affordable WiFi hacking platform for testing and learning
by espressif c
8437 LGPL-2.1
Arduino core for the ESP32
by ExistentialAudio c
7803 GPL-3.0
BlackHole is a modern macOS virtual audio driver that allows applications to pass audio to other applications with zero additional latency.
Trending New libraries in Arduino
by project-chip c++
3565 Apache-2.0
Matter (formerly Project CHIP) is creating more connections between more objects, simplifying development for manufacturers and increasing compatibility for consumers, guided by the Connectivity Standards Alliance (formerly Zigbee Alliance).
by Serial-Studio c++
2320 NOASSERTION
Multi-purpose serial data visualization & processing program
by isl-org java
2104 MIT
OpenBot leverages smartphones as brains for low-cost robots. We have designed a small electric vehicle that costs about $50 and serves as a robot body. Our software stack for Android smartphones supports advanced robotics workloads such as person following and real-time autonomous navigation.
by intel-isl java
1827 MIT
OpenBot leverages smartphones as brains for low-cost robots. We have designed a small electric vehicle that costs about $50 and serves as a robot body. Our software stack for Android smartphones supports advanced robotics workloads such as person following and real-time autonomous navigation.
by boston-dynamics python
1812 NOASSERTION
Spot SDK repo
by atc1441 c
1660
Custom firmware for the Xiaomi Thermometer LYWSD03MMC and Telink Flasher via USB to Serial converter
by mike4192 c++
1398 MIT
Spot Micro Quadruped Project
by positive-security c
1329 AGPL-3.0
Upload arbitrary data via Apple's Find My network.
by omriharel go
979 MIT
Set app volumes with real sliders! deej is an Arduino & Go project to let you build your own hardware mixer for Windows and Linux
Top Authors in Arduino
1
223 Libraries
5573
2
217 Libraries
18455
3
84 Libraries
1393
4
69 Libraries
2783
5
59 Libraries
20426
6
51 Libraries
788
7
41 Libraries
1309
8
40 Libraries
126
9
39 Libraries
9082
10
38 Libraries
1265
1
223 Libraries
5573
2
217 Libraries
18455
3
84 Libraries
1393
4
69 Libraries
2783
5
59 Libraries
20426
6
51 Libraries
788
7
41 Libraries
1309
8
40 Libraries
126
9
39 Libraries
9082
10
38 Libraries
1265
Trending Kits in Arduino
No Trending Kits are available at this moment for Arduino
Trending Discussions on Arduino
C++11 multithreaded cancellable slice-based work
Sending int from python to arduino, but there is an upper limit. How do I solve it?
exec: "python": executable file not found in $PATH on Arduino IDE
How to filter a class in Angular?
Reading Arduino Serial data on C# application but port access denied?
Stop text from overlapping html and css
What is the Rust equivalent of Serial.println from the Arduino C++ API?
How to fix Failed to connect to ESP32: Timed out waiting for packet header error?
How to make an object take and store an Array of arbitrary, but compile-time known size?
how to clear oled display in micropython
QUESTION
C++11 multithreaded cancellable slice-based work
Asked 2022-Apr-17 at 18:39I am trying to create a base class to manage a slice-based workload.
My approach was to create a base abstract class that handles the initialization/termination of the work and inherit from that class in specific classes that only specify the actual work and timings.
I also added the functionality in the base class to reinitialize the workload if a set number of errors occur.
This works as expected in a simple example (given below) and with most workloads that I have but when I try to use this with a specific workload (reading a serial port that's written to by an arduino) it completely messes up the stream read from arduino.
I suspect there is some problem with my approach but I couldn't figure it out...
Here is my code:
sliceWork.h
1#pragma once
2#include <future>
3using namespace ::std;
4
5class sliceWork
6{
7 int sliceIntervalMilliSeconds;
8 int failureCounter;
9 int maxFailsBeforeRestart;
10 char* label = NULL;
11
12 promise<void> workPromise;
13 thread* workerThread = NULL;
14
15 virtual void init() = 0;
16 virtual bool oneSliceWork() = 0;
17 void work(future<void> future);
18
19public:
20 sliceWork(int sliceInterval, int maxFails, const char* label);
21 ~sliceWork();
22 void initWork();
23 void signalTerminate();
24};
25
sliceWork.cpp
1#pragma once
2#include <future>
3using namespace ::std;
4
5class sliceWork
6{
7 int sliceIntervalMilliSeconds;
8 int failureCounter;
9 int maxFailsBeforeRestart;
10 char* label = NULL;
11
12 promise<void> workPromise;
13 thread* workerThread = NULL;
14
15 virtual void init() = 0;
16 virtual bool oneSliceWork() = 0;
17 void work(future<void> future);
18
19public:
20 sliceWork(int sliceInterval, int maxFails, const char* label);
21 ~sliceWork();
22 void initWork();
23 void signalTerminate();
24};
25#include <string.h>
26#include "sliceWork.h"
27
28sliceWork::sliceWork(int interval, int maxFails, const char* workLabel)
29{
30 sliceIntervalMilliSeconds = interval;
31 maxFailsBeforeRestart = maxFails;
32 label = new char[strlen(workLabel) + 1];
33 strcpy(label, workLabel);
34}
35
36sliceWork::~sliceWork()
37{
38 if (workerThread != NULL && workerThread->joinable())
39 workerThread->join();
40 printf("destructor %s\n", label);
41 delete label;
42 delete workerThread;
43}
44
45void sliceWork::initWork()
46{
47 failureCounter = 0;
48 init();
49 printf("Init work %s finished!\n", label);
50 future<void> futureWorker = workPromise.get_future();
51 workerThread = new thread(&sliceWork::work, this, move(futureWorker));
52}
53
54void sliceWork::work(future<void> future)
55{
56 using namespace ::std::chrono;
57 steady_clock::time_point t0 = steady_clock::now();
58
59 while (future.wait_for(chrono::milliseconds(1)) == future_status::timeout)
60 {
61 if (duration_cast<chrono::milliseconds>(steady_clock::now() - t0).count()
62 > sliceIntervalMilliSeconds)
63 {
64 if (!oneSliceWork())
65 {
66 if (++failureCounter > maxFailsBeforeRestart
67 && maxFailsBeforeRestart > 0)
68 {
69 init();
70 failureCounter = 0;
71 }
72 }
73 t0 = steady_clock::now();
74 }
75 }
76 printf("work terminated for %s!\n", label);
77}
78
79void sliceWork::signalTerminate()
80{
81 printf("request terminate for work %s...\n", label);
82 workPromise.set_value();
83}
84
And here is an example of using it that works as expected:
main.cpp
1#pragma once
2#include <future>
3using namespace ::std;
4
5class sliceWork
6{
7 int sliceIntervalMilliSeconds;
8 int failureCounter;
9 int maxFailsBeforeRestart;
10 char* label = NULL;
11
12 promise<void> workPromise;
13 thread* workerThread = NULL;
14
15 virtual void init() = 0;
16 virtual bool oneSliceWork() = 0;
17 void work(future<void> future);
18
19public:
20 sliceWork(int sliceInterval, int maxFails, const char* label);
21 ~sliceWork();
22 void initWork();
23 void signalTerminate();
24};
25#include <string.h>
26#include "sliceWork.h"
27
28sliceWork::sliceWork(int interval, int maxFails, const char* workLabel)
29{
30 sliceIntervalMilliSeconds = interval;
31 maxFailsBeforeRestart = maxFails;
32 label = new char[strlen(workLabel) + 1];
33 strcpy(label, workLabel);
34}
35
36sliceWork::~sliceWork()
37{
38 if (workerThread != NULL && workerThread->joinable())
39 workerThread->join();
40 printf("destructor %s\n", label);
41 delete label;
42 delete workerThread;
43}
44
45void sliceWork::initWork()
46{
47 failureCounter = 0;
48 init();
49 printf("Init work %s finished!\n", label);
50 future<void> futureWorker = workPromise.get_future();
51 workerThread = new thread(&sliceWork::work, this, move(futureWorker));
52}
53
54void sliceWork::work(future<void> future)
55{
56 using namespace ::std::chrono;
57 steady_clock::time_point t0 = steady_clock::now();
58
59 while (future.wait_for(chrono::milliseconds(1)) == future_status::timeout)
60 {
61 if (duration_cast<chrono::milliseconds>(steady_clock::now() - t0).count()
62 > sliceIntervalMilliSeconds)
63 {
64 if (!oneSliceWork())
65 {
66 if (++failureCounter > maxFailsBeforeRestart
67 && maxFailsBeforeRestart > 0)
68 {
69 init();
70 failureCounter = 0;
71 }
72 }
73 t0 = steady_clock::now();
74 }
75 }
76 printf("work terminated for %s!\n", label);
77}
78
79void sliceWork::signalTerminate()
80{
81 printf("request terminate for work %s...\n", label);
82 workPromise.set_value();
83}
84#include <string.h>
85#include "sliceWork.h"
86
87class A : public sliceWork
88{
89 void init() {
90 printf("Init A...\n");
91 }
92
93 bool oneSliceWork() {
94 printf("Working A...\n");
95 return true;
96 }
97public:
98 A(int slice, int max, const char* label)
99 : sliceWork(slice, max, label)
100 {
101 }
102};
103
104class B : public sliceWork
105{
106 void init() {
107 printf("Init B...\n");
108 }
109
110 bool oneSliceWork() {
111 printf("Working B...\n");
112 return true;
113 }
114public:
115 B(int slice, int max, const char* label)
116 : sliceWork(slice, max, label)
117 {
118 }
119};
120
121class C : public sliceWork
122{
123 void init() {
124 printf("Init C...\n");
125 }
126
127 bool oneSliceWork() {
128 printf("Working C...\n");
129 return false;
130 }
131public:
132 C(int slice, int max, const char* label)
133 : sliceWork(slice, max, label)
134 {
135 }
136};
137
138int main()
139{
140 {
141 A a(1000, 1000, "A");
142 a.initWork();
143 B b(2000, 1000, "B" );
144 b.initWork();
145 C c(700, 2, "C" );
146 c.initWork();
147 printf("Initializations finished!\n");
148 ::std::this_thread::sleep_for(::std::chrono::seconds(7));
149 a.signalTerminate();
150 ::std::this_thread::sleep_for(::std::chrono::seconds(5));
151 b.signalTerminate();
152 ::std::this_thread::sleep_for(::std::chrono::seconds(4));
153 c.signalTerminate();
154 }
155 getchar();
156 return 0;
157}
158
So, I want to ask if this approach is prone to error because the way I implemented the functionality.
Application is written in C++11 and targets an Raspberry PI 3b+ running the Raspberry's flavor of Debian 11 (Raspbian), if that is relevant.
ANSWER
Answered 2022-Apr-17 at 13:21While the code compiles and runs on Windows 10 in Visual Studio 2019, there are multiple problems here, not necessarily with threading.
- Without knowing what the expected output for the test case it is very difficult to determine if the code is running correctly.
- Object oriented programming rules are being broken, there doesn't seem to be any encapsulation. All of the variables and methods are public.
- This looks more like
C
code thatC++
code except for the classes themselves. - I am providing a review to address the C++ issues, we don't debug problems on code review, and we would need to see the the Arduno code to help debug it (on Stack Overflow, not Code Review).
using namespace std;
If you are coding professionally you probably should get out of the habit of using the using namespace std;
statement. The code will more clearly define where cout
and other identifiers are coming from (std::cin
, std::cout
). As you start using namespaces in your code it is better to identify where each function comes from because there may be function name collisions from different namespaces. The identifiercout
you may override within your own classes, and you may override the operator <<
in your own classes as well. This stack overflow question discusses this in more detail.
Never put using namespace std
into a header file.
A general best practice is to declare public
variables and methods at the top of the class followed by the protected variables and methods and then finally the private variables and methods. This makes it easier for the users of the class to find the interfaces they need.
While the C++ language does provide a default for variables and methods declared at the top of the class, the code is easier to read and maintain if the public
, protected
and private
keywords are explicit.
While C++ is backward compatible with C, using printf()
in C++ is exceptionally rare, std::cin and std::cout are preferred in C++.
You can provide default functions for the virtual
methods init()
and oneSliceWork()
. This would reduce the repetition of code in the test case, and still allow for the virtual methods to be overwritten when necessary.
1#pragma once
2#include <future>
3using namespace ::std;
4
5class sliceWork
6{
7 int sliceIntervalMilliSeconds;
8 int failureCounter;
9 int maxFailsBeforeRestart;
10 char* label = NULL;
11
12 promise<void> workPromise;
13 thread* workerThread = NULL;
14
15 virtual void init() = 0;
16 virtual bool oneSliceWork() = 0;
17 void work(future<void> future);
18
19public:
20 sliceWork(int sliceInterval, int maxFails, const char* label);
21 ~sliceWork();
22 void initWork();
23 void signalTerminate();
24};
25#include <string.h>
26#include "sliceWork.h"
27
28sliceWork::sliceWork(int interval, int maxFails, const char* workLabel)
29{
30 sliceIntervalMilliSeconds = interval;
31 maxFailsBeforeRestart = maxFails;
32 label = new char[strlen(workLabel) + 1];
33 strcpy(label, workLabel);
34}
35
36sliceWork::~sliceWork()
37{
38 if (workerThread != NULL && workerThread->joinable())
39 workerThread->join();
40 printf("destructor %s\n", label);
41 delete label;
42 delete workerThread;
43}
44
45void sliceWork::initWork()
46{
47 failureCounter = 0;
48 init();
49 printf("Init work %s finished!\n", label);
50 future<void> futureWorker = workPromise.get_future();
51 workerThread = new thread(&sliceWork::work, this, move(futureWorker));
52}
53
54void sliceWork::work(future<void> future)
55{
56 using namespace ::std::chrono;
57 steady_clock::time_point t0 = steady_clock::now();
58
59 while (future.wait_for(chrono::milliseconds(1)) == future_status::timeout)
60 {
61 if (duration_cast<chrono::milliseconds>(steady_clock::now() - t0).count()
62 > sliceIntervalMilliSeconds)
63 {
64 if (!oneSliceWork())
65 {
66 if (++failureCounter > maxFailsBeforeRestart
67 && maxFailsBeforeRestart > 0)
68 {
69 init();
70 failureCounter = 0;
71 }
72 }
73 t0 = steady_clock::now();
74 }
75 }
76 printf("work terminated for %s!\n", label);
77}
78
79void sliceWork::signalTerminate()
80{
81 printf("request terminate for work %s...\n", label);
82 workPromise.set_value();
83}
84#include <string.h>
85#include "sliceWork.h"
86
87class A : public sliceWork
88{
89 void init() {
90 printf("Init A...\n");
91 }
92
93 bool oneSliceWork() {
94 printf("Working A...\n");
95 return true;
96 }
97public:
98 A(int slice, int max, const char* label)
99 : sliceWork(slice, max, label)
100 {
101 }
102};
103
104class B : public sliceWork
105{
106 void init() {
107 printf("Init B...\n");
108 }
109
110 bool oneSliceWork() {
111 printf("Working B...\n");
112 return true;
113 }
114public:
115 B(int slice, int max, const char* label)
116 : sliceWork(slice, max, label)
117 {
118 }
119};
120
121class C : public sliceWork
122{
123 void init() {
124 printf("Init C...\n");
125 }
126
127 bool oneSliceWork() {
128 printf("Working C...\n");
129 return false;
130 }
131public:
132 C(int slice, int max, const char* label)
133 : sliceWork(slice, max, label)
134 {
135 }
136};
137
138int main()
139{
140 {
141 A a(1000, 1000, "A");
142 a.initWork();
143 B b(2000, 1000, "B" );
144 b.initWork();
145 C c(700, 2, "C" );
146 c.initWork();
147 printf("Initializations finished!\n");
148 ::std::this_thread::sleep_for(::std::chrono::seconds(7));
149 a.signalTerminate();
150 ::std::this_thread::sleep_for(::std::chrono::seconds(5));
151 b.signalTerminate();
152 ::std::this_thread::sleep_for(::std::chrono::seconds(4));
153 c.signalTerminate();
154 }
155 getchar();
156 return 0;
157}
158#pragma once
159#include <iostream>
160#include <future>
161
162class sliceWork
163{
164public:
165 sliceWork(int sliceInterval, int maxFails, const char* label);
166 ~sliceWork();
167 void initWork();
168 void signalTerminate();
169
170protected:
171 virtual void init()
172 {
173 std::cout << "Init " << label << "..\n";
174 }
175 virtual bool oneSliceWork()
176 {
177 std::cout << "Working " << label << "..\n";
178 return true;
179 }
180 void work(std::future<void> future);
181
182private:
183 int sliceIntervalMilliSeconds;
184 int failureCounter;
185 int maxFailsBeforeRestart;
186 char* label = NULL;
187 std::promise<void> workPromise;
188 std::thread* workerThread = NULL;
189};
190
main.cpp
1#pragma once
2#include <future>
3using namespace ::std;
4
5class sliceWork
6{
7 int sliceIntervalMilliSeconds;
8 int failureCounter;
9 int maxFailsBeforeRestart;
10 char* label = NULL;
11
12 promise<void> workPromise;
13 thread* workerThread = NULL;
14
15 virtual void init() = 0;
16 virtual bool oneSliceWork() = 0;
17 void work(future<void> future);
18
19public:
20 sliceWork(int sliceInterval, int maxFails, const char* label);
21 ~sliceWork();
22 void initWork();
23 void signalTerminate();
24};
25#include <string.h>
26#include "sliceWork.h"
27
28sliceWork::sliceWork(int interval, int maxFails, const char* workLabel)
29{
30 sliceIntervalMilliSeconds = interval;
31 maxFailsBeforeRestart = maxFails;
32 label = new char[strlen(workLabel) + 1];
33 strcpy(label, workLabel);
34}
35
36sliceWork::~sliceWork()
37{
38 if (workerThread != NULL && workerThread->joinable())
39 workerThread->join();
40 printf("destructor %s\n", label);
41 delete label;
42 delete workerThread;
43}
44
45void sliceWork::initWork()
46{
47 failureCounter = 0;
48 init();
49 printf("Init work %s finished!\n", label);
50 future<void> futureWorker = workPromise.get_future();
51 workerThread = new thread(&sliceWork::work, this, move(futureWorker));
52}
53
54void sliceWork::work(future<void> future)
55{
56 using namespace ::std::chrono;
57 steady_clock::time_point t0 = steady_clock::now();
58
59 while (future.wait_for(chrono::milliseconds(1)) == future_status::timeout)
60 {
61 if (duration_cast<chrono::milliseconds>(steady_clock::now() - t0).count()
62 > sliceIntervalMilliSeconds)
63 {
64 if (!oneSliceWork())
65 {
66 if (++failureCounter > maxFailsBeforeRestart
67 && maxFailsBeforeRestart > 0)
68 {
69 init();
70 failureCounter = 0;
71 }
72 }
73 t0 = steady_clock::now();
74 }
75 }
76 printf("work terminated for %s!\n", label);
77}
78
79void sliceWork::signalTerminate()
80{
81 printf("request terminate for work %s...\n", label);
82 workPromise.set_value();
83}
84#include <string.h>
85#include "sliceWork.h"
86
87class A : public sliceWork
88{
89 void init() {
90 printf("Init A...\n");
91 }
92
93 bool oneSliceWork() {
94 printf("Working A...\n");
95 return true;
96 }
97public:
98 A(int slice, int max, const char* label)
99 : sliceWork(slice, max, label)
100 {
101 }
102};
103
104class B : public sliceWork
105{
106 void init() {
107 printf("Init B...\n");
108 }
109
110 bool oneSliceWork() {
111 printf("Working B...\n");
112 return true;
113 }
114public:
115 B(int slice, int max, const char* label)
116 : sliceWork(slice, max, label)
117 {
118 }
119};
120
121class C : public sliceWork
122{
123 void init() {
124 printf("Init C...\n");
125 }
126
127 bool oneSliceWork() {
128 printf("Working C...\n");
129 return false;
130 }
131public:
132 C(int slice, int max, const char* label)
133 : sliceWork(slice, max, label)
134 {
135 }
136};
137
138int main()
139{
140 {
141 A a(1000, 1000, "A");
142 a.initWork();
143 B b(2000, 1000, "B" );
144 b.initWork();
145 C c(700, 2, "C" );
146 c.initWork();
147 printf("Initializations finished!\n");
148 ::std::this_thread::sleep_for(::std::chrono::seconds(7));
149 a.signalTerminate();
150 ::std::this_thread::sleep_for(::std::chrono::seconds(5));
151 b.signalTerminate();
152 ::std::this_thread::sleep_for(::std::chrono::seconds(4));
153 c.signalTerminate();
154 }
155 getchar();
156 return 0;
157}
158#pragma once
159#include <iostream>
160#include <future>
161
162class sliceWork
163{
164public:
165 sliceWork(int sliceInterval, int maxFails, const char* label);
166 ~sliceWork();
167 void initWork();
168 void signalTerminate();
169
170protected:
171 virtual void init()
172 {
173 std::cout << "Init " << label << "..\n";
174 }
175 virtual bool oneSliceWork()
176 {
177 std::cout << "Working " << label << "..\n";
178 return true;
179 }
180 void work(std::future<void> future);
181
182private:
183 int sliceIntervalMilliSeconds;
184 int failureCounter;
185 int maxFailsBeforeRestart;
186 char* label = NULL;
187 std::promise<void> workPromise;
188 std::thread* workerThread = NULL;
189};
190#include <string.h>
191#include "sliceWork.h"
192
193class A : public sliceWork
194{
195public:
196 A(int slice, int max, const char* label)
197 : sliceWork(slice, max, label)
198 {
199 }
200};
201
202class B : public sliceWork
203{
204public:
205 B(int slice, int max, const char* label)
206 : sliceWork(slice, max, label)
207 {
208 }
209};
210
211class C : public sliceWork
212{
213public:
214 C(int slice, int max, const char* label)
215 : sliceWork(slice, max, label)
216 {
217 }
218};
219
220int main()
221{
222 {
223 A a(1000, 1000, "A");
224 a.initWork();
225 B b(2000, 1000, "B");
226 b.initWork();
227 C c(700, 2, "C");
228 c.initWork();
229 std::cout << "Initializations finished!\n";
230 ::std::this_thread::sleep_for(::std::chrono::seconds(7));
231 a.signalTerminate();
232 ::std::this_thread::sleep_for(::std::chrono::seconds(5));
233 b.signalTerminate();
234 ::std::this_thread::sleep_for(::std::chrono::seconds(4));
235 c.signalTerminate();
236 }
237 getchar();
238 return 0;
239}
240
sliceWork.cpp
1#pragma once
2#include <future>
3using namespace ::std;
4
5class sliceWork
6{
7 int sliceIntervalMilliSeconds;
8 int failureCounter;
9 int maxFailsBeforeRestart;
10 char* label = NULL;
11
12 promise<void> workPromise;
13 thread* workerThread = NULL;
14
15 virtual void init() = 0;
16 virtual bool oneSliceWork() = 0;
17 void work(future<void> future);
18
19public:
20 sliceWork(int sliceInterval, int maxFails, const char* label);
21 ~sliceWork();
22 void initWork();
23 void signalTerminate();
24};
25#include <string.h>
26#include "sliceWork.h"
27
28sliceWork::sliceWork(int interval, int maxFails, const char* workLabel)
29{
30 sliceIntervalMilliSeconds = interval;
31 maxFailsBeforeRestart = maxFails;
32 label = new char[strlen(workLabel) + 1];
33 strcpy(label, workLabel);
34}
35
36sliceWork::~sliceWork()
37{
38 if (workerThread != NULL && workerThread->joinable())
39 workerThread->join();
40 printf("destructor %s\n", label);
41 delete label;
42 delete workerThread;
43}
44
45void sliceWork::initWork()
46{
47 failureCounter = 0;
48 init();
49 printf("Init work %s finished!\n", label);
50 future<void> futureWorker = workPromise.get_future();
51 workerThread = new thread(&sliceWork::work, this, move(futureWorker));
52}
53
54void sliceWork::work(future<void> future)
55{
56 using namespace ::std::chrono;
57 steady_clock::time_point t0 = steady_clock::now();
58
59 while (future.wait_for(chrono::milliseconds(1)) == future_status::timeout)
60 {
61 if (duration_cast<chrono::milliseconds>(steady_clock::now() - t0).count()
62 > sliceIntervalMilliSeconds)
63 {
64 if (!oneSliceWork())
65 {
66 if (++failureCounter > maxFailsBeforeRestart
67 && maxFailsBeforeRestart > 0)
68 {
69 init();
70 failureCounter = 0;
71 }
72 }
73 t0 = steady_clock::now();
74 }
75 }
76 printf("work terminated for %s!\n", label);
77}
78
79void sliceWork::signalTerminate()
80{
81 printf("request terminate for work %s...\n", label);
82 workPromise.set_value();
83}
84#include <string.h>
85#include "sliceWork.h"
86
87class A : public sliceWork
88{
89 void init() {
90 printf("Init A...\n");
91 }
92
93 bool oneSliceWork() {
94 printf("Working A...\n");
95 return true;
96 }
97public:
98 A(int slice, int max, const char* label)
99 : sliceWork(slice, max, label)
100 {
101 }
102};
103
104class B : public sliceWork
105{
106 void init() {
107 printf("Init B...\n");
108 }
109
110 bool oneSliceWork() {
111 printf("Working B...\n");
112 return true;
113 }
114public:
115 B(int slice, int max, const char* label)
116 : sliceWork(slice, max, label)
117 {
118 }
119};
120
121class C : public sliceWork
122{
123 void init() {
124 printf("Init C...\n");
125 }
126
127 bool oneSliceWork() {
128 printf("Working C...\n");
129 return false;
130 }
131public:
132 C(int slice, int max, const char* label)
133 : sliceWork(slice, max, label)
134 {
135 }
136};
137
138int main()
139{
140 {
141 A a(1000, 1000, "A");
142 a.initWork();
143 B b(2000, 1000, "B" );
144 b.initWork();
145 C c(700, 2, "C" );
146 c.initWork();
147 printf("Initializations finished!\n");
148 ::std::this_thread::sleep_for(::std::chrono::seconds(7));
149 a.signalTerminate();
150 ::std::this_thread::sleep_for(::std::chrono::seconds(5));
151 b.signalTerminate();
152 ::std::this_thread::sleep_for(::std::chrono::seconds(4));
153 c.signalTerminate();
154 }
155 getchar();
156 return 0;
157}
158#pragma once
159#include <iostream>
160#include <future>
161
162class sliceWork
163{
164public:
165 sliceWork(int sliceInterval, int maxFails, const char* label);
166 ~sliceWork();
167 void initWork();
168 void signalTerminate();
169
170protected:
171 virtual void init()
172 {
173 std::cout << "Init " << label << "..\n";
174 }
175 virtual bool oneSliceWork()
176 {
177 std::cout << "Working " << label << "..\n";
178 return true;
179 }
180 void work(std::future<void> future);
181
182private:
183 int sliceIntervalMilliSeconds;
184 int failureCounter;
185 int maxFailsBeforeRestart;
186 char* label = NULL;
187 std::promise<void> workPromise;
188 std::thread* workerThread = NULL;
189};
190#include <string.h>
191#include "sliceWork.h"
192
193class A : public sliceWork
194{
195public:
196 A(int slice, int max, const char* label)
197 : sliceWork(slice, max, label)
198 {
199 }
200};
201
202class B : public sliceWork
203{
204public:
205 B(int slice, int max, const char* label)
206 : sliceWork(slice, max, label)
207 {
208 }
209};
210
211class C : public sliceWork
212{
213public:
214 C(int slice, int max, const char* label)
215 : sliceWork(slice, max, label)
216 {
217 }
218};
219
220int main()
221{
222 {
223 A a(1000, 1000, "A");
224 a.initWork();
225 B b(2000, 1000, "B");
226 b.initWork();
227 C c(700, 2, "C");
228 c.initWork();
229 std::cout << "Initializations finished!\n";
230 ::std::this_thread::sleep_for(::std::chrono::seconds(7));
231 a.signalTerminate();
232 ::std::this_thread::sleep_for(::std::chrono::seconds(5));
233 b.signalTerminate();
234 ::std::this_thread::sleep_for(::std::chrono::seconds(4));
235 c.signalTerminate();
236 }
237 getchar();
238 return 0;
239}
240#include <string.h>
241#include "sliceWork.h"
242
243sliceWork::sliceWork(int interval, int maxFails, const char* workLabel)
244{
245 sliceIntervalMilliSeconds = interval;
246 maxFailsBeforeRestart = maxFails;
247 label = new char[strlen(workLabel) + 1];
248 strcpy(label, workLabel);
249}
250
251sliceWork::~sliceWork()
252{
253 if (workerThread != NULL && workerThread->joinable())
254 workerThread->join();
255 printf("destructor %s\n", label);
256 delete label;
257 delete workerThread;
258}
259
260void sliceWork::initWork()
261{
262 failureCounter = 0;
263 init();
264 printf("Init work %s finished!\n", label);
265 std::future<void> futureWorker = workPromise.get_future();
266 workerThread = new std::thread(&sliceWork::work, this, move(futureWorker));
267}
268
269void sliceWork::work(std::future<void> future)
270{
271 using namespace ::std::chrono;
272 steady_clock::time_point t0 = steady_clock::now();
273
274 while (future.wait_for(std::chrono::milliseconds(1)) == std::future_status::timeout)
275 {
276 if (duration_cast<std::chrono::milliseconds>(steady_clock::now() - t0).count()
277 > sliceIntervalMilliSeconds)
278 {
279 if (!oneSliceWork())
280 {
281 if (++failureCounter > maxFailsBeforeRestart
282 && maxFailsBeforeRestart > 0)
283 {
284 init();
285 failureCounter = 0;
286 }
287 }
288 t0 = steady_clock::now();
289 }
290 }
291 printf("work terminated for %s!\n", label);
292}
293
294void sliceWork::signalTerminate()
295{
296 printf("request terminate for work %s...\n", label);
297 workPromise.set_value();
298}
299
QUESTION
Sending int from python to arduino, but there is an upper limit. How do I solve it?
Asked 2022-Mar-29 at 10:02in my project I am sending int from python to arduino, which is the number of steps the stepper motor should take. Python code:
1while(1):
2userInput = input('Get data point?')
3
4ser.write("s{}".format(int(131.0268562*userInput)).encode())
5
6if userInput == 'c':
7 break;
8
9
Arduino Code:
1while(1):
2userInput = input('Get data point?')
3
4ser.write("s{}".format(int(131.0268562*userInput)).encode())
5
6if userInput == 'c':
7 break;
8
9if (Serial.available() > 0){
10
11
12int receivedValue = Serial.parseInt();
13
14digitalWrite(4,HIGH);
15
16
17if (receivedValue>=0){
18 for(Index = 0; Index < receivedValue; Index++)
19 {
20 digitalWrite(5,HIGH);
21
22 delayMicroseconds(s);
23
24 digitalWrite(5,LOW);
25
26 delayMicroseconds(s);
27 }
28 }
29
30}
31
But when the value is bigger than 30000, the code doesn't work. When I print the value on arduino side, it is either negative, or some small positive number
Why is it happening and how do I solve it?
Edit: I have solved it thanks to the helps of "quamrana" and "Serge Ballesta"
If anyone stumbles on a similar problem. Just use:
1while(1):
2userInput = input('Get data point?')
3
4ser.write("s{}".format(int(131.0268562*userInput)).encode())
5
6if userInput == 'c':
7 break;
8
9if (Serial.available() > 0){
10
11
12int receivedValue = Serial.parseInt();
13
14digitalWrite(4,HIGH);
15
16
17if (receivedValue>=0){
18 for(Index = 0; Index < receivedValue; Index++)
19 {
20 digitalWrite(5,HIGH);
21
22 delayMicroseconds(s);
23
24 digitalWrite(5,LOW);
25
26 delayMicroseconds(s);
27 }
28 }
29
30}
31long receivedValue = Serial.Stream::parseInt();
32
ANSWER
Answered 2022-Mar-29 at 08:55I would assume that the limit is exactly 32767 which is 2**15 - 1. This is a hint that on Arduino parseInt
return a 16 bits, 2's complement integer.
If you have a larger integral type (long ?) on Arduino, you should try to use it. An alternative would be to use a different encoding passing two integers each 15 bits max. At the end you would loop high value times 32767 times + low value times.
QUESTION
exec: "python": executable file not found in $PATH on Arduino IDE
Asked 2022-Mar-23 at 20:45So I'm trying to run this really simple code on my LCD display, using an ESP32. When I compile the code in the Arduino IDE I get no errors. But, when I try to upload it, I get the following error:
1exec: "python": executable file not found in $PATH
2Error compiling for board DOIT ESP32 DEVKIT V1.
3
I'm running the Mac OS Monterey, on an M1 MacBook Air.
I did find some information here, but it wasn't what I was looking for, it was made for Ubuntu.
I do have python3 installed on my Mac but it goes to /usr/bin/python3
and Mac automatically installs python,
I want python3 to go to /usr/bin/python
Running which python
returns python not found
which is weird, because python should be preinstalled on Mac.
Any ideas how to fix this error, and is my way possible?
Thanks!
ANSWER
Answered 2022-Feb-17 at 12:56Probably a soft link will do, try sudo ln -s /usr/bin/python3 /usr/bin/python
QUESTION
How to filter a class in Angular?
Asked 2022-Feb-04 at 16:12I'm trying to show in a list the projects whose method "arduino" is equal to true (it's a boolean).
I tried to do *ngFor and *ngIf on the same line but it gives an error, so when doing it on another line, the system loads the projects whose method "arduino" is equal to 'false' too, so it doesn't work well
How can I filter it from component.ts so that only projects with method project.arduino = true are loaded?
Here is the code in component.ts
1@Component({
2 selector: 'app-arduino-projects',
3 templateUrl: './arduino-projects.component.html',
4 styleUrls: ['./arduino-projects.component.css'],
5
6 providers: [ProjectService]
7
8})
9export class ArduinoProjectsComponent implements OnInit {
10
11 public projects: Project[];
12 public url: string;
13
14 constructor(
15 private _projectService: ProjectService
16 ) {
17 this.projects = [];
18 this.url = Global.url;
19 }
20
21 ngOnInit(): void {
22 this.getProjects();
23 }
24
25 getProjects(){
26 this._projectService.getProjects().subscribe(
27 response => {
28 if(response.projects){
29 this.projects = response.projects; **************** this is the property that I have to filter and I don't know how to do it
30 }
31 },
32 error => {
33 console.log(error)
34 }
35 )
36 }
37
And here is the relevant code in component.html
1@Component({
2 selector: 'app-arduino-projects',
3 templateUrl: './arduino-projects.component.html',
4 styleUrls: ['./arduino-projects.component.css'],
5
6 providers: [ProjectService]
7
8})
9export class ArduinoProjectsComponent implements OnInit {
10
11 public projects: Project[];
12 public url: string;
13
14 constructor(
15 private _projectService: ProjectService
16 ) {
17 this.projects = [];
18 this.url = Global.url;
19 }
20
21 ngOnInit(): void {
22 this.getProjects();
23 }
24
25 getProjects(){
26 this._projectService.getProjects().subscribe(
27 response => {
28 if(response.projects){
29 this.projects = response.projects; **************** this is the property that I have to filter and I don't know how to do it
30 }
31 },
32 error => {
33 console.log(error)
34 }
35 )
36 }
37<div class="project-list">
38 <ul>
39 <li *ngFor="let project of projects" class="project">
40 <ng-container *ngIf="project.arduino == true"> ************ Here i filtered it in that way
41 <a [routerLink]="['/project', project._id]">
42
43 <div class="image-box">
44 <div class="image">
45 <img src="{{ url+'get-image/'+project.imagefront }}" *ngIf="project.imagefront" />
46 </div>
47 </div>
48 </a>
49 </ng-container>
50 </li>
51 </ul>
52 </div>
53
In this way it is loading the projects whose project.arduino = false, it does not show them on the screen but it messes up the list in the browser
I'm using Angular 13
Thank you very much!
ANSWER
Answered 2022-Feb-04 at 16:12I think if the method "arduino" is a boolean you only need to do something like :
1@Component({
2 selector: 'app-arduino-projects',
3 templateUrl: './arduino-projects.component.html',
4 styleUrls: ['./arduino-projects.component.css'],
5
6 providers: [ProjectService]
7
8})
9export class ArduinoProjectsComponent implements OnInit {
10
11 public projects: Project[];
12 public url: string;
13
14 constructor(
15 private _projectService: ProjectService
16 ) {
17 this.projects = [];
18 this.url = Global.url;
19 }
20
21 ngOnInit(): void {
22 this.getProjects();
23 }
24
25 getProjects(){
26 this._projectService.getProjects().subscribe(
27 response => {
28 if(response.projects){
29 this.projects = response.projects; **************** this is the property that I have to filter and I don't know how to do it
30 }
31 },
32 error => {
33 console.log(error)
34 }
35 )
36 }
37<div class="project-list">
38 <ul>
39 <li *ngFor="let project of projects" class="project">
40 <ng-container *ngIf="project.arduino == true"> ************ Here i filtered it in that way
41 <a [routerLink]="['/project', project._id]">
42
43 <div class="image-box">
44 <div class="image">
45 <img src="{{ url+'get-image/'+project.imagefront }}" *ngIf="project.imagefront" />
46 </div>
47 </div>
48 </a>
49 </ng-container>
50 </li>
51 </ul>
52 </div>
53<ng-container *ngIf="this.project.arduino">
54
In your TypeScript try to add the adruino projects who are true to a new list:
1@Component({
2 selector: 'app-arduino-projects',
3 templateUrl: './arduino-projects.component.html',
4 styleUrls: ['./arduino-projects.component.css'],
5
6 providers: [ProjectService]
7
8})
9export class ArduinoProjectsComponent implements OnInit {
10
11 public projects: Project[];
12 public url: string;
13
14 constructor(
15 private _projectService: ProjectService
16 ) {
17 this.projects = [];
18 this.url = Global.url;
19 }
20
21 ngOnInit(): void {
22 this.getProjects();
23 }
24
25 getProjects(){
26 this._projectService.getProjects().subscribe(
27 response => {
28 if(response.projects){
29 this.projects = response.projects; **************** this is the property that I have to filter and I don't know how to do it
30 }
31 },
32 error => {
33 console.log(error)
34 }
35 )
36 }
37<div class="project-list">
38 <ul>
39 <li *ngFor="let project of projects" class="project">
40 <ng-container *ngIf="project.arduino == true"> ************ Here i filtered it in that way
41 <a [routerLink]="['/project', project._id]">
42
43 <div class="image-box">
44 <div class="image">
45 <img src="{{ url+'get-image/'+project.imagefront }}" *ngIf="project.imagefront" />
46 </div>
47 </div>
48 </a>
49 </ng-container>
50 </li>
51 </ul>
52 </div>
53<ng-container *ngIf="this.project.arduino">
54this.newList = [];
55if (this.project.arduino) {
56this.newList.push(project)
57}
58
Than you can only use the <li *ngFor="let project of newList" class="project">
and get rid off the condition <ng-container *ngIf="project.arduino == true">
QUESTION
Reading Arduino Serial data on C# application but port access denied?
Asked 2022-Feb-01 at 21:45When I connect my Arduino to my laptop and want to get serial data on my system, I get an exception about port access being denied. I am sure about the port number (COM4) and opening and closing ports. sometimes I reconnect the device it works.
ANSWER
Answered 2022-Feb-01 at 21:45Try these out:
if your Serial monitor on Arduino is not opened, be sure to close it.
check the port in system management of your system to be sure about the port number again.
be sure about closing your application each time you want to test, because running an application will reserve the port and will not let other apps use it.
QUESTION
Stop text from overlapping html and css
Asked 2022-Jan-28 at 15:52when I resize my window my text is overlapping. How do I stop this from happening? I want it to just resize if it colliding with the other text elements or, if not possible, that they just stack under each other? problem
I already tried
- flex box
- set min height
and can someone tell me what position I should use mainly?
1.flex_container {
2 display: flex;
3 flex-wrap: wrap;
4 min-width: 550px;
5 flex-direction: column;
6 position: relative;
7}
8
9.bar {
10 position: relative;
11 background-color: rgb(41, 80, 143);
12 border: 5px solid rgb(46, 84, 149);
13 height: 30px;
14 width: auto;
15 margin: 0;
16}
17
18.tekst {
19 color: white;
20 position: static;
21 text-transform: uppercase;
22 text-align: center;
23 margin: 0;
24 justify-content: center;
25 vertical-align: middle;
26 line-height: 30px;
27 font-family: Arial, Helvetica, sans-serif;
28}
29
30.ccontroller {
31 margin-top: -12px;
32}
33
34.creator1 {
35 font-size: 25px;
36 color: white;
37 text-align: left;
38 margin: 0;
39 justify-content: center;
40 vertical-align: middle;
41 line-height: 0px;
42 font-family: Arial, Helvetica, sans-serif;
43}
44
45.creator2 {
46 color: white;
47 font-size: 25px;
48 text-align: right;
49 margin: 0;
50 justify-content: center;
51 vertical-align: middle;
52 line-height: 0px;
53 font-family: Arial, Helvetica, sans-serif;
54}
55
56.aboutus {
57 margin: 0;
58 position: absolute;
59 top: 50%;
60 left: 25%;
61 -ms-transform: translate(-50%, -50%);
62 transform: translate(-50%, -50%);
63 color: white;
64 background-color: #123456;
65 height: 15px;
66 width: auto;
67}
68
69body {
70 background-color: #123456;
71 background-repeat: no-repeat;
72 background-position: center;
73 background-attachment: fixed;
74 background-size: 100% 100%;
75 min-width: 550px;
76 position: relative;
77 margin: 0;
78 padding: 0;
79}
80
81html,
82body {
83 min-width: 550px;
84 position: relative;
85 margin: 0;
86 padding: 0;
87}
1.flex_container {
2 display: flex;
3 flex-wrap: wrap;
4 min-width: 550px;
5 flex-direction: column;
6 position: relative;
7}
8
9.bar {
10 position: relative;
11 background-color: rgb(41, 80, 143);
12 border: 5px solid rgb(46, 84, 149);
13 height: 30px;
14 width: auto;
15 margin: 0;
16}
17
18.tekst {
19 color: white;
20 position: static;
21 text-transform: uppercase;
22 text-align: center;
23 margin: 0;
24 justify-content: center;
25 vertical-align: middle;
26 line-height: 30px;
27 font-family: Arial, Helvetica, sans-serif;
28}
29
30.ccontroller {
31 margin-top: -12px;
32}
33
34.creator1 {
35 font-size: 25px;
36 color: white;
37 text-align: left;
38 margin: 0;
39 justify-content: center;
40 vertical-align: middle;
41 line-height: 0px;
42 font-family: Arial, Helvetica, sans-serif;
43}
44
45.creator2 {
46 color: white;
47 font-size: 25px;
48 text-align: right;
49 margin: 0;
50 justify-content: center;
51 vertical-align: middle;
52 line-height: 0px;
53 font-family: Arial, Helvetica, sans-serif;
54}
55
56.aboutus {
57 margin: 0;
58 position: absolute;
59 top: 50%;
60 left: 25%;
61 -ms-transform: translate(-50%, -50%);
62 transform: translate(-50%, -50%);
63 color: white;
64 background-color: #123456;
65 height: 15px;
66 width: auto;
67}
68
69body {
70 background-color: #123456;
71 background-repeat: no-repeat;
72 background-position: center;
73 background-attachment: fixed;
74 background-size: 100% 100%;
75 min-width: 550px;
76 position: relative;
77 margin: 0;
78 padding: 0;
79}
80
81html,
82body {
83 min-width: 550px;
84 position: relative;
85 margin: 0;
86 padding: 0;
87}<body>
88 <div class="flex_container">
89 <div class="bar">
90 <h1 class="tekst" title="Coffeesnakes">Coffeesnakes</h1>
91 <button class="aboutus" onclick="func()">About us!</button>
92 <div class="ccontroller">
93 <p class="creator1" title="Can code: Java, c#, html, js, python, arduino">Christian2B</p>
94 <p class="creator2" title="Can code: html, python, js ">Timgb11</p>
95 </div>
96 </div>
97 </div>
98</body>
ANSWER
Answered 2022-Jan-28 at 09:25A way to fix your problem is using flex box, but this time add a part in the css where once the screen gets to a certain size, the flex-box goes from row, to column. Like this:
1.flex_container {
2 display: flex;
3 flex-wrap: wrap;
4 min-width: 550px;
5 flex-direction: column;
6 position: relative;
7}
8
9.bar {
10 position: relative;
11 background-color: rgb(41, 80, 143);
12 border: 5px solid rgb(46, 84, 149);
13 height: 30px;
14 width: auto;
15 margin: 0;
16}
17
18.tekst {
19 color: white;
20 position: static;
21 text-transform: uppercase;
22 text-align: center;
23 margin: 0;
24 justify-content: center;
25 vertical-align: middle;
26 line-height: 30px;
27 font-family: Arial, Helvetica, sans-serif;
28}
29
30.ccontroller {
31 margin-top: -12px;
32}
33
34.creator1 {
35 font-size: 25px;
36 color: white;
37 text-align: left;
38 margin: 0;
39 justify-content: center;
40 vertical-align: middle;
41 line-height: 0px;
42 font-family: Arial, Helvetica, sans-serif;
43}
44
45.creator2 {
46 color: white;
47 font-size: 25px;
48 text-align: right;
49 margin: 0;
50 justify-content: center;
51 vertical-align: middle;
52 line-height: 0px;
53 font-family: Arial, Helvetica, sans-serif;
54}
55
56.aboutus {
57 margin: 0;
58 position: absolute;
59 top: 50%;
60 left: 25%;
61 -ms-transform: translate(-50%, -50%);
62 transform: translate(-50%, -50%);
63 color: white;
64 background-color: #123456;
65 height: 15px;
66 width: auto;
67}
68
69body {
70 background-color: #123456;
71 background-repeat: no-repeat;
72 background-position: center;
73 background-attachment: fixed;
74 background-size: 100% 100%;
75 min-width: 550px;
76 position: relative;
77 margin: 0;
78 padding: 0;
79}
80
81html,
82body {
83 min-width: 550px;
84 position: relative;
85 margin: 0;
86 padding: 0;
87}<body>
88 <div class="flex_container">
89 <div class="bar">
90 <h1 class="tekst" title="Coffeesnakes">Coffeesnakes</h1>
91 <button class="aboutus" onclick="func()">About us!</button>
92 <div class="ccontroller">
93 <p class="creator1" title="Can code: Java, c#, html, js, python, arduino">Christian2B</p>
94 <p class="creator2" title="Can code: html, python, js ">Timgb11</p>
95 </div>
96 </div>
97 </div>
98</body>.bar-element {
99 display: flex;
100 flex-direction: row;
101}
102
103@media (max-width:700px){
104 .bar-element {
105 display: flex;
106 flex-direction: column;
107 }
108}
109
SO once the screen gets to a curtain size it changes the layout.
Here is a demo:
1.flex_container {
2 display: flex;
3 flex-wrap: wrap;
4 min-width: 550px;
5 flex-direction: column;
6 position: relative;
7}
8
9.bar {
10 position: relative;
11 background-color: rgb(41, 80, 143);
12 border: 5px solid rgb(46, 84, 149);
13 height: 30px;
14 width: auto;
15 margin: 0;
16}
17
18.tekst {
19 color: white;
20 position: static;
21 text-transform: uppercase;
22 text-align: center;
23 margin: 0;
24 justify-content: center;
25 vertical-align: middle;
26 line-height: 30px;
27 font-family: Arial, Helvetica, sans-serif;
28}
29
30.ccontroller {
31 margin-top: -12px;
32}
33
34.creator1 {
35 font-size: 25px;
36 color: white;
37 text-align: left;
38 margin: 0;
39 justify-content: center;
40 vertical-align: middle;
41 line-height: 0px;
42 font-family: Arial, Helvetica, sans-serif;
43}
44
45.creator2 {
46 color: white;
47 font-size: 25px;
48 text-align: right;
49 margin: 0;
50 justify-content: center;
51 vertical-align: middle;
52 line-height: 0px;
53 font-family: Arial, Helvetica, sans-serif;
54}
55
56.aboutus {
57 margin: 0;
58 position: absolute;
59 top: 50%;
60 left: 25%;
61 -ms-transform: translate(-50%, -50%);
62 transform: translate(-50%, -50%);
63 color: white;
64 background-color: #123456;
65 height: 15px;
66 width: auto;
67}
68
69body {
70 background-color: #123456;
71 background-repeat: no-repeat;
72 background-position: center;
73 background-attachment: fixed;
74 background-size: 100% 100%;
75 min-width: 550px;
76 position: relative;
77 margin: 0;
78 padding: 0;
79}
80
81html,
82body {
83 min-width: 550px;
84 position: relative;
85 margin: 0;
86 padding: 0;
87}<body>
88 <div class="flex_container">
89 <div class="bar">
90 <h1 class="tekst" title="Coffeesnakes">Coffeesnakes</h1>
91 <button class="aboutus" onclick="func()">About us!</button>
92 <div class="ccontroller">
93 <p class="creator1" title="Can code: Java, c#, html, js, python, arduino">Christian2B</p>
94 <p class="creator2" title="Can code: html, python, js ">Timgb11</p>
95 </div>
96 </div>
97 </div>
98</body>.bar-element {
99 display: flex;
100 flex-direction: row;
101}
102
103@media (max-width:700px){
104 .bar-element {
105 display: flex;
106 flex-direction: column;
107 }
108}
109.flex {
110 display: flex;
111 flex-direction: row;
112}
113
114p {
115 margin: 5px;
116}
117
118@media (max-width:700px){
119 .flex {
120 display: flex;
121 flex-direction: column;
122 }
123
124}
1.flex_container {
2 display: flex;
3 flex-wrap: wrap;
4 min-width: 550px;
5 flex-direction: column;
6 position: relative;
7}
8
9.bar {
10 position: relative;
11 background-color: rgb(41, 80, 143);
12 border: 5px solid rgb(46, 84, 149);
13 height: 30px;
14 width: auto;
15 margin: 0;
16}
17
18.tekst {
19 color: white;
20 position: static;
21 text-transform: uppercase;
22 text-align: center;
23 margin: 0;
24 justify-content: center;
25 vertical-align: middle;
26 line-height: 30px;
27 font-family: Arial, Helvetica, sans-serif;
28}
29
30.ccontroller {
31 margin-top: -12px;
32}
33
34.creator1 {
35 font-size: 25px;
36 color: white;
37 text-align: left;
38 margin: 0;
39 justify-content: center;
40 vertical-align: middle;
41 line-height: 0px;
42 font-family: Arial, Helvetica, sans-serif;
43}
44
45.creator2 {
46 color: white;
47 font-size: 25px;
48 text-align: right;
49 margin: 0;
50 justify-content: center;
51 vertical-align: middle;
52 line-height: 0px;
53 font-family: Arial, Helvetica, sans-serif;
54}
55
56.aboutus {
57 margin: 0;
58 position: absolute;
59 top: 50%;
60 left: 25%;
61 -ms-transform: translate(-50%, -50%);
62 transform: translate(-50%, -50%);
63 color: white;
64 background-color: #123456;
65 height: 15px;
66 width: auto;
67}
68
69body {
70 background-color: #123456;
71 background-repeat: no-repeat;
72 background-position: center;
73 background-attachment: fixed;
74 background-size: 100% 100%;
75 min-width: 550px;
76 position: relative;
77 margin: 0;
78 padding: 0;
79}
80
81html,
82body {
83 min-width: 550px;
84 position: relative;
85 margin: 0;
86 padding: 0;
87}<body>
88 <div class="flex_container">
89 <div class="bar">
90 <h1 class="tekst" title="Coffeesnakes">Coffeesnakes</h1>
91 <button class="aboutus" onclick="func()">About us!</button>
92 <div class="ccontroller">
93 <p class="creator1" title="Can code: Java, c#, html, js, python, arduino">Christian2B</p>
94 <p class="creator2" title="Can code: html, python, js ">Timgb11</p>
95 </div>
96 </div>
97 </div>
98</body>.bar-element {
99 display: flex;
100 flex-direction: row;
101}
102
103@media (max-width:700px){
104 .bar-element {
105 display: flex;
106 flex-direction: column;
107 }
108}
109.flex {
110 display: flex;
111 flex-direction: row;
112}
113
114p {
115 margin: 5px;
116}
117
118@media (max-width:700px){
119 .flex {
120 display: flex;
121 flex-direction: column;
122 }
123
124}<div class="flex">
125 <p>Hello</p>
126 <p>Hello</p>
127 <p>Hello</p>
128</div>
You can also check out the link here, to learn more on how to change properties on different screen sizes.
Here is your code edited:
1.flex_container {
2 display: flex;
3 flex-wrap: wrap;
4 min-width: 550px;
5 flex-direction: column;
6 position: relative;
7}
8
9.bar {
10 position: relative;
11 background-color: rgb(41, 80, 143);
12 border: 5px solid rgb(46, 84, 149);
13 height: 30px;
14 width: auto;
15 margin: 0;
16}
17
18.tekst {
19 color: white;
20 position: static;
21 text-transform: uppercase;
22 text-align: center;
23 margin: 0;
24 justify-content: center;
25 vertical-align: middle;
26 line-height: 30px;
27 font-family: Arial, Helvetica, sans-serif;
28}
29
30.ccontroller {
31 margin-top: -12px;
32}
33
34.creator1 {
35 font-size: 25px;
36 color: white;
37 text-align: left;
38 margin: 0;
39 justify-content: center;
40 vertical-align: middle;
41 line-height: 0px;
42 font-family: Arial, Helvetica, sans-serif;
43}
44
45.creator2 {
46 color: white;
47 font-size: 25px;
48 text-align: right;
49 margin: 0;
50 justify-content: center;
51 vertical-align: middle;
52 line-height: 0px;
53 font-family: Arial, Helvetica, sans-serif;
54}
55
56.aboutus {
57 margin: 0;
58 position: absolute;
59 top: 50%;
60 left: 25%;
61 -ms-transform: translate(-50%, -50%);
62 transform: translate(-50%, -50%);
63 color: white;
64 background-color: #123456;
65 height: 15px;
66 width: auto;
67}
68
69body {
70 background-color: #123456;
71 background-repeat: no-repeat;
72 background-position: center;
73 background-attachment: fixed;
74 background-size: 100% 100%;
75 min-width: 550px;
76 position: relative;
77 margin: 0;
78 padding: 0;
79}
80
81html,
82body {
83 min-width: 550px;
84 position: relative;
85 margin: 0;
86 padding: 0;
87}<body>
88 <div class="flex_container">
89 <div class="bar">
90 <h1 class="tekst" title="Coffeesnakes">Coffeesnakes</h1>
91 <button class="aboutus" onclick="func()">About us!</button>
92 <div class="ccontroller">
93 <p class="creator1" title="Can code: Java, c#, html, js, python, arduino">Christian2B</p>
94 <p class="creator2" title="Can code: html, python, js ">Timgb11</p>
95 </div>
96 </div>
97 </div>
98</body>.bar-element {
99 display: flex;
100 flex-direction: row;
101}
102
103@media (max-width:700px){
104 .bar-element {
105 display: flex;
106 flex-direction: column;
107 }
108}
109.flex {
110 display: flex;
111 flex-direction: row;
112}
113
114p {
115 margin: 5px;
116}
117
118@media (max-width:700px){
119 .flex {
120 display: flex;
121 flex-direction: column;
122 }
123
124}<div class="flex">
125 <p>Hello</p>
126 <p>Hello</p>
127 <p>Hello</p>
128</div><!DOCTYPE html>
129<html lang="en">
130
131<head>
132 <meta charset="UTF-8">
133 <meta http-equiv="X-UA-Compatible" content="IE=edge">
134 <meta name="viewport" content="width=device-width, initial-scale=1.0">
135 <title>Document</title>
136</head>
137<style>
138 /* .flex_container {
139 display: flex;
140 flex-wrap: wrap;
141 min-width: 550px;
142 flex-direction: column;
143 position: relative;
144 } */
145
146 .bar {
147 position: relative;
148 background-color: rgb(41, 80, 143);
149 border: 5px solid rgb(46, 84, 149);
150 height: 30px;
151 width: auto;
152 margin: 0;
153 display: flex;
154 flex-direction: row;
155 justify-content: space-between;
156 }
157
158 .tekst {
159 color: white;
160 text-transform: uppercase;
161 text-align: center;
162 margin: 0;
163 vertical-align: middle;
164 line-height: 30px;
165 font-family: Arial, Helvetica, sans-serif;
166 }
167
168 .ccontroller {
169 display: flex;
170 flex-direction: row;
171 width: fit-content;
172 flex-wrap: wrap;
173 justify-content: flex-end;
174 justify-self: flex-end;
175 }
176
177 .creator1 {
178 display: block;
179 font-size: 25px;
180 color: white;
181 text-align: left;
182 margin: 0;
183 line-height: auto;
184 font-family: Arial, Helvetica, sans-serif;
185 margin-right: 10px;
186 }
187
188 .creator2 {
189 display: block;
190 color: white;
191 font-size: 25px;
192 text-align: left;
193 margin: 0;
194 line-height: auto;
195 font-family: Arial, Helvetica, sans-serif;
196 }
197
198 .aboutus {
199 display: block;
200 -ms-transform: translate(-50%, -50%);
201 transform: translate(-50%, -50%);
202 color: white;
203 background-color: #123456;
204 height: fit-content;
205 width: auto;
206 margin-top: 16px;
207 margin-left: 50px;
208 text-align: center;
209 }
210
211 body {
212 background-color: #123456;
213 background-repeat: no-repeat;
214 background-position: center;
215 background-attachment: fixed;
216 background-size: 100% 100%;
217 min-width: 550px;
218 position: relative;
219 margin: 0;
220 padding: 0;
221 }
222
223 html,
224 body {
225 position: relative;
226 margin: 0;
227 padding: 0;
228 }
229
230 @media (max-width:700px) {
231 .bar {
232 display: flex;
233 flex-direction: column;
234 }
235 .ccontroller {
236 display: flex;
237 flex-direction: column;
238 }
239 }
240</style>
241
242<body>
243 <div class="flex_container">
244 <div class="bar">
245 <h1 class="tekst" title="Coffeesnakes">Coffeesnakes</h1>
246 <button class="aboutus" onclick="func()">About us!</button>
247 <div class="ccontroller">
248 <p class="creator1" title="Can code: Java, c#, html, js, python, arduino">Christian2B</p>
249 <p class="creator2" title="Can code: html, python, js ">Timgb11</p>
250 </div>
251 </div>
252 </div>
253</body>
254
255</html>
QUESTION
What is the Rust equivalent of Serial.println from the Arduino C++ API?
Asked 2022-Jan-27 at 18:21A common pattern in Arduino C++ sketches is the use of Serial.print()
or Serial.println()
to debug problems.
What is the corresponding Rust idiom when programming for the Arduino Uno?
ANSWER
Answered 2022-Jan-27 at 18:21One technique involves the use of arduino_hal::default_serial!
and ufmt::writeln!
For setup:
1let dp = arduino_hal::Peripherals::take().unwrap();
2let pins = arduino_hal::pins!(dp);
3let mut serial = arduino_hal::default_serial!(dp, pins, 115200);
4
and when you need to print
1let dp = arduino_hal::Peripherals::take().unwrap();
2let pins = arduino_hal::pins!(dp);
3let mut serial = arduino_hal::default_serial!(dp, pins, 115200);
4ufmt::uwriteln!(&mut serial, "{} bytes available", count);
5
The uwriteln!
macro is not as powerful as format!
or println!
from std
. For instance, it does not support {:02x}
for integers.
QUESTION
How to fix Failed to connect to ESP32: Timed out waiting for packet header error?
Asked 2022-Jan-24 at 17:16I tried to flash an ESP32 but I didn't succeed, I tried with esptool.py and Arduino IDE. I tried to press the boot button and reset but it didn't work. Here is the error:
1A fatal error occurred: Failed to connect to ESP32: Timed out waiting for packet header
2
ANSWER
Answered 2022-Jan-01 at 22:17To make your ESP32 board go into flashing/uploading mode automatically, you can connect a 10 μF electrolytic capacitor between the EN
pin and GND
. When I had the same problem, when I followed this advice, the problem was solved.
References
QUESTION
How to make an object take and store an Array of arbitrary, but compile-time known size?
Asked 2022-Jan-20 at 15:19For an embedded project, I want a class that takes a list of structs. This list is known at compile-time, so I shouldn't have to resort to dynamic memory allocation for this.
However, how do I make a struct/class that encapsulates this array without having to use its size as a template parameter?
TemplatesMy first idea was to do exactly that:
1struct Point {
2 const uint16_t a;
3 const double b;
4};
5
6template<size_t n>
7struct Profile {
8 Array<Point, n> points;
9
10 Profile(const Array<Point, n> &points) : points(points) {}
11};
12
Here, Profile
is the class that stores/encapsulates the array of points (the 2-member structs). n
, the size of the array, is a template parameter.
I'm using this implementation of Array, similar to std::array
, btw, because I don't have access to the STL on this embedded platform.
However, no I have another class that uses this Profile
that now also has to be templated because Profile
is templated with the size of the array:
1struct Point {
2 const uint16_t a;
3 const double b;
4};
5
6template<size_t n>
7struct Profile {
8 Array<Point, n> points;
9
10 Profile(const Array<Point, n> &points) : points(points) {}
11};
12template<size_t n>
13class Runner {
14private:
15 const Profile<n> profile;
16public:
17 Runner(const Profile<n> &profile) : profile(profile) {};
18
19 void foo() {
20 for(auto point : profile.points) {
21 // do something
22 }
23 }
24};
25
As can be seen, this Runner
class operates on a Profile
and iterates over it. Having to template Runner
is not that much of an issue by itself, but this Runner
in turn is used by another class in my project, because this other class calls Runner::foo()
. Now I have to template that class as well! And classes that use that class, etc.
That's getting out of hand! What started with just one template parameter to specify the size, now propagates through my entire application. Therefore, I don't think this is a good solution.
QuestionIs there a way to 'hide' the size of the array in Profile
or Runner
? Runner only needs to iterate over it, so the size should in principle only affect its implementation, not its public interface. How would I do that, though?
Also, can I avoid having to manually specify n
at all, and just pass an array to Profile
's constructor and let the compiler figure out how big it is? At compile-time, of course. I feel like this should be possible (given this array is known at compile-time), but I don't know how exactly.
I could write a macro like
1struct Point {
2 const uint16_t a;
3 const double b;
4};
5
6template<size_t n>
7struct Profile {
8 Array<Point, n> points;
9
10 Profile(const Array<Point, n> &points) : points(points) {}
11};
12template<size_t n>
13class Runner {
14private:
15 const Profile<n> profile;
16public:
17 Runner(const Profile<n> &profile) : profile(profile) {};
18
19 void foo() {
20 for(auto point : profile.points) {
21 // do something
22 }
23 }
24};
25#define n 12
26
and include that in both the Profile.h
and the place where I instantiate a Profile
. This feels dirty though, I and would like to avoid macros.
I could avoid this fuss by just using a std::vector
(or equivalent) instead, but that is allocated at run-time on the heap, which I would like to avoid here since it shouldn't be necessary.
ANSWER
Answered 2022-Jan-20 at 15:12Is there a way to 'hide' the size of the array in Profile or Runner?
Yes. The solution is indirection. Instead of storing the object directly, you can point to it. You don't need to know the size of what you're pointing at.
A convenient solution is to point into dynamic storage (for example std::vector
) because it allows you to "bind" the lifetime of the dynamically sized object to a member. That's not necessary in general, and you can use automatic storage instead. However, in that case you cannot bind the lifetime of the pointed object, and you must be very careful to not let the pointed object be destroyed before you stop using it.
The indirection can be done at whatever level you prefer. If you do it at the lowest level, you simply store the array outside of Profile
. In fact, if all that profile does is contain an array, then you don't need a class for it. Use a generic span
:
1struct Point {
2 const uint16_t a;
3 const double b;
4};
5
6template<size_t n>
7struct Profile {
8 Array<Point, n> points;
9
10 Profile(const Array<Point, n> &points) : points(points) {}
11};
12template<size_t n>
13class Runner {
14private:
15 const Profile<n> profile;
16public:
17 Runner(const Profile<n> &profile) : profile(profile) {};
18
19 void foo() {
20 for(auto point : profile.points) {
21 // do something
22 }
23 }
24};
25#define n 12
26struct Runner {
27 span<const Point> profile;
28 void foo() {
29 for(auto point : profile) {
30 // do something
31 }
32 }
33};
34
35Point points[] {
36 // ... number of points doesn't matter
37};
38Runner runner {
39 .profile = points,
40};
41
By span
, I mean something like std::span
. If you cannot use the standard library, then use another implementation. It's basically just a pointer and size, with convenient template constructor.
To clarify, you can pick any two, but you cannot have all three of these:
- Lifetime of the array bound to the class (safe)
- No compiletime constant size
- No dynamic storage
- 1,2 (no 3) =
std::vector
, RAII - 1,3 (no 2) =
std::array
, templates, no indirection - 2,3 (no 1) =
std::span
, be careful with lifetimes
QUESTION
how to clear oled display in micropython
Asked 2022-Jan-10 at 17:36I'm doing this on esp8266 with micro python and there is a way to clear OLED display in Arduino but I don't know how to clear display in micropython i used ssd1306 library to control my OLED
and this is my error I've written a code that prints on OLED from a list loop, but OLED prints it on the text that was printed before it (print one on top of the other not clear and printing) 7
1display = [ip, request_uri, country, region, city]
2
3for real_time in display:
4 oled.text(real_time, 0, 20)
5 oled.show()
6 time.sleep(2)
7 print(real_time)
8
ANSWER
Answered 2022-Jan-10 at 17:36The fill()
method is used to clean the OLED screen:
1display = [ip, request_uri, country, region, city]
2
3for real_time in display:
4 oled.text(real_time, 0, 20)
5 oled.show()
6 time.sleep(2)
7 print(real_time)
8oled.fill(0)
9oled.show()
10
Community Discussions contain sources that include Stack Exchange Network
Tutorials and Learning Resources in Arduino
Tutorials and Learning Resources are not available at this moment for Arduino