simpletools library  v1.1.8
Collection of convenience functions for common microcontroller tasks
Macros | Functions | Variables
simpletools.h File Reference

This library provides convenient functions for a variety of microcontroller I/O, timing, conversion, and communication tasks. This library also includes (and you can call functions from) simpletext and serial. More...

#include <propeller.h>
#include "simpletext.h"
#include <driver.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cog.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/stat.h>
#include <dirent.h>
#include <sys/sd.h>
#include <math.h>
#include "simplei2c.h"

Go to the source code of this file.

Macros

#define PI   3.141592653589793
 3.141592653589793
 
SimpleIDE Terminal Constants
#define HOME   (1)
 HOME character (1) sends SimpleIDE Terminal's cursor to top-left "home" position.
 
#define CRSRXY   (2)
 CRSRXY character (2) sends cursor to a certain number of spaces over (X) and returns (Y) down from SimpleIDE Terminal's top-left HOME position. This character has to be followed immediately by the X and Y values when transmitted to the SimpleIDE Terminal.
 
#define CRSRLF   (3)
 CRSRLF character (3) sends the SimpleIDE Terminal's cursor one column (space) to the left of its current position.
 
#define CRSRRT   (4)
 CRSRRT character (4) sends the SimpleIDE Terminal's cursor one column (space) to the right of its current position.
 
#define CRSRUP   (5)
 CRSRUP character (5) sends the SimpleIDE Terminal's cursor one row (carriage return) upward from its current position.
 
#define CRSRDN   (6)
 CRSRDN character (6) sends the SimpleIDE Terminal's cursor one row (carriage return) downward from its current position.
 
#define BEEP   (7)
 BEEP character (7) makes the system speaker in some computers beep when received by SimpleIDE Terminal.
 
#define BKSP   (8)
 BKSP character (8) sends the SimpleIDE Terminal's cursor one column (space) to the left of its current position and erases whatever character was there.
 
#define TAB   (9)
 TAB character (9) advances the cursor to the right by a tab's worth of spaces.
 
#define NL   (10)
 NL character (10) sends the SimpleIDE Terminal's cursor to the leftmost character in the next line down.
 
#define LF   (10)
 LF is same as NL.
 
#define CLREOL   (11)
 CLREOL character (11) erases all SimpleIDE Terminal characters to the right of the cursor.
 
#define CLRDN   (12)
 CLRDN character (12) erases all SimpleIDE Terminal characters below the cursor.
 
#define CR   (13)
 CR character (13) sends SimpleIDE Terminal's cursor one row downward.
 
#define CRSRX   (14)
 CRSRX character (14) positions SimpleIDE Terminal's cursor X characters from the its left edge.
 
#define CRSRY   (15)
 CRSRY character (15) sends SimpleIDE Terminal's cursor Y rows to the from its top edge.
 
#define CLS   (16)
 CLS character (16) clears SimpleIDE's screen, erasing all characters and placing the cursor in the top-left corner.
 
SPI Constants for shift_in & shift_out
#define MSBPRE   0
 For use with shift_in. Stands for most significant bit first, pre-clock.
 
#define LSBPRE   1
 For use with shift_in. Stands for least significant bit first, pre-clock.
 
#define MSBPOST   2
 For use with shift_in. Stands for most significant bit first, post-clock.
 
#define LSBPOST   3
 For use with shift_in. Stands for least significant bit first, post-clock.
 
#define LSBFIRST   0
 For use with shift_out. Stands for least significant bit first.
 
#define MSBFIRST   1
 For use with shift_out. Stands for most significant bit first.
 
Counter Module Constants
#define NCO_PWM_1   (0b00100 << 26)
 Building block for configuring a cog's counter module to PWM mode. Used by pwm functions. PWM stands for pulse width modulation.
 
#define CTR_NCO   (0b100 << 26)
 Building block for configuring a cog's counter module to NCO mode. Used by square_wave function. NCO stands for numerically controlled oscillator.
 
#define CTR_PLL   (0b10 << 26)
 Building block for configuring a cog's counter module to PLL mode. Used by square_wave function. PLL stands for phase locked loop.
 
#define DUTY_SE   (0b110 << 26)
 Building block for configuring a cog's counter module to DUTY_SE mode. Used by dac functions. DUTY_SE stands for duty single ended.
 
Reverse Compatibility Functions
#define ee_put_byte   ee_putByte
 ee_put_byte renamed ee_putByte.
 
#define ee_get_byte   ee_getByte
 ee_get_byte renamed ee_getByte.
 
#define ee_put_int   ee_putInt
 ee_put_int renamed ee_putInt.
 
#define ee_get_int   ee_getInt
 ee_get_int renamed ee_getInt.
 
#define ee_put_str   ee_putStr
 ee_put_str renamed ee_putStr.
 
#define ee_get_str   ee_getStr
 ee_get_str renamed ee_getStr.
 
#define ee_put_float32   ee_putFloat32
 ee_put_float32 renamed ee_putFloat32.
 
#define pause_ticks(pticks)   __builtin_propeller_waitcnt(pticks+CNT, 0)
 (Deprecated) Use waitcnt(CLKFREQ + CNT) for a delay that lasts 1 second, and use fractions of CLKFREQ for smaller numbers of system clock ticks.
 
Propeller EEPROM Address
#define EEPROM_ADDR   0x50
 Propeller EEPROM I2C bus address.
 

Functions

void high (int pin)
 Set an I/O pin to output-high. More...
 
void low (int pin)
 Set an I/O pin to output-low. More...
 
int input (int pin)
 Set an I/O pin to input and return 1 if pin detects a high signal, or 0 if it detects low. More...
 
More Individual I/O
unsigned int toggle (int pin)
 Toggle the output state of the I/O pin. More...
 
unsigned int reverse (int pin)
 Reverse the direction of an I/O pin. More...
 
unsigned int get_state (int pin)
 Check the state of an I/O pin without setting it to input. More...
 
unsigned int get_direction (int pin)
 Check the direction of the I/O pin. More...
 
unsigned int get_output (int pin)
 Get I/O pin output state. More...
 
void set_direction (int pin, int direction)
 Set an I/O pin to a given direction. More...
 
