Raspberry BASIC

Recent Posts

Pages: 1 ... 8 9 [10]
91
General Discussion / Re: LG Library
« Last post by John Spikowski 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.
92
General Discussion / Re: LG Library
« Last post by John Spikowski 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.
93
General Discussion / Re: LG Library
« Last post by John Spikowski 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.  
94
General Discussion / Re: LG Library
« Last post by John Spikowski 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$

95
General Discussion / Re: LG Library
« Last post by John Spikowski 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$

96
General Discussion / Re: LG Library
« Last post by John Spikowski 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

97
General Discussion / LG Library
« Last post by John Spikowski 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.  

98
General Discussion / Re: WiringPi
« Last post by John Spikowski on January 12, 2021, 07:07:19 AM »
I think I may have found a replacement for WiringPi. I'm not sure if it will compile on 64 bit OSs but sounds promising. I'll update this thread when I have time to give it a try.

lg library

99
General Discussion / Re: WiringPi
« Last post by John Spikowski on January 08, 2021, 12:34:29 AM »
After contacting Godon about my 64 bit WiringPi issue there doesn't seem to be a path to a solution. I have no interest developing for 32 bits under Linux any more so I'm going cease support for the ScriptBasic WiringPi extension module.

I hope to find a replacement in the future.

100
General Discussion / Re: WiringPi
« Last post by John Spikowski on January 05, 2021, 08:22:51 AM »
Unfortunately for me this update is for the 32 bit RPi OS.  :-\


ubuntu@rpi4b:~$ sudo dpkg -i wiringpi-latest.deb
dpkg: error processing archive wiringpi-latest.deb (--install):
 package architecture (armhf) does not match system (arm64)
Errors were encountered while processing:
 wiringpi-latest.deb
ubuntu@rpi4b:~$

Pages: 1 ... 8 9 [10]