portapack-mayhem/firmware/application/hw/encoder.cpp

78 lines
2.4 KiB
C++
Raw Normal View History

2015-07-08 11:39:24 -04:00
/*
* Copyright (C) 2014 Jared Boone, ShareBrained Technology, Inc.
* Copyright (C) 2024 Mark Thompson
2015-07-08 11:39:24 -04:00
*
* This file is part of PortaPack.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#include "encoder.hpp"
#include "utility.hpp"
Support for Rotary Encoder Dial sensitivity levels, issue #965 (#1057) * Support for 3 levels of rotary encoder sensitivity #965 Backend support; UI will still need to call set function to configure. * Support for 3 levels of rotary encoder sensitivity #965 Backend support only. UI will still need to be changed to call the set_sensitivity() function to configure. * Removed trailing space * Deleted blank lines to see if format checker will be happier * Simpler support for multiple levels of encoder sensitivity, for issue #965 Removed the convoluted code :-) and instead just using a 2-dimensional array to choose which transition map to use. For now I only have 2 (vs 3) levels enabled as well, to save code space and because high-sensitivity is very touchy. * Simpler version of configurable encoder sensitivity, issue #965 * Formatting * Formatting test for Clang * Formatting test * Formatting (removed helpful comment) * Formatting test (remove commented-out code) * Formatting & swapping medium/low so default mode=0 * Swapped medium/low so default mode=0 * Adding UI & PMEM support to make encoder dial sensitivity configurable, issue #965 * Adding UI & PMEM support to make encoder dial sensitivity configurable, issue #965 * Adding UI & PMEM support to make encoder dial sensitivity configurable, issue #965 * Adding UI & PMEM support to make encoder dial sensitivity configurable, issue #965 * Adding UI & PMEM support to make encoder dial sensitivity configurable, issue #965 * Adding UI & PMEM support to make encoder dial sensitivity configurable, issue #965 * Removed unneeded range check (trusting in pmem checksum)
2023-05-24 22:32:12 -04:00
#include "portapack.hpp"
#include "portapack_persistent_memory.hpp"
// Transition map for rotary encoder phase bits
// 00 -> 01 -> 11 -> 10 cw
// 00 -> 10 -> 11 -> 01 ccw
// NB: Bits are swapped versus older FW versions
static const int8_t transition_map[16] = {
0, // 00->00: noop
1, // 00->01: cw start
-1, // 00->10: ccw start
0, // 00->11: rate
-1, // 01->00: ccw end
0, // 01->01: noop
0, // 01->10: rate
1, // 01->11: cw end
1, // 10->00: cw end
0, // 10->01: rate
0, // 10->10: noop
-1, // 10->11: ccw end
0, // 11->00: rate
-1, // 11->01: ccw start
1, // 11->10: cw start
0, // 11->11: noop
};
// Rotary encoder dial sensitivity (transition ignored if bit is 0)
static const uint16_t sensitivity_map[] = {
0x0990, // DIAL_SENSITIVITY_NORMAL
0x0110, // DIAL_SENSITIVITY_LOW
0x6996, // DIAL_SENSITIVITY_HIGH
2015-07-08 11:39:24 -04:00
};
int_fast8_t Encoder::update(const uint_fast8_t phase_bits) {
state = ((state << 2) | phase_bits) & 0x0F;
int_fast8_t direction = transition_map[state];
// Require 2 state changes in same direction to register movement -- for additional level of contact switch debouncing
if (direction == prev_direction) {
if ((sensitivity_map[portapack::persistent_memory::encoder_dial_sensitivity()] & (1 << state)) == 0)
return 0;
return direction;
}
// It's normal for transition map to return 0 between every +1/-1, so discarding those
if (direction != 0)
prev_direction = direction;
2015-07-08 11:39:24 -04:00
return 0;
2015-07-08 11:39:24 -04:00
}