Shop OBEX P1 Docs P2 Docs Learn Events
flexGUI and BME680 — Parallax Forums

flexGUI and BME680

I am going to use the flexBASIC part of flexGUI, and I am considering trying to get the BME680 C code to work with, that is available form SimpleIDE.
Now the part that is a little confusing is how do I get the code that is in BME680.h to work with flexBASIC.

The BME680.h file has some 'typedef struct' that contain things like 'i2c *i2c_ptr', and spi. Now I am not sure how or where the i2c comes from or does flexBASIC have is own version of i2c/spi. I guess I need some guidance as to what the best approach for doin this, would be.

I will try to get this thing to run on the P1, and than hopefully transfer the code to the P2, to see hoe the P2 handles it.

Ray

Comments

  • "i2c" is presumably a struct defined elsewhere in the simple library header files. The "simple" libraries are designed to be easy to use, but they are pretty large. Porting them over to FlexC looks do-able, but it'll be some work. I think @"Roy Eltham" had looked into that at one point, but I'm not sure how far he got.

    Is there Spin code for driving the BME680? That would be the easiest to get working, I think, since fastspin's Spin support is pretty mature at this point. Also, both Spin and BASIC are case insensitive, which avoids some interoperability problems (it's very hard to call C functions with uppercase letters in them from BASIC).

    Using Spin or C code from BASIC is generally pretty easy. The pseudo-code would look like:
       ' this is *not* a working example, just some
       ' pseudo code showing the general idea
       dim i as class using "i2c.spin"
       dim b as class using "bme680.spin"
       
       i.init(whatever parameters you need to initialize i2c)
       b.init_i2c(@i, parameters for initializing bme680)
       b.begin()
    
  • Is there Spin code for driving the BME680?
    I did not see any Spin BME680 code. The BME680 C code was developed for BlocklyProp, not sure how the code for the "simple" libraries, was developed.

    I guess that leaves open either going with flexBASIC or flexC. If I am not mistaken, I think I read that flexC is not "fully" developed, not sure what the limitations are. I would hate to start in with flexC and find out that there are some important parts missing.

    Ray
  • If the BME C library doesn't use any GCC extensions then it'll probably be fine to use with FlexC. At first glance it looks like it should compile, although you'll have to port over the i2c code from the simple libraries as well.
  • So, I thought I would give flexC a try, this is like learning how to program in C again.

    Simple little program:
    // test3.c
    #include <stdio.h>
    #include <time.h>
    
    usleep(200);
    
    void main()
    {
    	printf("Where am I!\n");
    }
    
    I get: error: syntax error, unexpected constant, for the 'usleep(200);'. Not sure what else is necessary for this to work.

    Ray
  • Rsadeika wrote: »
    So, I thought I would give flexC a try, this is like learning how to program in C again.

    Simple little program:
    // test3.c
    #include <stdio.h>
    #include <time.h>
    
    usleep(200);
    
    void main()
    {
    	printf("Where am I!\n");
    }
    
    I get: error: syntax error, unexpected constant, for the 'usleep(200);'. Not sure what else is necessary for this to work.

    That's not a legal C program. "usleep(200);" is a function call, which needs to be inside a function declaration. I'm guessing you wanted to put it inside of main().

    I'm not sure why you said this is like learning to program in C again. PropGCC would have produced an error for the above program as well.

  • Ah, I see another potential problem -- usleep isn't defined in the FlexC libraries yet. You'll have to do something like:
    #include <propeller.h>
    
    void main()
    {
        waitcnt(getcnt() + CLKFREQ/5);
        // anything else you want to do
    }
    
  • I was thinking that 'time.h' was a library that contains the function 'usleep()'. Something like 'stdio.h' contains 'printf()'. There is C code for the usleep() in the 'time' folder.

    As for the learning C again, yes, I got used to cut and paste, plus having 'simpletools.h' that, I guess contains everything you need to use. I forgot that in order to use printf() you need to '#include <stdio.h>', that kind of stuff. You also presented waitcnt(), you think I remember how to use that. LOL

    Ray
  • I did look at getting simple libraries working with FlexC (back before you fixed a great many things) and some of it will compile without issue, some of it needs to be changed since it uses P1 specific objects, and all of it is broken into many many (MANY MANY) little files (many of which depend on the base set of stuff).

    It really needs linking support in order to not be a giant pain in the &*#$^. I kind of gave up on it, waiting for a later time when more stuff was done. I also kind of stopped having free time as much.
  • The small program now works as expected. I just cut/paste int usleep(unsigned int n) function that is in the 'time' folder. For whatever reason, when you do a '#include <time.h>', the 'int usleep(unsigned int n)', the program comes up with an error concerning that function.

    Is the 'time' folder a library, just like 'stdio', if not, how do you make a library for use with flexC.

    Ray
    #include <stdio.h>
    #include <propeller.h>
    
    
    int usleep(unsigned int n)
    {
      unsigned waitcycles;
      unsigned usecond = _clkfreq/1000000;
    
      waitcycles = getcnt() + n*usecond;
      waitcnt(waitcycles);
    
      return 0;
    }
    
    
    void main()
    {
    	usleep(900);  // Need this so you do not see preceding garbage chars.
    	
    	printf("Where am I!\n");
    
    }
    
  • The header file that declares usleep is <unistd.h>, not <time.h>. This is confusing, but usleep() is not a standard C function, it's part of the Unix standard instead (the header file for which is unistd.h).

    Header files like <time.h> and <stdio.h> are not libraries in traditional C. They just contain function declarations, the actual functions go elsewhere. That is, saying:
    #include <stdio.h>
    
    Is exactly the same as just copying and pasting the contents of stdio.h at that point in the file. You'll notice that stdio.h doesn't actually have any function implementations in it.

    In most C compilers the actual compiled functions are stored in a library called libc.a, but the math functions are in libm.a (and the in SimpleIDE the simple libraries are in other library files).

    FlexC is a little different. You can link libc.a by putting it on the command line, but FlexC also has an extension which allows it to automatically link in the appropriate code for a header file. You do this by writing __fromfile("path/to/file.c") after the function declaration, e.g. you could write:
       int bme680_init(int x) __fromfile("Sensor/libbme680/bme680.c");
    
    The header file <compiler.h> has a macro _IMPL(x) which expands to __fromfile(x) in FlexC and an empty string in other compilers (like Catalina or PropGCC). So a slightly better way to do this is:
    #include <compiler.h>
    
       int bme680_init(int x) _IMPL("Sensor/libbme680/bme680.c");
    

    To recap:

    (1) The .h file contains declarations, not code; things like #define, structure typedefs, and function prototypes.
    (2) The .c file contains the implementation. Traditionally this gets compiled to an object file, .o and then placed into a library file (.a).
    (3) In FlexC you can extend the function prototypes with a FlexC specific __fromfile() attribute which automatically will locate the code for the C function, without needing a .a file.

Sign In or Register to comment.