Contenu
INFORMATIONS GENERALE
FONCTIONS DISPONIBLES
- C Library Functions
- Mathematical Functions
- Extended C Library Functions
- SPS Functions
- File Functions
- HTTP Functions
- Stream-Funktionen
DESCRIPTION DE LA LIBRAIRE DE FONCTION C ETENDUE
- atoi
- batof
- strstrskip
- index
- lineno
- getxmlvalue
- getcpuinfo
- getheapusage
- getmaxheap
- getspsstatus
- localwebservice
DESCRIPTION DES FONCTIONS SPS
DESCRIPTION OF TIME FUNCTIONS
- sleep
- sleeps
- getcurrenttime
- getyear
- getmonth
- getday
- gethour
- getminute
- getsecond
- gettimeval
- convertutc2local
- convertlocal2utc
DESCRIPTION OF FILE FUNCTIONS
DESCRIPTION OF HTTP FUNCTIONS
DESCRIPTION OF STREAM FUNCTIONS
EXAMPLES
- Computing
- website download per tcp stream
- Connecting to an APC USV, if apcupsd is used on the PC
- Read from RS232 Extension via RS232 stream
Information générales
Cette documentation décrit comment un objet spécifique peut être programmé
Prérequis: Vous devez avoir LoxoneConfig 2.1 pour utiliser les scripts PicoC dans le logiciel de configuration. Veuillez consulter nos plus récents communiqués de bêta sur l’utilisation des scripts.
Origine
The programming language PicoC (http://code.google.com/p/picoc/) is used. PicoC is a very small C interpreter for scripting. It was originally written for scripting a UAV’s on-board flight system and it’s also very suitable for other robotic, embedded and non-embedded applications too.
Important
- Note that the complete C specification can not be met!
- The program code is interpreted. Therefore keep the code as low as possible. Avoid complex program structures.
- Each program runs in a separate task, therefore, asynchronous to the SPS.
- Outputs with printf will be written to the log window of Loxone Config.
- Note also that with an own programming, the system can be lead to crash. Program your instructions carefully. In case of emergency an SD card must be created, so the system gets operational again.
- If you want to run the program indefinitely, add in the program loops implicitly a sleep- function so that the program does not consume unnecessary CPU time.
- An integer has 32 bits, little endian.
- 128kB are available for heap and stack. Use memory thrifty; do not forget to release memory.
- All functions which return a pointer must reallocate the memory with free (), unless the pointer was passed as a parameter.
- Time values are indicated in seconds since 1.1.2009 0 UTC clock.
- Strings are encoded in UTF-8.
- Use mathematical function sparsely – the processor in the Miniserver has no mathematical hardware unit.
The scripts only work in Liveview.
Objets SPS
There are program objects with 1, 4, 8 and 16 inputs and outputs. You can insert a program-object by chosing it under « General » in the tab « Program ».
Each program also has an error output (TeQ), where eventual interpreter- error messages are released. This allows the detection of syntax errors.
The program code can be edited by double-clicking on the program- object.
Fonctions disponibles
In the following chapters all available functions are listed and all Miniserver specific functions are described. For all standard C functions plenty of information can be found in appropriate literature (e.g. www.cplusplus.com/reference/clibrary/).
Fonction de librairie C
char *getprogramname();
void printf(char *format, …);
char *sprintf(char *ptr, char *format, …);
void exit();
void *malloc(int size);
void *calloc(int num,int size);
void *realloc(void *ptr,int size);
void free(void *ptr);
int atoi(char *str);
float atof(char *str);
void strcpy(char *dst,char *src);
void strncpy(char *dst,char *src,int len);
int strcmp(char *str1,char *str2);
int strncmp(char *str1,char *str2,int len);
void strcat(char *dst,char *src);
char *strdup(char *str);
char* strstr(char *str,char *strfind);
int strfind(char *str,char *strfind,int pos);
int strlen(char *str);
void memset(void *ptr,int val,int len);
void memcpy(void *dst,void *src,int len);
int memcmp(void *ptr1,void *ptr2,int len);
void errorprintf(char *format,…);
Print to standard error device. Only used in shell command.
Fonctions mathématiques
float
sin
(
float
x);
Returns the sine of an angle of x radians.
float
cos
(
float
x);
Returns the cosine of an angle of x radians.
float
tan
(
float
x);
Returns the tangent of an angle of x radians.
float
asin
(
float
x);
Returns the principal value of the arc sine of x, expressed in radians. In trigonometrics, arc sine is the inverse operation of sine.
float
acos
(
float
x);
Returns the principal value of the arc cosine of x, expressed in radians. In trigonometrics, arc cosine is the inverse operation of cosine.
float
atan
(
float
x);
Returns the principal value of the arc tangent of x, expressed in radians. In trigonometrics, arc tangent is the inverse operation of tangent.
float
sinh
(
float
x);
Returns the hyperbolic sine of x.
float
cosh
(
float
x);
Returns the hyperbolic cosine of x.
float
tanh
(
float
x);
Returns the hyperbolic tangent of x.
float
exp
(
float
x);
Returns the base-e exponential function of x, which is the e number raised to the power x.
float
fabs
(
float
x);
Returns the absolute value of x
float
log
(
float
x);
Returns the natural logarithm of x.
float
log10
(
float
x);
Returns the common (base-10) logarithm of x.
float
pow
(
float
base,
float
exponent);
Returns base raised to the power exponent.
float
sqrt
(
float
x);
Returns the square root of x.
float
round(
float
x);
Returns rounded value of x.
float
ceil
(
float
x);
Returns the smallest integral value that is not less than x.
float
floor
(
float
x);
Returns the largest integral value that is not greater than x.
Fonctions de librairie C etendues
int
batoi(
char
*str);
float
batof(
char
*str);
char
* strstrskip(
char
*str,
char
*strfind);
char
*index(
char
*,
int
);
int
lineno();
Fonction SPS
void
setlogtext(
char
*str);
float
getio(
char
*str);
int
setio(
char
*str,
float
value);
int
getinputevent();
float
getinput(
int
input);
char
*getinputtext(
int
input);
void
setoutput(
int
output,
float
value);
void
setoutputtext(
int
output,
char
*str);
char
*getweatherserveraddress();
int
setweatherdata(
int
type,unsigned
int
time
,
float
value);
float
getweatherdata(
int
type,unsigned
int
time
);
void
saveweatherdata();
Fonctions fichier
FILE
*
fopen
(
char
*filename,
char
*mode);
int
fclose
(
FILE
*f);
int
fprintf
(
FILE
*f,
char
*format, ... );
int
fputc
(
int
character,
FILE
*f);
int
fputs
(
char
*str,
FILE
*f);
int
fflush
(
FILE
*f);
int
fwrite
(
void
*ptr,
int
size,
int
count,
FILE
*f);
int
fgetc
(
FILE
*f);
char
*
fgets
(
char
*str,
int
num,
FILE
*f);
int
fread
(
void
*,
int
size,
int
count,
FILE
*f);
int
fseek
(
FILE
*f,
long
offset,
int
origin);
int
remove
(
char
*filename);
int
rename
(
char
*oldname,
char
*newname);
FILE
*
tmpfile
();
char
*
tmpnam
(
char
* str);
Fonction HTTP
char
*httpget(
char
*address,
char
*page);
Fonction stream
STREAM *stream_create(
char
* filename,
int
read,
int
append);
void
stream_printf(STREAM* stream,
char
*format, ...);
int
stream_write(STREAM* stream,
void
* ptr,
int
size);
void
stream_flush(STREAM* stream);
int
stream_read(STREAM* stream,
void
* ptr,
int
size,
int
timeout);
int
stream_readline(STREAM* stream,
void
* ptr,
int
maxsize,
int
timeout);
void
stream_close(STREAM* stream);
Description des fonctions de librairie C étendues
atoi
int batoi(char *str);
Works like the atoi standard C function with the exception that blanks at the
beginning of parameter str are allowed.
Example:
atoi(“ 21”); // return value = NULL
batoi(“ 21”); // return value = 21
batof
float batof(char *str);
Works like the atof standard C function with the exception that blanks at the beginning of parameter str are allowed. Example:
atof(“ 2.1”); // return value = NULL
batof(“ 2.1”); // return value = 2.1
strstrskip
char* strstrskip(char *str,char *strfind);
Works like the strstr standard C function with the exception that the length of parameter strfind is added to the result pointer.
Example:
strstr(“das ist ein test”,”ein ”); // return value = “ein test”
strstrskip(“das ist ein test”,”ein ”); // return value = “test”
index
char *index(char *str,int charfind);
Returns a pointer to the first occurrence of character charfind in str, or a null pointer if charfind is not part of str.
Example:
index(“0123456789”,0x32); // return value = “23456789”
lineno
int lineno();]
Returns the current line number in script.
getxmlvalue
char *getxmlvalue(char *str,int index,char* name);
Returns the value of name in str, or a null pointer if name is not part of str.
Parameters:
str: XML-String to parse
index: the index of occurence of name (first element is 0)
name: XML-Attribute name to get
The memory where the returned pointer points to must be deallocated with function free().
Example:
getxmlvalue(“”,0,”name”); // return value = “test”
getcpuinfo
int getcpuinfo();
Returns actual value of CPU usage in %.
getheapusage
int getheapusage();
Returns actual value of system heap in kB.
getmaxheap
int getmaxheap();
Returns maximum value of system heap in kB.
getspsstatus
int getspsstatus();
Returns actual number of cycles processed by SPS.
localwebservice
char *localwebservice(char *str);
Execute webservice and return a pointer to the answer XML string.
Parameters:
str: webservice
The memory where the returned pointer points to must be deallocated with function free(). Example:
char* p = localwebservice(“dev/sps/status”);
free(p);
1
|
unsigned int gettimeval( int year, int month, int day, int hour, int minutes, int seconds, int local); |
Get UTC time in seconds since 2009. If parameters year, month, day, hour, minutes and seconds are UTC time, parameter local must be set to 0. If parameters year, month, day, hour, minutes and seconds are local time, parameter local must be set to 1.
DESCRIPTION OF FILE FUNCTIONS
1
|
int fseek ( FILE *f, long offset, int origin); |
There is a difference to the standard C fseek function: origin must be 0, 1 or 2.
If somebody wants to use the standard C syntax he has to make the following definitions in PicoC script:
#define SEEK_CUR 0 // seek from current position
#define SEEK_SET 1 // seek from beginning of file
#define SEEK_END 2 // seek from end of file
For all other standard C file functions plenty of information can be found in appropriate literature (e.g.www.cplusplus.com/reference/clibrary/).
DESCRIPTION OF HTTP FUNCTIONS
1
|
char *httpget( char *address, char *page); |
Downloads the specified page from the specified address. You can set the following parameters:
address: Server address e.g. “de.wikipedia.org”
page: Page on the specified server e.g. »/wiki/Intelligentes_Wohnen”
The memory where the returned pointer points to must be deallocated with function free().
DESCRIPTION OF STREAM FUNCTIONS
1
|
STREAM *stream_create( char * filename, int read, int append); |
Creates a file stream, a tcp stream, an udp stream a syslog stream or a RS232/RS485 stream (see example 2 and example 3)
filename:
Value | Description |
/path/filename | File-Stream |
/dev/tcp/server-address/port | TCP-Stream |
/dev/udp/server-address/port | UDP-Stream |
/dev/syslog | Syslog-Stream |
/dev/tty/name of RS232 or RS485 | RS232/RS485 Stream (only) |
Extension | stream_read is possible, no stream_write) |
read: If stream is a file stream: 1 = read file; 0 = write file
append: 1 = append text to file; 0 = overwrite text in file
1
|
int stream_write(STREAM* stream, void * ptr, int size); |
Writes an array of size elements, from the block of memory pointed by ptr to the to the output buffer of the specified stream (see example 2).
The postion indicator of the stream is advanced by the total number of bytes written.
The total number of elements successfully written is returned. If this number differs from the size parameter, it indicates an error.
1
|
void stream_flush(STREAM* stream); |
If the given stream is a file stream and the file was opened for writing any unwritten data in the output buffer is written to the file (see example 2).
If the given stream is a tcp stream, an udp stream or a syslog stream any data in the output buffer is sent to the server.
1
|
int stream_read(STREAM* stream, void * ptr, int size, int timeout); |
Reads an array of size elements from the input buffer of the specified stream and stores them in the block of memory specified by ptr (see example 2).
The timeout is given in milliseconds. The postion indicator of the stream is advanced by the total amount of bytes read. The total number of elements successfully read is returned.
EXAMPLES
The examples should show how script programms can be created.
EXAMPLE 1: COMPUTING
This example shows how an own computing is done:
If a change on any input occurs, the value of input 1 multiplied with the value on input 2 plus the value on input 3 is given to output 1.
The formula is outputted as text on text output 1.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
// write program here in PicoC char szBuffer[128]; float f1,f2,f3,f4; int nEvents; while (TRUE) { nEvents = getinputevent(); if (nEvents & 0xe) { f1 = getinput(0); f2 = getinput(1); f3 = getinput(2); f4 = f1 * f2 + f3; setoutput(0,f4); sprintf (szBuffer, "/ * %f + %f = %f" ,f1,f2,f3,f4); setoutputtext(0,szBuffer); printf (szBuffer); } sleep(100); } |
EXAMPLE 2: WEBSITE DOWNLOAD PER TCP STREAM
This example shows how to download a website with a tcp stream and how to save the website in a file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
#define BUFF_SIZE 40000 #define RD_BLOCK_SIZE 128 STREAM* pTcpStream = stream_create( "/dev/tcp/www.w3.org/80" ,0,0); // create tcp stream char * pTcpCmd = "GET / HTTP/1.1\r\nHost: www.w3.org\r\nUser-Agent: LoxLIVE [en]\r\nContent-Type: text/html; charset=utf-8\r\n\r\n" ; stream_write(pTcpStream,pTcpCmd, strlen (pTcpCmd)); // write to output buffer stream_flush(pTcpStream); // flush output buffer char szBuffer[BUFF_SIZE]; char szTmpBuffer[RD_BLOCK_SIZE]; int nCnt; int nBytesReceived = 0; int i = 0; // read stream do { nCnt = stream_read(pTcpStream,szTmpBuffer,RD_BLOCK_SIZE,4000); if (nCnt > 0) strncpy (( char *)szBuffer+i*RD_BLOCK_SIZE,szTmpBuffer,nCnt); nBytesReceived += nCnt; i++; } while (nCnt > 0); FILE * pw = fopen ( "/w3.html" , "w" ); // open file fprintf (pw, "%s" ,szBuffer); // write file printf ( "Bytes received: %d" ,nBytesReceived); fclose (pw); stream_close(pTcpStream); |
EXAMPLE 3: CONNECTING TO AN APC USV, IF APCUPSD IS USED ON THE PC
This example shows the actual load (AQ1), battery capacity (AQ2) as well as power failures (AQ3).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
char * p,*pS; char szBuffer[1500]; int nLen; int bOnline = 0; double dCharge,dLoad; printf ( "Start APC watch" ); while (TRUE) { STREAM* stream = stream_create( "/dev/tcp/192.168.1.9/3551" ,0,0); if (stream != NULL) { szBuffer[0] = 0; szBuffer[1] = 6; stream_write(stream,szBuffer,2); stream_flush(stream); stream_write(stream, "status" ,6); stream_flush(stream); nLen = stream_read(stream,szBuffer,2,1000); nLen = stream_read(stream,szBuffer, sizeof (szBuffer) - 1,1000); stream_close(stream); szBuffer[nLen] = 0; szBuffer[nLen + 1] = 0; //printf("APC Len=%d",nLen); pS = szBuffer; while (*pS) { p = strstr (pS, "STATUS :" ); if (p != NULL) { p = strstr (pS, "STATUS : ONLINE" ); if (p != NULL) bOnline = 1; else bOnline = 0; } else { p = strstr (pS, "LOADPCT :" ); if (p != NULL) { dLoad = batof(p + 11); setoutput(0,dLoad); } else { p = strstr (pS, "BCHARGE :" ); if (p != NULL) { dCharge = batof(p + 11); setoutput(1,dCharge); break ; } } } pS += ( strlen (pS) + 1); } if (bOnline) { setoutput(2,1); //printf("Power OK Charge: %f%% Load: %f%%",dCharge,dLoad); } else { setoutput(2,0); //printf("Power lost Charge: %f%% Load: %f%%",dCharge,dLoad); } } else printf ( "APC no connection" ); sleeps(10); // wait 10 seconds } |
EXAMPLE 4: READ FROM RS232 EXTENSION VIA RS232 STREAM
This example reads the bytes received from RS232 extension and puts it on text output 1. The total number of received bytes is put to the first analog output.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#define BUFF_SIZE 256 STREAM* pRs232Stream = stream_create( "/dev/tty/my_rs232_extension" ,0,0); // create rs232 stream char szBuffer[BUFF_SIZE]; int nCnt; int nBytesReceived = 0; while (1) { nCnt = stream_read(pRs232Stream,szBuffer,BUFF_SIZE,100); // read stream nBytesReceived += nCnt; setoutput(0,( float )nBytesReceived); setoutputtext(0,szBuffer); sleep(100); } |
PICO-C COPYRIGHT
picoc is published under the « New BSD License ».
http://www.opensource.org/licenses/bsd-license.php
Copyright (c) 2009, Zik Saleeba
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
- Neither the name of the Zik Saleeba nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS « AS IS » AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |