Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
IhorNehrutsa committed Aug 14, 2023
1 parent 5ef41fa commit 470af85
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 0 deletions.
30 changes: 30 additions & 0 deletions ports/esp32/machine_encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ See also
https://github.com/espressif/esp-idf/tree/master/examples/peripherals/pcnt/rotary_encoder
*/

// #define DBG(...)
#define DBG(...) mp_printf(&mp_plat_print, __VA_ARGS__); mp_printf(&mp_plat_print, "\n");

#include "py/mpprint.h"
#include "py/runtime.h"
#include "mphalport.h"
Expand Down Expand Up @@ -103,13 +106,25 @@ STATIC void IRAM_ATTR pcnt_intr_handler(void *arg) {
self->status = 0;
if (PCNT.status_unit[id].THRES1_LAT) {
if (self->counter == self->counter_match1) {
/*
int16_t count;
pcnt_get_counter_value(self->unit, &count);
self->counter += count;
pcnt_counter_clear(self->unit);
*/
self->status |= EVT_THRES_1;
mp_sched_schedule(self->handler_match1, MP_OBJ_FROM_PTR(self));
mp_hal_wake_main_task_from_isr();
}
}
if (PCNT.status_unit[id].THRES0_LAT) {
if (self->counter == self->counter_match2) {
/*
int16_t count;
pcnt_get_counter_value(self->unit, &count);
self->counter += count;
pcnt_counter_clear(self->unit);
*/
self->status |= EVT_THRES_0;
mp_sched_schedule(self->handler_match2, MP_OBJ_FROM_PTR(self));
mp_hal_wake_main_task_from_isr();
Expand Down Expand Up @@ -344,12 +359,20 @@ STATIC mp_obj_t machine_PCNT_irq(size_t n_pos_args, const mp_obj_t *pos_args, mp
} else {
if (trigger & EVT_THRES_1) {
if (args[ARG_value].u_obj != MP_OBJ_NULL) {

int16_t count;
pcnt_get_counter_value(self->unit, &count);
self->counter += count;
DBG("self->counter=%d, count=%d", self->counter, count);
pcnt_counter_clear(self->unit);

self->match1 = GET_INT(args[ARG_value].u_obj);
#ifdef USE_INT64
self->counter_match1 = remainder_of_division(self->match1, INT16_ROLL);
#else
self->counter_match1 = self->match1 % INT16_ROLL;
#endif
DBG("self->match1=%d, self->counter_match1=%d", self->match1, self->counter_match1);
check_esp_err(pcnt_set_event_value(self->unit, EVT_THRES_1, (int16_t)self->counter_match1));
self->counter_match1 = self->match1 - self->counter_match1;
}
Expand All @@ -358,12 +381,19 @@ STATIC mp_obj_t machine_PCNT_irq(size_t n_pos_args, const mp_obj_t *pos_args, mp
}
if (trigger & EVT_THRES_0) {
if (args[ARG_value].u_obj != MP_OBJ_NULL) {

int16_t count;
pcnt_get_counter_value(self->unit, &count);
self->counter += count;
pcnt_counter_clear(self->unit);

self->match2 = GET_INT(args[ARG_value].u_obj);
#ifdef USE_INT64
self->counter_match2 = remainder_of_division(self->match2, INT16_ROLL);
#else
self->counter_match2 = self->match2 % INT16_ROLL;
#endif
DBG("self->match2=%d, self->counter_match2=%d", self->match2, self->counter_match2);
check_esp_err(pcnt_set_event_value(self->unit, EVT_THRES_0, (int16_t)self->counter_match2));
self->counter_match2 = self->match2 - self->counter_match2;
}
Expand Down
110 changes: 110 additions & 0 deletions ports/esp32/test/test_counter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
if 1:
#try:
import machine, time, esp

from stepper import Stepper


pin = 26 # machine.Pin(26) #
dir_pin = machine.Pin(23, machine.Pin.OUT, value=1)

counter = machine.Counter(0, pin, direction=dir_pin)
match1 = 100
match2 = 200

stepper = Stepper('', pin, dir_pin)
print(stepper)

def irq_handler1(obj):
obj_value = obj.value()
print()
print("1 obj.value()=", obj_value, 'obj.status()=', obj.status())
#counter.irq(handler=irq_handler1, trigger=machine.Counter.IRQ_MATCH1, value=obj_value+2500)
print("irq_handler1: obj=", obj)
print()

def irq_handler2(obj):
obj_value = obj.value()
print()
print("2 obj.value()=", obj_value, 'obj.status()=', obj.status())
#counter.irq(handler=irq_handler2, trigger=machine.Counter.IRQ_MATCH2, value=obj_value+2500)
print("irq_handler2: obj=", obj)
print()

def irq_handler0(obj):
obj_value = obj.value()
print()
print("0 obj.value()=", obj_value, 'obj.status()=', obj.status())
#counter.irq(handler=irq_handler2, trigger=machine.Counter.IRQ_MATCH2, value=obj_value+2500)
print("irq_handler0: obj=", obj)
print()

counter.value(0)
counter.irq(handler=irq_handler1, trigger=machine.Counter.IRQ_MATCH1, value=match1)
# counter.irq(handler=irq_handler2, trigger=machine.Counter.IRQ_MATCH2, value=match2)
# counter.irq(handler=irq_handler0, trigger=machine.Counter.IRQ_ZERO)
counter.value(0)
print(counter)
print(counter.value())

stepper.go(250)
print(counter.value())

time.sleep(2)
#1/0

stepper.go(0)
print(counter.value())

time.sleep(2)
#1/0

counter.irq(handler=irq_handler1, trigger=machine.Counter.IRQ_MATCH1, value=-match1)
# counter.irq(handler=irq_handler2, trigger=machine.Counter.IRQ_MATCH2, value=-match2)
print(counter)
print(counter.value())

stepper.go(250)
print(counter.value())
time.sleep(2)

stepper.go(-250)
print(counter.value())

time.sleep(2)
1/0

stepper.go(0)
print(counter.value())

time.sleep(2)
1/0



while 1:
print("counter.value()=", counter.value(), 'match1=', match1, 'match2=', match2)
if (counter.value() >= match2) and (match2 > 0) or (counter.value() <= match2) and (match2 < 0):
#match1 += 2000
match2 = - match2
#counter.pause()
counter.irq(handler=irq_handler2, trigger=machine.Counter.IRQ_MATCH2, value=match2)
#counter.resume()
dir_pin.value(not dir_pin.value())
print('set irq2:', dir_pin.value(), counter)


# if counter.value() > match2:
# match2 += 5000
# counter.irq(handler=irq_handler2, trigger=machine.Counter.IRQ_MATCH2, value=match2)
# print('set irq2:', counter)

time.sleep(1.0)

# except Exception as e:
# try:
# counter.deinit()
# print('counter.deinit()')
# except:
# pass
#

0 comments on commit 470af85

Please sign in to comment.