Raspberry BASIC

Author Topic: LG Library  (Read 6978 times)

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 234
    • View Profile
    • ScriptBasic
LG Library
« on: January 14, 2021, 05:44:13 AM »
I was able to compile the lg library under Ubuntu 20.04 LTS 64 bits on the RPi 4B 8GB with no errors or complaints with the make file included.

Since I don't have anything connected to my GPIO bus I wanted to see if the lgGpiochipOpen() fuction would return anything. A positive response is zero or greater. I got a -78
 as a response. I assume since there is no chips on the bus it's returning an error code. There is an example for the DHT11 which I have that is going to be my next test.

Code: C
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #include <lgpio.h>
  5.  
  6. #define LFLAGS 0
  7.  
  8. int main(int argc, char *argv[])
  9. {
  10.    int h;
  11.    h = lgGpiochipOpen(0);
  12.    printf("%i\n", h);
  13.    if (h >= 0)
  14.    {printf("Got Here\n");}
  15.    lgGpiochipClose(h);
  16. }
  17.  

DHT11 Example
Code: C
  1. /*
  2. dhtxx.c
  3. 2020-11-18
  4. Public Domain
  5.  
  6. http://abyz.me.uk/lg/lgpio.html
  7.  
  8. gcc -Wall -o dhtxx dhtxx.c -llgpio
  9.  
  10. ./dhtxx gpio ...
  11. */
  12.  
  13. #include <stdio.h>
  14. #include <stdint.h>
  15. #include <stdlib.h>
  16. #include <unistd.h>
  17. #include <inttypes.h>
  18.  
  19. #include <lgpio.h>
  20.  
  21. #define DHTAUTO 0
  22. #define DHT11   1
  23. #define DHTXX   2
  24.  
  25. #define DHT_GOOD         0
  26. #define DHT_BAD_CHECKSUM 1
  27. #define DHT_BAD_DATA     2
  28. #define DHT_TIMEOUT      3
  29.  
  30. /*
  31. gcc -o dhtxx dhtxx.c -llg
  32. */
  33.  
  34. #define MAX_GPIO 32
  35.  
  36. static int decode_dhtxx(uint64_t reading, int model, float *rh, float *temp)
  37. {
  38. /*
  39.       +-------+-------+
  40.       | DHT11 | DHTXX |
  41.       +-------+-------+
  42. Temp C| 0-50  |-40-125|
  43.       +-------+-------+
  44. RH%   | 20-80 | 0-100 |
  45.       +-------+-------+
  46.  
  47.          0      1      2      3      4
  48.       +------+------+------+------+------+
  49. DHT11 |check-| 0    | temp |  0   | RH%  |
  50.       |sum   |      |      |      |      |
  51.       +------+------+------+------+------+
  52. DHT21 |check-| temp | temp | RH%  | RH%  |
  53. DHT22 |sum   | LSB  | MSB  | LSB  | MSB  |
  54. DHT33 |      |      |      |      |      |
  55. DHT44 |      |      |      |      |      |
  56.       +------+------+------+------+------+
  57.  
  58. */
  59.    uint8_t byte[5];
  60.    uint8_t chksum;
  61.    float div;
  62.    float t, h;
  63.    int valid;
  64.    int status;
  65.  
  66.    byte[0] = (reading    ) & 255;
  67.    byte[1] = (reading>> 8) & 255;
  68.    byte[2] = (reading>>16) & 255;
  69.    byte[3] = (reading>>24) & 255;
  70.    byte[4] = (reading>>32) & 255;
  71.  
  72.    chksum = (byte[1] + byte[2] + byte[3] + byte[4]) & 0xFF;
  73.  
  74.    valid = 0;
  75.  
  76.    if (chksum == byte[0])
  77.    {
  78.       if (model == DHT11)
  79.       {
  80.          if ((byte[1] == 0) && (byte[3] == 0))
  81.          {
  82.             valid = 1;
  83.  
  84.             t = byte[2];
  85.  
  86.             if (t > 60.0) valid = 0;
  87.  
  88.             h = byte[4];
  89.  
  90.             if ((h < 10.0) || (h > 90.0)) valid = 0;
  91.          }
  92.       }
  93.       else if (model == DHTXX)
  94.       {
  95.          valid = 1;
  96.  
  97.          h = ((float)((byte[4]<<8) + byte[3]))/10.0;
  98.  
  99.          if (h > 110.0) valid = 0;
  100.  
  101.          if (byte[2] & 128) div = -10.0; else div = 10.0;
  102.  
  103.          t = ((float)(((byte[2]&127)<<8) + byte[1])) / div;
  104.  
  105.          if ((t < -50.0) || (t > 135.0)) valid = 0;
  106.       }
  107.       else /* AUTO */
  108.       {
  109.          valid = 1;
  110.  
  111.          /* Try DHTXX first. */
  112.  
  113.          h = ((float)((byte[4]<<8) + byte[3]))/10.0;
  114.  
  115.          if (h > 110.0) valid = 0;
  116.  
  117.          if (byte[2] & 128) div = -10.0; else div = 10.0;
  118.  
  119.          t = ((float)(((byte[2]&127)<<8) + byte[1])) / div;
  120.  
  121.          if ((t < -50.0) || (t > 135.0)) valid = 0;
  122.  
  123.          if (!valid)
  124.          {
  125.             /* If not DHTXX try DHT11. */
  126.  
  127.             if ((byte[1] == 0) && (byte[3] == 0))
  128.             {
  129.                valid = 1;
  130.  
  131.                t = byte[2];
  132.  
  133.                if (t > 60.0) valid = 0;
  134.  
  135.                h = byte[4];
  136.  
  137.                if ((h < 10.0) || (h > 90.0)) valid = 0;
  138.             }
  139.          }
  140.       }
  141.  
  142.       if (valid)
  143.       {
  144.          status = DHT_GOOD;
  145.  
  146.          *rh = h;
  147.          *temp = t;
  148.       }
  149.       else status = DHT_BAD_DATA;
  150.    }
  151.    else status = DHT_BAD_CHECKSUM;
  152.  
  153.    return status;
  154. }
  155.  
  156. static int status;
  157. static float h=0, t=0;
  158.  
  159. void afunc(int e, lgGpioAlert_p evt, void *data)
  160. {
  161.    int i;
  162.    uint64_t edge_len, now_tick;
  163.    static int bits = 0;
  164.    static uint64_t reading = 0;
  165.    static uint64_t last_tick = 0;
  166.  
  167.    for (i=0; i<e; i++)
  168.    {
  169.       if (evt[i].report.level != LG_TIMEOUT)
  170.       {
  171.          now_tick = evt[i].report.timestamp;
  172.          edge_len = now_tick - last_tick;
  173.          last_tick = now_tick;
  174.          if (edge_len > 1e6) // a millisecond
  175.          {
  176.             reading = 0;
  177.             bits = 0;
  178.          }
  179.          else
  180.          {
  181.             reading <<= 1;
  182.             if (edge_len > 1e5) reading |= 1; // longer than 100 micros
  183.             ++bits;
  184.          }
  185.       }
  186.       else
  187.       {
  188.          status = decode_dhtxx(reading, DHTAUTO, &t, &h);
  189.          reading = 0;
  190.          bits = 0;
  191.       }
  192.    }
  193. }
  194.  
  195. int main(int argc, char *argv[])
  196. {
  197.    int i, g;
  198.    int v;
  199.    int loop;
  200.    int fd;
  201.    int chip;
  202.    int num_gpio;
  203.    int gpio[MAX_GPIO];
  204.    int err;
  205.    int count;
  206.  
  207.    chip = lgGpiochipOpen(0);
  208.  
  209.    if (argc > 1)
  210.    {
  211.       num_gpio=argc-1;
  212.       if (num_gpio > MAX_GPIO) num_gpio = MAX_GPIO;
  213.       for (g=0; g<num_gpio; g++) gpio[g] = atoi(argv[g+1]);
  214.    }
  215.    else
  216.    {
  217.       num_gpio = 1;
  218.       gpio[0] = 4;
  219.    }
  220.  
  221.    if (chip >= 0)
  222.    {
  223.       lgGpioSetUser(chip, "niagra");
  224.       lgGpioSetSamplesFunc(afunc, (void*)123456);
  225.  
  226.       for (g=0; g<num_gpio; g++)
  227.       {
  228.          lgGpioSetWatchdog(chip, gpio[g], 1000); /* millisecond watchdog */
  229.       }
  230.  
  231.       for (loop=0; loop<200000; loop++)
  232.       {
  233.          for (g=0; g<num_gpio; g++)
  234.          {
  235.             err = lgGpioClaimOutput(chip, 0, gpio[g], 0);
  236.             if (err) fprintf(stderr, "set out err %d\n", err);
  237.  
  238.             usleep(15000);
  239.  
  240.             err = lgGpioClaimAlert(
  241.                chip, 0, LG_RISING_EDGE, gpio[g], -1);
  242.             if (err) fprintf(stderr, "set event err %d\n", err);
  243.  
  244.             count = 0;
  245.             status = DHT_TIMEOUT;
  246.  
  247.             while ((status == DHT_TIMEOUT) && (++count<11)) usleep(1000);
  248.  
  249.             printf("%d %.1f %.1f (g=%d)\n", status, t, h, gpio[g]);
  250.  
  251.             fflush(NULL);
  252.          }
  253.          sleep(3);
  254.       }
  255.  
  256.       lgGpiochipClose(chip);
  257.    }
  258.    else
  259.    {
  260.       fprintf(stderr, "open /dev/gpiochip0 failed (%d)\n", chip);
  261.    }
  262. }
  263.  

