Page 1 of 1

WSD scripts a modified pigpio library

Posted: Mon Dec 17, 2018 3:27 pm
by Pancho88
Hello everyone.

Refer to Other Programmimg Languages sub-forum, topic Yet Another Programming
Language Nov 4, 2018, for more information on WSD.

There are two programming efforts. One is to build the Dynamic Libraries.
Two is to script those libraries.

I modified the pigpio library into a "Lite" shared object version. Making it
available for WSD scripting.

Here is a conversion of a Python 7segment to a WSD 7segment script.

Code: Select all

c$=chr(10)

import xmath
if err then print errmsg & c$ : end

import xpigpio
if err then print errmsg & c$ : end

r$=xpigpio.init()
if err then print errmsg & c$ : end

' *** Set all used gpios as outputs. ***

' This defines which gpios are connected to which segments
'          a   b   c   d   e   f   g  dp
'SEG2GPIO=[ 4, 17, 18, 22, 23, 10, 24,  9]
dim i1(7)
i1(0)=4 : i1(1)=17 : i1(2)=18 : i1(3)=22 : i1(4)=23 : i1(5)=10 : i1(6)=24 : i1(7)=9

for i=0 to 7
   r$=xpigpio.do(0, i1(i), 1)  'CMD_MODES, PI_OUTPUT 1
next

'This defines the gpio used to switch on a LED
'          1   2   3   4   5
'LED2GPIO=[ 5,  6,  7,  8, 11]
dim i2(4)
i2(0)=5 : i2(1)=6 : i2(2)=7 : i2(3)=8 : i2(4)=11:

for i=0 to 4
   r$=xpigpio.do(0, i2(i), 1)  'CMD_MODES, PI_OUTPUT 1
next

'showing
dim i3(4)

dim S1(25)

S1(0)=" : 0b00000000:0"
S1(1)="0: 0b11111100:252"
S1(2)="1: 0b01100000:96"
S1(3)="2: 0b11011010:218"
S1(4)="3: 0b11110010:242"
S1(5)="4: 0b01100110:102"
S1(6)="5: 0b10110110:182"
S1(7)="6: 0b00111110:62"
S1(8)="7: 0b11100000:224"
S1(9)="8: 0b11111110:254"
S1(10)="9: 0b11100110:230"
S1(11)="A: 0b11101110:238"
S1(12)="b: 0b00111110:62"
S1(13)="C: 0b10011100:156"
S1(14)="c: 0b00011010:26"
S1(15)="d: 0b01111010:122"
S1(16)="E: 0b10011110:158"
S1(17)="F: 0b10001110:142"
S1(18)="H: 0b01101110:110"
S1(19)="h: 0b00101110:46"
S1(20)="L: 0b00011100:28"
S1(21)="l: 0b01100000:96"
S1(22)="O: 0b11111100:252"
S1(23)="o: 0b00111010:58"
S1(24)="P: 0b11001110:206"
S1(25)="S: 0b10110110:182"

char=1
wid=0
Refresh=100000
goto loop:

upd_display:
    zchar$=parse(bin$, ":", 1)
    s$=""
    for led=0 to 4
        segments = i3(led)
        on=0 : off=0
        for l=0 to 4
            ix=xmath.lshift(1, i2(l))
            if l=led then
                on=on OR ix
            else
                off=off OR ix
            endif
        next
        for b=0 to 7
            ix=xmath.lshift(1, i1(b))
            if segments and xmath.lshift(1, [7-b]) then
                on=on OR ix
            else
                off=off OR ix
            endif
        next
        s$=s$ & on & "," : s$=s$ & off & "," : s$=s$ & Refresh & ";"
    next

    print zchar$ & " " & s$ & c$ & c$

    r$=xpigpio.do(28, s$)   'CMD_WVAG gpioWaveAddGeneric
    nwid=xpigpio.do(49)     'CMD_WVCRE gpioWaveCreate
    r$=xpigpio.do(52, nwid) 'CMD_WVTXR gpioWaveTxSend

    if wid>0 then r$=xpigpio.do(50, wid)    'CMD_WVDEL delete no longer used waveform

    'print "wid" & wid & " new_wid" & new_wid

    wid = new_wid
    return

loop:
    if char>25 then goto exit:

    for i=0 to 4
        LED=i
        gosub display:
    next

    gosub upd_display:

    char=char + 1

    sleep 2000  '2 seconds

    goto loop:

exit:
    print "***" & c$
    r$=xpigpio.do(50, wid) 'CMD_WVDEL delete no longer used waveform
    end

display:
    bin$=S1(char)
    xchar$=parse(bin$, ":", 3)
    i3(LED)=val(xchar$)
    return

All comments are welcome.

Thanks

Re: WSD scripts a modified pigpio library

Posted: Thu Dec 27, 2018 6:31 pm
by Pancho88
Hello everyone

Three more commands have been added to a modified pigpio library.
xpigpio.do(CMD_SAF, gpio) 'CMD_SAF=117 gpioSetAlertFunc
xpigpio.do(CMD_ISR, gpio, ...) 'CMD_ISR=118 gpioSetISRFunc
xpigpio.do(CMD_STF, gpio, xid, xmillis) 'CMD_STF=119 gpioSetTimerFunc

Unable to test the first 2 functions, have a Rpi zerow with no devices.
Tested the Timer function with no problem.
All these functions are callbacks, and work very similar.

Thanks

Re: WSD scripts a modified pigpio library