void set_output (int pin, int state)
 Set I/O pin output register bit to either 1 or 0. More...
 
Group I/O
unsigned int get_states (int endPin, int startPin)
 Get states of a contiguous group of I/O pins. More...
 
unsigned int get_directions (int endPin, int startPin)
 Get directions for a contiguous group of I/O pins. More...
 
unsigned int get_outputs (int endPin, int startPin)
 Get output settings for a contiguous group of I/O pins. More...
 
void set_directions (int endPin, int startPin, unsigned int pattern)
 Set directions for a contiguous group of I/O pins. More...
 
void set_outputs (int endPin, int startPin, unsigned int pattern)
 Set output states for a contiguous group of I/O pins. More...
 
Timing
void pause (int time)
 Delay cog from moving on to the next statement for a certain length of time. More...
 
void set_pause_dt (int clockticks)
 Set time increment for pause function. More...
 
Timed I/O
long count (int pin, long duration)
 Count number of low to high transitions an external input applies to an I/O pin over a certain period of time. More...
 
void dac_ctr (int pin, int channel, int dacVal)
 Set D/A voltage. More...
 
void dac_ctr_res (int bits)
 Set D/A voltage resolution. More...
 
void dac_ctr_stop (void)
 Stop the cog that's transmitting the DAC signal(s). More...
 
void freqout (int pin, int msTime, int frequency)
 Use same cog to send square wave of a certain frequency for a certain amount of time. For set and forget with another cog, try square_wave function instead. More...
 
int pwm_start (unsigned int cycleMicroseconds)
 Start pulse width modulation (PWM) process in another cog. More...
 
void pwm_set (int pin, int channel, int tHigh)
 Set a PWM signal's high time. More...
 
void pwm_stop (void)
 Shut down PWM process and reclaim cog and I/O pins for other uses. More...
 
long pulse_in (int pin, int state)
 Measure the duration of a pulse applied to an I/O pin. More...
 
void pulse_out (int pin, int time)
 Transmit a pulse with an I/O pin. More...
 
long rc_time (int pin, int state)
 Set I/O pin to input and measure the time it takes a signal to transition from a start state to the opposite state. More...
 
void square_wave (int pin, int channel, int freq)
 Make I/O pin transmit a repeated high/low signal at a certain frequency. High and low times are the same. Frequency can range from 1 Hz to 128 MHz. More...
 
void square_wave_stop (void)
 Stop the cog that's transmitting a square wave. More...
 
void set_io_timeout (long clockTicks)
 Sets the timeout value for the following timed I/O functions: pulse_in, rc_time. More...
 
void set_io_dt (long clockticks)
 Sets the time increment for the following timed I/O functions: count, pulse_in, pulse_out, rc_time. More...
 
SPI
int shift_in (int pinDat, int pinClk, int mode, int bits)
 Receive data from a synchronous serial device. More...
 
void shift_out (int pinDat, int pinClk, int mode, int bits, int value)
 Send data to a synchronous serial device. More...
 
I2C
i2c * i2c_newbus (int sclPin, int sdaPin, int sclDrive)
 Set up a simple serial driver with transmit & receive pins. More...
 
HUBTEXT int i2c_out (i2c *busID, int i2cAddr, int memAddr, int memAddrCount, const unsigned char *data, int dataCount)
 Send data to device using I2C protocol. More...
 
HUBTEXT int i2c_in (i2c *busID, int i2cAddr, int memAddr, int memAddrCount, unsigned char *data, int dataCount)
 Receive data from device using I2C protocol. More...
 
HUBTEXT int i2c_busy (i2c *busID, int i2cAddr)
 Check if I2C device is busy or responding. More...
 
Propeller EEPROM
void ee_putByte (unsigned char value, int addr)
 Store a byte value at a certain address in the Propeller Chip's dedicated EEPROM. More...
 
char ee_getByte (int addr)
 Get a byte value from a certain address in the Propeller Chip's dedicated EEPROM. More...
 
void ee_putInt (int value, int addr)
 Store an int value at a certain address in the Propeller Chip's dedicated EEPROM. An int value occupies four bytes, so the next value should be stored at an address value that's four bytes higher. More...
 
int ee_getInt (int addr)
 Get an int value from a certain address in the Propeller Chip's dedicated EEPROM. If you are fetching several int values, make sure to add 4 to the addr value with each successive call. More...
 
void ee_putStr (unsigned char *s, int n, int addr)
 Store a string of byte values starting at a certain address in the Propeller Chip's dedicated EEPROM. More...
 
unsigned char * ee_getStr (unsigned char *s, int n, int addr)
 Fetch a string of byte values starting at a certain address in Propeller Chip's dedicated EEPROM. More...
 
void ee_putFloat32 (float value, int addr)
 Store a 32-bit precision floating point value at a certain address in the Propeller Chip's dedicated EEPROM. A 32-bit value occupies four bytes so if you are storing values in a sequence, make sure to add 4 to each addr parameter value. More...
 
float ee_getFloat32 (int addr)
 Fetch a 32-bit precision floating point value from a certain address in the Propeller Chip's dedicated EEPROM. A 32-bit value occupies four bytes so if you are fetching values in a sequence, make sure to add 4 to each addr parameter value. More...
 
void ee_config (int sclPin, int sdaPin, int sclDrive)
 Optional function for setting a custom EEPROM configuration. Other ee_ functions automatically check if the EEPROM has been initialized, and if not, they use default settings equivalent to ee_config(28, 29, 0). This function can be called before any other ee_ functions to replace those defaults with custom settings. More...
 
SD Card
int sd_mount (int doPin, int clkPin, int diPin, int csPin)
 Mount an SD card with the minimal 4-pin interface. For Parallax Learn Site examples, see: SD Card Data and Play WAV Files. More...
 
Multicore
int * cog_run (void(*function)(void *par), int stacksize)
 Run a function's code in the next available cog (processor). More...
 
int cog_num (int *coginfo)
 Get the cog ID. More...
 
void cog_end (int *coginfo)
 End function code running in another cog that was launched with cog_run. More...
 