« Last Edit: January 14, 2021, 05:52:39 AM by John Spikowski »
ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 234
    • View Profile
    • ScriptBasic
Re: LG Library
« Reply #1 on: January 15, 2021, 11:16:41 PM »
Here is a list of exported functions from the lg GPIO library.


GPIO

lgGpiochipOpen               Opens a gpiochip device
lgGpiochipClose              Closes a gpiochip device

lgGpioGetChipInfo            Gets gpiochip information
lgGpioGetLineInfo            Gets gpiochip line information
lgGpioGetMode                Gets the mode of a GPIO

lgGpioSetUser                Notifies Linux of the GPIO user

lgGpioClaimInput             Claims a GPIO for input
lgGpioClaimOutput            Claims a GPIO for output
lgGpioClaimAlert             Claims a GPIO for alerts
lgGpioFree                   Frees a GPIO

lgGroupClaimInput            Claims a group of GPIO for inputs
lgGroupClaimOutput           Claims a group of GPIO for outputs
lgGroupFree                  Frees a group of GPIO

lgGpioRead                   Reads a GPIO
lgGpioWrite                  Writes a GPIO

lgGroupRead                  Reads a group of GPIO
lgGroupWrite                 Writes a group of GPIO

lgTxPulse                    Starts pulses on a GPIO
lgTxPwm                      Starts PWM pulses on a GPIO
lgTxServo                    Starts Servo pulses on a GPIO
lgTxWave                     Starts a wave on a group of GPIO
lgTxBusy                     See if tx is active on a GPIO or group
lgTxRoom                     See if more room for tx on a GPIO or group

