Support
Quality
Security
License
Reuse
kandi has reviewed Arduino and discovered the below as its top functions. This is intended to give you an instant insight into Arduino implemented functionality, and help decide if they suit your requirements.
open-source electronics platform
C++11 multithreaded cancellable slice-based work
#pragma once
#include <iostream>
#include <future>
class sliceWork
{
public:
sliceWork(int sliceInterval, int maxFails, const char* label);
~sliceWork();
void initWork();
void signalTerminate();
protected:
virtual void init()
{
std::cout << "Init " << label << "..\n";
}
virtual bool oneSliceWork()
{
std::cout << "Working " << label << "..\n";
return true;
}
void work(std::future<void> future);
private:
int sliceIntervalMilliSeconds;
int failureCounter;
int maxFailsBeforeRestart;
char* label = NULL;
std::promise<void> workPromise;
std::thread* workerThread = NULL;
};
#include <string.h>
#include "sliceWork.h"
class A : public sliceWork
{
public:
A(int slice, int max, const char* label)
: sliceWork(slice, max, label)
{
}
};
class B : public sliceWork
{
public:
B(int slice, int max, const char* label)
: sliceWork(slice, max, label)
{
}
};
class C : public sliceWork
{
public:
C(int slice, int max, const char* label)
: sliceWork(slice, max, label)
{
}
};
int main()
{
{
A a(1000, 1000, "A");
a.initWork();
B b(2000, 1000, "B");
b.initWork();
C c(700, 2, "C");
c.initWork();
std::cout << "Initializations finished!\n";
::std::this_thread::sleep_for(::std::chrono::seconds(7));
a.signalTerminate();
::std::this_thread::sleep_for(::std::chrono::seconds(5));
b.signalTerminate();
::std::this_thread::sleep_for(::std::chrono::seconds(4));
c.signalTerminate();
}
getchar();
return 0;
}
#include <string.h>
#include "sliceWork.h"
sliceWork::sliceWork(int interval, int maxFails, const char* workLabel)
{
sliceIntervalMilliSeconds = interval;
maxFailsBeforeRestart = maxFails;
label = new char[strlen(workLabel) + 1];
strcpy(label, workLabel);
}
sliceWork::~sliceWork()
{
if (workerThread != NULL && workerThread->joinable())
workerThread->join();
printf("destructor %s\n", label);
delete label;
delete workerThread;
}
void sliceWork::initWork()
{
failureCounter = 0;
init();
printf("Init work %s finished!\n", label);
std::future<void> futureWorker = workPromise.get_future();
workerThread = new std::thread(&sliceWork::work, this, move(futureWorker));
}
void sliceWork::work(std::future<void> future)
{
using namespace ::std::chrono;
steady_clock::time_point t0 = steady_clock::now();
while (future.wait_for(std::chrono::milliseconds(1)) == std::future_status::timeout)
{
if (duration_cast<std::chrono::milliseconds>(steady_clock::now() - t0).count()
> sliceIntervalMilliSeconds)
{
if (!oneSliceWork())
{
if (++failureCounter > maxFailsBeforeRestart
&& maxFailsBeforeRestart > 0)
{
init();
failureCounter = 0;
}
}
t0 = steady_clock::now();
}
}
printf("work terminated for %s!\n", label);
}
void sliceWork::signalTerminate()
{
printf("request terminate for work %s...\n", label);
workPromise.set_value();
}
-----------------------
#pragma once
#include <iostream>
#include <future>
class sliceWork
{
public:
sliceWork(int sliceInterval, int maxFails, const char* label);
~sliceWork();
void initWork();
void signalTerminate();
protected:
virtual void init()
{
std::cout << "Init " << label << "..\n";
}
virtual bool oneSliceWork()
{
std::cout << "Working " << label << "..\n";
return true;
}
void work(std::future<void> future);
private:
int sliceIntervalMilliSeconds;
int failureCounter;
int maxFailsBeforeRestart;
char* label = NULL;
std::promise<void> workPromise;
std::thread* workerThread = NULL;
};
#include <string.h>
#include "sliceWork.h"
class A : public sliceWork
{
public:
A(int slice, int max, const char* label)
: sliceWork(slice, max, label)
{
}
};
class B : public sliceWork
{
public:
B(int slice, int max, const char* label)
: sliceWork(slice, max, label)
{
}
};
class C : public sliceWork
{
public:
C(int slice, int max, const char* label)
: sliceWork(slice, max, label)
{
}
};
int main()
{
{
A a(1000, 1000, "A");
a.initWork();
B b(2000, 1000, "B");
b.initWork();
C c(700, 2, "C");
c.initWork();
std::cout << "Initializations finished!\n";
::std::this_thread::sleep_for(::std::chrono::seconds(7));
a.signalTerminate();
::std::this_thread::sleep_for(::std::chrono::seconds(5));
b.signalTerminate();
::std::this_thread::sleep_for(::std::chrono::seconds(4));
c.signalTerminate();
}
getchar();
return 0;
}
#include <string.h>
#include "sliceWork.h"
sliceWork::sliceWork(int interval, int maxFails, const char* workLabel)
{
sliceIntervalMilliSeconds = interval;
maxFailsBeforeRestart = maxFails;
label = new char[strlen(workLabel) + 1];
strcpy(label, workLabel);
}
sliceWork::~sliceWork()
{
if (workerThread != NULL && workerThread->joinable())
workerThread->join();
printf("destructor %s\n", label);
delete label;
delete workerThread;
}
void sliceWork::initWork()
{
failureCounter = 0;
init();
printf("Init work %s finished!\n", label);
std::future<void> futureWorker = workPromise.get_future();
workerThread = new std::thread(&sliceWork::work, this, move(futureWorker));
}
void sliceWork::work(std::future<void> future)
{
using namespace ::std::chrono;
steady_clock::time_point t0 = steady_clock::now();
while (future.wait_for(std::chrono::milliseconds(1)) == std::future_status::timeout)
{
if (duration_cast<std::chrono::milliseconds>(steady_clock::now() - t0).count()
> sliceIntervalMilliSeconds)
{
if (!oneSliceWork())
{
if (++failureCounter > maxFailsBeforeRestart
&& maxFailsBeforeRestart > 0)
{
init();
failureCounter = 0;
}
}
t0 = steady_clock::now();
}
}
printf("work terminated for %s!\n", label);
}
void sliceWork::signalTerminate()
{
printf("request terminate for work %s...\n", label);
workPromise.set_value();
}
-----------------------
#pragma once
#include <iostream>
#include <future>
class sliceWork
{
public:
sliceWork(int sliceInterval, int maxFails, const char* label);
~sliceWork();
void initWork();
void signalTerminate();
protected:
virtual void init()
{
std::cout << "Init " << label << "..\n";
}
virtual bool oneSliceWork()
{
std::cout << "Working " << label << "..\n";
return true;
}
void work(std::future<void> future);
private:
int sliceIntervalMilliSeconds;
int failureCounter;
int maxFailsBeforeRestart;
char* label = NULL;
std::promise<void> workPromise;
std::thread* workerThread = NULL;
};
#include <string.h>
#include "sliceWork.h"
class A : public sliceWork
{
public:
A(int slice, int max, const char* label)
: sliceWork(slice, max, label)
{
}
};
class B : public sliceWork
{
public:
B(int slice, int max, const char* label)
: sliceWork(slice, max, label)
{
}
};
class C : public sliceWork
{
public:
C(int slice, int max, const char* label)
: sliceWork(slice, max, label)
{
}
};
int main()
{
{
A a(1000, 1000, "A");
a.initWork();
B b(2000, 1000, "B");
b.initWork();
C c(700, 2, "C");
c.initWork();
std::cout << "Initializations finished!\n";
::std::this_thread::sleep_for(::std::chrono::seconds(7));
a.signalTerminate();
::std::this_thread::sleep_for(::std::chrono::seconds(5));
b.signalTerminate();
::std::this_thread::sleep_for(::std::chrono::seconds(4));
c.signalTerminate();
}
getchar();
return 0;
}
#include <string.h>
#include "sliceWork.h"
sliceWork::sliceWork(int interval, int maxFails, const char* workLabel)
{
sliceIntervalMilliSeconds = interval;
maxFailsBeforeRestart = maxFails;
label = new char[strlen(workLabel) + 1];
strcpy(label, workLabel);
}
sliceWork::~sliceWork()
{
if (workerThread != NULL && workerThread->joinable())
workerThread->join();
printf("destructor %s\n", label);
delete label;
delete workerThread;
}
void sliceWork::initWork()
{
failureCounter = 0;
init();
printf("Init work %s finished!\n", label);
std::future<void> futureWorker = workPromise.get_future();
workerThread = new std::thread(&sliceWork::work, this, move(futureWorker));
}
void sliceWork::work(std::future<void> future)
{
using namespace ::std::chrono;
steady_clock::time_point t0 = steady_clock::now();
while (future.wait_for(std::chrono::milliseconds(1)) == std::future_status::timeout)
{
if (duration_cast<std::chrono::milliseconds>(steady_clock::now() - t0).count()
> sliceIntervalMilliSeconds)
{
if (!oneSliceWork())
{
if (++failureCounter > maxFailsBeforeRestart
&& maxFailsBeforeRestart > 0)
{
init();
failureCounter = 0;
}
}
t0 = steady_clock::now();
}
}
printf("work terminated for %s!\n", label);
}
void sliceWork::signalTerminate()
{
printf("request terminate for work %s...\n", label);
workPromise.set_value();
}
exec: "python": executable file not found in $PATH on Arduino IDE
sudo ln -s /path/to/python3 /usr/local/bin/python
How to filter a class in Angular?
<ng-container *ngIf="this.project.arduino">
this.newList = [];
if (this.project.arduino) {
this.newList.push(project)
}
-----------------------
<ng-container *ngIf="this.project.arduino">
this.newList = [];
if (this.project.arduino) {
this.newList.push(project)
}
-----------------------
<div class="project-list">
<ul>
<ng-container *ngFor="let project of projects">
<li *ngIf="project.arduino == true" class="project">
<a [routerLink]="['/project', project._id]">
<div class="image-box">
<div class="image">
<img src="{{ url+'get-image/'+project.imagefront }}" *ngIf="project.imagefront" />
</div>
</div>
</a>
</li>
</ng-container>
</ul>
</div>
Stop text from overlapping html and css
.bar-element {
display: flex;
flex-direction: row;
}
@media (max-width:700px){
.bar-element {
display: flex;
flex-direction: column;
}
}
.flex {
display: flex;
flex-direction: row;
}
p {
margin: 5px;
}
@media (max-width:700px){
.flex {
display: flex;
flex-direction: column;
}
}
<div class="flex">
<p>Hello</p>
<p>Hello</p>
<p>Hello</p>
</div>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
/* .flex_container {
display: flex;
flex-wrap: wrap;
min-width: 550px;
flex-direction: column;
position: relative;
} */
.bar {
position: relative;
background-color: rgb(41, 80, 143);
border: 5px solid rgb(46, 84, 149);
height: 30px;
width: auto;
margin: 0;
display: flex;
flex-direction: row;
justify-content: space-between;
}
.tekst {
color: white;
text-transform: uppercase;
text-align: center;
margin: 0;
vertical-align: middle;
line-height: 30px;
font-family: Arial, Helvetica, sans-serif;
}
.ccontroller {
display: flex;
flex-direction: row;
width: fit-content;
flex-wrap: wrap;
justify-content: flex-end;
justify-self: flex-end;
}
.creator1 {
display: block;
font-size: 25px;
color: white;
text-align: left;
margin: 0;
line-height: auto;
font-family: Arial, Helvetica, sans-serif;
margin-right: 10px;
}
.creator2 {
display: block;
color: white;
font-size: 25px;
text-align: left;
margin: 0;
line-height: auto;
font-family: Arial, Helvetica, sans-serif;
}
.aboutus {
display: block;
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
color: white;
background-color: #123456;
height: fit-content;
width: auto;
margin-top: 16px;
margin-left: 50px;
text-align: center;
}
body {
background-color: #123456;
background-repeat: no-repeat;
background-position: center;
background-attachment: fixed;
background-size: 100% 100%;
min-width: 550px;
position: relative;
margin: 0;
padding: 0;
}
html,
body {
position: relative;
margin: 0;
padding: 0;
}
@media (max-width:700px) {
.bar {
display: flex;
flex-direction: column;
}
.ccontroller {
display: flex;
flex-direction: column;
}
}
</style>
<body>
<div class="flex_container">
<div class="bar">
<h1 class="tekst" title="Coffeesnakes">Coffeesnakes</h1>
<button class="aboutus" onclick="func()">About us!</button>
<div class="ccontroller">
<p class="creator1" title="Can code: Java, c#, html, js, python, arduino">Christian2B</p>
<p class="creator2" title="Can code: html, python, js ">Timgb11</p>
</div>
</div>
</div>
</body>
</html>
-----------------------
.bar-element {
display: flex;
flex-direction: row;
}
@media (max-width:700px){
.bar-element {
display: flex;
flex-direction: column;
}
}
.flex {
display: flex;
flex-direction: row;
}
p {
margin: 5px;
}
@media (max-width:700px){
.flex {
display: flex;
flex-direction: column;
}
}
<div class="flex">
<p>Hello</p>
<p>Hello</p>
<p>Hello</p>
</div>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
/* .flex_container {
display: flex;
flex-wrap: wrap;
min-width: 550px;
flex-direction: column;
position: relative;
} */
.bar {
position: relative;
background-color: rgb(41, 80, 143);
border: 5px solid rgb(46, 84, 149);
height: 30px;
width: auto;
margin: 0;
display: flex;
flex-direction: row;
justify-content: space-between;
}
.tekst {
color: white;
text-transform: uppercase;
text-align: center;
margin: 0;
vertical-align: middle;
line-height: 30px;
font-family: Arial, Helvetica, sans-serif;
}
.ccontroller {
display: flex;
flex-direction: row;
width: fit-content;
flex-wrap: wrap;
justify-content: flex-end;
justify-self: flex-end;
}
.creator1 {
display: block;
font-size: 25px;
color: white;
text-align: left;
margin: 0;
line-height: auto;
font-family: Arial, Helvetica, sans-serif;
margin-right: 10px;
}
.creator2 {
display: block;
color: white;
font-size: 25px;
text-align: left;
margin: 0;
line-height: auto;
font-family: Arial, Helvetica, sans-serif;
}
.aboutus {
display: block;
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
color: white;
background-color: #123456;
height: fit-content;
width: auto;
margin-top: 16px;
margin-left: 50px;
text-align: center;
}
body {
background-color: #123456;
background-repeat: no-repeat;
background-position: center;
background-attachment: fixed;
background-size: 100% 100%;
min-width: 550px;
position: relative;
margin: 0;
padding: 0;
}
html,
body {
position: relative;
margin: 0;
padding: 0;
}
@media (max-width:700px) {
.bar {
display: flex;
flex-direction: column;
}
.ccontroller {
display: flex;
flex-direction: column;
}
}
</style>
<body>
<div class="flex_container">
<div class="bar">
<h1 class="tekst" title="Coffeesnakes">Coffeesnakes</h1>
<button class="aboutus" onclick="func()">About us!</button>
<div class="ccontroller">
<p class="creator1" title="Can code: Java, c#, html, js, python, arduino">Christian2B</p>
<p class="creator2" title="Can code: html, python, js ">Timgb11</p>
</div>
</div>
</div>
</body>
</html>
-----------------------
.bar-element {
display: flex;
flex-direction: row;
}
@media (max-width:700px){
.bar-element {
display: flex;
flex-direction: column;
}
}
.flex {
display: flex;
flex-direction: row;
}
p {
margin: 5px;
}
@media (max-width:700px){
.flex {
display: flex;
flex-direction: column;
}
}
<div class="flex">
<p>Hello</p>
<p>Hello</p>
<p>Hello</p>
</div>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
/* .flex_container {
display: flex;
flex-wrap: wrap;
min-width: 550px;
flex-direction: column;
position: relative;
} */
.bar {
position: relative;
background-color: rgb(41, 80, 143);
border: 5px solid rgb(46, 84, 149);
height: 30px;
width: auto;
margin: 0;
display: flex;
flex-direction: row;
justify-content: space-between;
}
.tekst {
color: white;
text-transform: uppercase;
text-align: center;
margin: 0;
vertical-align: middle;
line-height: 30px;
font-family: Arial, Helvetica, sans-serif;
}
.ccontroller {
display: flex;
flex-direction: row;
width: fit-content;
flex-wrap: wrap;
justify-content: flex-end;
justify-self: flex-end;
}
.creator1 {
display: block;
font-size: 25px;
color: white;
text-align: left;
margin: 0;
line-height: auto;
font-family: Arial, Helvetica, sans-serif;
margin-right: 10px;
}
.creator2 {
display: block;
color: white;
font-size: 25px;
text-align: left;
margin: 0;
line-height: auto;
font-family: Arial, Helvetica, sans-serif;
}
.aboutus {
display: block;
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
color: white;
background-color: #123456;
height: fit-content;
width: auto;
margin-top: 16px;
margin-left: 50px;
text-align: center;
}
body {
background-color: #123456;
background-repeat: no-repeat;
background-position: center;
background-attachment: fixed;
background-size: 100% 100%;
min-width: 550px;
position: relative;
margin: 0;
padding: 0;
}
html,
body {
position: relative;
margin: 0;
padding: 0;
}
@media (max-width:700px) {
.bar {
display: flex;
flex-direction: column;
}
.ccontroller {
display: flex;
flex-direction: column;
}
}
</style>
<body>
<div class="flex_container">
<div class="bar">
<h1 class="tekst" title="Coffeesnakes">Coffeesnakes</h1>
<button class="aboutus" onclick="func()">About us!</button>
<div class="ccontroller">
<p class="creator1" title="Can code: Java, c#, html, js, python, arduino">Christian2B</p>
<p class="creator2" title="Can code: html, python, js ">Timgb11</p>
</div>
</div>
</div>
</body>
</html>
-----------------------
.bar-element {
display: flex;
flex-direction: row;
}
@media (max-width:700px){
.bar-element {
display: flex;
flex-direction: column;
}
}
.flex {
display: flex;
flex-direction: row;
}
p {
margin: 5px;
}
@media (max-width:700px){
.flex {
display: flex;
flex-direction: column;
}
}
<div class="flex">
<p>Hello</p>
<p>Hello</p>
<p>Hello</p>
</div>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
/* .flex_container {
display: flex;
flex-wrap: wrap;
min-width: 550px;
flex-direction: column;
position: relative;
} */
.bar {
position: relative;
background-color: rgb(41, 80, 143);
border: 5px solid rgb(46, 84, 149);
height: 30px;
width: auto;
margin: 0;
display: flex;
flex-direction: row;
justify-content: space-between;
}
.tekst {
color: white;
text-transform: uppercase;
text-align: center;
margin: 0;
vertical-align: middle;
line-height: 30px;
font-family: Arial, Helvetica, sans-serif;
}
.ccontroller {
display: flex;
flex-direction: row;
width: fit-content;
flex-wrap: wrap;
justify-content: flex-end;
justify-self: flex-end;
}
.creator1 {
display: block;
font-size: 25px;
color: white;
text-align: left;
margin: 0;
line-height: auto;
font-family: Arial, Helvetica, sans-serif;
margin-right: 10px;
}
.creator2 {
display: block;
color: white;
font-size: 25px;
text-align: left;
margin: 0;
line-height: auto;
font-family: Arial, Helvetica, sans-serif;
}
.aboutus {
display: block;
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
color: white;
background-color: #123456;
height: fit-content;
width: auto;
margin-top: 16px;
margin-left: 50px;
text-align: center;
}
body {
background-color: #123456;
background-repeat: no-repeat;
background-position: center;
background-attachment: fixed;
background-size: 100% 100%;
min-width: 550px;
position: relative;
margin: 0;
padding: 0;
}
html,
body {
position: relative;
margin: 0;
padding: 0;
}
@media (max-width:700px) {
.bar {
display: flex;
flex-direction: column;
}
.ccontroller {
display: flex;
flex-direction: column;
}
}
</style>
<body>
<div class="flex_container">
<div class="bar">
<h1 class="tekst" title="Coffeesnakes">Coffeesnakes</h1>
<button class="aboutus" onclick="func()">About us!</button>
<div class="ccontroller">
<p class="creator1" title="Can code: Java, c#, html, js, python, arduino">Christian2B</p>
<p class="creator2" title="Can code: html, python, js ">Timgb11</p>
</div>
</div>
</div>
</body>
</html>
-----------------------
.flex-box {
display: flex;
justify-content: space-between;
flex-wrap: wrap;
}
p {
margin: 5px;
min-width:200px;
}
<div class="flex-box">
<p>foo</p>
<p>bar</p>
<p>baz</p>
</div>
-----------------------
.flex-box {
display: flex;
justify-content: space-between;
flex-wrap: wrap;
}
p {
margin: 5px;
min-width:200px;
}
<div class="flex-box">
<p>foo</p>
<p>bar</p>
<p>baz</p>
</div>
-----------------------
.flex_container {
display: flex;
flex-wrap: wrap;
min-width: 550px;
flex-direction: column;
position: relative;
}
.bar {
position: relative;
background-color: rgb(41, 80, 143);
border: 5px solid rgb(46, 84, 149);
height: 30px;
width: auto;
margin: 0;
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.tekst {
color: white;
position: static;
text-transform: uppercase;
text-align: center;
margin: 0;
justify-content: center;
vertical-align: middle;
line-height: 30px;
font-family: Arial, Helvetica, sans-serif;
}
.ccontroller {
margin-top: -12px;
}
.creator1 {
font-size: 25px;
color: white;
margin: 0;
font-family: Arial, Helvetica, sans-serif;
}
.creator2 {
color: white;
font-size: 25px;
margin: 0;
font-family: Arial, Helvetica, sans-serif;
}
.aboutus {
margin: 0;
color: white;
background-color: #123456;
width: auto;
}
body {
background-color: #123456;
background-repeat: no-repeat;
background-position: center;
background-attachment: fixed;
background-size: 100% 100%;
min-width: 550px;
position: relative;
margin: 0;
padding: 0;
}
html,
body {
min-width: 550px;
position: relative;
margin: 0;
padding: 0;
}
<body>
<div class="flex_container">
<div class="bar">
<p class="creator1" title="Can code: Java, c#, html, js, python, arduino">Christian2B</p>
<h1 class="tekst" title="Coffeesnakes">Coffeesnakes</h1>
<p class="creator2" title="Can code: html, python, js ">Timgb11</p>
<button class="aboutus" onclick="func()">About us!</button>
</div>
</div>
</body>
-----------------------
.flex_container {
display: flex;
flex-wrap: wrap;
min-width: 550px;
flex-direction: column;
position: relative;
}
.bar {
position: relative;
background-color: rgb(41, 80, 143);
border: 5px solid rgb(46, 84, 149);
height: 30px;
width: auto;
margin: 0;
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.tekst {
color: white;
position: static;
text-transform: uppercase;
text-align: center;
margin: 0;
justify-content: center;
vertical-align: middle;
line-height: 30px;
font-family: Arial, Helvetica, sans-serif;
}
.ccontroller {
margin-top: -12px;
}
.creator1 {
font-size: 25px;
color: white;
margin: 0;
font-family: Arial, Helvetica, sans-serif;
}
.creator2 {
color: white;
font-size: 25px;
margin: 0;
font-family: Arial, Helvetica, sans-serif;
}
.aboutus {
margin: 0;
color: white;
background-color: #123456;
width: auto;
}
body {
background-color: #123456;
background-repeat: no-repeat;
background-position: center;
background-attachment: fixed;
background-size: 100% 100%;
min-width: 550px;
position: relative;
margin: 0;
padding: 0;
}
html,
body {
min-width: 550px;
position: relative;
margin: 0;
padding: 0;
}
<body>
<div class="flex_container">
<div class="bar">
<p class="creator1" title="Can code: Java, c#, html, js, python, arduino">Christian2B</p>
<h1 class="tekst" title="Coffeesnakes">Coffeesnakes</h1>
<p class="creator2" title="Can code: html, python, js ">Timgb11</p>
<button class="aboutus" onclick="func()">About us!</button>
</div>
</div>
</body>
What is the Rust equivalent of Serial.println from the Arduino C++ API?
let dp = arduino_hal::Peripherals::take().unwrap();
let pins = arduino_hal::pins!(dp);
let mut serial = arduino_hal::default_serial!(dp, pins, 115200);
ufmt::uwriteln!(&mut serial, "{} bytes available", count);
-----------------------
let dp = arduino_hal::Peripherals::take().unwrap();
let pins = arduino_hal::pins!(dp);
let mut serial = arduino_hal::default_serial!(dp, pins, 115200);
ufmt::uwriteln!(&mut serial, "{} bytes available", count);
How to make an object take and store an Array of arbitrary, but compile-time known size?
struct Runner {
span<const Point> profile;
void foo() {
for(auto point : profile) {
// do something
}
}
};
Point points[] {
// ... number of points doesn't matter
};
Runner runner {
.profile = points,
};
-----------------------
#include <iostream>
struct RunnerInterface {
virtual int* begin() = 0;
virtual int* end() = 0;
virtual ~RunnerInterface(){}
};
template <unsigned size>
struct Runner : RunnerInterface {
int data[size];
int* begin() override { return data; }
int* end() override { return data+size; } // pointer one past the end if fine (it won't get dereferenced)
};
void foo(RunnerInterface& ri) {
for (auto it = ri.begin(); it != ri.end(); ++it){
*it = 42;
}
}
void bar(RunnerInterface& ri){
for (auto it = ri.begin(); it != ri.end(); ++it){
std::cout << *it;
}
}
int main() {
Runner<42> r;
foo(r);
bar(r);
}
how to clear oled display in micropython
oled.fill(0)
oled.show()
Kalman filtering IMU noise c#
auto state = K.getxcopy();
for (int i = 0; i < state.Cols; i++)
{
for (int j = 0; j < state.Rows; j++)
{
Serial.print(state(i, j));
Serial.print(" | ");
}
Serial.println();
}
How do I clear a stream in C++ for a nanoPB protocol buffer to use?
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
-----------------------
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
Python serial.read() doesn't read data from Arduino in the first loop
ser = serial.Serial("/dev/cu.usbserial-1420", baudrate=115200, timeout=0)
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
#pragma once
#include <future>
using namespace ::std;
class sliceWork
{
int sliceIntervalMilliSeconds;
int failureCounter;
int maxFailsBeforeRestart;
char* label = NULL;
promise<void> workPromise;
thread* workerThread = NULL;
virtual void init() = 0;
virtual bool oneSliceWork() = 0;
void work(future<void> future);
public:
sliceWork(int sliceInterval, int maxFails, const char* label);
~sliceWork();
void initWork();
void signalTerminate();
};
sliceWork.cpp
#include <string.h>
#include "sliceWork.h"
sliceWork::sliceWork(int interval, int maxFails, const char* workLabel)
{
sliceIntervalMilliSeconds = interval;
maxFailsBeforeRestart = maxFails;
label = new char[strlen(workLabel) + 1];
strcpy(label, workLabel);
}
sliceWork::~sliceWork()
{
if (workerThread != NULL && workerThread->joinable())
workerThread->join();
printf("destructor %s\n", label);
delete label;
delete workerThread;
}
void sliceWork::initWork()
{
failureCounter = 0;
init();
printf("Init work %s finished!\n", label);
future<void> futureWorker = workPromise.get_future();
workerThread = new thread(&sliceWork::work, this, move(futureWorker));
}
void sliceWork::work(future<void> future)
{
using namespace ::std::chrono;
steady_clock::time_point t0 = steady_clock::now();
while (future.wait_for(chrono::milliseconds(1)) == future_status::timeout)
{
if (duration_cast<chrono::milliseconds>(steady_clock::now() - t0).count()
> sliceIntervalMilliSeconds)
{
if (!oneSliceWork())
{
if (++failureCounter > maxFailsBeforeRestart
&& maxFailsBeforeRestart > 0)
{
init();
failureCounter = 0;
}
}
t0 = steady_clock::now();
}
}
printf("work terminated for %s!\n", label);
}
void sliceWork::signalTerminate()
{
printf("request terminate for work %s...\n", label);
workPromise.set_value();
}
And here is an example of using it that works as expected:
main.cpp
#include <string.h>
#include "sliceWork.h"
class A : public sliceWork
{
void init() {
printf("Init A...\n");
}
bool oneSliceWork() {
printf("Working A...\n");
return true;
}
public:
A(int slice, int max, const char* label)
: sliceWork(slice, max, label)
{
}
};
class B : public sliceWork
{
void init() {
printf("Init B...\n");
}
bool oneSliceWork() {
printf("Working B...\n");
return true;
}
public:
B(int slice, int max, const char* label)
: sliceWork(slice, max, label)
{
}
};
class C : public sliceWork
{
void init() {
printf("Init C...\n");
}
bool oneSliceWork() {
printf("Working C...\n");
return false;
}
public:
C(int slice, int max, const char* label)
: sliceWork(slice, max, label)
{
}
};
int main()
{
{
A a(1000, 1000, "A");
a.initWork();
B b(2000, 1000, "B" );
b.initWork();
C c(700, 2, "C" );
c.initWork();
printf("Initializations finished!\n");
::std::this_thread::sleep_for(::std::chrono::seconds(7));
a.signalTerminate();
::std::this_thread::sleep_for(::std::chrono::seconds(5));
b.signalTerminate();
::std::this_thread::sleep_for(::std::chrono::seconds(4));
c.signalTerminate();
}
getchar();
return 0;
}
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.
C
code that C++
code except for the classes themselves.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.
#pragma once
#include <iostream>
#include <future>
class sliceWork
{
public:
sliceWork(int sliceInterval, int maxFails, const char* label);
~sliceWork();
void initWork();
void signalTerminate();
protected:
virtual void init()
{
std::cout << "Init " << label << "..\n";
}
virtual bool oneSliceWork()
{
std::cout << "Working " << label << "..\n";
return true;
}
void work(std::future<void> future);
private:
int sliceIntervalMilliSeconds;
int failureCounter;
int maxFailsBeforeRestart;
char* label = NULL;
std::promise<void> workPromise;
std::thread* workerThread = NULL;
};
main.cpp
#include <string.h>
#include "sliceWork.h"
class A : public sliceWork
{
public:
A(int slice, int max, const char* label)
: sliceWork(slice, max, label)
{
}
};
class B : public sliceWork
{
public:
B(int slice, int max, const char* label)
: sliceWork(slice, max, label)
{
}
};
class C : public sliceWork
{
public:
C(int slice, int max, const char* label)
: sliceWork(slice, max, label)
{
}
};
int main()
{
{
A a(1000, 1000, "A");
a.initWork();
B b(2000, 1000, "B");
b.initWork();
C c(700, 2, "C");
c.initWork();
std::cout << "Initializations finished!\n";
::std::this_thread::sleep_for(::std::chrono::seconds(7));
a.signalTerminate();
::std::this_thread::sleep_for(::std::chrono::seconds(5));
b.signalTerminate();
::std::this_thread::sleep_for(::std::chrono::seconds(4));
c.signalTerminate();
}
getchar();
return 0;
}
sliceWork.cpp
#include <string.h>
#include "sliceWork.h"
sliceWork::sliceWork(int interval, int maxFails, const char* workLabel)
{
sliceIntervalMilliSeconds = interval;
maxFailsBeforeRestart = maxFails;
label = new char[strlen(workLabel) + 1];
strcpy(label, workLabel);
}
sliceWork::~sliceWork()
{
if (workerThread != NULL && workerThread->joinable())
workerThread->join();
printf("destructor %s\n", label);
delete label;
delete workerThread;
}
void sliceWork::initWork()
{
failureCounter = 0;
init();
printf("Init work %s finished!\n", label);
std::future<void> futureWorker = workPromise.get_future();
workerThread = new std::thread(&sliceWork::work, this, move(futureWorker));
}
void sliceWork::work(std::future<void> future)
{
using namespace ::std::chrono;
steady_clock::time_point t0 = steady_clock::now();
while (future.wait_for(std::chrono::milliseconds(1)) == std::future_status::timeout)
{
if (duration_cast<std::chrono::milliseconds>(steady_clock::now() - t0).count()
> sliceIntervalMilliSeconds)
{
if (!oneSliceWork())
{
if (++failureCounter > maxFailsBeforeRestart
&& maxFailsBeforeRestart > 0)
{
init();
failureCounter = 0;
}
}
t0 = steady_clock::now();
}
}
printf("work terminated for %s!\n", label);
}
void sliceWork::signalTerminate()
{
printf("request terminate for work %s...\n", label);
workPromise.set_value();
}
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
No vulnerabilities reported
Save this library and start creating your kit
Explore Related Topics
Save this library and start creating your kit