Skip to content

Commit

Permalink
Hold off on SynchronousPrinter and remove its uses from Examples
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidZemon committed Dec 10, 2014
1 parent 550ad51 commit 855039a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 133 deletions.
8 changes: 4 additions & 4 deletions Examples/PropWare_DuplexUART/DuplexUART_Demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ int main () {

// Start our new cog and initialize the speaking UART
const uint8_t cog = init_main_cog(&threadData, &speaker);
pwSyncOut.printf("New cog %u. Ready to send!!!" CRLF, cog);
// pwSyncOut.printf("New cog %u. Ready to send!!!" CRLF, cog); // TODO

while (1) {
waitcnt(500 * MILLISECOND + CNT);
Expand All @@ -93,14 +93,14 @@ void listen_silently (void *arg) {

// Initialize the listener UART and clear the buffer
init_listener_cog(buffer, &listener);
pwSyncOut.puts("Ready to receive!" CRLF);
// pwSyncOut.puts("Ready to receive!" CRLF); // TODO

while (1) {
chars = 0;
if ((err = listener.fgets(buffer, &chars)))
error(err);

pwSyncOut.printf("Data (%d chars): \"%s\"" CRLF, chars, buffer);
// pwSyncOut.printf("Data (%d chars): \"%s\"" CRLF, chars, buffer); // TODO
}
}

Expand All @@ -117,7 +117,7 @@ void init_listener_cog (char buffer[], PropWare::HalfDuplexUART *listener) {
void error (const PropWare::ErrorCode err) {
PropWare::SimplePort debugLEDs(PropWare::Port::P16, 8, PropWare::Pin::OUT);

pwSyncOut.printf("Unknown error: %u" CRLF, err);
// pwSyncOut.printf("Unknown error: %u" CRLF, err); // TODO

while (1) {
debugLEDs.write((uint32_t) err);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ volatile uint32_t wait_time = SECOND;
volatile bool syncStart = false;
volatile uint32_t startCnt;

// TODO: This should probably be fixed whenever SynchronousPrinter is complete
int main (int argc, char *argv[]) {
int8_t n;
int8_t cog;
Expand All @@ -35,7 +36,7 @@ int main (int argc, char *argv[]) {
for (n = 1; n < COGS; n++) {
cog = (int8_t) _start_cog_thread(cog_stack[n] + sizeof(cog_stack[n]),
run_cog, nullptr, &thread_data);
pwSyncOut.printf("Toggle COG %d Started" CRLF, cog);
// pwSyncOut.printf("Toggle COG %d Started" CRLF, cog);
}

startCnt = CNT;
Expand All @@ -46,7 +47,7 @@ int main (int argc, char *argv[]) {
PropWare::Pin::flash_pin(
(PropWare::Port::Mask) (1 << (cogid() + 16)), 3);

pwSyncOut.printf("Hello from cog %d" CRLF, cogid());
// pwSyncOut.printf("Hello from cog %d" CRLF, cogid());
nextCnt = waitcnt2(nextCnt, wait_time);
}
return 0;
Expand All @@ -64,7 +65,7 @@ void run_cog (void *arg) {
PropWare::Pin::flash_pin(
(PropWare::Port::Mask) (1 << (cogid() + 16)), 3);

pwSyncOut.printf("Hello from cog %d" CRLF, cogid());
// pwSyncOut.printf("Hello from cog %d" CRLF, cogid());
nextCnt = waitcnt2(nextCnt, wait_time);
}
}
72 changes: 25 additions & 47 deletions PropWare/printer.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,21 +102,20 @@ class Printer {
* object such as a PropWare::UART
*/
Printer (const PrintCapable *printCapable)
: m_printCapable(printCapable),
m_lock(-1) {
: m_printCapable(printCapable) {
}

/**
* @see PropWare::PrintCapable::put_char
*/
void put_char (const char c, const bool bypassLock = false) const {
void put_char (const char c) const {
this->m_printCapable->put_char(c);
}

/**
* @see PropWare::PrintCapable::puts
*/
void puts (const char string[], const bool bypassLock = false) const {
void puts (const char string[]) const {
this->m_printCapable->puts(string);
}

Expand All @@ -131,12 +130,11 @@ class Printer {
* value.
*/
void put_int (int32_t x, uint16_t width = 0,
const char fillChar = DEFAULT_FILL_CHAR,
const bool bypassLock = false) const {
const char fillChar = DEFAULT_FILL_CHAR) const {
if (0 > x)
this->m_printCapable->put_char('-');

this->put_uint((uint32_t) abs(x), width, fillChar, true);
this->put_uint((uint32_t) abs(x), width, fillChar);
}

/**
Expand All @@ -146,12 +144,9 @@ class Printer {
* @param[in] width Minimum number of characters to print
* @param[in] fillChar Character to print to the left of the number
* if the number's width is less than `width`
* @param[in] bypassLock For internal use only. Leave as default
* value.
*/
void put_uint (uint32_t x, uint16_t width = 0,
const char fillChar = DEFAULT_FILL_CHAR,
const bool bypassLock = false) const {
const char fillChar = DEFAULT_FILL_CHAR) const {
const uint8_t radix = 10;
char buf[sizeof(x) * 8];
uint8_t i = 0;
Expand Down Expand Up @@ -187,8 +182,7 @@ class Printer {
* value.
*/
void put_hex (uint32_t x, uint16_t width = 0,
const char fillChar = DEFAULT_FILL_CHAR,
const bool bypassLock = false) const {
const char fillChar = DEFAULT_FILL_CHAR) const {
char buf[sizeof(x)*2];
uint8_t temp, j, i = 0;

Expand Down Expand Up @@ -226,12 +220,9 @@ class Printer {
* the decimal point
* @param[in] fillChar Character to print to the left of the number
* if the number's width is less than `width`
* @param[in] bypassLock For internal use only. Leave as default
* value.
*/
void put_float (double f, uint16_t width = 0, uint16_t precision = 6,
const char fillChar = DEFAULT_FILL_CHAR,
const bool bypassLock = false) const {
const char fillChar = DEFAULT_FILL_CHAR) const {
////////////////////////////////////////////////////////////////////
// Code taken straight from Parallax's floatToString! Thank you!!!
////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -372,7 +363,7 @@ class Printer {
* @see PropWare::Printer::printf(const char *fmt, const T first,
* Targs... remaining)
*/
void printf(const char *fmt, const bool bypassLock = false) const {
void printf(const char *fmt) const {
this->puts(fmt);
}

Expand Down Expand Up @@ -413,9 +404,6 @@ class Printer {
char c;
Format format;

if (0 <= this->m_lock)
while (lockset(this->m_lock));

while (*s) {
c = *s;

Expand Down Expand Up @@ -450,18 +438,18 @@ class Printer {
switch (c) {
case 'i':
case 'd':
this->print((int32_t) first, format, true);
this->print((int32_t) first, format);
break;
case 'X':
format.radix = 16;
// No "break;" after 'X' - let it flow into 'u'
case 'u':
this->print((uint32_t) first, format, true);
this->print((uint32_t) first, format);
break;
case 'f':
case 's':
case 'c':
this->print(first, format, true);
this->print(first, format);
break;
default:
this->m_printCapable->put_char(
Expand All @@ -480,11 +468,6 @@ class Printer {

++s;
}

if (0 <= this->m_lock) {
// this->printCapable->puts("cleared" CRLF);
lockclr(this->m_lock);
}
}

/**
Expand All @@ -493,8 +476,7 @@ class Printer {
* @param[in] c Character to be printed
* @param format Unused
*/
void print (const char c, const Format format = DEFAULT_FORMAT,
const bool bypassLock = false) const {
void print (const char c, const Format format = DEFAULT_FORMAT) const {
this->put_char(c);
}

Expand All @@ -504,8 +486,8 @@ class Printer {
* @param[in] string[] String to be printed
* @param format Unused
*/
void print (const char string[], const Format format = DEFAULT_FORMAT,
const bool bypassLock = false) const {
void print (const char string[],
const Format format = DEFAULT_FORMAT) const {
this->puts(string);
}

Expand All @@ -515,16 +497,14 @@ class Printer {
* @param[in] x Unsigned value to be printed
* @param format
*/
void print (const uint32_t x, const Format format = DEFAULT_FORMAT,
const bool bypassLock = false) const {
void print (const uint32_t x,
const Format format = DEFAULT_FORMAT) const {
switch (format.radix) {
case 16:
this->put_hex(x, format.width, format.fillChar,
bypassLock);
this->put_hex(x, format.width, format.fillChar);
break;
default:
this->put_uint(x, format.width, format.fillChar,
bypassLock);
this->put_uint(x, format.width, format.fillChar);
}
}

Expand All @@ -534,9 +514,9 @@ class Printer {
* @param[in] x Unsigned value to be printed
* @param format
*/
void print (const int32_t x, const Format format = DEFAULT_FORMAT,
const bool bypassLock = false) const {
this->put_int(x, format.width, format.fillChar, bypassLock);
void print (const int32_t x,
const Format format = DEFAULT_FORMAT) const {
this->put_int(x, format.width, format.fillChar);
}

/**
Expand All @@ -545,15 +525,13 @@ class Printer {
* @param[in] x Unsigned value to be printed
* @param format
*/
void print (const double f, const Format format = DEFAULT_FORMAT,
const bool bypassLock = false) const {
this->put_float(f, format.width, format.precision, format.fillChar,
bypassLock);
void print (const double f,
const Format format = DEFAULT_FORMAT) const {
this->put_float(f, format.width, format.precision, format.fillChar);
}

protected:
const PrintCapable *m_printCapable;
int32_t m_lock; // Only used in PropWare::SynchronousPrinter
};

}
Expand Down
84 changes: 5 additions & 79 deletions PropWare/synchronousprinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace PropWare {
* @brief IMPORTANT! SynchronousPrinter is not yet working! DO NOT attempt to
* use SynchronousPrinter until this note disappears
*/
class SynchronousPrinter: public Printer {
class SynchronousPrinter {
public:
/**
* @brief Creates a synchronous instance of a Printer that can be used
Expand All @@ -44,7 +44,7 @@ class SynchronousPrinter: public Printer {
* shared across multiple cogs
*/
SynchronousPrinter (PrintCapable const *printCapable)
: Printer(printCapable) {
: m_printCapable(printCapable) {
this->m_lock = locknew();
lockclr(this->m_lock);
}
Expand Down Expand Up @@ -90,83 +90,9 @@ class SynchronousPrinter: public Printer {
return this->hasLock();
}

/**
* @see PropWare::Printer::put_char
*/
void put_char (const char c) const {
while (lockset(this->m_lock));
Printer::put_char(c);
lockclr(this->m_lock);
}

/**
* @see PropWare::Printer::puts
*/
void puts (const char string[]) const {
while (lockset(this->m_lock));
this->m_printCapable->puts(string);
lockclr(this->m_lock);
}

/**
* @see PropWare::Printer::put_int
*/
void put_int (int32_t x, uint16_t width = 0, const char fillChar = ' ',
const bool bypassLock = false) const {
if (bypassLock)
Printer::put_int(x, width, fillChar, true);
else {
while (lockset(this->m_lock));
Printer::put_int(x, width, fillChar, true);
lockclr(this->m_lock);
}
}

/**
* @see PropWare::Printer::put_uint
*/
void put_uint (uint32_t x, uint16_t width = 0,
const char fillChar = ' ',
const bool bypassLock = false) const {
if (bypassLock)
Printer::put_uint(x, width, fillChar, true);
else {
while (lockset(this->m_lock));
Printer::put_uint(x, width, fillChar, true);
lockclr(this->m_lock);
}
}

/**
* @see PropWare::Printer::put_hex
*/
void put_hex (uint32_t x, uint16_t width = 0, const char fillChar = ' ',
const bool bypassLock = false) const {
if (bypassLock)
Printer::put_hex(x, width, fillChar, true);
else {
while (lockset(this->m_lock));
Printer::put_hex(x, width, fillChar, true);
lockclr(this->m_lock);
}
}

#ifdef ENABLE_PROPWARE_PRINT_FLOAT
/**
* @see PropWare::Printer::put_float
*/
void put_float (double f, uint16_t width, uint16_t precision,
const char fillChar,
const bool bypassLock = false) const {
if (bypassLock)
Printer::put_float(f, width, precision, fillChar, true);
else {
while (lockset(this->m_lock));
Printer::put_float(f, width, precision, fillChar, true);
lockclr(this->m_lock);
}
}
#endif
protected:
const PrintCapable *m_printCapable;
int m_lock;
};

}
Expand Down

0 comments on commit 855039a

Please sign in to comment.