simpletools library  v1.1.8
Collection of convenience functions for common microcontroller tasks
simpletools.h
Go to the documentation of this file.
1 
110 #ifndef SIMPLETOOLS_H
111 #define SIMPLETOOLS_H
112 
113 #if defined(__cplusplus)
114 extern "C" {
115 #endif
116 
117 #include <propeller.h>
118 #include "simpletext.h"
119 #include <driver.h>
120 #include <stdio.h>
121 #include <stdlib.h>
122 #include <string.h>
123 #include <cog.h>
124 #include <ctype.h>
125 #include <unistd.h>
126 #include <sys/stat.h>
127 #include <dirent.h>
128 #include <sys/sd.h>
129 #include <math.h>
130 #include "simplei2c.h"
131 
132 
136 extern int ms;
137 
138 
142 extern int us;
143 
144 
145 
157 extern int st_msTicks;
158 
159 
164 extern int st_usTicks;
165 
166 
171 extern int st_iodt;
172 
177 extern int st_timeout;
178 
183 extern int st_pauseTicks;
184 
188 extern int st_mark;
189 
193 extern unsigned int st_buscnt;
194 
198 extern i2c *st_eeprom;
199 
203 extern int st_eeInitFlag;
204 
205 
212 #ifndef PI
213 
216 #define PI 3.141592653589793
217 #endif
218 
219 
220 
228 /* Values for use with SimpleIDE Terminal */
229 #ifndef HOME
230 
233 #define HOME (1)
234 #endif
235 
236 #ifndef CRSRXY
237 
243 #define CRSRXY (2)
244 #endif
245 
246 #ifndef CRSRLF
247 
251 #define CRSRLF (3)
252 #endif
253 
254 #ifndef CRSRRT
255 
259 #define CRSRRT (4)
260 #endif
261 
262 #ifndef CRSRUP
263 
267 #define CRSRUP (5)
268 #endif
269 
270 #ifndef CRSRDN
271 
275 #define CRSRDN (6)
276 #endif
277 
278 #ifndef BEEP
279 
283 #define BEEP (7)
284 #endif
285 
286 #ifndef BKSP
287 
292 #define BKSP (8)
293 #endif
294 
295 #ifndef TAB
296 
300 #define TAB (9)
301 #endif
302 
303 #ifndef NL
304 
308 #define NL (10)
309 #endif
310 
311 #ifndef LF
312 
315 #define LF (10)
316 #endif
317 
318 #ifndef CLREOL
319 
323 #define CLREOL (11)
324 #endif
325 
326 #ifndef CLRDN
327 
331 #define CLRDN (12)
332 #endif
333 
334 #ifndef CR
335 
339 #define CR (13)
340 #endif
341 
342 #ifndef CRSRX
343 
347 #define CRSRX (14)
348 #endif
349 
350 #ifndef CRSRY
351 
355 #define CRSRY (15)
356 #endif
357 
358 #ifndef CLS
359 
363 #define CLS (16)
364 #endif
365 
366 
367 
377 #ifndef MSBPRE
378 
381 #define MSBPRE 0
382 #endif
383 
384 #ifndef LSBPRE
385 
388 #define LSBPRE 1
389 #endif
390 
391 #ifndef MSBPOST
392 
395 #define MSBPOST 2
396 #endif
397 
398 #ifndef LSBPOST
399 
402 #define LSBPOST 3
403 #endif
404 
405 // Values for use with shift_out
406 #ifndef LSBFIRST
407 
410 #define LSBFIRST 0
411 #endif
412 
413 #ifndef MSBFIRST
414 
417 #define MSBFIRST 1
418 #endif
419 
420 
421 
431 #ifndef NCO_PWM_1
432 
436 #define NCO_PWM_1 (0b00100 << 26)
437 #endif
438 
439 #ifndef CTR_NCO
440 
444 #define CTR_NCO (0b100 << 26)
445 #endif
446 
447 #ifndef CTR_PLL
448 
452 #define CTR_PLL (0b10 << 26)
453 #endif
454 
455 #ifndef DUTY_SE
456 
460 #define DUTY_SE (0b110 << 26)
461 #endif
462 
463 
464 
477 #define ee_put_byte ee_putByte
478 
479 
483 #define ee_get_byte ee_getByte
484 
485 
489 #define ee_put_int ee_putInt
490 
491 
495 #define ee_get_int ee_getInt
496 
497 
501 #define ee_put_str ee_putStr
502 
503 
507 #define ee_get_str ee_getStr
508 
509 
513 #define ee_put_float32 ee_putFloat32
514 
515 
520 #define pause_ticks(pticks) __builtin_propeller_waitcnt(pticks+CNT, 0)
521 
522 
523 
533 #ifndef EEPROM_ADDR
534 
537 #define EEPROM_ADDR 0x50
538 #endif
539 
540 
541 
558 void high(int pin);
559 
560 
571 void low(int pin);
572 
573 
588 int input(int pin);
589 
590 
591 
611 unsigned int toggle(int pin);
612 
624 unsigned int reverse(int pin);
625 
626 
644 unsigned int get_state(int pin);
645 
660 unsigned int get_direction(int pin);
661 
674 unsigned int get_output(int pin);
675 
684 void set_direction(int pin, int direction);
685 
698 void set_output(int pin, int state);
699 
700 
701 
724 unsigned int get_states(int endPin, int startPin);
725 
738 unsigned int get_directions(int endPin, int startPin);
739 
752 unsigned int get_outputs(int endPin, int startPin);
753 
765 void set_directions(int endPin, int startPin, unsigned int pattern);
766 
778 void set_outputs(int endPin, int startPin, unsigned int pattern);
779 
780 
781 
782 
803 void pause(int time);
804 
815 void set_pause_dt(int clockticks);
816 
817 
818 
819 
839 long count(int pin, long duration);
840 
869 void dac_ctr(int pin, int channel, int dacVal);
870 
884 void dac_ctr_res(int bits);
885 
893 void dac_ctr_stop(void);
894 
905 void freqout(int pin, int msTime, int frequency);
906 
921 int pwm_start(unsigned int cycleMicroseconds);
922 
948 void pwm_set(int pin, int channel, int tHigh);
949 
957 void pwm_stop(void);
958 
971 long pulse_in(int pin, int state);
972 
988 void pulse_out(int pin, int time);
989 
1005 long rc_time(int pin, int state);
1006 
1025 void square_wave(int pin, int channel, int freq);
1026 
1034 void square_wave_stop(void);
1035 
1045 void set_io_timeout(long clockTicks);
1046 
1057 void set_io_dt(long clockticks);
1058 
1059 
1060 
1061 
1083 int shift_in(int pinDat, int pinClk, int mode, int bits);
1084 
1094 void shift_out(int pinDat, int pinClk, int mode, int bits, int value);
1095 
1096 
1097 
1098 
1127 i2c *i2c_newbus(int sclPin, int sdaPin, int sclDrive);
1128 
1129 
1157 HUBTEXT int i2c_out(i2c *busID, int i2cAddr,
1158  int memAddr, int memAddrCount,
1159  const unsigned char *data, int dataCount);
1160 
1161 
1189 HUBTEXT int i2c_in(i2c *busID, int i2cAddr,
1190  int memAddr, int memAddrCount,
1191  unsigned char *data, int dataCount);
1192 
1193 
1203 HUBTEXT int i2c_busy(i2c *busID, int i2cAddr);
1204 
1205 
1206 
1207 
1227 void ee_putByte(unsigned char value, int addr);
1228 
1238 char ee_getByte(int addr);
1239 
1240 
1250 void ee_putInt(int value, int addr);
1251 
1261 int ee_getInt(int addr);
1262 
1273 void ee_putStr(unsigned char *s, int n, int addr);
1274 
1289 unsigned char* ee_getStr(unsigned char* s, int n, int addr);
1290 
1305 void ee_putFloat32(float value, int addr);
1306 
1322 float ee_getFloat32(int addr);
1323 
1345 void ee_config(int sclPin, int sdaPin, int sclDrive);
1346 
1347 
1348 
1372 int sd_mount(int doPin, int clkPin, int diPin, int csPin);
1373 
1374 
1375 
1411 int *cog_run(void (*function)(void *par), int stacksize);
1412 
1413 
1421 int cog_num(int *coginfo);
1422 
1423 
1433 void cog_end(int *coginfo);
1434 
1435 
1436 
1479 void term_cmd(int termConst, ...);
1480 
1481 
1482 
1507 float constrainFloat(float value, float min, float max);
1508 
1509 
1525 int constrainInt(int value, int min, int max);
1526 
1527 
1541 void endianSwap(void *resultAddr, void *varAddr, int byteCount);
1542 
1543 
1563 float mapFloat(float value, float fromMin, float fromMax, float toMin, float toMax);
1564 
1565 
1584 int mapInt(int value, int fromMin, int fromMax, int toMin, int toMax);
1585 
1586 
1599 int random(int limitLow, int limitHigh);
1600 
1601 
1602 
1603 
1627 void mark(void);
1628 
1629 
1652 int timeout(int time);
1653 
1680 void wait(int time);
1681 
1682 
1683 
1690 #if defined(__cplusplus)
1691 }
1692 #endif
1693 /* __cplusplus */
1694 #endif
1695 /* SIMPLETOOLS_H */
1696 
void pause(int time)
Delay cog from moving on to the next statement for a certain length of time.
int st_eeInitFlag
Initialization flag used by ee_ functions.
void shift_out(int pinDat, int pinClk, int mode, int bits, int value)
Send data to a synchronous serial device.
int * cog_run(void(*function)(void *par), int stacksize)
Run a function&#39;s code in the next available cog (processor).
void wait(int time)
Waits a certain number of time increments from the last call to mark or wait functions (deprecated)...
unsigned int toggle(int pin)
Toggle the output state of the I/O pin.
int st_mark
Variable shared by mark and time_out functions.
unsigned int get_state(int pin)
Check the state of an I/O pin without setting it to input.
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.
void set_io_dt(long clockticks)
Sets the time increment for the following timed I/O functions: count, pulse_in, pulse_out, rc_time.
void dac_ctr(int pin, int channel, int dacVal)
Set D/A voltage.
void set_direction(int pin, int direction)
Set an I/O pin to a given direction.
HUBTEXT int i2c_in(i2c *busID, int i2cAddr, int memAddr, int memAddrCount, unsigned char *data, int dataCount)
Receive data from device using I2C protocol.
void mark(void)
Mark the current time (deprecated).
unsigned int get_output(int pin)
Get I/O pin output state.
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 s...
void set_directions(int endPin, int startPin, unsigned int pattern)
Set directions for a contiguous group of I/O pins.
int ee_getInt(int addr)
Get an int value from a certain address in the Propeller Chip&#39;s dedicated EEPROM. If you are fetching...
void square_wave_stop(void)
Stop the cog that&#39;s transmitting a square wave.
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.
int us
Propeller system clock ticks in 1 millisecond (us).
unsigned int get_states(int endPin, int startPin)
Get states of a contiguous group of I/O pins.
void pwm_set(int pin, int channel, int tHigh)
Set a PWM signal&#39;s high time.
i2c * st_eeprom
The busID for the Propeller Activity Board&#39;s EEPROM bus.
void ee_putByte(unsigned char value, int addr)
Store a byte value at a certain address in the Propeller Chip&#39;s dedicated EEPROM. ...
unsigned int get_directions(int endPin, int startPin)
Get directions for a contiguous group of I/O pins.
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.
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...
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&#39;s dedicated EEPROM...
void dac_ctr_stop(void)
Stop the cog that&#39;s transmitting the DAC signal(s).
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...
void pwm_stop(void)
Shut down PWM process and reclaim cog and I/O pins for other uses.
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 differ...
int st_pauseTicks
Clock ticks in a time increment used by pause function. Default value is the number of system clock t...
void set_pause_dt(int clockticks)
Set time increment for pause function.
HUBTEXT int i2c_busy(i2c *busID, int i2cAddr)
Check if I2C device is busy or responding.
void pulse_out(int pin, int time)
Transmit a pulse with an I/O pin.
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 o...
char ee_getByte(int addr)
Get a byte value from a certain address in the Propeller Chip&#39;s dedicated EEPROM. ...
unsigned int reverse(int pin)
Reverse the direction of an I/O pin.
int pwm_start(unsigned int cycleMicroseconds)
Start pulse width modulation (PWM) process in another cog.
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 ra...
void dac_ctr_res(int bits)
Set D/A voltage resolution.
float ee_getFloat32(int addr)
Fetch a 32-bit precision floating point value from a certain address in the Propeller Chip&#39;s dedicate...
unsigned int st_buscnt
Variable used by i2c_newbus.
int st_usTicks
Propeller system clock ticks in 1 microsecond. Changing this value is not recommended because it can ...
void cog_end(int *coginfo)
End function code running in another cog that was launched with cog_run.
long pulse_in(int pin, int state)
Measure the duration of a pulse applied to an I/O pin.
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.
int ms
Propeller system clock ticks in 1 millisecond (ms).
void set_io_timeout(long clockTicks)
Sets the timeout value for the following timed I/O functions: pulse_in, rc_time.
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 result...
unsigned int get_direction(int pin)
Check the direction of the I/O pin.
void ee_putFloat32(float value, int addr)
Store a 32-bit precision floating point value at a certain address in the Propeller Chip&#39;s dedicated ...
void ee_config(int sclPin, int sdaPin, int sclDrive)
Optional function for setting a custom EEPROM configuration. Other ee_ functions automatically check ...
int st_iodt
Clock ticks in a time increment used by pulse_in, pulse_out, and rc_time. Default value is the number...
void low(int pin)
Set an I/O pin to output-low.
unsigned int get_outputs(int endPin, int startPin)
Get output settings for a contiguous group of I/O pins.
void high(int pin)
Set an I/O pin to output-high.
void set_outputs(int endPin, int startPin, unsigned int pattern)
Set output states for a contiguous group of I/O pins.
int st_timeout
Clock ticks in a time increment used by pulse_in, pulse_out, and rc_time. Default value is the number...
int shift_in(int pinDat, int pinClk, int mode, int bits)
Receive data from a synchronous serial device.
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.
void set_output(int pin, int state)
Set I/O pin output register bit to either 1 or 0.
int timeout(int time)
Compares the time against the time elapsed since mark (deprecated).
int st_msTicks
Propeller system clock ticks in 1 millisecond. Changing this value is not recommended because it can ...
void ee_putInt(int value, int addr)
Store an int value at a certain address in the Propeller Chip&#39;s dedicated EEPROM. An int value occupi...
int cog_num(int *coginfo)
Get the cog ID.
i2c * i2c_newbus(int sclPin, int sdaPin, int sclDrive)
Set up a simple serial driver with transmit & receive pins.
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.
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...
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&#39;s dedicated EEPROM...