Commit 2dcd065b authored by trilader's avatar trilader
Browse files

feat(pycardium): Add (wristband) GPIO module

parent 2fb78caa
...@@ -24,6 +24,7 @@ Last but not least, if you want to start hacking the lower-level firmware, the ...@@ -24,6 +24,7 @@ Last but not least, if you want to start hacking the lower-level firmware, the
pycardium/buttons pycardium/buttons
pycardium/color pycardium/color
pycardium/display pycardium/display
pycardium/gpio
pycardium/leds pycardium/leds
pycardium/light-sensor pycardium/light-sensor
pycardium/os pycardium/os
......
.. py:module:: gpio
``gpio`` - GPIO Pins
==========================
The :py:mod:`gpio` module allows you to use card10's GPIO pins as input and
output in your scripts.
**Example**:
.. code-block:: python
import gpio
gpio.set_mode(gpio.WRISTBAND_1, gpio.mode.OUTPUT)
gpio.write(gpio.WRISTBAND_1, True)
gpio.set_mode(gpio.WRISTBAND_2, gpio.mode.INPUT | gpio.mode.PULL_UP)
state = gpio.read(gpio.WRISTBAND_2)
print("State of Wristband pin 2:", state)
.. py:function:: gpio.set_mode(pin, mode)
Configure GPIO pin state.
:param int pin: ID of the pin to be configured.
:param int mode: An integer with the bits for the wanted mode set. Create your
integer by ORing :py:data:`gpio.mode.OUTPUT`, :py:data:`gpio.mode.INPUT`,
:py:data:`gpio.mode.PULL_UP`, :py:data:`gpio.mode.PULL_DOWN`.
.. py:function:: gpio.get_mode(pin)
Get GPIO pin state.
:param int pin: ID of the pin of to get the mode of.
:returns: An integer with the configure mode bits set.
.. py:function:: gpio.write(pin, value)
Write a value to a GPIO pin.
:param int pin: ID of the pin of to get the mode of.
:param bool value: New pin value.
.. py:function:: gpio.read(pin)
Read GPIO pin value.
:param int pin: ID of the pin of to get the mode of.
:returns: Current value of the GPIO pin.
.. py:data:: gpio.WRISTBAND_1
Pin ID for Wristband GPIO 1.
.. py:data:: gpio.WRISTBAND_2
Pin ID for Wristband GPIO 2.
.. py:data:: gpio.WRISTBAND_3
Pin ID for Wristband GPIO 3.
.. py:data:: gpio.WRISTBAND_4
Pin ID for Wristband GPIO 4.
.. py:data:: gpio.mode.OUTPUT
Configures a pin as output.
.. py:data:: gpio.mode.INPUT
Configures a pin as input.
.. py:data:: gpio.mode:PULL_UP
Enables the internal pull-up resistor of a pin.
.. py:data:: gpio.mode:PULL_DOwn
Enables the internal pull-down resistor of a pin.
...@@ -4,6 +4,7 @@ modsrc = files( ...@@ -4,6 +4,7 @@ modsrc = files(
'modules/buttons.c', 'modules/buttons.c',
'modules/fat_file.c', 'modules/fat_file.c',
'modules/fat_reader_import.c', 'modules/fat_reader_import.c',
'modules/gpio.c',
'modules/interrupt.c', 'modules/interrupt.c',
'modules/sys_leds.c', 'modules/sys_leds.c',
'modules/light_sensor.c', 'modules/light_sensor.c',
......
#include "py/obj.h"
#include "py/runtime.h"
#include "epicardium.h"
static mp_obj_t mp_gpio_set_mode(mp_obj_t pin_obj, mp_obj_t mode_obj)
{
int pin = mp_obj_get_int(pin_obj);
int mode = mp_obj_get_int(mode_obj);
int rc = epic_gpio_set_pin_mode(pin, mode);
if (rc < 0)
mp_raise_OSError(-rc);
return mp_const_none;
};
static MP_DEFINE_CONST_FUN_OBJ_2(gpio_set_mode, mp_gpio_set_mode);
static mp_obj_t mp_gpio_get_mode(mp_obj_t pin_obj)
{
int pin = mp_obj_get_int(pin_obj);
int mode = epic_gpio_get_pin_mode(pin);
if (mode < 0)
mp_raise_OSError(-mode);
return MP_OBJ_NEW_SMALL_INT(mode);
};
static MP_DEFINE_CONST_FUN_OBJ_1(gpio_get_mode, mp_gpio_get_mode);
static mp_obj_t mp_gpio_read(mp_obj_t pin_obj)
{
int pin = mp_obj_get_int(pin_obj);
uint32_t value = epic_gpio_read_pin(pin);
if (value == -EINVAL)
mp_raise_OSError(EINVAL);
return MP_OBJ_NEW_SMALL_INT(value);
};
static MP_DEFINE_CONST_FUN_OBJ_1(gpio_read, mp_gpio_read);
static mp_obj_t mp_gpio_write(mp_obj_t pin_obj, mp_obj_t on_obj)
{
int pin = mp_obj_get_int(pin_obj);
bool on = mp_obj_is_true(on_obj);
int rc = epic_gpio_write_pin(pin, on);
if (rc < 0)
mp_raise_OSError(-rc);
return mp_const_none;
};
static MP_DEFINE_CONST_FUN_OBJ_2(gpio_write, mp_gpio_write);
static const mp_rom_map_elem_t gpio_module_modes_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_mode) },
{ MP_ROM_QSTR(MP_QSTR_INPUT), MP_OBJ_NEW_SMALL_INT(GPIO_MODE_IN) },
{ MP_ROM_QSTR(MP_QSTR_OUTPUT), MP_OBJ_NEW_SMALL_INT(GPIO_MODE_OUT) },
{ MP_ROM_QSTR(MP_QSTR_PULL_UP), MP_OBJ_NEW_SMALL_INT(GPIO_PULL_UP) },
{ MP_ROM_QSTR(MP_QSTR_PULL_DOWN),
MP_OBJ_NEW_SMALL_INT(GPIO_PULL_DOWN) },
};
static MP_DEFINE_CONST_DICT(gpio_module_modes_dict, gpio_module_modes_table);
const mp_obj_module_t gpio_module_modes = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t *)&gpio_module_modes_dict,
};
/* The globals table for this module */
static const mp_rom_map_elem_t gpio_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_gpio) },
{ MP_ROM_QSTR(MP_QSTR_set_mode), MP_ROM_PTR(&gpio_set_mode) },
{ MP_ROM_QSTR(MP_QSTR_get_mode), MP_ROM_PTR(&gpio_get_mode) },
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&gpio_read) },
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&gpio_write) },
{ MP_ROM_QSTR(MP_QSTR_WRISTBAND_1),
MP_OBJ_NEW_SMALL_INT(GPIO_WRISTBAND_1) },
{ MP_ROM_QSTR(MP_QSTR_WRISTBAND_2),
MP_OBJ_NEW_SMALL_INT(GPIO_WRISTBAND_2) },
{ MP_ROM_QSTR(MP_QSTR_WRISTBAND_3),
MP_OBJ_NEW_SMALL_INT(GPIO_WRISTBAND_3) },
{ MP_ROM_QSTR(MP_QSTR_WRISTBAND_4),
MP_OBJ_NEW_SMALL_INT(GPIO_WRISTBAND_4) },
{ MP_ROM_QSTR(MP_QSTR_mode), MP_ROM_PTR(&gpio_module_modes) },
};
static MP_DEFINE_CONST_DICT(gpio_module_globals, gpio_module_globals_table);
const mp_obj_module_t gpio_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t *)&gpio_module_globals,
};
/* This is a special macro that will make MicroPython aware of this module */
/* clang-format off */
MP_REGISTER_MODULE(MP_QSTR_gpio, gpio_module, MODULE_GPIO_ENABLED);
...@@ -100,3 +100,18 @@ Q(exit) ...@@ -100,3 +100,18 @@ Q(exit)
Q(exec) Q(exec)
Q(listdir) Q(listdir)
Q(unlink) Q(unlink)
/* gpio */
Q(gpio)
Q(set_mode)
Q(get_mode)
Q(read)
Q(write)
Q(WRISTBAND_1)
Q(WRISTBAND_2)
Q(WRISTBAND_3)
Q(WRISTBAND_4)
Q(INPUT)
Q(OUTPUT)
Q(PULL_UP)
Q(PULL_DOWN)
...@@ -41,6 +41,7 @@ ...@@ -41,6 +41,7 @@
/* Modules */ /* Modules */
#define MODULE_BUTTONS_ENABLED (1) #define MODULE_BUTTONS_ENABLED (1)
#define MODULE_DISPLAY_ENABLED (1) #define MODULE_DISPLAY_ENABLED (1)
#define MODULE_GPIO_ENABLED (1)
#define MODULE_INTERRUPT_ENABLED (1) #define MODULE_INTERRUPT_ENABLED (1)
#define MODULE_LEDS_ENABLED (1) #define MODULE_LEDS_ENABLED (1)
#define MODULE_LIGHT_SENSOR_ENABLED (1) #define MODULE_LIGHT_SENSOR_ENABLED (1)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment