Skip to content
Snippets Groups Projects
Commit 3fb239fd authored by trilader's avatar trilader
Browse files

feat(pycardium): Add pycardium module for personal state support

parent dd841b5a
No related branches found
No related tags found
No related merge requests found
......@@ -29,6 +29,7 @@ Last but not least, if you want to start hacking the lower-level firmware, the
pycardium/leds
pycardium/light-sensor
pycardium/os
pycardium/personal_state
pycardium/utime
pycardium/vibra
......
.. py:module:: personal_state
``personal_state`` - Personal State
===================================
The :py:mod:`personal_state` module allows you to set and get the card10 users personal state from your script. The personal state is displayed on the top-left LED on the bottom of the harmonics board. While the personal state is set the LED can't be controlled by the :py:mod:`leds` module.
**Example**:
.. code-block:: python
import personal_state
# Enable the "camp" state only while the app is running.
personal_state.set(personal_state.CAMP, False)
# Enable the "chaos" state and keep it after the app exits.
personal_state.set(personal_state.CHAOS, True)
# Query the currently configured state and if it's persistent.
state, persistent = personal_state.get()
# Clear the currently configured state
personal_state.clear()
.. py:function:: personal_state.set(state, persistent)
Set the users personal state.
:param int state: ID of the personal state to set. Must be one of :py:data:`personal_state.NO_CONTACT`, :py:data:`personal_state.CHAOS`, :py:data:`personal_state.COMMUNICATION`, :py:data:`personal_state.CAMP`.
:param int persistent: Controls whether the personal state is persistent. A persistent state is not reset when the pycardium application is changed or restarted. In persistent mode the personal state LED is not controllable by the pycardium application.
.. py:function:: personal_state.clear()
Clears a previously set personal state.
If no personal state was set this function does nothing. It does not matter
if a set state is marked as persistent or not.
.. py:function:: personal_state.get()
Get the users personal state.
:returns: A tuple containing the currently set state and a boolean indicating if it's persistent or not.
.. py:data:: personal_state.NO_STATE
State ID reported when no personal state is set.
.. py:data:: personal_state.NO_CONTACT
State ID for the "No Contact" personal state.
.. py:data:: personal_state.CHAOS
State ID for the "Chaos" personal state.
.. py:data:: personal_state.COMMUNICATION
State ID for the "Communicatoin" personal state.
.. py:data:: personal_state.CAMP
State ID for the "Camp" personal state.
......@@ -9,6 +9,7 @@ modsrc = files(
'modules/sys_leds.c',
'modules/light_sensor.c',
'modules/os.c',
'modules/personal_state.c',
'modules/sys_display.c',
'modules/utime.c',
'modules/vibra.c',
......
#include "epicardium.h"
#include "py/builtin.h"
#include "py/obj.h"
#include "py/runtime.h"
static mp_obj_t
mp_personal_state_set(mp_obj_t state_obj, mp_obj_t persistent_obj)
{
int state = mp_obj_get_int(state_obj);
int rc = epic_personal_state_set(state, mp_obj_is_true(persistent_obj));
if (rc < 0)
mp_raise_OSError(-rc);
return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_2(personal_state_set, mp_personal_state_set);
static mp_obj_t mp_personal_state_clear()
{
int rc = epic_personal_state_set(STATE_NONE, 0);
if (rc < 0)
mp_raise_OSError(-rc);
return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_0(personal_state_clear, mp_personal_state_clear);
static mp_obj_t mp_personal_state_get()
{
int state = epic_personal_state_get();
int persistent = epic_personal_state_is_persistent();
mp_obj_t persistent_obj = mp_const_false;
if (persistent > 0)
persistent_obj = mp_const_true;
mp_obj_t tup[] = {
mp_obj_new_int(state & 0xff),
persistent_obj,
};
return mp_obj_new_tuple(2, tup);
}
static MP_DEFINE_CONST_FUN_OBJ_0(personal_state_get, mp_personal_state_get);
static const mp_rom_map_elem_t personal_state_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_personal_state) },
{ MP_ROM_QSTR(MP_QSTR_set), MP_ROM_PTR(&personal_state_set) },
{ MP_ROM_QSTR(MP_QSTR_get), MP_ROM_PTR(&personal_state_get) },
{ MP_ROM_QSTR(MP_QSTR_clear), MP_ROM_PTR(&personal_state_clear) },
{ MP_ROM_QSTR(MP_QSTR_NO_STATE), MP_OBJ_NEW_SMALL_INT(STATE_NONE) },
{ MP_ROM_QSTR(MP_QSTR_NO_CONTACT),
MP_OBJ_NEW_SMALL_INT(STATE_NO_CONTACT) },
{ MP_ROM_QSTR(MP_QSTR_CHAOS), MP_OBJ_NEW_SMALL_INT(STATE_CHAOS) },
{ MP_ROM_QSTR(MP_QSTR_COMMUNICATION),
MP_OBJ_NEW_SMALL_INT(STATE_COMMUNICATION) },
{ MP_ROM_QSTR(MP_QSTR_CAMP), MP_OBJ_NEW_SMALL_INT(STATE_CAMP) },
};
static MP_DEFINE_CONST_DICT(
personal_state_module_globals, personal_state_module_globals_table
);
// Define module object.
const mp_obj_module_t personal_state_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t *)&personal_state_module_globals,
};
/* Register the module to make it available in Python */
/* clang-format off */
MP_REGISTER_MODULE(MP_QSTR_personal_state, personal_state_module, MODULE_PERSONAL_STATE_ENABLED);
......@@ -118,3 +118,12 @@ Q(INPUT)
Q(OUTPUT)
Q(PULL_UP)
Q(PULL_DOWN)
/* personal_state */
Q(personal_state)
Q(get)
Q(NO_STATE)
Q(NO_CONTACT)
Q(CHAOS)
Q(COMMUNICATION)
Q(CAMP)
......@@ -46,6 +46,7 @@
#define MODULE_LEDS_ENABLED (1)
#define MODULE_LIGHT_SENSOR_ENABLED (1)
#define MODULE_OS_ENABLED (1)
#define MODULE_PERSONAL_STATE_ENABLED (1)
#define MODULE_UTIME_ENABLED (1)
#define MODULE_VIBRA_ENABLED (1)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment