PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
SimpleText_Demo.c

Project and Test harness for library simpletext

cmake_minimum_required(VERSION 3.12)
find_package(PropWare REQUIRED)
project(SimpleText_Demo)
create_simple_executable(${PROJECT_NAME} SimpleText_Demo.c)
#include "serial.h"
#define MORETESTING
int main(void)
{
int n;
char buffer[80];
char sval[80];
int buflen = 80;
int xval = 0;
int ival = 0x55;
float fval = 355/113.0;
float fval2 = 1.4;
float e = 2.71828184590;
char bigtext[] = "abcdefghijklmnopqrstuvwxyz";
/*
* global serial module pointer - can be local.
*/
serial *text;
/* no need to wait for terminal startup.
* delay is done in the default serial open function. */
/* traditional hello message. */
putln("Hello, world!");
putStrLen(&bigtext[20],5);
putln("");
writeStrLen(simpleterm_pointer(),&bigtext[4],5);
putln("");
#ifdef DIV0_NAN_TEST
float fproblem;
float f;
for(f = 5.0; f > -5.0; f -= 1.0) {
fproblem = 1.0 / f;
print("f = %02.2f, fproblem = %02.2f\n", f, fproblem);
}
for(f = 5.0; f > -5.0; f -= 1.0) {
fproblem = 1.0 / -f;
putStr("f = "); putFloat(f); putStr(" fproblem = "); putFloat(fproblem); putLine("");
}
for(f = 5.0; f > -5.0; f -= 1.0) {
fproblem = atan(0.0/f);
print("f = %02.2f, fproblem = %02.2f\n", f, fproblem);
}
for(f = 5.0; f > -5.0; f -= 1.0) {
fproblem = atan(0.0/-f);
putStr("f = "); putFloat(f); putStr(" fproblem = "); putFloat(fproblem); putLine("");
}
#endif
#ifdef MORETESTING
putDec(1);
putChar(' ');
putDec(-1);
putLine("");
putLine("Hello, again!");
sprint(sval,"Toast Test");
putln(sval);
#if 1
putStr("\nEnter scan float string : ");
scan("%f %s", &fval, sval);
print("%f %s\n", fval, sval);
putFloat(fval);
putStr("\n");
putln(sval);
putStr("\nEnter sscan float string: ");
getStr(buffer, buflen);
putln(buffer);
sscan(buffer, "%f %s", &fval, sval);
sprint(buffer, "%f %s", fval, sval);
putln(buffer);
putFloat(fval);
putStr("\n");
putln(sval);
#endif
putFloat(101.3);
putStr("\n");
putStr("\n");
putStr("\n");
putStr("\nDecimal ");
sprint(buffer, "%2d %4d %6d %8d %10d %15d", 2, 4, 6, 8, 10, 15);
putStr(buffer);
putStr("\nHex ");
sprint(buffer, "%2x %4x %6x %8x %10x %15x", 2, 4, 6, 8, 10, 15);
putStr(buffer);
putStr("\nBinary ");
sprint(buffer, "%2b %4b %6b %8b %10b %15b", 2, 4, 6, 8, 10, 15);
putStr(buffer);
putStr("\nFloat(e) ");
sprint(buffer, "%2.0f %4.1f %6.2f %8.3f %10.4f %15.6f\n", e, e, e, e, e, e);
putln(buffer);
for(n = 0; n < 1; n++) {
float f;
putStr("\nEnter two floating point numbers");
if(!n)
putStr(": ");
else
putStr(" again: ");
getStr(buffer, buflen);
sscan(buffer, "%f %f", &f, &fval2);
putChar(' ');
putFloat(fval2);
}
putStr("\nEnter a floating point number: ");
/* Close SimpleTerm so we can use port with FdSerial */
putStr("\nClose default console.\n");
/* restart device */
text = serial_open(31,30,0,115200);
writeStr(text, "SimpleSerial Started.\n");
/* traditional hello message using buffer printf. */
sprint(buffer, "Hello, world! Again!\n");
writeStr(text, buffer);
#if 1
writeStr(text, "\nEnter dscan float string: ");
dscan(text, "%f %s", &fval, sval);
dprint(text, "%f %s\n", fval, sval);
writeFloat(text, fval);
writeLine(text, "");
writeLine(text, sval);
#endif
writeChar(text, 'T');
writeChar(text, 'o');
writeChar(text, 'o');
writeChar(text, 't');
writeStr(text, "\n");
writeDec(text, ival);
writeChar(text, ' ');
writeHex(text, ival);
writeChar(text, ' ');
writeBin(text, ival);
fval = 355/113.0;
writeStr(text, "\n");
writeFloatPrecision(text, fval, 2, 10);
writeChar(text, ' ');
writeFloatPrecision(text, fval*100.0, 2, 10);
writeLine(text, "");
writeLine(text, "Press any key: ");
writeChar(text, readChar(text));
writeStr(text, "\nEnter a decimal number: ");
writeDecLen(text, readDec(text),8);
writeStr(text, "\nEnter a hexadecimal number: ");
writeHexLen(text, readHex(text),8);
writeStr(text, "\nEnter a binary number: ");
writeBinLen(text, readBin(text),8);
writeStr(text, "\nEnter a floating point number: ");
writeFloatPrecision(text, readFloat(text),8,8);
writeStr(text, "\nEnter values as: decimal hex float float string\n");
readStr(text, buffer, buflen);
writeStr(text, buffer);
sscan(buffer, "%d %x %f %f %s", &ival, &xval, &fval, &fval2, sval);
sprint(buffer, "%s %d %x %f %f\n", sval, ival, xval, fval, fval2);
writeLine(text, "");
writeLine(text, buffer);
writeLine(text, "All done.");
serial_close(text);
#endif
return 0;
}
/*
+--------------------------------------------------------------------
| TERMS OF USE: MIT License
+--------------------------------------------------------------------
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+--------------------------------------------------------------------
*/
writeDecLen
void writeDecLen(text_t *device, int value, int width)
Print string representation of a decimal number to output device with a fixed number of digits.
Definition: writeDecDigits.c:10
writeHexLen
void writeHexLen(text_t *device, int value, int digits)
Print string representation of a hexadecimal number to output device with a fixed number of digits.
Definition: writeHexDigits.c:10
putln
int putln(const char *str)
Print string + new line on the transmit debug port.
Definition: putln.c:13
writeBinLen
void writeBinLen(text_t *device, int value, int digits)
Print string representation of a binary number to output with a fixed number of digits.
Definition: writeBinDigits.c:10
simpleterm_pointer
terminal * simpleterm_pointer(void)
Get default device pointer to SimpleIDE Terminal.
Definition: simpleterm.c:51
putStrLen
int putStrLen(char *str, int width)
Send a string with a certain number of characters.
Definition: putStrDigits.c:11
writeLine
int writeLine(text_t *device, char *str)
Send a string + new line to the device.
Definition: writeLine.c:10
dscan
int int dscan(text_t *device, const char *fmt,...) __attribute__((format(printf
Convert formatted device input to the "..." args. The input is limited to 256 bytes.
putFloat
void putFloat(float value)
Print string representation of a 32 bit floating point number to the debug port.
Definition: putFloat.c:10
dprint
int dprint(text_t *device, const char *format,...) __attribute__((format(printf
Print format "..." args to the device The output is limited to 256 bytes.
putLine
int putLine(const char *str)
Print string + new line on the transmit debug port. This is an alias of putln.
Definition: putLine.c:14
putDec
void putDec(int value)
Print string representation of a decimal number to the debug port.
Definition: putDec.c:10
getStr
char * getStr(char *buffer, int max)
Get string of chars from the debug port.
Definition: getStr.c:19
serial_close
void serial_close(serial *device)
Close serial connection.
Definition: serial_close.c:11
writeDec
void writeDec(text_t *device, int value)
Print string representation of a decimal number to output.
Definition: writeDec.c:10
readStr
char * readStr(text_t *device, char *buffer, int max)
Get string of chars from the device.
Definition: getStr.c:25
print
int print(const char *format,...) __attribute__((format(printf
Print format "..." args to the default simple terminal device. The output is limited to 256 bytes.
writeFloatPrecision
void writeFloatPrecision(text_t *device, float value, int width, int precision)
Print string representation of a 32 bit floating point number to device with a certain number of deci...
Definition: writeFloatPrecision.c:10
readDec
int readDec(text_t *device)
Get decimal number from the device.
Definition: getDec.c:17
simpleterm_close
void simpleterm_close(void)
Closes the SimpleIDE Terminal connection in one cog so that it can be opened in another cog with simp...
Definition: simpleterm_close.c:19
getFloat
float getFloat(void)
Get floating point number from the debug port.
Definition: getFloat.c:10
writeFloat
void writeFloat(text_t *device, float value)
Print string representation of a 32 bit floating point number to device.
Definition: writeFloat.c:10
main
int main(void)
Definition: GraphicsTest.c:20
readChar
int readChar(text_t *device)
Get char from the device.
Definition: getChar.c:17
writeHex
void writeHex(text_t *device, int value)
Print string representation of a hexadecimal number to output device.
Definition: writeHex.c:10
writeChar
void writeChar(text_t *device, char c)
Send a character to the device.
Definition: writeChar.c:11
putChar
void putChar(char c)
Print a char to the debug port.
Definition: putChar.c:30
scan
int int scan(const char *fmt,...) __attribute__((format(printf
Convert formatted simple terminal input to the "..." args. The input is limited to 256 bytes.
text_struct
Structure that contains data used by simple text device libraries.
Definition: simpletext.h:81
readFloat
float readFloat(text_t *device)
Get floating point number from the device.
Definition: getFloat.c:16
serial_open
serial * serial_open(int rxpin, int txpin, int mode, int baudrate)
Open a simple (half duplex) serial connection.
Definition: serial_open.c:14
sprint
int int int sprint(char *buffer, const char *format,...) __attribute__((format(printf
Print format "..." args to the output buffer. The output buffer must be big enough for the output.
writeStr
int writeStr(text_t *device, char *str)
Send a string to the device.
Definition: writeStr.c:10
writeBin
void writeBin(text_t *device, int value)
Print string representation of a binary number to the receive device.
Definition: writeBin.c:10
PropWare
Generic definitions and functions for the Parallax Propeller.
Definition: runnable.h:33
putStr
int putStr(const char *str)
Print string to the debug port.
Definition: putStr.c:10
readHex
int readHex(text_t *device)
Get hexadecimal number from the device.
Definition: getHex.c:15
writeStrLen
int writeStrLen(text_t *device, char *str, int width)
Send a string to the device.
Definition: writeStrDigits.c:11
sscan
int int int int sscan(const char *buffer, const char *fmt,...) __attribute__((format(printf
Convert formatted buffer to the "..." args.
readBin
int readBin(text_t *device)
Get binary number from the device.
Definition: getBin.c:16