Nýtt branch og kóðar/files fyrir part 1

This commit is contained in:
2026-09-24 11:20:26 +00:00
parent 46c761dd25
commit 00c251b26d
11 changed files with 173 additions and 92 deletions
+27 -14
View File
@@ -12,17 +12,20 @@ RotaryEncoderDriver::RotaryEncoderDriver()
, led(5)
, eA(2)
, eB(5)
, delta(0)
{
for (uint8_t i = 0; i < HIST_LEN; i++) {
for (uint8_t i = 0; i < HIST_LEN; i++)
{
history[i] = 0;
}
}
void RotaryEncoderDriver::begin() {
void RotaryEncoderDriver::begin()
{
driver = this;
led.init();
//led.init();
eA.init();
eB.init();
@@ -32,12 +35,14 @@ void RotaryEncoderDriver::begin() {
timer.init(4000);
}
void RotaryEncoderDriver::update() {
void RotaryEncoderDriver::update()
{
count += eB.is_hi() ? 1 : -1;
led.set_hi();
//led.set_hi();
}
int RotaryEncoderDriver::position() {
int RotaryEncoderDriver::position()
{
uint8_t oldSREG = SREG;
@@ -47,27 +52,31 @@ int RotaryEncoderDriver::position() {
SREG = oldSREG;
led.set_lo();
//led.set_lo();
return local_count;
}
void RotaryEncoderDriver::update_delta() {
void RotaryEncoderDriver::update_delta()
{
int16_t oldest = history[hist_idx];
history[hist_idx] = count;
hist_idx = (hist_idx + 1) % HIST_LEN;
delta = count - oldest; // breyting yfir HIST_LEN * Ts (40 ms) glugga
delta = count - oldest; // breyting yfir HIST_LEN * Ts (16 ms) glugga
prev_count = count;
speed_ready = true;
led.set_lo();
//led.set_lo();
}
float RotaryEncoderDriver::count_per_sec() {
float RotaryEncoderDriver::count_per_sec()
{
uint8_t oldSREG = SREG;
@@ -118,18 +127,22 @@ void RotaryEncoderDriver::reset_speed_measurement()
delta = 0;
speed_ready = false;
for (uint8_t i = 0; i < HIST_LEN; i++) {
for (uint8_t i = 0; i < HIST_LEN; i++)
{
history[i] = count;
}
hist_idx = 0;
SREG = oldSREG;
}
ISR(INT0_vect) {
ISR(INT0_vect)
{
driver->update();
}
ISR(TIMER1_COMPA_vect) {
ISR(TIMER1_COMPA_vect)
{
driver->update_delta();
}