Explore all Arduino open source software, libraries, packages, source code, cloud functions and APIs.

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

Tasmota

by arendst doticoncdoticon

star image 17398 doticonGPL-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

Arduino

by esp8266 doticonc++doticon

star image 13604 doticonLGPL-2.1

ESP8266 core for Arduino

Arduino

by arduino doticonjavadoticon

star image 12500 doticonNOASSERTION

open-source electronics platform

johnny-five

by rwaldron doticonjavascriptdoticon

star image 12386 doticonNOASSERTION

JavaScript Robotics and IoT programming framework, developed at Bocoup.

qmk_firmware

by qmk doticoncdoticon

star image 12112 doticonNOASSERTION

Open-source keyboard firmware for Atmel AVR and Arm USB families

Marlin

by MarlinFirmware doticonc++doticon

star image 11790 doticonGPL-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.

esp8266_deauther

by SpacehuhnTech doticoncdoticon

star image 8891 doticonNOASSERTION

Affordable WiFi hacking platform for testing and learning

arduino-esp32

by espressif doticoncdoticon

star image 8437 doticonLGPL-2.1

Arduino core for the ESP32

BlackHole

by ExistentialAudio doticoncdoticon

star image 7803 doticonGPL-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

connectedhomeip

by project-chip doticonc++doticon

star image 3565 doticonApache-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).

Serial-Studio

by Serial-Studio doticonc++doticon

star image 2320 doticonNOASSERTION

Multi-purpose serial data visualization & processing program

OpenBot

by isl-org doticonjavadoticon

star image 2104 doticonMIT

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.

OpenBot

by intel-isl doticonjavadoticon

star image 1827 doticonMIT

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.

spot-sdk

by boston-dynamics doticonpythondoticon

star image 1812 doticonNOASSERTION

Spot SDK repo

ATC_MiThermometer

by atc1441 doticoncdoticon

star image 1660 doticon

Custom firmware for the Xiaomi Thermometer LYWSD03MMC and Telink Flasher via USB to Serial converter

spotMicro

by mike4192 doticonc++doticon

star image 1398 doticonMIT

Spot Micro Quadruped Project

send-my

by positive-security doticoncdoticon

star image 1329 doticonAGPL-3.0

Upload arbitrary data via Apple's Find My network.

deej

by omriharel doticongodoticon

star image 979 doticonMIT

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

sparkfun

223 Libraries

star icon5573

2

adafruit

217 Libraries

star icon18455

3

Seeed-Studio

84 Libraries

star icon1393

4

arduino-libraries

69 Libraries

star icon2783

5

arduino

59 Libraries

star icon20426

6

G6EJD

51 Libraries

star icon788

7

khoih-prog

41 Libraries

star icon1309

8

proyectoTEOS

40 Libraries

star icon126

9

platformio

39 Libraries

star icon9082

10

pololu

38 Libraries

star icon1265

1

223 Libraries

star icon5573

2

217 Libraries

star icon18455

3

84 Libraries

star icon1393

4

69 Libraries

star icon2783

5

59 Libraries

star icon20426

6

51 Libraries

star icon788

7

41 Libraries

star icon1309

8

40 Libraries

star icon126

9

39 Libraries

star icon9082

10

38 Libraries

star icon1265

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:39

I 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:21
General Observations:

While the code compiles and runs on Windows 10 in Visual Studio 2019, there are multiple problems here, not necessarily with threading.

  1. Without knowing what the expected output for the test case it is very difficult to determine if the code is running correctly.
  2. Object oriented programming rules are being broken, there doesn't seem to be any encapsulation. All of the variables and methods are public.
  3. This looks more like C code that C++ code except for the classes themselves.
  4. 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).
Avoid 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.

Declare Public Variables and Methods at the Top of the Class

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.

Prefer C++ I/O Over C Programming I/O

While C++ is backward compatible with C, using printf() in C++ is exceptionally rare, std::cin and std::cout are preferred in C++.

