adding driver to pyb

Discuss development of drivers for external hardware and components, such as LCD screens, sensors, motor drivers, etc.
Target audience: Users and developers of drivers.
Post Reply
User avatar
roland_vs
Posts: 89
Joined: Tue Dec 08, 2015 8:28 pm
Location: Netherlands
Contact:

adding driver to pyb

Post by roland_vs » Tue Dec 08, 2015 8:51 pm

Hello,

I'm fiddling with micropython and creating a driver for the PCA9557. You can have up to 8 of these devices on a single I2C bus. I have looked at servo and accel as examples to roll my own.

So far I can see my device appear in the dir(pyb), however I'm not done yet with passing and getting the right parameters.

Is there somebody who has a skeleton or at least some more explanation on the mechanism with the MP_DEFINE_CONST_FUN_OBJ... variations?
Is it possible to have the names like "inputreg" (0), "outputreg" (1), "polarity"(2) and "config"(3) as parameter and as such known to the dictionary of micropython?

Basically what I try to do

Code: Select all

device = pyb.PCA9557(1..8)
device.init()
device.read(register=0..3)
device.write(register=0..3, value=0..255)
device.deinit()
Created locals_dict_table...

Code: Select all

{ MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&pyb_pca9557_read_obj 	},
{ MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&pyb_pca9557_write_obj 	},
{ MP_OBJ_NEW_QSTR(MP_QSTR_init), (mp_obj_t)&pyb_pca9557_init_obj 	},
{ MP_OBJ_NEW_QSTR(MP_QSTR_deinit), (mp_obj_t)&pyb_pca9557_deinit_obj 	},
Defined function for writing "device.write(register=0..3, value=0..255)"

Code: Select all

STATIC mp_obj_t pyb_pca9557_write(mp_obj_t reg, mp_obj_t val) {
...
} MP_DEFINE_CONST_FUN_OBJ_2(pyb_pca9557_write_obj, pyb_pca9557_write);
Defined function for reading "device.read(register=0..3)"

Code: Select all


STATIC mp_obj_t pyb_pca9557_read(mp_obj_t value) {
...
} MP_DEFINE_CONST_FUN_OBJ_1(pyb_pca9557_read_obj, pyb_pca9557_read);

Any pointers...

R

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: adding driver to pyb

Post by dhylands » Tue Dec 08, 2015 9:29 pm

I assume you're talking about creating a C driver.

You should be able to do your driver for the PCA9557 entirely in python.

If you really need to, you could do it in C. In there was some discussion here: http://forum.micropython.org/viewtopic. ... =153&p=586

You can add constants to your type, just like you add functions. Here's an example in spi.c:
https://github.com/micropython/micropyt ... #L673-L676

The variations for the MP_DEFINE_CONST_FUN_OBJ... just cover passing in various numbers of arguments.
The best way to find examples would to grep for MP_DEFINE_CONST_FUN_OBJ in the stmhal directory.

MP_DEFINE_CONST_FUN_OBJ_0..3 defines a function which takes 0..3 arguments
MP_DEFINE_CONST_FUN_OBJ_VAR defines a function which takes a totally variable number of arguments
MP_DEFINE_CONST_FUN_OBJ_VR_BETWEEN defines a function which takes a variable number of arguments between n_args_min and n_args_max
MP_DEFINE_CONST_FUN_OBJ_KW defines a function which takes keyword arguments.


You seem to be writing your functions to just be global. Instead you should define them to work as if they were members of a class. So that you can support multiple PCA9557 devices connected to potentially multiple i2c busses. Otherwise you'd need to pass in the i2c bus and the device id of the PCA9557 device that you want to talk to to each and every function.

Post Reply