Pico-Arduino
PicoThread.h
1 #pragma once
2 
3 #include "pico/stdlib.h"
4 #include "pico/multicore.h"
5 
6 namespace pico_arduino {
7 
8 
9 typedef void (*ThreadCB)(void* arg);
10 
17 class Thread {
18  public:
19  Thread(){
20  started(false);
21  }
22 
23  ~Thread(){
24  stop();
25  }
26 
28  bool start(ThreadCB callback, void* ptr=nullptr){
29  bool result = false;
30  // set arguments to callback
31  staticPtr(ptr);
32  staticCallback(callback);
33 
34  if (!started()){
35  multicore_launch_core1(callback_handler);
36  started(true);
37  result = true;
38  Logger.info("Thread started on core1");
39  } else {
40  Logger.info("Thread not started - because one is already active");
41  }
42  return result;
43  }
44 
46  void stop() {
47  multicore_reset_core1();
48  started(false);
49  }
50 
52  bool isRunning() {
53  return started();
54  }
55 
56  static void callback_handler(){
57  ThreadCB cb = staticCallback();
58  void* ptr = staticPtr();
59  // calling callback
60  cb(ptr);
61  }
62 
63  protected:
64  // static setter / getter for ptr
65  static void *staticPtr(void* ptr = nullptr){
66  static void *stat_ptr = {ptr};
67  return stat_ptr;
68  }
69 
70  // static setter / getter for callback
71  static ThreadCB staticCallback(ThreadCB cb = nullptr) {
72  static ThreadCB stat_cb = {cb};
73  return stat_cb;
74  };
75 
76 
77  // get or update static started flag
78  bool started(int flag=-1){
79  static bool started_flag;
80  if (flag!=-1)
81  started_flag = flag;
82  return started_flag;
83  }
84 
85 };
86 
87 }
88 
virtual void info(const char *str, const char *str1=nullptr, const char *str2=nullptr)
logs an info message
Definition: PicoLogger.h:50
Adds support for running code on the second processor core (core1)
Definition: PicoThread.h:17
bool isRunning()
check if the core1 has been started and is not stoped
Definition: PicoThread.h:52
bool start(ThreadCB callback, void *ptr=nullptr)
Run code on core 1 - we can pass a reference to an object which will be given to the callback as argu...
Definition: PicoThread.h:28
void stop()
resets the core 1
Definition: PicoThread.h:46
Pico Arduino Framework.
Definition: Arduino.cpp:26