A Base Class Does Not Need to be an Abstract Class

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 &lt;future&gt;
3using namespace ::std;
4
5class sliceWork
6{
7    int sliceIntervalMilliSeconds;
8    int failureCounter;
9    int maxFailsBeforeRestart;
10    char* label = NULL;
11    
12    promise&lt;void&gt; workPromise;
13    thread* workerThread = NULL;
14
15    virtual void init() = 0;
16    virtual bool oneSliceWork() = 0;
17    void work(future&lt;void&gt; future);
18
19public:
20    sliceWork(int sliceInterval, int maxFails, const char* label);
21    ~sliceWork();
22    void initWork();
23    void signalTerminate();
24};
25#include &lt;string.h&gt;
26#include &quot;sliceWork.h&quot;
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 &amp;&amp; workerThread-&gt;joinable())
39        workerThread-&gt;join();
40    printf(&quot;destructor %s\n&quot;, label);
41    delete label;
42    delete workerThread;
43}
44
45void sliceWork::initWork()
46{
47    failureCounter = 0;
48    init();
49    printf(&quot;Init work %s finished!\n&quot;, label);
50    future&lt;void&gt; futureWorker = workPromise.get_future();
51    workerThread = new thread(&amp;sliceWork::work, this, move(futureWorker));
52}
53
54void sliceWork::work(future&lt;void&gt; 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&lt;chrono::milliseconds&gt;(steady_clock::now() - t0).count() 
62            &gt; sliceIntervalMilliSeconds)
63        {
64            if (!oneSliceWork())
65            {
66                if (++failureCounter &gt; maxFailsBeforeRestart 
67                    &amp;&amp; maxFailsBeforeRestart &gt; 0)
68                {
69                    init();
70                    failureCounter = 0;
71                }
72            }
73            t0 = steady_clock::now();
74        }
75    }
76    printf(&quot;work terminated for %s!\n&quot;, label);
77}
78
79void sliceWork::signalTerminate()
80{
81    printf(&quot;request terminate for work %s...\n&quot;, label);
82    workPromise.set_value();
83}
84#include &lt;string.h&gt;
85#include &quot;sliceWork.h&quot;
86
87class A : public sliceWork
88{
89    void init() {
90        printf(&quot;Init A...\n&quot;);
91    }
92
93    bool oneSliceWork() {
94        printf(&quot;Working A...\n&quot;);
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(&quot;Init B...\n&quot;);
108    }
109
110    bool oneSliceWork() {
111        printf(&quot;Working B...\n&quot;);
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(&quot;Init C...\n&quot;);
125    }
126
127    bool oneSliceWork() {
128        printf(&quot;Working C...\n&quot;);
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, &quot;A&quot;);
142         a.initWork();
143         B b(2000, 1000, &quot;B&quot; );
144         b.initWork();
145         C c(700, 2, &quot;C&quot; );
146         c.initWork();
147         printf(&quot;Initializations finished!\n&quot;);
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 &lt;iostream&gt;
160#include &lt;future&gt;
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 &lt;&lt; &quot;Init &quot; &lt;&lt; label &lt;&lt; &quot;..\n&quot;;
174    }
175    virtual bool oneSliceWork()
176    {
177        std::cout &lt;&lt; &quot;Working &quot; &lt;&lt; label &lt;&lt; &quot;..\n&quot;;
178        return true;
179    }
180    void work(std::future&lt;void&gt; future);
181
182private:
183    int sliceIntervalMilliSeconds;
184    int failureCounter;
185    int maxFailsBeforeRestart;
186    char* label = NULL;
187    std::promise&lt;void&gt; workPromise;
188    std::thread* workerThread = NULL;
189};
190

main.cpp