Posted: Mon Jan 07, 2019 10:22 pm
by Pancho88
Hello everyone

Here is a conversion of a C program freq_count_1.c to a WSD script freqcnt1.sd

Code: Select all

c$=chr(10)
import xmath
if err then print errmsg & c$ : end

import xpigpio
if err then print errmsg & c$ : end

OPT_P_MIN=1
OPT_P_MAX=1000
OPT_P_DEF=20

OPT_R_MIN=1
OPT_R_MAX=300
OPT_R_DEF=10

OPT_S_MIN=1
OPT_S_MAX=10
OPT_S_DEF=5

g_opt_p = OPT_P_DEF
g_opt_r = OPT_R_DEF
g_opt_s = OPT_S_DEF
g_opt_t = 0


DIM i1(32)          'g_gpio[MAX_GPIOS]
DIM i2(32)          'g_reset_counts

DIM s1(32)          'g_gpio_data
    'l_gpio_data
dim i3(32)  'first tick
dim i4(32)  'last tick
dim i5(32)  'pulse_count

        'command line parameters
        'wsdvm0 freqcnt1 4 7 8 9 r10 s1 p2

    ngpios = 0 : g_mask=0
    for i=0 to 9
        n=val(arg(i))
        if err then
            i=9
        else
            if n=0 then
                par$=mid(arg(i), 1, 1)
                r=val(mid(arg(i), 2))
                opt=0
                if par$="p" then
                    if r >= OPT_P_MIN and  r <= OPT_P_MAX then
                        g_opt_p = r
                    else
                        print "invalid -p option (" & r & ")" & c$ : end
                    endif
                    g_opt_t = 1
                    opt=1
                endif

                if par$="r" then
                    if r >= OPT_R_MIN and r <= OPT_R_MAX then
                        g_opt_r = r
                    else
                        print "invalid -r option (" & r & ")" & c$ : end
                    endif
                    opt=1
                endif

                if par$="s" then
                    if r >= OPT_S_MIN and r <= OPT_S_MAX then
                        g_opt_s = r
                    else
                        print "invalid -s option (" & r & ")" & c$ : end
                    endif
                    opt=1
                endif
                if opt=0 then print par$ & " not a valid option" & c$ : end
            else
                if n>32 then print n & " not a valid g_gpio number" & c$ : end
                i1(ngpios) = n      'g_gpio
                ngpios=ngpios + 1
                ix=xmath.lshift(1, n)
                g_mask= g_mask or ix
            endif
        endif

    next
    if ngpios=0 then print "At least one gpio must be specified" & c$ : end

    print "Monitoring gpios"
    for i=0 to ngpios-1
        print " " & i1(i)       'g_gpio
    next
    print c$ & "Sample rate " & g_opt_s & " micros, refresh rate " & g_opt_r & " deciseconds" & c$

    'gpioCfgClock(g_opt_s, 1, 1)

    if g_opt_s = OPT_S_DEF then
        r$=xpigpio.init()
    else
        x$="s" & g_opt_s
        r$=xpigpio.init(x$)
    endif
    if err then print errmsg & c$ : end

    r$=xpigpio.do(27)                          'CMD_WVCLR=27 gpioWaveClear()

    s$=g_mask & "," & "0" & "," & g_opt_p & ";"
    s$=s$ & "0" & "," & g_mask & "," & g_opt_p & ";"

    r$=xpigpio.do(28, s$)                   'CMD_WVAG=28 gpioWaveAddGeneric
    wave_wid=xpigpio.do(49)                 'CMD_WVCRE=49 gpioWaveCreate

            'monitor g_gpio level changes

    for i=0 to ngpios-1
        r$=xpigpio.do(117, i1(i))       'CMD_SAF=117 gpioSetAlertFunc i1=g_gpio
    next

    mode = 0    'PI_INPUT

    if g_opt_t>0 then
        r$=xpigpio.do(52, nwid)     'CMD_WVTXR gpioWaveTxSend PI_WAVE_MODE_REPEAT
        mode = 1    'PI_OUTPUT
    endif

    for i=0 to ngpios-1
        r$=xpigpio.do(0, i1(i), mode)   'CMD_MODES gpioSetMode i1=g_gpio
    next

loop:
    if x>=5 then end                    'Set a Limit for Testing
        'gpioDelay(g_opt_r * 100000);
    r$=xpigpio.do(46, g_opt_r * 100000)    'CMD_MICS=46 myGpioDelay

    for i=0 to ngpios-1
        g = i1(i)          'i1=g_gpio
        s1(g)=i3(g) & "," i4(g) & "," i5(g)

            /*diff = g_gpio_data[g].last_tick - g_gpio_data[g].first_tick;*/
        diff=i4(g) - i3(g)

            'tally = g_gpio_data[g].pulse_count;
        tally=i5(g)

        if diff=0 then diff = 1

        i2(g) = 1
        r=1000000.0 * tally / diff
        print "g=" & g & " " & r & " " & tally & " " & diff & c$
    next
    x=x+1
    goto loop:

cback:
        'edges
    p0=val(arg(0))  'gpio
    p1=val(arg(1))  'level
    p2=val(arg(2))  'tick

    i4(p0)=p2       'last tick

    if p1=1 then i5(p0)=i5(p0)+1    'level=1 then pulse count up by 1

    if i2(p0)>0 then
        i3(p0)=p2   'first tick
        i5(p0)=0    'pulse count        
    endif
Bye