Terminal Control
void term_cmd (int termConst,...)
 Send a command to SimpleIDE Terminal. Examples of commands include HOME, CLS, BKSP, CRSRXY, and others. All sixteen are listed in the SimpleIDE Terminal Constants section above. Click the term_cmd link to go to the details section and see parameter descriptions and code examples. More...
 
Calculation Extras
float constrainFloat (float value, float min, float max)
 Constrains a floating point value to a range from a minimum value to a maximum value. If the value is above the max constraint, this function returns the maximum constraint value. If the value is below the min constraint, it returns the minimum constraint value. If value falls between the max and min constraints, it returns the same value that was received. More...
 
int constrainInt (int value, int min, int max)
 Constrains an integer value to a range from a minimum value to a maximum value. If the value is above the max constraint, this function returns the maximum constraint value. If the value is below the min constraint, it returns the minimum constraint value. If value falls between the max and min constraints, it returns the same value that was received. More...
 
void endianSwap (void *resultAddr, void *varAddr, int byteCount)
 Take bytes in one variable at varAddr, swap their order, and store them in another variable at resultAddr. This is useful for communication with peripherals that transmit/receive bytes in multi-byte values in reverse order from how the Propeller stores it in RAM. More...
 
float mapFloat (float value, float fromMin, float fromMax, float toMin, float toMax)
 Maps a floating point value from its position in one range to its corresponding. position in a different range. For example, 3.0 in a range of 0.0 to 10.0 would map to 30.0 in a range of 0.0 to 100.0. Note: In some cases, 32 bit floating point values will round slightly. For example 2.0/3.0 = 0.666667. More...
 
int mapInt (int value, int fromMin, int fromMax, int toMin, int toMax)
 Maps an integer value from its position in one range to its corresponding. position in a different range. For example, 3 in a range of 0 to 10 would map to 30 in a range of 0 to 100. More...
 
int random (int limitLow, int limitHigh)
 Generates a pseudo-random integer value that falls in a range from limitLow to limitHigh. This function uses the system clock and I/O registers to create a new seed with each call, so it is very unlikely to generate the same sequence twice in a row. More...
 
Deprecated
void mark (void)
 Mark the current time (deprecated). More...
 
int timeout (int time)
 Compares the time against the time elapsed since mark (deprecated). More...
 
void wait (int time)
 Waits a certain number of time increments from the last call to mark or wait functions (deprecated). More...
 

Variables

int ms
 Propeller system clock ticks in 1 millisecond (ms).
 
int us
 Propeller system clock ticks in 1 millisecond (us).
 
Private (used by simpletools library)
int st_msTicks
 Propeller system clock ticks in 1 millisecond. Changing this value is not recommended because it can affect library performance.
 
int st_usTicks
 Propeller system clock ticks in 1 microsecond. Changing this value is not recommended because it can affect library performance.
 
int st_iodt
 Clock ticks in a time increment used by pulse_in, pulse_out, and rc_time. Default value is the number of system clock ticks in a microsecond = CLKFREQ/1000000.
 
int st_timeout
 Clock ticks in a time increment used by pulse_in, pulse_out, and rc_time. Default value is the number of system clock ticks in 1/4 s = CLKFREQ/4.
 
int st_pauseTicks
 Clock ticks in a time increment used by pause function. Default value is the number of system clock ticks in 1/1000 s = CLKFREQ/1000.
 
int st_mark
 Variable shared by mark and time_out functions.
 
unsigned int st_buscnt
 Variable used by i2c_newbus.
 
i2c * st_eeprom
 The busID for the Propeller Activity Board's EEPROM bus.
 
int st_eeInitFlag
 Initialization flag used by ee_ functions.
 

Detailed Description

This library provides convenient functions for a variety of microcontroller I/O, timing, conversion, and communication tasks. This library also includes (and you can call functions from) simpletext and serial.

Author
Andy Lindsay

This library provides a set of introductory functions that simplify:

Applications include: monitoring, control and communication with simple peripherals, like lights, buttons, dials, motors, peripheral integrated circuits and prototyping with simple systems that use pulse, or serial communication. (A few examples from the very large list of devices includes: servos, ultrasonic distance sensors, accelerometers, serial liquid crystal, display, etc.)

Intended use: Accompanies introductory electronics, robotics and programming lessons and projects on learn.parallax.com. After these lessons, bridge lessons will be added to familiarize the programmer with standard practices used by the community for adding libraries to support and endless variety of peripherals and applications.

Core Usage
Any of these functions, if called, will launch a process into another cog and leave it launched for set it/forget it processes:
Memory Models
Use with CMM or LMM.
Version
1.1.8 Add constrainFloat, constrainInt, mapFloat, mapInt, and random.
1.1.7 Update pause function for up to 2,147,483,647 ms.
0.98.2 Add term_cmd function for SimpleIDE Terminal cursor, screen, and audio control.
0.98 fpucog floating point coprocessor no longer self-starts by default. All floating point functionality is still supported, processing just happens in the same cog. i2c_out and i2c_in char regAddr parameter changed to int memAddr. itoa removed, use sprint(charArray, "%d", intVal) to make int to ASCII conversions. st_msTicks and st_usTicks global variables are pre-initialized to the number of system clock ticks in a millisecond and microsecond for convenience in library development. Variables named us and ms are initialized to the same values for user applications. Function endianSwap added to simplify communication with devices that send/receive byte data in big endian format.
0.97 Add cog_run and cog_end for simplified running of functioncode in other cogs.
0.96.1 Add documentation for start_fpu_cog and stop_fpu_cog.
0.96 ee_putStr updated to support 128 byte page writes. More corrections to ee_put for contiguous data crossing address/128 boundary.
0.95 square_wave bug that prevented output frequency changes (fixed).
0.94 Fixed bug in ee_put* that prevented contiguous data from crossing the EEPROM's address/128 buffer boundaries. Updated stack array to static in mstimer.c.
0.93 i2c_newbus now uses
.../Learn/Simple Libraries/Protocol/simplei2c/
Added:
i2c_out, i2c_in to cover most common I2C applications EEPROM ee_get_* and ee_put_* changed to ee_get* and ee_put* where the * term is camel-case.
0.92 Simpletext functionality incorporated for use of character and string I/O with both terminal and peripheral devices. Simple Text folder replaces PropGCC serial driver support for simple and full duplex serial peripherals.
0.91 shift_in function pre-clock mode bug fixed.

