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

Communicate via independent and buffered UART drivers. Note that this is just an easy way to show buffered UART drivers, not a recommended application. For interaction with a human, the static variables pwOut and pwIn will be more effecient. Buffered UART drivers are great for interacting with other machines/microcontrollers/sensors/etc.

cmake_minimum_required(VERSION 3.12)
find_package(PropWare REQUIRED)
project(BufferedUART_Demo CXX COGCXX)
create_simple_executable(${PROJECT_NAME}
BufferedUART_Demo.cpp
buffereduarttx.cogcpp
buffereduartrx.cogcpp
)
#pragma once
#define STACK_SIZE 64
typedef struct {
unsigned int stack[STACK_SIZE];
volatile CharQueue *queue;
_NATIVE int main () {
CharQueue *buffer = *reinterpret_cast<CharQueue **>(PAR);
const UARTTX tx;
while (1) {
while (buffer->is_empty());
const char c = buffer->dequeue();
tx.send(static_cast<uint16_t>(c));
}
}
_NATIVE int main () {
CharQueue *buffer = *reinterpret_cast<CharQueue **>(PAR);
const UARTRX rx;
while (1) {
const char c = static_cast<char>(rx.receive());
while (buffer->is_full());
buffer->enqueue(c);
}
}
static const size_t BUFFER_SIZE = 256;
static void runScannerDemo (Printer &queuePrinter, Scanner &queueScanner);
static bool isAnswerNo (char const userInput[]);
int main () {
char transmitBuffer[BUFFER_SIZE];
CharQueue transmitQueue(transmitBuffer, BUFFER_SIZE, locknew());
BufferedUARTMailbox transmitMailbox;
transmitMailbox.queue = &transmitQueue;
extern unsigned int _load_start_buffereduarttx_cog[];
cognew(_load_start_buffereduarttx_cog, &transmitMailbox.queue);
Printer queuePrinter(transmitQueue);
char receiveBuffer[BUFFER_SIZE];
CharQueue receiveQueue(receiveBuffer);
BufferedUARTMailbox receiveMailbox;
receiveMailbox.queue = &receiveQueue;
extern unsigned int _load_start_buffereduartrx_cog[];
cognew(_load_start_buffereduartrx_cog, &receiveMailbox.queue);
Scanner queueScanner(receiveQueue, &queuePrinter);
// This should look very familiar - it is the same code as Examples/PropWare_Scanner/Scanner_Demo.cpp
runScannerDemo(queuePrinter, queueScanner);
while (1);
}
void runScannerDemo (Printer &queuePrinter, Scanner &queueScanner) {
// A nice big buffer that can hold up to 63 characters from the user (the
// 64th is used by the null-terminator)
char userInput[64];
char name[64];
unsigned int age;
queuePrinter << "Hello! I'd like to teach you how to use PropWare to read from the terminal!\n";
do {
queuePrinter << "First, what's your name?\n>>> ";
queueScanner >> name;
queuePrinter << "And how old are you?\n>>> ";
queueScanner >> age;
queuePrinter << "Is your name " << name << " and are you " << age << " years old?\n>>> ";
queueScanner.input_prompt("", "Please enter either 'yes' or 'no' (y/n)\n>>> ", userInput, sizeof(userInput),
} while (isAnswerNo(userInput));
queuePrinter << "Hello, " << name << "!\n";
}
bool isAnswerNo (char const userInput[]) {
return 0 == strcmp("n", userInput) || 0 == strcmp("no", userInput);
}
printer.h
BufferedUART_Demo.h
_NATIVE
#define _NATIVE
Can be used in per-function declarations to tell compiler that function will use cog "call/ret" calli...
Definition: cog.h:57
PropWare::Scanner
Interface for all classes capable of scanning.
Definition: scanner.h:43
cognew
#define cognew(code, param)
Start a new Propeller PASM COG.
Definition: propeller.h:94
uartrx.h
uarttx.h
PropWare::UARTRX
Receive routines for basic UART communication.
Definition: uartrx.h:41
PropWare::UARTTX
Definition: uarttx.h:38
charqueue.h
YES_NO_COMP
const PropWare::YesNoComparator YES_NO_COMP
Global instance for shared use by PropWare applications.
Definition: comparator.cpp:29
main
int main(void)
Definition: GraphicsTest.c:20
scanner.h
PropWare::CharQueue
Provide a communication buffer for character data between cogs.
Definition: charqueue.h:46
BufferedUARTMailbox
Definition: BufferedUART_Demo.h:33
locknew
#define locknew()
Get a new lock from the pool of Propeller hardware locks.
Definition: propeller.h:147
PAR
#define PAR
Parameter register is used for sharing HUB RAM address info with the COG.
Definition: propeller1.h:149
PropWare::Printer
Container class that has formatting methods for human-readable output. This class can be constructed ...
Definition: printer.h:76
PropWare
Generic definitions and functions for the Parallax Propeller.
Definition: runnable.h:33