1#pragma once
2#include &lt;future&gt;
3using namespace ::std;
4
5class sliceWork
6{
7    int sliceIntervalMilliSeconds;
8    int failureCounter;
9    int maxFailsBeforeRestart;
10    char* label = NULL;
11    
12    promise&lt;void&gt; workPromise;
13    thread* workerThread = NULL;
14
15    virtual void init() = 0;
16    virtual bool oneSliceWork() = 0;
17    void work(future&lt;void&gt; future);
18
19public:
20    sliceWork(int sliceInterval, int maxFails, const char* label);
21    ~sliceWork();
22    void initWork();
23    void signalTerminate();
24};
25#include &lt;string.h&gt;
26#include &quot;sliceWork.h&quot;
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 &amp;&amp; workerThread-&gt;joinable())
39        workerThread-&gt;join();
40    printf(&quot;destructor %s\n&quot;, label);
41    delete label;
42    delete workerThread;
43}
44
45void sliceWork::initWork()
46{
47    failureCounter = 0;
48    init();
49    printf(&quot;Init work %s finished!\n&quot;, label);
50    future&lt;void&gt; futureWorker = workPromise.get_future();
51    workerThread = new thread(&amp;sliceWork::work, this, move(futureWorker));
52}
53
54void sliceWork::work(future&lt;void&gt; 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&lt;chrono::milliseconds&gt;(steady_clock::now() - t0).count() 
62            &gt; sliceIntervalMilliSeconds)
63        {
64            if (!oneSliceWork())
65            {
66                if (++failureCounter &gt; maxFailsBeforeRestart 
67                    &amp;&amp; maxFailsBeforeRestart &gt; 0)
68                {
69                    init();
70                    failureCounter = 0;
71                }
72            }
73            t0 = steady_clock::now();
74        }
75    }
76    printf(&quot;work terminated for %s!\n&quot;, label);
77}
78
79void sliceWork::signalTerminate()
80{
81    printf(&quot;request terminate for work %s...\n&quot;, label);
82    workPromise.set_value();
83}
84#include &lt;string.h&gt;
85#include &quot;sliceWork.h&quot;
86
87class A : public sliceWork
88{
89    void init() {
90        printf(&quot;Init A...\n&quot;);
91    }
92
93    bool oneSliceWork() {
94        printf(&quot;Working A...\n&quot;);
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(&quot;Init B...\n&quot;);
108    }
109
110    bool oneSliceWork() {
111        printf(&quot;Working B...\n&quot;);
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(&quot;Init C...\n&quot;);
125    }
126
127    bool oneSliceWork() {
128        printf(&quot;Working C...\n&quot;);
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, &quot;A&quot;);
142         a.initWork();
143         B b(2000, 1000, &quot;B&quot; );
144         b.initWork();
145         C c(700, 2, &quot;C&quot; );
146         c.initWork();
147         printf(&quot;Initializations finished!\n&quot;);
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 &lt;iostream&gt;
160#include &lt;future&gt;
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 &lt;&lt; &quot;Init &quot; &lt;&lt; label &lt;&lt; &quot;..\n&quot;;
174    }
175    virtual bool oneSliceWork()
176    {
177        std::cout &lt;&lt; &quot;Working &quot; &lt;&lt; label &lt;&lt; &quot;..\n&quot;;
178        return true;
179    }
180    void work(std::future&lt;void&gt; future);
181
182private:
183    int sliceIntervalMilliSeconds;
184    int failureCounter;
185    int maxFailsBeforeRestart;
186    char* label = NULL;
187    std::promise&lt;void&gt; workPromise;
188    std::thread* workerThread = NULL;
189};
190#include &lt;string.h&gt;
191#include &quot;sliceWork.h&quot;
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, &quot;A&quot;);
224        a.initWork();
225        B b(2000, 1000, &quot;B&quot;);
226        b.initWork();
227        C c(700, 2, &quot;C&quot;);
228        c.initWork();
229        std::cout &lt;&lt; &quot;Initializations finished!\n&quot;;
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 &lt;future&gt;
3using namespace ::std;
4
5class sliceWork
6{
7    int sliceIntervalMilliSeconds;
8    int failureCounter;
9    int maxFailsBeforeRestart;
10    char* label = NULL;
11    
12    promise&lt;void&gt; workPromise;
13    thread* workerThread = NULL;
14
15    virtual void init() = 0;
16    virtual bool oneSliceWork() = 0;
17    void work(future&lt;void&gt; future);
18
19public:
20    sliceWork(int sliceInterval, int maxFails, const char* label);
21    ~sliceWork();
22    void initWork();
23    void signalTerminate();
24};
25#include &lt;string.h&gt;
26#include &quot;sliceWork.h&quot;
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 &amp;&amp; workerThread-&gt;joinable())
39        workerThread-&gt;join();
40    printf(&quot;destructor %s\n&quot;, label);
41    delete label;
42    delete workerThread;
43}
44
45void sliceWork::initWork()
46{
47    failureCounter = 0;
48    init();
49    printf(&quot;Init work %s finished!\n&quot;, label);
50    future&lt;void&gt; futureWorker = workPromise.get_future();
51    workerThread = new thread(&amp;sliceWork::work, this, move(futureWorker));
52}
53
54void sliceWork::work(future&lt;void&gt; 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&lt;chrono::milliseconds&gt;(steady_clock::now() - t0).count() 
62            &gt; sliceIntervalMilliSeconds)
63        {
64            if (!oneSliceWork())
65            {
66                if (++failureCounter &gt; maxFailsBeforeRestart 
67                    &amp;&amp; maxFailsBeforeRestart &gt; 0)
68                {
69                    init();
70                    failureCounter = 0;
71                }
72            }
73            t0 = steady_clock::now();
74        }
75    }
76    printf(&quot;work terminated for %s!\n&quot;, label);
77}
78
79void sliceWork::signalTerminate()
80{
81    printf(&quot;request terminate for work %s...\n&quot;, label);
82    workPromise.set_value();
83}
84#include &lt;string.h&gt;
85#include &quot;sliceWork.h&quot;
86
87class A : public sliceWork
88{
89    void init() {
90        printf(&quot;Init A...\n&quot;);
91    }
92
93    bool oneSliceWork() {
94        printf(&quot;Working A...\n&quot;);
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(&quot;Init B...\n&quot;);
108    }
109
110    bool oneSliceWork() {
111        printf(&quot;Working B...\n&quot;);
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(&quot;Init C...\n&quot;);
125    }
126
127    bool oneSliceWork() {
128        printf(&quot;Working C...\n&quot;);
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, &quot;A&quot;);
142         a.initWork();
143         B b(2000, 1000, &quot;B&quot; );
144         b.initWork();
145         C c(700, 2, &quot;C&quot; );
146         c.initWork();
147         printf(&quot;Initializations finished!\n&quot;);
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 &lt;iostream&gt;
160#include &lt;future&gt;
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 &lt;&lt; &quot;Init &quot; &lt;&lt; label &lt;&lt; &quot;..\n&quot;;
174    }
175    virtual bool oneSliceWork()
176    {
177        std::cout &lt;&lt; &quot;Working &quot; &lt;&lt; label &lt;&lt; &quot;..\n&quot;;
178        return true;
179    }
180    void work(std::future&lt;void&gt; future);
181
182private:
183    int sliceIntervalMilliSeconds;
184    int failureCounter;
185    int maxFailsBeforeRestart;
186    char* label = NULL;
187    std::promise&lt;void&gt; workPromise;
188    std::thread* workerThread = NULL;
189};
190#include &lt;string.h&gt;
191#include &quot;sliceWork.h&quot;
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, &quot;A&quot;);
224        a.initWork();
225        B b(2000, 1000, &quot;B&quot;);
226        b.initWork();
227        C c(700, 2, &quot;C&quot;);
228        c.initWork();
229        std::cout &lt;&lt; &quot;Initializations finished!\n&quot;;
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 &lt;string.h&gt;
241#include &quot;sliceWork.h&quot;
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 &amp;&amp; workerThread-&gt;joinable())
254        workerThread-&gt;join();
255    printf(&quot;destructor %s\n&quot;, label);
256    delete label;
257    delete workerThread;
258}
259
260void sliceWork::initWork()
261{
262    failureCounter = 0;
263    init();
264    printf(&quot;Init work %s finished!\n&quot;, label);
265    std::future&lt;void&gt; futureWorker = workPromise.get_future();
266    workerThread = new std::thread(&amp;sliceWork::work, this, move(futureWorker));
267}
268
269void sliceWork::work(std::future&lt;void&gt; 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&lt;std::chrono::milliseconds&gt;(steady_clock::now() - t0).count()
277            &gt; sliceIntervalMilliSeconds)
278        {
279            if (!oneSliceWork())
280            {
281                if (++failureCounter &gt; maxFailsBeforeRestart
282                    &amp;&amp; maxFailsBeforeRestart &gt; 0)
283                {
284                    init();
285                    failureCounter = 0;
286                }
287            }
288            t0 = steady_clock::now();
289        }
290    }
291    printf(&quot;work terminated for %s!\n&quot;, label);
292}
293
294void sliceWork::signalTerminate()
295{
296    printf(&quot;request terminate for work %s...\n&quot;, label);
297    workPromise.set_value();
298}
299