Help Improve this Library
Please submit bug reports, suggestions, and improvements to this code to edito.nosp@m.r@pa.nosp@m.ralla.nosp@m.x.co.nosp@m.m.

Definition in file simpletools.h.

Function Documentation

◆ cog_end()

void cog_end ( int *  coginfo)

End function code running in another cog that was launched with cog_run.

This function uses the value returned by cog_run to stop a function running in another cog and free the stack space cog_run allocated with its stacksize parameter.

Parameters
*coginfothe address returned by cog_run.

◆ cog_num()

int cog_num ( int *  coginfo)

Get the cog ID.

Parameters
*coginfothe address returned by cog_run.
Returns
The cog ID number.

◆ cog_run()

int* cog_run ( void(*)(void *par)  function,
int  stacksize 
)

Run a function's code in the next available cog (processor).

cog_run is designed to make launching application level functions (typically from the main file) quick and easy. All you have to do is pass a pointer to a function with no return value or parameters along with the number for extra memory to reserve. The value returned can be used to shut down the process and free up memory and a cog later by passing it to cog_end.

Parameters
*functionpointer to a function with no parameters or return value. Example, if your function is void myFunction(), then pass &myFunction.
stacksizeNumber of extra int variables for local variable declarations and call/return stack. This also needs to cover any local variable declarations in functions that your function calls, including library functions. Be liberal with extra stack space for prototyping, and if in doubt, 40 to whatever value you calculate.
Returns
*coginfo Address of memory set aside for the cog. Make sure to save this value in a variable if you intend to stop the process later with cog_end or check which cog the process was launched into with cog_num.

◆ constrainFloat()

float constrainFloat ( float  value,
float  min,
float  max 
)

Constrains a floating point value to a range from a minimum value to a maximum value. If the value is above the max constraint, this function returns the maximum constraint value. If the value is below the min constraint, it returns the minimum constraint value. If value falls between the max and min constraints, it returns the same value that was received.

Parameters
valueValue to constrain.
minMinimum constraint.
maxMaximum constraint.
Returns
Constrained result.

◆ constrainInt()

int constrainInt ( int  value,
int  min,
int  max 
)

Constrains an integer value to a range from a minimum value to a maximum value. If the value is above the max constraint, this function returns the maximum constraint value. If the value is below the min constraint, it returns the minimum constraint value. If value falls between the max and min constraints, it returns the same value that was received.

Parameters
valueValue to constrain.
minMinimum constraint.
maxMaximum constraint.
Returns
Constrained result.

◆ count()

long count ( int  pin,
long  duration 
)

Count number of low to high transitions an external input applies to an I/O pin over a certain period of time.

Parameters
pinI/O pin number
durationAmount of time the measurement counts transitions
Returns
The number of low to high transitions

◆ dac_ctr()

void dac_ctr ( int  pin,
int  channel,
int  dacVal 
)

Set D/A voltage.

Launches process into another cog for up to two channels of D/A conversion on any I/O pin. Other libraries may be available that provide D/A for more channels. Check SimpleIDE/Learn/Simple Libraries/Convert for options. For more options, check obex.parallax.com.

This library uses another cog's counter modules (2 per cog) to perform duty modulation, which is useful for D/A conversion. The digital signal it generates will affect LED brightness. The signal can be passed through a low pass RC filter for digital to analog voltage conversion. Add an op amp buffer if it needs to drive a load.

Default resolution is 8 bits for output voltages ranging from 0 V to (255/256) of 3.3 V.

General equation is dacVal * (3.3 V/2^bits)

Default is 8 bits, which results in dacVal * (3.3 V/ 256), so dacVal specifies the number of 256ths of 3.3 V. You can change the resolution with the dac_ctr_res function.

Parameters
pinI/O pin number.
channelUse 0 or 1 to select the cog's CTRA or CTRB counter modules, which are used for D/A conversion.
dacValNumber of 256ths of 3.3 V by default. Use a value from 0 (0 V) to 255 .

◆ dac_ctr_res()

void dac_ctr_res ( int  bits)

Set D/A voltage resolution.

Default resolution is 8-bits for output voltages ranging from 0 V to (255/256) of 3.3 V.

General equation is dacVal * (3.3 V/2^bits)

Default is 8 bits, which results in dacVal * (3.3 V/ 256), so dacVal specifies the number of 256ths of 3.3 V.

Parameters
bitsThe D/A converter's resolution in bits.

◆ dac_ctr_stop()

void dac_ctr_stop ( void  )

Stop the cog that's transmitting the DAC signal(s).

Stops any signals, lets go of any I/O pins, and reclaims the cog for other uses.

◆ ee_config()

void ee_config ( int  sclPin,
int  sdaPin,
int  sclDrive 
)

Optional function for setting a custom EEPROM configuration. Other ee_ functions automatically check if the EEPROM has been initialized, and if not, they use default settings equivalent to ee_config(28, 29, 0). This function can be called before any other ee_ functions to replace those defaults with custom settings.

Warning
: If you're going to call this function, make sure to do it before calling any other ee_ functions. If one ee_ function gets called before this, it'll set up defaults, and this function cannot override them after they have been set.
Parameters
sclPinPropeller I/O pin connected to the EEPROM's SCL (serial clock) pin.
sdaPinPropeller I/O pin connected to the EEPROM's SDA (serial data) pin.
sclDriveUse 0 for standard design where the SCL pin has a pull-up resistor, or 1 if it does not have a pull-up and needs to be driven.

◆ ee_getByte()

char ee_getByte ( int  addr)

Get a byte value from a certain address in the Propeller Chip's dedicated EEPROM.

Parameters
addrThe EEPROM address that with the byte value that should be fetched.
Returns
value The byte value stored by the EEPROM at the address specified by the addr parameter.

◆ ee_getFloat32()

float ee_getFloat32 ( int  addr)

