9
9
#include "shared-bindings/countio/Counter.h"
10
10
#include "shared-bindings/util.h"
11
11
12
- //| .. currentmodule:: countio
12
+ //| class Counter:
13
+ //| """Counter will keep track of the number of falling edge transistions (pulses) on a
14
+ //| given pin"""
13
15
//|
14
- //| :class:`Counter` -- Track the count of falling edge transistions (pulses) on a given pin
15
- //| ========================================================================================
16
+ //| def __init__(self, pin_a):
17
+ //| """Create a Counter object associated with the given pin. It tracks the number of
18
+ //| falling pulses relative when the object is constructed.
16
19
//|
17
- //| Counter will keep track of the number of falling edge transistions (pulses) on a given pin
20
+ //| :param ~microcontroller.Pin pin_a: Pin to read pulses from.
18
21
//|
19
- //| .. class:: Counter(pin_a)
20
22
//|
21
- //| Create a Counter object associated with the given pin. It tracks the number of
22
- //| falling pulses relative when the object is constructed.
23
+ //| For example::
23
24
//|
24
- //| :param ~microcontroller.Pin pin_a: Pin to read pulses from.
25
- //|
25
+ //| import countio
26
+ //| import time
27
+ //| from board import *
26
28
//|
27
- //| For example::
28
- //|
29
- //| import countio
30
- //| import time
31
- //| from board import *
32
- //|
33
- //| pin_counter = countio.Counter(board.D1)
34
- //| #reset the count after 100 counts
35
- //| while True:
36
- //| if pin_counter.count == 100:
37
- //| pin_counter.reset()
38
- //| print(pin_counter.count)
29
+ //| pin_counter = countio.Counter(board.D1)
30
+ //| #reset the count after 100 counts
31
+ //| while True:
32
+ //| if pin_counter.count == 100:
33
+ //| pin_counter.reset()
34
+ //| print(pin_counter.count)"""
39
35
//|
40
36
STATIC mp_obj_t countio_counter_make_new (const mp_obj_type_t * type , size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
41
37
enum { ARG_pin_a };
42
38
static const mp_arg_t allowed_args [] = {
43
39
{ MP_QSTR_pin_a , MP_ARG_REQUIRED | MP_ARG_OBJ }
44
-
40
+
45
41
};
46
42
mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
47
43
mp_arg_parse_all (n_args , pos_args , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
48
44
49
45
const mcu_pin_obj_t * pin_a = validate_obj_is_free_pin (args [ARG_pin_a ].u_obj );
50
-
46
+
51
47
52
48
countio_counter_obj_t * self = m_new_obj (countio_counter_obj_t );
53
49
self -> base .type = & countio_counter_type ;
@@ -57,9 +53,8 @@ STATIC mp_obj_t countio_counter_make_new(const mp_obj_type_t *type, size_t n_arg
57
53
return MP_OBJ_FROM_PTR (self );
58
54
}
59
55
60
- //| .. method:: deinit()
61
- //|
62
- //| Deinitializes the Counter and releases any hardware resources for reuse.
56
+ //| def deinit(self):
57
+ //| """Deinitializes the Counter and releases any hardware resources for reuse."""
63
58
//|
64
59
STATIC mp_obj_t countio_counter_deinit (mp_obj_t self_in ) {
65
60
countio_counter_obj_t * self = MP_OBJ_TO_PTR (self_in );
@@ -74,16 +69,14 @@ STATIC void check_for_deinit(countio_counter_obj_t *self) {
74
69
}
75
70
}
76
71
77
- //| .. method:: __enter__()
78
- //|
79
- //| No-op used by Context Managers.
72
+ //| def __enter__(self):
73
+ //| """No-op used by Context Managers."""
80
74
//|
81
75
// Provided by context manager helper.
82
76
83
- //| .. method:: __exit__()
84
- //|
85
- //| Automatically deinitializes the hardware when exiting a context. See
86
- //| :ref:`lifetime-and-contextmanagers` for more info.
77
+ //| def __exit__(self):
78
+ //| """Automatically deinitializes the hardware when exiting a context. See
79
+ //| :ref:`lifetime-and-contextmanagers` for more info."""
87
80
//|
88
81
STATIC mp_obj_t countio_counter_obj___exit__ (size_t n_args , const mp_obj_t * args ) {
89
82
(void )n_args ;
@@ -93,10 +86,8 @@ STATIC mp_obj_t countio_counter_obj___exit__(size_t n_args, const mp_obj_t *args
93
86
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (countio_counter___exit___obj , 4 , 4 , countio_counter_obj___exit__ );
94
87
95
88
96
- //| .. attribute:: count
97
- //|
98
- //| The current count in terms of pulses.
99
- //|
89
+ //| count: int = ...
90
+ //| """The current count in terms of pulses."""
100
91
//|
101
92
STATIC mp_obj_t countio_counter_obj_get_count (mp_obj_t self_in ) {
102
93
countio_counter_obj_t * self = MP_OBJ_TO_PTR (self_in );
@@ -122,14 +113,17 @@ const mp_obj_property_t countio_counter_count_obj = {
122
113
(mp_obj_t )& mp_const_none_obj },
123
114
};
124
115
116
+ //| def reset(self):
117
+ //| """Resets the count back to 0."""
118
+ //|
125
119
STATIC mp_obj_t countio_counter_reset (mp_obj_t self_in ){
126
120
countio_counter_obj_t * self = MP_OBJ_TO_PTR (self_in );
127
121
check_for_deinit (self );
128
122
//set the position to zero for reset
129
123
common_hal_countio_counter_reset (self );
130
124
return mp_const_none ;
131
125
}
132
-
126
+
133
127
134
128
MP_DEFINE_CONST_FUN_OBJ_1 (countio_counter_reset_obj , countio_counter_reset );
135
129
0 commit comments