lgGpioSetDebounce            Sets the debounce time for a GPIO
lgGpioSetWatchdog            Sets the watchdog time for a GPIO

lgGpioSetAlertsFunc          Starts a GPIO callback
lgGpioSetSamplesFunc         Starts a GPIO callback for all GPIO

I2C

lgI2cOpen                    Opens an I2C device
lgI2cClose                   Closes an I2C device

lgI2cWriteQuick              SMBus write quick

lgI2cReadByte                SMBus read byte
lgI2cWriteByte               SMBus write byte

lgI2cReadByteData            SMBus read byte data
lgI2cWriteByteData           SMBus write byte data

lgI2cReadWordData            SMBus read word data
lgI2cWriteWordData           SMBus write word data

lgI2cReadBlockData           SMBus read block data
lgI2cWriteBlockData          SMBus write block data

lgI2cReadI2CBlockData        SMBus read I2C block data
lgI2cWriteI2CBlockData       SMBus write I2C block data

lgI2cReadDevice              Reads the raw I2C device
lgI2cWriteDevice             Writes the raw I2C device

lgI2cProcessCall             SMBus process call
lgI2cBlockProcessCall        SMBus block process call

lgI2cSegments                Performs multiple I2C transactions
lgI2cZip                     Performs multiple I2C transactions

