PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
Runnable_Demo.cpp

Run code in a total of four cogs. Two of them will simply blink LEDs. The other two demonstrate a thread-safe way to use a serial terminal simultaneously from two different cogs.

cmake_minimum_required(VERSION 3.12)
find_package(PropWare REQUIRED)
project(Runnable_Demo)
create_simple_executable(${PROJECT_NAME} Runnable_Demo.cpp)
class TalkingThread: public Runnable {
public:
template<size_t N>
TalkingThread(const uint32_t (&stack)[N])
: Runnable(stack) {
}
void run() {
while (1) {
pwSyncOut.printf("Hello from cog %u (0x%08X)! %u\n", cogid(), (unsigned int) this, CNT);
waitcnt(250 * MILLISECOND + CNT);
}
}
};
class BlinkingThread: public Runnable {
public:
template<size_t N>
BlinkingThread(const uint32_t (&stack)[N], const Pin::Mask mask)
: Runnable(stack),
m_mask(mask) {
}
void run() {
const Pin pin(this->m_mask, Pin::Dir::OUT);
while (1) {
pin.toggle();
waitcnt(250 * MILLISECOND + CNT);
}
}
private:
const Pin::Mask m_mask;
};
int main(int argc, char *argv[]) {
uint32_t stack[3][70];
TalkingThread talkingThread(stack[0]);
BlinkingThread blink16(stack[1], Pin::Mask::P16);
BlinkingThread blink17(stack[2], Pin::Mask::P17);
int8_t cog = Runnable::invoke(talkingThread);
pwSyncOut.printf("Talking thread (0x%08X) started in cog %d\n", (unsigned int) &talkingThread, cog);
cog = Runnable::invoke(blink16);
pwSyncOut.printf("Blink16 thread (0x%08X) started in cog %d\n", (unsigned int) &blink16, cog);
cog = Runnable::invoke(blink17);
pwSyncOut.printf("Blink17 thread (0x%08X) started in cog %d\n", (unsigned int) &blink17, cog);
while (1) {
pwSyncOut.printf("Hello from cog %u! %u\n", cogid(), CNT);
waitcnt(250 * MILLISECOND + CNT);
}
}
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
PropWare
Generic definitions and functions for the Parallax Propeller.
Definition: runnable.h:33
BlinkingThread::run
void run()
Invoked in the new cog, this method should be the root of the business logic.
Definition: Runnable_Demo.cpp:38