Fetch a 32-bit precision floating point value from a certain address in the Propeller Chip's dedicated EEPROM. A 32-bit value occupies four bytes so if you are fetching values in a sequence, make sure to add 4 to each addr parameter value.

Make sure that the Math box is checked in the Project Manager. In Simple View, click the Show Project Manager button in SimpleIDE's bottom-left corner. Then click the Linker tab, and check the Math Lib box.

Parameters
addrThe EEPROM address with the 32-bit floating point float value that should be fetched.
Returns
value The float value stored by the EEPROM at the specified address.

◆ ee_getInt()

int ee_getInt ( int  addr)

Get an int value from a certain address in the Propeller Chip's dedicated EEPROM. If you are fetching several int values, make sure to add 4 to the addr value with each successive call.

Parameters
addrThe EEPROM address with the int value that should be fetched.
Returns
value The int value stored by the EEPROM at the specified address.

◆ ee_getStr()

unsigned char* ee_getStr ( unsigned char *  s,
int  n,
int  addr 
)

Fetch a string of byte values starting at a certain address in Propeller Chip's dedicated EEPROM.

Parameters
sAddress of a char array to receive the string of bytes fetched from EEPROM.
nThe number of bytes to copy from EEPROM to the array.
addrThe EEPROM address of the first byte in the string.
Returns
The address of the array that stores the characters that were fetched.

◆ ee_putByte()

void ee_putByte ( unsigned char  value,
int  addr 
)

Store a byte value at a certain address in the Propeller Chip's dedicated EEPROM.

Parameters
valueThe byte value to store in EEPROM.
addrThe EEPROM address where the value is to be stored.

◆ ee_putFloat32()

void ee_putFloat32 ( float  value,
int  addr 
)

Store a 32-bit precision floating point value at a certain address in the Propeller Chip's dedicated EEPROM. A 32-bit value occupies four bytes so if you are storing values in a sequence, make sure to add 4 to each addr parameter value.

Make sure that the Math box is checked in the Project Manager. In Simple View, click the Show Project Manager button in SimpleIDE's bottom-left corner. Then click the Linker tab, and check the Math Lib box.

Parameters
valueThe 32-bit floating point float value to store in EEPROM.
addrThe EEPROM address where the value is to be stored.

◆ ee_putInt()

void ee_putInt ( int  value,
int  addr 
)

Store an int value at a certain address in the Propeller Chip's dedicated EEPROM. An int value occupies four bytes, so the next value should be stored at an address value that's four bytes higher.

Parameters
valueThe int value to store in EEPROM.
addrThe EEPROM address where the value is to be stored.

◆ ee_putStr()

void ee_putStr ( unsigned char *  s,
int  n,
int  addr 
)

Store a string of byte values starting at a certain address in the Propeller Chip's dedicated EEPROM.

Parameters
sAddress of a char array containing the string of bytes.
nThe number of bytes to copy from the array.
addrThe EEPROM address of the first byte in the string.

◆ endianSwap()

void endianSwap ( void *  resultAddr,
void *  varAddr,
int  byteCount 
)

Take bytes in one variable at varAddr, swap their order, and store them in another variable at resultAddr. This is useful for communication with peripherals that transmit/receive bytes in multi-byte values in reverse order from how the Propeller stores it in RAM.

Parameters
*resultAddrAddress of variable to store result. Make sure it's the same type as the varAddr parameter.
*varAddrAddress of source variable. Accepts any variable type.
*byteCountNumber of bytes in the variable.

◆ freqout()

void freqout ( int  pin,
int  msTime,
int  frequency 
)

Use same cog to send square wave of a certain frequency for a certain amount of time. For set and forget with another cog, try square_wave function instead.

Parameters
pinI/O pin that sends the frequency
msTimeTime in ms that the signal lasts
frequencyFrequency of the signal in Hz. Accepts values from 1 Hz to 128000000 Hz (128 MHz).

◆ get_direction()

unsigned int get_direction ( int  pin)

Check the direction of the I/O pin.

This function will tell you the direction of the I/O pin as seen by the cog executing it. Keep in mind that that your program might make other cogs use the I/O pin as an output, and a cog that treats a pin as an output wins over one that treats it as an input.

Parameters
pinI/O pin number
Returns
I/O pin direction as seen by the cog that runs the function.

◆ get_directions()

unsigned int get_directions ( int  endPin,
int  startPin 
)

Get directions for a contiguous group of I/O pins.

Get direction register states from a contiguous group of bits in the cog's output register.

Parameters
endPinThe highest numbered pin.
startPinThe lowest numbered pin.
Returns
States value containing a binary bit pattern. The value for startPin should be in bit-0, next in bit-1, etc.

◆ get_output()

unsigned int get_output ( int  pin)

Get I/O pin output state.

Keep in mind that this function reports the value in the output register for the cog running the function. That doesn't tell you if the I/O pin is set to input, or whether another cog is sending a different output state.

Parameters
pinI/O pin number.
Returns
In a register bit for I/O pin, either 1 or 0.

◆ get_outputs()

unsigned int get_outputs ( int  endPin,
int  startPin 
)

Get output settings for a contiguous group of I/O pins.

Get output register settings for a contiguous group of bits in the cog's output register.

Parameters
endPinThe highest numbered pin.
startPinThe lowest numbered pin.
Returns
Pattern value containing a binary bit pattern. The value for startPin should be in bit-0, next in bit-1, etc.

◆ get_state()

unsigned int get_state ( int  pin)

Check the state of an I/O pin without setting it to input.

Use this function instead of input if the Propeller needs to maintain an output. For example, you can use this to monitor another cog's or counter's output signal activity on a pin. (Note: if the pin is already set to input, it will return the state the external circuit is applying, just like input.)

Parameters
pinNumber of the I/O pin
Returns
The pin's state. If the pin is an output, 1 = 3.3 V and 0 = 0 V. If the pin is an input, 1 means V > 1.65 V, 0 means it's less.

◆ get_states()

unsigned int get_states ( int  endPin,
int  startPin 
)

Get states of a contiguous group of I/O pins.

This works the same as getState, but for a group of pins. It tells you the actual state of each pin, regardless of whether it's a voltage applied to input or transmitted by an output.

