Pico-Arduino
PicoSemaphore.h
1 #pragma once
2 
3 #include "pico/stdlib.h"
4 #include "pico/sync.h"
5 
6 namespace pico_arduino {
7 
13 class Semaphore {
14  public:
16  Semaphore(int maxPermits=1, int initalPermits=1){
17  sem_init(&sem, initalPermits,maxPermits);
18  }
19 
22  sem_release(&sem);
23  }
24 
26  int available(){
27  return sem_available(&sem);
28  }
29 
31  void reset(int permits=1) {
32  sem_reset(&sem, permits);
33  }
34 
36  bool aquire(int64_t timeoutMs=-1){
37  bool result = false;
38  if (timeoutMs<0){
39  sem_acquire_blocking(&sem);
40  result = true;
41  } else {
42  result = sem_acquire_timeout_ms(&sem, timeoutMs);
43  }
44  return result;
45  }
46 
48  bool release() {
49  return sem_release(&sem);
50  }
51 
53  bool up(){
54  return aquire();
55  }
56 
58  bool down() {
59  return release();
60  }
61 
63  bool wait() {
64  return aquire();
65  }
66 
68  bool signal(){
69  return release();
70  }
71 
72  protected:
73  semaphore_t sem;
74 };
75 
76 }
Pico Semaphore which might be useful if you use the 2 processors (e.g. with the Thread class)
Definition: PicoSemaphore.h:13
bool signal()
identical with release
Definition: PicoSemaphore.h:68
bool release()
release the semaphore and makes it availale for other processes
Definition: PicoSemaphore.h:48
Semaphore(int maxPermits=1, int initalPermits=1)
constructor - calls sem_init
Definition: PicoSemaphore.h:16
bool down()
identical with release
Definition: PicoSemaphore.h:58
int available()
checks if a semaphore is available
Definition: PicoSemaphore.h:26
~Semaphore()
descructor - calls sem_release
Definition: PicoSemaphore.h:21
bool aquire(int64_t timeoutMs=-1)
aquires a semaphore - if no time is indicated it is blocking
Definition: PicoSemaphore.h:36
bool up()
identical with aquire
Definition: PicoSemaphore.h:53
void reset(int permits=1)
calls sem_reset
Definition: PicoSemaphore.h:31
bool wait()
identical with aquire
Definition: PicoSemaphore.h:63
Pico Arduino Framework.
Definition: Arduino.cpp:26