Source https://stackoverflow.com/questions/71904521

QUESTION

Sending int from python to arduino, but there is an upper limit. How do I solve it?

Asked 2022-Mar-29 at 10:02

in 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(&quot;s{}&quot;.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(&quot;s{}&quot;.format(int(131.0268562*userInput)).encode())
5
6if userInput == 'c':
7    break;
8    
9if (Serial.available() &gt; 0){
10
11
12int receivedValue = Serial.parseInt();
13
14digitalWrite(4,HIGH);
15
16
17if (receivedValue&gt;=0){
18  for(Index = 0; Index &lt; 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(&quot;s{}&quot;.format(int(131.0268562*userInput)).encode())
5
6if userInput == 'c':
7    break;
8    
9if (Serial.available() &gt; 0){
10
11
12int receivedValue = Serial.parseInt();
13
14digitalWrite(4,HIGH);
15
16
17if (receivedValue&gt;=0){
18  for(Index = 0; Index &lt; 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:55

I 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.

Source https://stackoverflow.com/questions/71659042

QUESTION

exec: &quot;python&quot;: executable file not found in $PATH on Arduino IDE

Asked 2022-Mar-23 at 20:45

So 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: &quot;python&quot;: 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:56

Probably a soft link will do, try sudo ln -s /usr/bin/python3 /usr/bin/python

Source https://stackoverflow.com/questions/71143707

QUESTION

How to filter a class in Angular?

Asked 2022-Feb-04 at 16:12

I'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 =&gt; {
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 =&gt; {
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 =&gt; {
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 =&gt; {
33        console.log(error)
34      }
35    )
36  }
37&lt;div class=&quot;project-list&quot;&gt;
38        &lt;ul&gt;
39            &lt;li *ngFor=&quot;let project of projects&quot; class=&quot;project&quot;&gt;
40              &lt;ng-container *ngIf=&quot;project.arduino == true&quot;&gt; ************ Here i filtered it in that way
41                    &lt;a [routerLink]=&quot;['/project', project._id]&quot;&gt;
42            
43                    &lt;div class=&quot;image-box&quot;&gt;
44                        &lt;div class=&quot;image&quot;&gt;
45                        &lt;img src=&quot;{{ url+'get-image/'+project.imagefront }}&quot; *ngIf=&quot;project.imagefront&quot; /&gt;
46                        &lt;/div&gt;
47                    &lt;/div&gt;
48                  &lt;/a&gt;
49              &lt;/ng-container&gt;
50            &lt;/li&gt;
51        &lt;/ul&gt;
52      &lt;/div&gt;
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:12

I 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 =&gt; {
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 =&gt; {
33        console.log(error)
34      }
35    )
36  }
37&lt;div class=&quot;project-list&quot;&gt;
38        &lt;ul&gt;
39            &lt;li *ngFor=&quot;let project of projects&quot; class=&quot;project&quot;&gt;
40              &lt;ng-container *ngIf=&quot;project.arduino == true&quot;&gt; ************ Here i filtered it in that way
41                    &lt;a [routerLink]=&quot;['/project', project._id]&quot;&gt;
42            
43                    &lt;div class=&quot;image-box&quot;&gt;
44                        &lt;div class=&quot;image&quot;&gt;
45                        &lt;img src=&quot;{{ url+'get-image/'+project.imagefront }}&quot; *ngIf=&quot;project.imagefront&quot; /&gt;
46                        &lt;/div&gt;
47                    &lt;/div&gt;
48                  &lt;/a&gt;
49              &lt;/ng-container&gt;
50            &lt;/li&gt;
51        &lt;/ul&gt;
52      &lt;/div&gt;
53&lt;ng-container *ngIf=&quot;this.project.arduino&quot;&gt;
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 =&gt; {
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 =&gt; {
33        console.log(error)
34      }
35    )
36  }
37&lt;div class=&quot;project-list&quot;&gt;
38        &lt;ul&gt;
39            &lt;li *ngFor=&quot;let project of projects&quot; class=&quot;project&quot;&gt;
40              &lt;ng-container *ngIf=&quot;project.arduino == true&quot;&gt; ************ Here i filtered it in that way
41                    &lt;a [routerLink]=&quot;['/project', project._id]&quot;&gt;
42            
43                    &lt;div class=&quot;image-box&quot;&gt;
44                        &lt;div class=&quot;image&quot;&gt;
45                        &lt;img src=&quot;{{ url+'get-image/'+project.imagefront }}&quot; *ngIf=&quot;project.imagefront&quot; /&gt;
46                        &lt;/div&gt;
47                    &lt;/div&gt;
48                  &lt;/a&gt;
49              &lt;/ng-container&gt;
50            &lt;/li&gt;
51        &lt;/ul&gt;
52      &lt;/div&gt;
53&lt;ng-container *ngIf=&quot;this.project.arduino&quot;&gt;
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">

Source https://stackoverflow.com/questions/70988822

QUESTION

Reading Arduino Serial data on C# application but port access denied?

Asked 2022-Feb-01 at 21:45

When 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:45

Try 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.

Source https://stackoverflow.com/questions/70947903

QUESTION

Stop text from overlapping html and css

Asked 2022-Jan-28 at 15:52

when 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}&lt;body&gt;
88  &lt;div class="flex_container"&gt;
89    &lt;div class="bar"&gt;
90      &lt;h1 class="tekst" title="Coffeesnakes"&gt;Coffeesnakes&lt;/h1&gt;
91      &lt;button class="aboutus" onclick="func()"&gt;About us!&lt;/button&gt;
92      &lt;div class="ccontroller"&gt;
93        &lt;p class="creator1" title="Can code: Java, c#, html, js, python, arduino"&gt;Christian2B&lt;/p&gt;
94        &lt;p class="creator2" title="Can code: html, python, js "&gt;Timgb11&lt;/p&gt;
95      &lt;/div&gt;
96    &lt;/div&gt;
97  &lt;/div&gt;
98&lt;/body&gt;

ANSWER

Answered 2022-Jan-28 at 09:25

A 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}&lt;body&gt;
88  &lt;div class="flex_container"&gt;
89    &lt;div class="bar"&gt;
90      &lt;h1 class="tekst" title="Coffeesnakes"&gt;Coffeesnakes&lt;/h1&gt;
91      &lt;button class="aboutus" onclick="func()"&gt;About us!&lt;/button&gt;
92      &lt;div class="ccontroller"&gt;
93        &lt;p class="creator1" title="Can code: Java, c#, html, js, python, arduino"&gt;Christian2B&lt;/p&gt;
94        &lt;p class="creator2" title="Can code: html, python, js "&gt;Timgb11&lt;/p&gt;
95      &lt;/div&gt;
96    &lt;/div&gt;
97  &lt;/div&gt;
98&lt;/body&gt;.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}&lt;body&gt;
88  &lt;div class="flex_container"&gt;
89    &lt;div class="bar"&gt;
90      &lt;h1 class="tekst" title="Coffeesnakes"&gt;Coffeesnakes&lt;/h1&gt;
91      &lt;button class="aboutus" onclick="func()"&gt;About us!&lt;/button&gt;
92      &lt;div class="ccontroller"&gt;
93        &lt;p class="creator1" title="Can code: Java, c#, html, js, python, arduino"&gt;Christian2B&lt;/p&gt;
94        &lt;p class="creator2" title="Can code: html, python, js "&gt;Timgb11&lt;/p&gt;
95      &lt;/div&gt;
96    &lt;/div&gt;
97  &lt;/div&gt;
98&lt;/body&gt;.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}&lt;body&gt;
88  &lt;div class="flex_container"&gt;
89    &lt;div class="bar"&gt;
90      &lt;h1 class="tekst" title="Coffeesnakes"&gt;Coffeesnakes&lt;/h1&gt;
91      &lt;button class="aboutus" onclick="func()"&gt;About us!&lt;/button&gt;
92      &lt;div class="ccontroller"&gt;
93        &lt;p class="creator1" title="Can code: Java, c#, html, js, python, arduino"&gt;Christian2B&lt;/p&gt;
94        &lt;p class="creator2" title="Can code: html, python, js "&gt;Timgb11&lt;/p&gt;
95      &lt;/div&gt;
96    &lt;/div&gt;
97  &lt;/div&gt;
98&lt;/body&gt;.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}&lt;div class="flex"&gt;
125  &lt;p&gt;Hello&lt;/p&gt;
126  &lt;p&gt;Hello&lt;/p&gt;
127  &lt;p&gt;Hello&lt;/p&gt;
128&lt;/div&gt;

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}&lt;body&gt;
88  &lt;div class="flex_container"&gt;
89    &lt;div class="bar"&gt;
90      &lt;h1 class="tekst" title="Coffeesnakes"&gt;Coffeesnakes&lt;/h1&gt;
91      &lt;button class="aboutus" onclick="func()"&gt;About us!&lt;/button&gt;
92      &lt;div class="ccontroller"&gt;
93        &lt;p class="creator1" title="Can code: Java, c#, html, js, python, arduino"&gt;Christian2B&lt;/p&gt;
94        &lt;p class="creator2" title="Can code: html, python, js "&gt;Timgb11&lt;/p&gt;
95      &lt;/div&gt;
96    &lt;/div&gt;
97  &lt;/div&gt;
98&lt;/body&gt;.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}&lt;div class="flex"&gt;
125  &lt;p&gt;Hello&lt;/p&gt;
126  &lt;p&gt;Hello&lt;/p&gt;
127  &lt;p&gt;Hello&lt;/p&gt;
128&lt;/div&gt;&lt;!DOCTYPE html&gt;
129&lt;html lang="en"&gt;
130
131&lt;head&gt;
132    &lt;meta charset="UTF-8"&gt;
133    &lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&gt;
134    &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
135    &lt;title&gt;Document&lt;/title&gt;
136&lt;/head&gt;
137&lt;style&gt;
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&lt;/style&gt;
241
242&lt;body&gt;
243    &lt;div class="flex_container"&gt;
244        &lt;div class="bar"&gt;
245            &lt;h1 class="tekst" title="Coffeesnakes"&gt;Coffeesnakes&lt;/h1&gt;
246            &lt;button class="aboutus" onclick="func()"&gt;About us!&lt;/button&gt;
247            &lt;div class="ccontroller"&gt;
248                &lt;p class="creator1" title="Can code: Java, c#, html, js, python, arduino"&gt;Christian2B&lt;/p&gt;
249                &lt;p class="creator2" title="Can code: html, python, js "&gt;Timgb11&lt;/p&gt;
250            &lt;/div&gt;
251        &lt;/div&gt;
252    &lt;/div&gt;
253&lt;/body&gt;
254
255&lt;/html&gt;