Parameters
endPinThe highest numbered pin.
startPinThe lowest numbered pin.
Returns
States value containing the binary bit pattern. The value for startPin should be in bit-0, next in bit-1, etc.

◆ high()

void high ( int  pin)

Set an I/O pin to output-high.

This function set makes the Propeller P8X32A connect the I/O pin to its positive 3.3 V supply voltage enabling it to source up to 40 mA of current (max 1 W dissipation per chip).

Parameters
pinNumber of the I/O pin to set high.

◆ i2c_busy()

HUBTEXT int i2c_busy ( i2c *  busID,
int  i2cAddr 
)

Check if I2C device is busy or responding.

Parameters
*busIDI2C bus identifier. i2c_newbus returns this pointer.
i2cAddr7 bit I2C device address.
Returns
1 if busy, 0 if ready.

◆ i2c_in()

HUBTEXT int i2c_in ( i2c *  busID,
int  i2cAddr,
int  memAddr,
int  memAddrCount,
unsigned char *  data,
int  dataCount 
)

Receive data from device using I2C protocol.

This function uses Simple Libraries/Protocol/libsimplei2c for clock and data line signaling. You can also use this library to create custom I2C functions. Other I2C signaling options are included in Propeller GCC. Search for i2C in the propgcc folder for more info.

Parameters
*busIDI2C bus identifier. i2c_newbus returns this pointer.
i2cAddr7 bit I2C device address.
memAddrValue for setting memory address pointer inside the I2C device.
memAddrCountNumber of bytes to use for memAddr. This value can be zero for no register or memory address data, in which case memAddr can be set to NULL.
*dataPointer to bytes set aside for receiving data from the I2C device.
dataCountNumber of bytes in data to send. Use a positive value to load data into result variable(s) least significant byte first, or a negative value for most significant byte first.
Returns
total number of bytes written. Should be 1 + memAddrCount + dataCount.

◆ i2c_newbus()

i2c* i2c_newbus ( int  sclPin,
int  sdaPin,
int  sclDrive 
)

Set up a simple serial driver with transmit & receive pins.

Parameters
sclPinthe I2C bus' serial clock pin.
sdaPinthe I2C bus' serial data pin.
sclDrivesets I/O pin connected to SCL line to send high signals by either (sclDrive = 0) allowing the pull-up resistor on the bus to pull the line high, or (sclDrive = 1) by setting the I/O pin to output and driving the line high. sclDrive = 0 is by far the most common arrangement. sclDrive = 1 is used with some Propeller boards that do not have a pull-up resistor on the EEPROM's SCL line.
Returns
busID - a pointer to the I2C bus info in memory. The busID value gets passed to i2c_out, i2c_in, and i2c_busy's busID parameter to select which I2C bus to use.

◆ i2c_out()

HUBTEXT int i2c_out ( i2c *  busID,
int  i2cAddr,
int  memAddr,
int  memAddrCount,
const unsigned char *  data,
int  dataCount 
)

Send data to device using I2C protocol.

This function uses Simple Libraries/Protocol/libsimplei2c for clock and data line signaling. You can also use this library to create custom I2C functions. Other I2C signaling options are included in Propeller GCC. Search for i2C in the propgcc folder for more info.

Parameters
*busIDI2C bus identifier. i2c_newbus returns this pointer.
i2cAddr7 bit I2C device address.
memAddrValue for setting memory address pointer inside the I2C device.
memAddrCountNumber of bytes to use for memAddr. This value can be zero for no register or memory address data, in which case memAddr can be set to NULL.
*dataPointer to bytes to send to the I2C device.
dataCountNumber of bytes in data to send. Use a positive value to transmit least significant byte first, or a negative value to transmit most significant byte first.
Returns
total number of bytes written. Should be 1 + memAddrCount + dataCount.

◆ input()

int input ( int  pin)

Set an I/O pin to input and return 1 if pin detects a high signal, or 0 if it detects low.

This function makes the Propeller connect the I/O pin to its input buffer so that it can return the binary value of the voltage applied by an external circuit.

Parameters
pinNumber of the I/O pin to set to input.
Returns
1 to indicate high (above 1.65 V) received or 0 to indicate low (below 1.65 V) received.

◆ low()

void low ( int  pin)

Set an I/O pin to output-low.

This function makes the Propeller P8X32A connect the I/O pin to its ground 0 V supply voltage enabling it to sink up to 40 mA of current (max 1 W dissipation per chip).

Parameters
pinNumber of the I/O pin to set low.

◆ mapFloat()

float mapFloat ( float  value,
float  fromMin,
float  fromMax,
float  toMin,
float  toMax 
)

Maps a floating point value from its position in one range to its corresponding. position in a different range. For example, 3.0 in a range of 0.0 to 10.0 would map to 30.0 in a range of 0.0 to 100.0. Note: In some cases, 32 bit floating point values will round slightly. For example 2.0/3.0 = 0.666667.

Parameters
valueThe value to map.
fromMinMinimum in value's range.
fromMaxMaxiumum in value's range.
toMinNew range's minimum.
toMaxNew ranges maximum.
Returns
A result with a position in the new range that's equivalent to value's position in its range.

◆ mapInt()

int mapInt ( int  value,
int  fromMin,
int  fromMax,
int  toMin,
int  toMax 
)

Maps an integer value from its position in one range to its corresponding. position in a different range. For example, 3 in a range of 0 to 10 would map to 30 in a range of 0 to 100.

Parameters
valueThe value to map.
fromMinMinimum in value's range.
fromMaxMaximum in value's range.
toMinNew range's minimum.
toMaxNew ranges maximum.
Returns
A result with a position in the new range that's equivalent to value's position in its range.

◆ mark()

void mark ( void  )

Mark the current time (deprecated).

The timeout function uses the marked time to determine if a timeout has occurred.

Note
This function has been deprecated because it doesn't support use in more than one cog. Use this code instead:
// CNT stores current number of system clock ticks elapsed.
int t = CNT; // Mark current time by storing in variable

◆ pause()

void pause ( int  time)

Delay cog from moving on to the next statement for a certain length of time.

The default time increment is 1 ms, so pause(100) would delay for 100 ms = 1/10th of a second. This time increment can be changed with a call to the set_pause_dt function.

