PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
Runnable_Demo.cpp
Go to the documentation of this file.
1 
8 #include <PropWare/PropWare.h>
10 #include <PropWare/gpio/pin.h>
11 
12 using PropWare::Runnable;
13 using PropWare::Pin;
14 
15 class TalkingThread: public Runnable {
16  public:
17  template<size_t N>
18  TalkingThread(const uint32_t (&stack)[N])
19  : Runnable(stack) {
20  }
21 
22  void run() {
23  while (1) {
24  pwSyncOut.printf("Hello from cog %u (0x%08X)! %u\n", cogid(), (unsigned int) this, CNT);
25  waitcnt(250 * MILLISECOND + CNT);
26  }
27  }
28 };
29 
30 class BlinkingThread: public Runnable {
31  public:
32  template<size_t N>
33  BlinkingThread(const uint32_t (&stack)[N], const Pin::Mask mask)
34  : Runnable(stack),
35  m_mask(mask) {
36  }
37 
38  void run() {
39  const Pin pin(this->m_mask, Pin::Dir::OUT);
40  while (1) {
41  pin.toggle();
42  waitcnt(250 * MILLISECOND + CNT);
43  }
44  }
45 
46  private:
47  const Pin::Mask m_mask;
48 };
49 
58 int main(int argc, char *argv[]) {
59  uint32_t stack[3][70];
60  TalkingThread talkingThread(stack[0]);
61  BlinkingThread blink16(stack[1], Pin::Mask::P16);
62  BlinkingThread blink17(stack[2], Pin::Mask::P17);
63 
64  int8_t cog = Runnable::invoke(talkingThread);
65  pwSyncOut.printf("Talking thread (0x%08X) started in cog %d\n", (unsigned int) &talkingThread, cog);
66 
67  cog = Runnable::invoke(blink16);
68  pwSyncOut.printf("Blink16 thread (0x%08X) started in cog %d\n", (unsigned int) &blink16, cog);
69 
70  cog = Runnable::invoke(blink17);
71  pwSyncOut.printf("Blink17 thread (0x%08X) started in cog %d\n", (unsigned int) &blink17, cog);
72 
73  while (1) {
74  pwSyncOut.printf("Hello from cog %u! %u\n", cogid(), CNT);
75  waitcnt(250 * MILLISECOND + CNT);
76  }
77 }
runnable.h
pwSyncOut
const PropWare::SynchronousPrinter pwSyncOut
Global and shared instance for easy printing to the terminal (thread safe)
TalkingThread::run
void run()
Invoked in the new cog, this method should be the root of the business logic.
Definition: Runnable_Demo.cpp:22
BlinkingThread
Definition: Runnable_Demo.cpp:30
synchronousprinter.h
PropWare::Pin
Utility class to handle general purpose I/O pins.
Definition: pin.h:36
TalkingThread
Definition: Runnable_Demo.cpp:15
PropWare::SynchronousPrinter::printf
void printf(const char fmt[]) const
Definition: synchronousprinter.h:139
main
int main(void)
Definition: GraphicsTest.c:20
PropWare.h
PropWare::Runnable
Helper class for creating easy parallel applications.
Definition: runnable.h:75
waitcnt
#define waitcnt(a)
Wait until system counter reaches a value.
Definition: propeller.h:176
CNT
#define CNT
The system clock count.
Definition: propeller1.h:151
cogid
#define cogid()
Return the id of the current cog.
Definition: propeller.h:76
pin.h
BlinkingThread::run
void run()
Invoked in the new cog, this method should be the root of the business logic.
Definition: Runnable_Demo.cpp:38