Source https://stackoverflow.com/questions/70890759

QUESTION

What is the Rust equivalent of Serial.println from the Arduino C++ API?

Asked 2022-Jan-27 at 18:21

A 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:21

One 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!(&amp;mut serial, &quot;{} bytes available&quot;, count);
5

The uwriteln! macro is not as powerful as format! or println! from std. For instance, it does not support {:02x} for integers.

Source https://stackoverflow.com/questions/70883797

QUESTION

How to fix Failed to connect to ESP32: Timed out waiting for packet header error?

Asked 2022-Jan-24 at 17:16

I 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:17

To 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

Source https://stackoverflow.com/questions/70532139

QUESTION

How to make an object take and store an Array of arbitrary, but compile-time known size?

Asked 2022-Jan-20 at 15:19
Background

For 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?

Templates

My first idea was to do exactly that:

1struct Point {
2    const uint16_t a;
3    const double b;
4};
5
6template&lt;size_t n&gt;
7struct Profile {
8    Array&lt;Point, n&gt; points;
9
10    Profile(const Array&lt;Point, n&gt; &amp;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&lt;size_t n&gt;
7struct Profile {
8    Array&lt;Point, n&gt; points;
9
10    Profile(const Array&lt;Point, n&gt; &amp;points) : points(points) {}
11};
12template&lt;size_t n&gt;
13class Runner {
14private:
15    const Profile&lt;n&gt; profile;
16public:
17    Runner(const Profile&lt;n&gt; &amp;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.

Question

Is 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.

Other approaches Macros

I could write a macro like

1struct Point {
2    const uint16_t a;
3    const double b;
4};
5
6template&lt;size_t n&gt;
7struct Profile {
8    Array&lt;Point, n&gt; points;
9
10    Profile(const Array&lt;Point, n&gt; &amp;points) : points(points) {}
11};
12template&lt;size_t n&gt;
13class Runner {
14private:
15    const Profile&lt;n&gt; profile;
16public:
17    Runner(const Profile&lt;n&gt; &amp;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.

Vector

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:12

Is 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&lt;size_t n&gt;
7struct Profile {
8    Array&lt;Point, n&gt; points;
9
10    Profile(const Array&lt;Point, n&gt; &amp;points) : points(points) {}
11};
12template&lt;size_t n&gt;
13class Runner {
14private:
15    const Profile&lt;n&gt; profile;
16public:
17    Runner(const Profile&lt;n&gt; &amp;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&lt;const Point&gt; 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:

  1. Lifetime of the array bound to the class (safe)
  2. No compiletime constant size
  3. 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

Source https://stackoverflow.com/questions/70788168

QUESTION

how to clear oled display in micropython

Asked 2022-Jan-10 at 17:36

I'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) 7enter image description here

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:36

The 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

Source https://stackoverflow.com/questions/70656586

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

Share this Page

share link

Get latest updates on Arduino