NOTIFICATIONS

lgNotifyOpen                 Request a notification
lgNotifyClose                Close a notification
lgNotifyPause                Pause notifications
lgNotifyResume               Start notifications

SERIAL

lgSerialOpen                 Opens a serial device
lgSerialClose                Closes a serial device

lgSerialReadByte             Reads a byte from a serial device
lgSerialWriteByte            Writes a byte to a serial device

lgSerialRead                 Reads bytes from a serial device
lgSerialWrite                Writes bytes to a serial device

lgSerialDataAvailable        Returns number of bytes ready to be read

SPI

lgSpiOpen                    Opens a SPI device
lgSpiClose                   Closes a SPI device

lgSpiRead                    Reads bytes from a SPI device
lgSpiWrite                   Writes bytes to a SPI device

lgSpiXfer                    Transfers bytes with a SPI device

THREADS

lgThreadStart                Start a new thread
lgThreadStop                 Stop a previously started thread

UTILITIES

lguVersion                   Gets the library version
lguSbcName                   Gets the host name of the SBC

lguGetInternal               Get an internal configuration value
lguSetInternal               Set an internal configuration value

lguSleep                     Sleeps for a given time

lguTimestamp                 Gets the current timestamp
lguTime                      Gets the current time

lguErrorText                 Gets a text description of an error code

lguSetWorkDir                Set the working directory
lguGetWorkDir                Get the working directory

ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 234
    • View Profile
    • ScriptBasic
Re: LG Library
« Reply #2 on: January 15, 2021, 11:32:51 PM »
I modified mytest.c to generate the text for the error code. I'm assuming since I don't have anything connected to the GPIO bus it may be working.  :)

Code: Script BASIC
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #include <lgpio.h>
  5.  
  6. #define LFLAGS 0
  7.  
  8. int main(int argc, char *argv[])
  9. {
  10.    int h;
  11.    h = lgGpiochipOpen(0);
  12.    printf("%s\n", lguErrorText(h));
  13.    if (h >= 0)
  14.    {printf("Got Here\n");}
  15.    lgGpiochipClose(h);
  16. }
  17.  


ubuntu@rpi4b:~/lg/EXAMPLES/lgpio$ gcc mytest.c -o mytest -llgpio
ubuntu@rpi4b:~/lg/EXAMPLES/lgpio$ ./mytest
can not open gpiochip
ubuntu@rpi4b:~/lg/EXAMPLES/lgpio$

ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 234
    • View Profile
    • ScriptBasic
Re: LG Library
« Reply #3 on: January 16, 2021, 03:56:45 AM »
I hooked up my DHT11 to the GPIO bus and ran the example provided. It failed with the chip not found like in mytest.c. My guess is like the WiringPi library hasn't been updated to handle the RPi 4B. Please chime in if you have any ideas. I'm going to try this library on my RPi 3B with 32 Bit Raspian OS just to see if the library works there,


ubuntu@rpi4b:~/lg/EXAMPLES/lgpio$ ./dhtxx
open /dev/gpiochip0 failed (-78)
ubuntu@rpi4b:~/lg/EXAMPLES/lgpio$

« Last Edit: January 16, 2021, 04:44:41 AM by John Spikowski »
ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 234
    • View Profile
    • ScriptBasic
Re: LG Library
« Reply #4 on: January 16, 2021, 05:07:09 AM »
I took a peek at the lgpio.c source and the reason it can't find a chip is /dev/gpiochip0 is a null file. The open() function must be returning the error. Seems I've hit a dead end on Ubuntu 64 if this device doesn't exist.

Code: C
  1. sprintf(chipName, "/dev/gpiochip%d", gpioDev);
  2.  
  3. fd = open(chipName, O_RDWR | O_CLOEXEC);
  4.  
  5. if (fd < 0)
  6.    PARAM_ERROR(LG_CANNOT_OPEN_CHIP, "can't open gpiochip (%s)", chipName);
  7.  
ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 234
    • View Profile
    • ScriptBasic