Parameters
timeThe number of time increments to delay.

◆ pulse_in()

long pulse_in ( int  pin,
int  state 
)

Measure the duration of a pulse applied to an I/O pin.

Default time increments are specified in 1 microsecond units. Unit size can be changed with a call to set_io_dt function.

Parameters
pinI/O pin number
stateState of the pulse (1 for positive or high pulses, 0 for negative or low pulses.
Returns
Number of time units the pulse lasted.

◆ pulse_out()

void pulse_out ( int  pin,
int  time 
)

Transmit a pulse with an I/O pin.

Default time increments are specified in 1 microsecond units. Unit size can be changed with a call to set_io_dt function. The pulse will be positive if the I/O pin is transmitting a low signal before the call. The pulse will be negative if it transmits a high signal before the call. When the pulse is done, the pin returns to whatever state it was in before the pulse. If the pin was an input, it will be changed to output and use whatever value was in the output register bit for the pin. This defaults to low on start-up, but you can pre-set it while leaving the pin set to input with the set_output function (or check it with get_output).

Parameters
pinI/O pin number.
timeAmount of time the pulse lasts.

◆ pwm_set()

void pwm_set ( int  pin,
int  channel,
int  tHigh 
)

Set a PWM signal's high time.

After a single call to pwm_start, this function allows you to set a PWM signal's high time. For example, if your pwm_start call sets up 1000 us (1 ms) you could use this function to make the signal high for 3/4 of its cycle with pwm_set(pin, channel, 750). If the signal goes to a DC motor through an H bridge or other driver circuit, the motor will behave as though it's only getting 3/4 of the supply and turn at roughly 3/4 of full speed.

Parameters
pinI/O pin to send the PWM signal. You can change this value on the fly, which is useful for speed control of a DC motor in two different directions. When the PWM signal changes to a new pin, the cog sets the previous pin to input. If you want it to stay low when the PWM cog lets go, just set the pin low in your code before calling pwm_start.
channelYou have options of 0 or 1 for up to two simultaneous PWM signals. If you have an application in mind that requires more PWM signals, check the SimpleIDE/Learn/Simple Libraries/Motor directory, and also online at obex.parallax.com.
tHighThe high time for each PWM cycle repetition.

◆ pwm_start()

int pwm_start ( unsigned int  cycleMicroseconds)

Start pulse width modulation (PWM) process in another cog.

Great for DC motors, can also be used for servos, but the servo library is probably a better choice for that.

A PWM signal sends repeated high signals with a fixed cycle time. Your code will typically control the amount of time a PWM signal is high during each cycle. For example, pwm_start(1000) will establish a 1000 us PWM cycle. You can then use the pwm_set function to send high signals that last anywhere from 0 to 1000 us.

Parameters
cycleMicrosecondsNumber of microseconds the PWM cycle lasts.

◆ pwm_stop()

void pwm_stop ( void  )

Shut down PWM process and reclaim cog and I/O pins for other uses.

Shut down PWM process and reclaim cog and I/O pins for other uses

◆ random()

int random ( int  limitLow,
int  limitHigh 
)

Generates a pseudo-random integer value that falls in a range from limitLow to limitHigh. This function uses the system clock and I/O registers to create a new seed with each call, so it is very unlikely to generate the same sequence twice in a row.

Parameters
limitLowMinimum limit in the random number's range.
limitHighMaximum limit in the random number's range.
Returns
A pseudo-random number within the defined range.

◆ rc_time()

long rc_time ( int  pin,
int  state 
)

Set I/O pin to input and measure the time it takes a signal to transition from a start state to the opposite state.

Named rc_time because it is often used to measure a resistor-capacitor circuit's tendency to "decay" to either ground or 5 V (depending on wiring). Default time increments are specified in 1 microsecond units. Unit size can be changed with a call to set_io_dt function. The pulse will be positive if the I/O pin is transmitting a low signal before the call.

Parameters
pinI/O pin number.
stateStarting pin state.
Returns
Time from starting pin.

◆ reverse()

unsigned int reverse ( int  pin)

Reverse the direction of an I/O pin.

If an I/O pin's direction is set to input, this function changes it to output. If it's set to output, this function changes it to input.

Parameters
pinI/O pin number.
Returns
The new pin direction.

◆ sd_mount()

int sd_mount ( int  doPin,
int  clkPin,
int  diPin,
int  csPin 
)

Mount an SD card with the minimal 4-pin interface. For Parallax Learn Site examples, see: SD Card Data and Play WAV Files.

Parameters
doPinThe SD card's data out pin.
clkPinThe SD card's clock pin.
diPinThe SD card's data in pin.
csPinThe SD card's chip select pin.
Returns
status 0 if successful, or an error code.

◆ set_direction()

void set_direction ( int  pin,
int  direction 
)

Set an I/O pin to a given direction.

This function sets an I/O pin to either output or input.

Parameters
pinI/O pin number.
directionI/O pin direction.

◆ set_directions()

void set_directions ( int  endPin,
int  startPin,
unsigned int  pattern 
)

Set directions for a contiguous group of I/O pins.

Set directions values in a contiguous group of bits in the cog's output register.

Parameters
endPinThe highest numbered pin.
startPinThe lowest numbered pin.
patternValue containing the binary bit pattern. The value for startPin should be in bit-0, next in bit-1, etc.

◆ set_io_dt()

void set_io_dt ( long  clockticks)

Sets the time increment for the following timed I/O functions: count, pulse_in, pulse_out, rc_time.

Time increment is set in clock ticks. For example, the default of 1 us units is specified with set_io_dt(CLKFREQ/1000000). For 2 microsecond units, use set_io_dt(CLKFREQ/500000).

Parameters
clockticksNumber of clock ticks that represents one I/O time increment.

◆ set_io_timeout()

void set_io_timeout ( long  clockTicks)

Sets the timeout value for the following timed I/O functions: pulse_in, rc_time.

Time increment is set in clock ticks. For example, the default of 0.25 seconds is set with set_io_timeout(CLKFREQ/4). To set the timeout to 20 ms, you could use set_io_timeout(CLKFREQ/50).

Parameters
clockTicksNumber of clock ticks for timed I/O

◆ set_output()

void set_output ( int  pin,
int  state 
)

Set I/O pin output register bit to either 1 or 0.

This function focuses on the I/O pin's output register. If you intend to use it to send high or low signals, consider using high or low functions. This function can also be used in conjunction with set_direction to send high or low signals.

Parameters
pinI/O pin to set high or low.
state1 for high, 0 for low (when pin is actually set to output, which can be done with setDirection.

◆ set_outputs()

void set_outputs ( int  endPin,
int  startPin,
unsigned int  pattern 
)

Set output states for a contiguous group of I/O pins.

Set output states of a contiguous group of bits in the cog's output register.

Parameters
endPinThe highest numbered pin.
startPinThe lowest numbered pin.
patternValue containing the binary bit pattern. The value for startPin should be in bit-0, next in bit-1, etc.

◆ set_pause_dt()

void set_pause_dt ( int  clockticks)

Set time increment for pause function.

Default time increment for pause function is 1 ms. This function allows you to change that delay to custom values. For example, set_pause_dt(CLKFREQ/2000) would set it to 1/2 ms increments. To return to default 1 ms increments, use set_pause_dt(CLKFREQ/1000).

Parameters
clockticksthe number of clock ticks that pause(1) will delay.

◆ shift_in()

int shift_in ( int  pinDat,
int  pinClk,
int  mode,
int  bits 
)

Receive data from a synchronous serial device.

Parameters
pinDatData pin.
pinClkClock pin.
modeOrder and orientation to clock pulse options: MSBPRE, LSBPRE, MSBPOST,LSBPOST.
bitsNumber of binary values to transfer.
Returns
Value received from the synchronous serial device.

◆ shift_out()

void shift_out ( int  pinDat,
int  pinClk,
int  mode,
int  bits,
int  value 
)

Send data to a synchronous serial device.

Parameters
pinDatData pin
pinClkClock pin
modeOrder that bits are transmitted, either LSBFIRST or MSBFIRST.
bitsNumber of binary values to transfer.
valueto transmit.

◆ square_wave()

void square_wave ( int  pin,
int  channel,
int  freq 
)

Make I/O pin transmit a repeated high/low signal at a certain frequency. High and low times are the same. Frequency can range from 1 Hz to 128 MHz.

Uses one additional cog with up to two active channels, each with a selectable frequency. You can change transmit pins on the fly by calling this function on the same channel, but with a different pin. The previous pin will be set to input in that cog. If your code is set to output, it will not affect that setting, only the setting for the cog that is transmitting the square wave. Code in your cog, or some other cog can modulate the signal. A low signal allows the square wave to transmit, and a high signal prevents it.

Parameters
pinI/O pin that transmits square wave frequency. To stop sending the signal. and change the pin back to input, pass the pin as a negative number.
channel0 or 1 selects the counter module to transmit the frequency.
freqSquare wave frequency.

◆ square_wave_stop()

void square_wave_stop ( void  )

Stop the cog that's transmitting a square wave.

Stops any signals, lets go of any I/O pins, and reclaims the cog for other uses.

◆ term_cmd()

void term_cmd ( int  termConst,
  ... 
)

Send a command to SimpleIDE Terminal. Examples of commands include HOME, CLS, BKSP, CRSRXY, and others. All sixteen are listed in the SimpleIDE Terminal Constants section above. Click the term_cmd link to go to the details section and see parameter descriptions and code examples.

// Examples
term_cmd(HOME); // Cursor -> home (0, 0) position
term_cmd(CLS); // Clear screen and send cursor HOME
term_cmd(BKSP); // Backspace one character
// Position cursor 4 spaces from left and 2 rows down from top.
term_cmd(CRSRXY, 4, 2);
// Position cursor 4 spaces from left, but do not change row.
// Position cursor 2 rows from top, but do not change spaces
// from left.
Parameters
termConstOne of the sixteen terminal control constants listed in the SimpleIDE Terminal Constants part of the Macros section above.
...No additional parameters are required for CLS, HOME, and most of the others. Only CRSRXY requires two additional parameters, and CRSRX and CRSRY require a single additional parameter. If CRSRXY is used, arguments for x (spaces from left) and y (linefeeds from top) are required. If CRSRX is used, only the x value is required, and if CRSRY is used, only the y value is required.

◆ timeout()

int timeout ( int  time)

Compares the time against the time elapsed since mark (deprecated).

The default time increment is 1 us, so timeout(2000) will return 1 if 2 ms or more has elapsed since mark, or 0 if it has not.

Note
This function has been deprecated because it doesn't support use in more than one cog. Use this code instead:
// CLKFREQ stores number of system clock ticks in 1 second.
// CNT stores current number of system clock ticks elapsed.
int dt = CLKFREQ/2; // Pick a timeout, 1/2 a second in this case
int t = CNT; // Mark current time by storing in variable
while(CNT - t < dt) // Repeat until timeout
{
// Add code repeated until time elapsed is larger than dt here.
}
Parameters
timeNumber of time increments.

◆ toggle()

unsigned int toggle ( int  pin)

Toggle the output state of the I/O pin.

Change I/O pin's output state from low to high or high to low. This function assumes that some other function has already set the I/O pin to output.

Parameters
pinI/O pin number.
Returns
The new pin state.

◆ wait()

void wait ( int  time)

Waits a certain number of time increments from the last call to mark or wait functions (deprecated).

The default time increment is 1 us, so wait(2000) will return wait until 2 us after the last call to mark or wait. This function automatically updates the marked time; you can call it repeatedly without having to call mark.

Note
This function has been deprecated because it doesn't support use in more than one cog. Use this code instead:
// CLKFREQ stores number of system clock ticks in 1 second.
// CNT stores current number of system clock ticks elapsed.
int t = CNT; // Mark current time by storing in variable
int dt = CLKFREQ/10; // Pick time increment, 1/10 second in this case
while(1) // Repeat indefinitely
{
// Variable timed code here. Must last less than dt.
waitcnt(t += dt);
// Code that must start at precise intervals here.
}
Parameters
timeNumber of time increments.