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

Demonstrate multiple cogs printing to the same serial terminal simultaneously

cmake_minimum_required(VERSION 3.12)
find_package(PropWare REQUIRED)
project(SynchronousPrinter_Demo)
create_simple_executable(${PROJECT_NAME} SynchronousPrinter_Demo.cpp)
static const uint16_t COGS = 8;
static const uint16_t STACK_SIZE = 128;
static const unsigned int DELAY_IN_SECONDS = 2;
static const uint32_t WAIT_TIME = DELAY_IN_SECONDS * SECOND;
class SyncedPrinterCog: public Runnable {
public:
template<size_t N>
SyncedPrinterCog(const uint32_t (&stack)[N])
: Runnable(stack) {
}
virtual void run() {
const Port::Mask pinMaskOfCogId = (Port::Mask) (1 << (cogid() + 16));
uint32_t nextCnt;
nextCnt = WAIT_TIME + CNT;
while (1) {
// Visual recognition that the cog is running
Pin::flash_pin(pinMaskOfCogId, 3);
pwSyncOut.printf("Hello from cog %d\n", cogid());
nextCnt = waitcnt2(nextCnt, WAIT_TIME);
}
}
};
int main(int argc, char *argv[]) {
const uint32_t stacks[COGS][STACK_SIZE] = {{0}};
SyncedPrinterCog syncedPrinterCogs[] = {
SyncedPrinterCog(stacks[0]),
SyncedPrinterCog(stacks[1]),
SyncedPrinterCog(stacks[2]),
SyncedPrinterCog(stacks[3]),
SyncedPrinterCog(stacks[4]),
SyncedPrinterCog(stacks[5]),
SyncedPrinterCog(stacks[6]),
SyncedPrinterCog(stacks[7])
};
// If the comm port was not initialized successfully, just sit here and complain
while (1)
Port::flash_port(PropWare::BYTE_2, PropWare::BYTE_2);
for (uint8_t n = 1; n < COGS; n++)
Runnable::invoke(syncedPrinterCogs[n]);
syncedPrinterCogs[0].run();
}
runnable.h
pwSyncOut
const PropWare::SynchronousPrinter pwSyncOut
Global and shared instance for easy printing to the terminal (thread safe)
PropWare::SynchronousPrinter::has_lock
bool has_lock() const
Determine if an instance of a SynchronousPrinter successfully retrieved a lock.
Definition: synchronousprinter.h:66
SyncedPrinterCog
Definition: SynchronousPrinter_Demo.cpp:21
synchronousprinter.h
SyncedPrinterCog::run
virtual void run()
Invoked in the new cog, this method should be the root of the business logic.
Definition: SynchronousPrinter_Demo.cpp:29
PropWare::Pin
Utility class to handle general purpose I/O pins.
Definition: pin.h:36
waitcnt2
#define waitcnt2(a, b)
Wait until system counter reaches a value.
Definition: propeller.h:183
PropWare::SynchronousPrinter::printf
void printf(const char fmt[]) const
Definition: synchronousprinter.h:139
PropWare::Port
Flexible port that can have any pin enabled or disabled. Pins are independent of each other.
Definition: port.h:38
main
int main(void)
Definition: GraphicsTest.c:20
PropWare.h
PropWare::Runnable
Helper class for creating easy parallel applications.
Definition: runnable.h:75
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