Re: LG Library
« Reply #5 on: January 16, 2021, 05:18:34 AM »
Success!

The problem was /dev/gpiochip0 needed root permissions.


ubuntu@rpi4b:~/lg/EXAMPLES/lgpio$ sudo ./dhtxx
3 0.0 0.0 (g=4)
2 0.0 0.0 (g=4)
1 0.0 0.0 (g=4)
2 0.0 0.0 (g=4)
0 47.0 23.0 (g=4)
1 47.0 23.0 (g=4)
1 47.0 23.0 (g=4)
0 46.0 23.0 (g=4)
2 46.0 23.0 (g=4)
1 46.0 23.0 (g=4)
3 46.0 23.0 (g=4)
0 46.0 23.0 (g=4)



Metric
Humidity: 46.00 rh
Temperature: 23.00 C
Pressure: 1013.24 hPa

English
Humidity: 46.00 rh
Temperature: 73.40 F
Pressure: 29.92 In. Hg


Next I need to wrap this library in a ScriptBasic extension module.
« Last Edit: January 16, 2021, 07:09:55 AM by John Spikowski »
ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 234
    • View Profile
    • ScriptBasic
Re: LG Library
« Reply #6 on: January 16, 2021, 08:41:21 PM »
I'm setting up my RPi 4B 4GB with Ubuntu 20.10 64 bit as my RPi development target. With my RPi 4B 8 GB fully loaded with desktop and running GitLab-ce, trying to use the GPIO bus for pulse sampling is asking too much.

I also plan to split this thread into its own development LG thread.
« Last Edit: January 16, 2021, 09:48:03 PM by John Spikowski »
ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 234
    • View Profile
    • ScriptBasic
Re: LG Library
« Reply #7 on: January 17, 2021, 02:42:10 AM »
I installed Ubuntu 20.10 64 bit using the Raspberry Pi Imaging Tool.

Attached is a screenshot.
« Last Edit: January 17, 2021, 02:49:42 AM by John Spikowski »
ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 234
    • View Profile
    • ScriptBasic
Re: LG Library
« Reply #8 on: January 17, 2021, 02:57:26 AM »
Here is the chipline.c program.

Code: C
  1. /*
  2. chipline.c
  3. 2020-11-18
  4. Public Domain
  5.  
  6. http://abyz.me.uk/lg/lgpio.html
  7.  
  8. gcc -Wall -o chipline chipline.c -llgpio
  9.  
  10. ./chipline
  11. */
  12. #include <stdio.h>
  13.  
  14. #include <lgpio.h>
  15.  
  16. int main(int argc, char *argv[])
  17. {
  18.    int h;
  19.    int i;
  20.    lgChipInfo_t cinf;
  21.    lgLineInfo_t linf;
  22.  
  23.    h = lgGpiochipOpen(0);
  24.    if (h >= 0)
  25.    {
  26.       if (lgGpioGetChipInfo(h, &cinf) == LG_OKAY)
  27.       {
  28.          printf("%d \"%s\" \"%s\"\n", cinf.lines, cinf.name, cinf.label);
  29.  
  30.          for (i=0; i<cinf.lines; i++)
  31.          {
  32.             if (lgGpioGetLineInfo(h, i, &linf) == LG_OKAY)
  33.             {
  34.                printf("%d %d \"%s\" \"%s\"\n",
  35.                   linf.offset, linf.lFlags, linf.name, linf.user);
  36.             }
  37.          }
  38.       }
  39.  
  40.       lgGpiochipClose(h);
  41.    }
  42. }
  43.  


