forked from 0xD34D/klipper_ender3_v3_se
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from jpcurti/1-configure-rotary-encoder-buttons…
…-with-buttons-module-from-klipper-display 1 configure rotary encoder buttons with buttons module from klipper display
- Loading branch information
Showing
1 changed file
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import logging | ||
import time | ||
from .display import menu_keys | ||
|
||
class E3v3seDisplay: | ||
|
||
ENCODER_DIFF_NO = 0 # no state | ||
ENCODER_DIFF_CW = 1 # clockwise rotation | ||
ENCODER_DIFF_CCW = 2 # counterclockwise rotation | ||
ENCODER_DIFF_ENTER = 3 # click | ||
|
||
EncoderRateLimit = True | ||
|
||
def __init__(self, config): | ||
self.printer = config.get_printer() | ||
self.config = config | ||
self.mutex = self.printer.get_reactor().mutex() | ||
self.name = config.get_name() | ||
self.reactor = self.printer.get_reactor() | ||
self._logging = config.getboolean("logging", False) | ||
self.gcode = self.printer.lookup_object("gcode") | ||
self.printer.register_event_handler("klippy:ready", self.handle_ready) | ||
self.encoder_state = self.ENCODER_DIFF_NO | ||
|
||
# register for key events | ||
menu_keys.MenuKeys(config, self.key_event) | ||
|
||
self._update_interval = 1 | ||
self._update_timer = self.reactor.register_timer(self._screen_update) | ||
|
||
def key_event(self, key, eventtime): | ||
if key == 'click': | ||
self.log("click") | ||
self.encoder_state = self.ENCODER_DIFF_ENTER | ||
elif key == 'long_click': | ||
self.log("long_click") | ||
self.encoder_state = self.ENCODER_DIFF_ENTER | ||
elif key == 'up': | ||
self.log("up") | ||
self.encoder_state = self.ENCODER_DIFF_CCW | ||
elif key == 'down': | ||
self.log("down") | ||
self.encoder_state = self.ENCODER_DIFF_CW | ||
|
||
self.encoder_has_data() | ||
|
||
def get_encoder_state(self): | ||
last_state = self.encoder_state | ||
self.encoder_state = self.ENCODER_DIFF_NO | ||
return last_state | ||
|
||
def encoder_has_data(self): | ||
self.log("Key event") | ||
return | ||
|
||
def _screen_update(self, eventtime): | ||
#self.log("Display update: ") | ||
return eventtime + self._update_interval | ||
|
||
def _screen_init(self, eventtime): | ||
self.reactor.update_timer( | ||
self._update_timer, eventtime + self._update_interval) | ||
return self.reactor.NEVER | ||
|
||
def handle_ready(self): | ||
self.reactor.register_timer( | ||
self._reset_screen, self.reactor.monotonic()) | ||
|
||
def _reset_screen(self, eventtime): | ||
self.log("Reset") | ||
self.reactor.register_timer( | ||
self._screen_init, self.reactor.monotonic() + 2.) | ||
return self.reactor.NEVER | ||
|
||
def log(self, msg, *args, **kwargs): | ||
if self._logging: | ||
logging.info("E3V3SE Display: " + str(msg)) | ||
|
||
def error(self, msg, *args, **kwargs): | ||
logging.error("E3V3SE Display: " + str(msg)) | ||
|
||
def load_config(config): | ||
return E3v3seDisplay(config) | ||
|