-
Notifications
You must be signed in to change notification settings - Fork 502
/
Copy pathbreakout_as7343.cpp
140 lines (98 loc) · 4.67 KB
/
breakout_as7343.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "drivers/as7343/as7343.hpp"
#include "common/pimoroni_i2c.hpp"
#include "micropython/modules/util.hpp"
using namespace pimoroni;
extern "C" {
#include "breakout_as7343.h"
#include "pimoroni_i2c.h"
/***** Variables Struct *****/
typedef struct _breakout_as7343_BreakoutAS7343_obj_t {
mp_obj_base_t base;
AS7343 *breakout;
_PimoroniI2C_obj_t *i2c;
} breakout_as7343_BreakoutAS7343_obj_t;
/***** Constructor *****/
mp_obj_t BreakoutAS7343_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
breakout_as7343_BreakoutAS7343_obj_t *self = nullptr;
enum { ARG_i2c, ARG_int };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_i2c, MP_ARG_OBJ, {.u_obj = nullptr} },
{ MP_QSTR_interrupt, MP_ARG_INT, {.u_int = PIN_UNUSED} },
};
// Parse args.
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
self = m_new_obj(breakout_as7343_BreakoutAS7343_obj_t);
self->base.type = &breakout_as7343_BreakoutAS7343_type;
self->i2c = PimoroniI2C_from_machine_i2c_or_native(args[ARG_i2c].u_obj);
self->breakout = m_new_class(AS7343, (pimoroni::I2C *)(self->i2c->i2c), args[ARG_int].u_int);
if(!self->breakout->init()) {
mp_raise_msg(&mp_type_RuntimeError, "BreakoutAS7343: breakout not found when initialising");
}
return MP_OBJ_FROM_PTR(self);
}
/***** Methods *****/
mp_obj_t BreakoutAS7343_reset(mp_obj_t self_in) {
breakout_as7343_BreakoutAS7343_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_as7343_BreakoutAS7343_obj_t);
self->breakout->reset();
return mp_const_none;
}
mp_obj_t BreakoutAS7343_version(mp_obj_t self_in) {
breakout_as7343_BreakoutAS7343_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_as7343_BreakoutAS7343_obj_t);
uint8_t aux_id, revision_id, hardware_id;
self->breakout->get_version(aux_id, revision_id, hardware_id);
mp_obj_t tuple[3];
tuple[0] = mp_obj_new_int(aux_id);
tuple[1] = mp_obj_new_int(revision_id);
tuple[2] = mp_obj_new_int(hardware_id);
return mp_obj_new_tuple(3, tuple);
}
mp_obj_t BreakoutAS7343_read(mp_obj_t self_in) {
breakout_as7343_BreakoutAS7343_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_as7343_BreakoutAS7343_obj_t);
AS7343::reading reading = self->breakout->read();
mp_obj_t tuple[12];
tuple[0] = mp_obj_new_float(reading.FZ);
tuple[1] = mp_obj_new_float(reading.FY);
tuple[2] = mp_obj_new_float(reading.FXL);
tuple[3] = mp_obj_new_float(reading.NIR);
tuple[4] = mp_obj_new_float(reading.F2);
tuple[5] = mp_obj_new_float(reading.F3);
tuple[6] = mp_obj_new_float(reading.F4);
tuple[7] = mp_obj_new_float(reading.F6);
tuple[8] = mp_obj_new_float(reading.F1);
tuple[9] = mp_obj_new_float(reading.F5);
tuple[10] = mp_obj_new_float(reading.F7);
tuple[11] = mp_obj_new_float(reading.F8);
return mp_obj_new_tuple(12, tuple);
}
mp_obj_t BreakoutAS7343_set_gain(mp_obj_t self_in, mp_obj_t value_in) {
breakout_as7343_BreakoutAS7343_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_as7343_BreakoutAS7343_obj_t);
self->breakout->set_gain(mp_obj_get_int(value_in));
return mp_const_none;
}
mp_obj_t BreakoutAS7343_set_measurement_time(mp_obj_t self_in, mp_obj_t value_in) {
breakout_as7343_BreakoutAS7343_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_as7343_BreakoutAS7343_obj_t);
self->breakout->set_measurement_time(mp_obj_get_float(value_in));
return mp_const_none;
}
mp_obj_t BreakoutAS7343_set_integration_time(mp_obj_t self_in, mp_obj_t value_in) {
breakout_as7343_BreakoutAS7343_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_as7343_BreakoutAS7343_obj_t);
self->breakout->set_integration_time(mp_obj_get_float(value_in));
return mp_const_none;
}
mp_obj_t BreakoutAS7343_set_illumination_current(mp_obj_t self_in, mp_obj_t value_in) {
breakout_as7343_BreakoutAS7343_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_as7343_BreakoutAS7343_obj_t);
self->breakout->set_illumination_current(mp_obj_get_int(value_in));
return mp_const_none;
}
mp_obj_t BreakoutAS7343_set_illumination_led(mp_obj_t self_in, mp_obj_t value_in) {
breakout_as7343_BreakoutAS7343_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_as7343_BreakoutAS7343_obj_t);
self->breakout->set_illumination_led(value_in == mp_const_true);
return mp_const_none;
}
mp_obj_t BreakoutAS7343_set_channels(mp_obj_t self_in, mp_obj_t value_in) {
breakout_as7343_BreakoutAS7343_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_as7343_BreakoutAS7343_obj_t);
self->breakout->set_channels((AS7343::channel_count)mp_obj_get_int(value_in));
return mp_const_none;
}
}