jrs@RPi4B:~/lg/EXAMPLES/lgpio$ sudo ./chipline
58 "gpiochip0" "pinctrl-bcm2711"
0 0 "ID_SDA" ""
1 0 "ID_SCL" ""
2 0 "SDA1" ""
3 0 "SCL1" ""
4 0 "GPIO_GCLK" ""
5 0 "GPIO5" ""
6 0 "GPIO6" ""
7 7 "SPI_CE1_N" "spi0 CS1"
8 7 "SPI_CE0_N" "spi0 CS0"
9 0 "SPI_MISO" ""
10 0 "SPI_MOSI" ""
11 0 "SPI_SCLK" ""
12 0 "GPIO12" ""
13 0 "GPIO13" ""
14 0 "TXD1" ""
15 0 "RXD1" ""
16 0 "GPIO16" ""
17 0 "GPIO17" ""
18 0 "GPIO18" ""
19 0 "GPIO19" ""
20 0 "GPIO20" ""
21 0 "GPIO21" ""
22 0 "GPIO22" ""
23 0 "GPIO23" ""
24 0 "GPIO24" ""
25 0 "GPIO25" ""
26 0 "GPIO26" ""
27 0 "GPIO27" ""
28 0 "RGMII_MDIO" ""
29 0 "RGMIO_MDC" ""
30 0 "CTS0" ""
31 0 "RTS0" ""
32 0 "TXD0" ""
33 0 "RXD0" ""
34 0 "SD1_CLK" ""
35 0 "SD1_CMD" ""
36 0 "SD1_DATA0" ""
37 0 "SD1_DATA1" ""
38 0 "SD1_DATA2" ""
39 0 "SD1_DATA3" ""
40 0 "PWM0_MISO" ""
41 0 "PWM1_MOSI" ""
42 3 "STATUS_LED_G_CLK" "led0"
43 0 "SPIFLASH_CE_N" ""
44 0 "SDA0" ""
45 0 "SCL0" ""
46 0 "RGMII_RXCLK" ""
47 0 "RGMII_RXCTL" ""
48 0 "RGMII_RXD0" ""
49 0 "RGMII_RXD1" ""
50 0 "RGMII_RXD2" ""
51 0 "RGMII_RXD3" ""
52 0 "RGMII_TXCLK" ""
53 0 "RGMII_TXCTL" ""
54 0 "RGMII_TXD0" ""
55 0 "RGMII_TXD1" ""
56 0 "RGMII_TXD2" ""
57 0 "RGMII_TXD3" ""
jrs@RPi4B:~/lg/EXAMPLES/lgpio$
ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 234
    • View Profile
    • ScriptBasic
Re: LG Library
« Reply #9 on: January 17, 2021, 03:00:49 AM »
A toggle benchmark.

Code: C
  1. /*
  2. bench.c
  3. 2020-11-18
  4. Public Domain
  5.  
  6. http://abyz.me.uk/lg/lgpio.html
  7.  
  8. gcc -Wall -o bench bench.c -llgpio
  9.  
  10. ./bench
  11. */
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15.  
  16. #include <lgpio.h>
  17.  
  18. #define LFLAGS 0
  19.  
  20. #define OUT 21
  21. #define LOOPS 20000
  22.  
  23. int main(int argc, char *argv[])
  24. {
  25.    int h;
  26.    int i;
  27.    double t0, t1;
  28.  
  29.    h = lgGpiochipOpen(0);
  30.  
  31.    if (h >= 0)
  32.    {
  33.       if (lgGpioClaimOutput(h, LFLAGS, OUT, 0) == LG_OKAY)
  34.       {
  35.  
  36.          t0 = lguTime();
  37.  
  38.          for (i=0; i<LOOPS; i++)
  39.          {
  40.             lgGpioWrite(h, OUT, 0);
  41.             lgGpioWrite(h, OUT, 1);
  42.          }
  43.  
  44.          t1 = lguTime();
  45.  
  46.          printf("%.0f toggles per second\n", (1.0 * LOOPS)/(t1-t0));
  47.       }
  48.  
  49.       lgGpiochipClose(h);
  50.    }
  51. }
  52.  


jrs@RPi4B:~/lg/EXAMPLES/lgpio$ sudo ./bench
139130 toggles per second
jrs@RPi4B:~/lg/EXAMPLES/lgpio$

ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 234
    • View Profile
    • ScriptBasic
Re: LG Library
« Reply #10 on: January 17, 2021, 03:11:29 AM »
The tx_pulse.c example.

