-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented an encoder wheel for e.g. zooming (#31)
Implemented an encoder wheel for e.g. zooming based on the idea by https://github.com/JoseLuisGZA/ErgonoMouse. Use ROTARY_AXIS to choose which axis is replaced by the encoder. When ROTARY_AXIS == 0, the encoder library is not needed in ARDUINO IDE. PlatformIO is loading it via platformio.ini
- Loading branch information
Showing
7 changed files
with
152 additions
and
13 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
Binary file not shown.
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 |
---|---|---|
@@ -1,12 +1,22 @@ | ||
; PlatformIO Project Configuration File | ||
; | ||
; Build options: build flags, source filter | ||
; Upload options: custom upload port, speed and extra flags | ||
; Library options: dependencies, extra library storages | ||
; Advanced options: extra scripting | ||
; | ||
; Please visit documentation for the other options and examples | ||
; https://docs.platformio.org/page/projectconf.html | ||
|
||
[env:micro] | ||
platform = atmelavr | ||
board = micro | ||
framework = arduino | ||
extra_scripts = pre:set_hwids.py | ||
# send on enter with a cr lf typo and show this output in the console: | ||
monitor_filters = send_on_enter | ||
monitor_filters = send_on_enter | ||
monitor_eol = CRLF | ||
monitor_echo = yes | ||
lib_deps = paulstoffregen/Encoder@^1.4.4 | ||
|
||
[platformio] | ||
src_dir = spacemouse-keys |
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
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,77 @@ | ||
/* | ||
* This code gives the space mouse the ability to include an encoder wheel. | ||
* The encoder gives positions but the space mouse reports velocities. | ||
* Therefore, we calculate a filtered derivative from the encoder position and | ||
* replace the desired velocity from the original space mouse. | ||
* | ||
* Based on the idea by JoseLuisGZA, rewritten by Andun HH | ||
*/ | ||
|
||
#include <Arduino.h> | ||
#include "config.h" | ||
#if ROTARY_AXIS > 0 | ||
#include "encoderWheel.h" | ||
|
||
// Include Encoder library by Paul Stoffregen | ||
#include <Encoder.h> | ||
|
||
Encoder myEncoder(ENCODER_CLK, ENCODER_DT); | ||
|
||
int32_t previousEncoderValue = 0; | ||
int32_t newEncoderValue; // Store encoder readings | ||
int32_t delta = 0; // Tracks encoder increments when turned | ||
|
||
int zoomIterator = ECHOES; // Counter for echoing the delta through a number of loops for a smoother zoom animation | ||
float simpull; // calculated velocity of the encoder wheel | ||
|
||
void initEncoderWheel() | ||
{ | ||
// Read initial value from encoder | ||
newEncoderValue = myEncoder.read(); | ||
} | ||
|
||
/// @brief Calculate the encoder wheel and update the result in the velocity array | ||
/// @param velocity Array with the velocity, which gets updated at position ROTARY_AXIS-1 | ||
/// @param debug Generate a debug output if debug=9 | ||
void calcEncoderWheel(int16_t *velocity, int debug) | ||
{ | ||
static int factor = 100; // | ||
// read encoder | ||
newEncoderValue = myEncoder.read(); | ||
if (newEncoderValue != previousEncoderValue) | ||
{ | ||
// position changed, how much? | ||
delta = newEncoderValue - previousEncoderValue; | ||
previousEncoderValue = newEncoderValue; | ||
zoomIterator = 0; | ||
} | ||
|
||
// Distribute encoder delta through the echoes in the loop and based on simulated axis chosen by the user | ||
// Faded intensity for echoing the encoder reading. | ||
if (zoomIterator < ECHOES) | ||
{ | ||
factor = 100 - ((zoomIterator * 100) / ECHOES); // factor shall be percent: between 0 and 100 | ||
simpull = (factor * SIMSTRENGTH) / 100 * delta; | ||
zoomIterator++; // iterate | ||
// add the velocity of the encoder wheel to one of the 6 axis | ||
// the ROTARY_AXIS definition is one above the array definition used for the velocity array (see calibration.h) | ||
// Therefore ROTARY_AXIS-1 is used to change the velocity value | ||
velocity[ROTARY_AXIS - 1] = velocity[ROTARY_AXIS - 1] + simpull; | ||
} | ||
else | ||
{ | ||
// fading has ended | ||
simpull = 0; | ||
} | ||
if (debug == 9) | ||
{ | ||
// create debug output | ||
Serial.print("Enc Val: "); | ||
Serial.print(newEncoderValue); | ||
Serial.print(", factor: "); | ||
Serial.print(factor); | ||
Serial.print(", simpull: "); | ||
Serial.println(simpull); | ||
} | ||
} | ||
#endif // whole file is only implemented #if ROTARY_AXIS > 0 |
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,4 @@ | ||
// Header file for the encoderWheel.cpp | ||
|
||
void initEncoderWheel(); | ||
void calcEncoderWheel(int16_t* velocity, int debug); |
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