Hello,
Here are my questions more analytically:
I have been reading some other threads here and I noticed that when people are saying that a function is blocking, they mean that it is blocking the device and prevents it from running other processes, just like a while () loop that waits for something to be complete.
However in the above question, I mean exactly the opposite, that the function will block, meaning it will stop execution and the scheduler will perform a task switching, doing whatever it needs to do. Then, when the operation is c…
However you mention C++11 threading primitives, which I have no idea what it is!
I will read some documentation. I hope it is an RTOS!
I will get back to you if I have more questions.
Thanks for the answer!
Vasileios.
chixxi
February 18, 2018, 6:33pm
22
I was just wondering: Is it possible to forward another parameter to such a separate thread once it is already running?
Trying to use a separate thread for updating an oled display:
// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_SSD1306.h>
#include "application.h"
//https://community.particle.io/t/particle-photon-multi-blink-sample-using-threads/16214
// LED Param, defines pin and delay between blinks
#define OLED_RESET D4
Adafruit_SSD1306 oled(OLED_RESET);
typedef struct {
char *title;
char *line1;
char *line2;
char *line3;
char *line4;
char *line5;
char *line6;
} SCREEN_PARAM;
//Variables (lines on oled display)
char *TITLE = "TITLE";
char *L1 = "L1";
char *L2 = "L2";
char *L3 = "L3";
char *L4 = "L4";
char *L5 = "L5";
char *L6 = "L6";
// Defines thread parameters
SCREEN_PARAM screenParams[3] = {
{TITLE, L1, L2, L3, L4, L5, L6}
};
Thread* displayThread;
os_thread_return_t ledBlink(void* param){
SCREEN_PARAM *p = (SCREEN_PARAM*)param;
// Start never ending loop
for(;;) {
oled.clearDisplay();
oled.setTextSize(2);
oled.setTextColor(WHITE);
oled.setCursor(0,0);
oled.println(p->title);
oled.setTextSize(1);
oled.println(p->line1);
oled.println(p->line2);
oled.println(p->line3);
oled.println(p->line4);
oled.println(p->line5);
oled.println(p->line6);
oled.display();
}
}
void setup() {
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64)
Serial.begin(115200);
// Start seperate thread for display
displayThread = new Thread("sample", ledBlink, &screenParams[0]);
}
void loop() {
TITLE = "TEST1";
delay(2000);
TITLE = "TEST2";
delay(2000);
}
EDIT: FOUND THE SOLUTION.
// ClickButton Library for button inputs
#include <clickButton.h>
// Display Lobrary for I2C/SPI Oled display
#include <Adafruit_SSD1306.h>
/* ============== MAIN =====================*/
//Use I2C with OLED RESET pin on D4 (not used I guess)
#define OLED_RESET D4
Adafruit_SSD1306 oled(OLED_RESET);
//Definition of the button
const int buttonPin1 = 5;
ClickButton button1(buttonPin1, LOW, CLICKBTN_PULLUP);
//Initilisiation running mode
int mode = 0;
//Button results
int function = 0;
//Variables (lines on oled display)
char *TITLE = "TITLE";
char *L1 = "L1";
char *L2 = "L2";
char *L3 = "L3";
char *L4 = "L4";
char *L5 = "L5";
char *L6 = "L6";
//Definition for seperate display thread
Thread* displayThread;
os_thread_return_t ledBlink(void*) {
// Start never ending loop
for(;;) {
oled.clearDisplay();
oled.setTextSize(2);
oled.setTextColor(WHITE);
oled.setCursor(0,0);
oled.println(TITLE);
oled.setTextSize(1);
oled.println(L1);
oled.println(L2);
oled.println(L3);
oled.println(L4);
oled.println(L5);
oled.println(L6);
oled.display();
delay(2000);
//just a test
oled.clearDisplay();
oled.setTextSize(2);
oled.setTextColor(WHITE);
oled.setCursor(0,0);
oled.println("TITLE");
oled.setTextSize(1);
oled.println("L1");
oled.println("L2");
oled.println("L3");
oled.println("L4");
oled.println("L5");
oled.println("L6");
oled.display();
delay(2000);
}
}
void setup() {
//Initialisation of oled display
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64)
Serial.begin(9600);
//Configuration of inout buttin
pinMode(D5, INPUT_PULLUP);
//Setup button timers (all in milliseconds / ms)
button1.debounceTime = 20; // Debounce timer in ms
button1.multiclickTime = 250; // Time limit for multi clicks
button1.longClickTime = 1000; // time until "held-down clicks" register
//Start seperate display thread
displayThread = new Thread("sample", ledBlink);
}
//Starting general loop
void loop()
{
//Update button state
button1.Update();
//Save click codes in LEDfunction, as click codes are reset at next Update()
if(button1.clicks != 0) function = button1.clicks;
//runmode = ready (0)
if(mode == 0) {
TITLE = "EINLEGEN!";
L1 = "";
L2 = "PDM Presnets:";
L3 = "Ostersaufen 2018";
L4 = "Haesli-Choepfer 2.0";
L5 = "";
L6 = "";
//oled.clearDisplay();
//oled.setTextSize(2);
//oled.setTextColor(WHITE);
//oled.setCursor(0,0);
//oled.println("EINLEGEN!");
//oled.setTextSize(1);
//oled.println();
//oled.println("1. Draht anheben");
//oled.println("2. Hase einlegen");
//oled.println("3. Start Knopf (1s)");
//oled.display();
//delay(2000);
}
//runmode = finished (1)
if(mode == 1) {
TITLE = "KOPF AB!";
L1 = "";
L2 = "Kampfhase und Kopf";
L3 = "entfernen!";
L4 = "";
L5 = "Kindersicherung nicht";
L6 = "vergessen!";
//oled.clearDisplay();
//oled.setTextSize(2);
//oled.setTextColor(WHITE);
//oled.setCursor(0,0);
//oled.println("KOPF AB!");
//oled.println();
//oled.setTextSize(1);
//oled.println("Kindersicherung nicht");
//oled.println("vergessen!");
//oled.display();
//delay(2000);
}
//mode = burning (2)
if(mode == 2) {
TITLE = "PDM Presnets:";
L1 = "";
L2 = "SHORT PRESS";
L3 = "BURNING MODE";
L4 = "";
L5 = "";
L6 = "";
}
//short end-stop input
if(function == 1) mode = 2;
//long end-stop input
if(function == -1) mode = 1;
//if(function == 2) Serial.println("DOUBLE click");
//if(function == 3) Serial.println("TRIPLE click");
//if(function == -2) Serial.println("DOUBLE LONG click");
//if(function == -3) Serial.println("TRIPLE LONG click");
function = 0;
delay(5);
}
ScruffR
February 18, 2018, 7:27pm
23
Threads can access global variables.
To expand on @ScruffR reply, “Threads can access global variables” … and they should be atomic
For those following this thread, @rickkas7 created a sweet, descriptive “Particle threads tutorial” doc in April 2018. Well written with good examples that one can reference and follow … https://github.com/rickkas7/particle-threads