Code: C
  1. /*
  2. tx_pulse.c
  3. 2020-11-18
  4. Public Domain
  5.  
  6. http://abyz.me.uk/lg/lgpio.html
  7.  
  8. gcc -Wall -o tx_pulse tx_pulse.c -llgpio
  9.  
  10. ./tx_pulse
  11. */
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15.  
  16. #include <lgpio.h>
  17.  
  18. #define OUT 21
  19. #define LOOPS 120
  20.  
  21. #define LFLAGS 0
  22.  
  23. int main(int argc, char *argv[])
  24. {
  25.    int h;
  26.    int i;
  27.    double start, end;
  28.  
  29.    h = lgGpiochipOpen(0);
  30.  
  31.    if (h >= 0)
  32.    {
  33.       if (lgGpioClaimOutput(h, LFLAGS, OUT, 0) == LG_OKAY)
  34.       {
  35.          lgTxPulse(h, OUT, 20000, 30000, 0, 0);
  36.  
  37.          lguSleep(2);
  38.  
  39.          lgTxPulse(h, OUT, 20000, 5000, 0, LOOPS);
  40.  
  41.          start = lguTime();
  42.  
  43.          while (lgTxBusy(h, OUT, LG_TX_PWM)) lguSleep(0.01);
  44.  
  45.          end = lguTime();
  46.  
  47.          printf("%d cycles at 40 Hz took %.1f seconds\n", LOOPS, end-start);
  48.       }
  49.  
  50.       lgGpiochipClose(h);
  51.    }
  52. }
  53.  


jrs@RPi4B:~/lg/EXAMPLES/lgpio$ sudo ./tx_pulse
120 cycles at 40 Hz took 3.0 seconds
jrs@RPi4B:~/lg/EXAMPLES/lgpio$

ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 234
    • View Profile
    • ScriptBasic
Re: LG Library
« Reply #11 on: January 17, 2021, 03:55:42 AM »
I created a repository for the lg library on basic-sandbox.us. The only source I know of is from the author's site as a ZIP.

https://basic-sandbox.us/lg/lg-dev

I created a subdomain of RaspberryBASIC.org to host the lg library. The HTML for the site was created by a Python script that comes in the zip distribution.

https://lg.raspberrybasic.org

I feel much better that this public domain open source project won't disappear after spending time building a ScriptBasic extension module around it.
 





« Last Edit: January 17, 2021, 06:35:22 AM by John Spikowski »
ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 234
    • View Profile
    • ScriptBasic
Re: LG Library
« Reply #12 on: January 18, 2021, 01:47:11 AM »
I can't seem to get the i2c 1-wire interface going on Ubuntu 64. I was able to get /sys/bus/w1/devices to show up after the modprobe commands but the devices folder is empty. Any advice how to get my DS18B20 to show up?

ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 234
    • View Profile
    • ScriptBasic
Re: LG Library
« Reply #13 on: January 18, 2021, 03:56:20 AM »
I was able to get access to the DS18B20 using the OWFS interface. Problem is the returned temperature is all over the place. Not sure if this is progress or not.  :(

Reading a 1-wire DS18B20 temperature sensor using OWFS
« Last Edit: January 18, 2021, 04:52:09 AM by John Spikowski »
ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 234
    • View Profile
    • ScriptBasic
Re: LG Library
« Reply #14 on: January 18, 2021, 06:16:42 PM »
I tried the DS18B20 on my RPi 3B under Raspbian and was able to get a valid result, It looks like the OWFS library doesn't work.


pi@RPi3B:~ $ cat /sys/bus/w1/devices/28-041780c226ff/w1_slave
3f 01 4b 46 7f ff 0c 10 f3 : crc=f3 YES
3f 01 4b 46 7f ff 0c 10 f3 t=19937
pi@RPi3B:~ $



From what my research has dug up so far is to get one-wire to work from the kernel a re-link needs to be done. One would think Ubuntu's RPi branch would have already addressed this issue of made it easy like Raspian to enable.

« Last Edit: January 19, 2021, 05:20:58 AM by John Spikowski »
ScriptBasic Project Manager/Facilitator