Use atomic block for accessing lcd_encoder_diff

There was still the possibility of lcd_encoder_diff being updated from the ISR while the new enc_diff was being computed.

Flash: +8B
SRAM: 0B
This commit is contained in:
Alex Voinea 2023-04-23 09:58:56 +02:00
parent 1f181a949a
commit c037e6dfba
No known key found for this signature in database
GPG Key ID: 37EDFD565CB33BAD
1 changed files with 9 additions and 8 deletions

View File

@ -4,6 +4,7 @@
#include <stdio.h>
#include <stdarg.h>
#include <avr/pgmspace.h>
#include <util/atomic.h>
#include <util/delay.h>
#include "Timer.h"
@ -684,15 +685,15 @@ void lcd_knob_update() {
if (lcd_backlight_wake_trigger) {
lcd_backlight_wake_trigger = false;
backlight_wake();
int8_t enc_diff = lcd_encoder_diff;
if (abs(enc_diff) >= ENCODER_PULSES_PER_STEP) {
lcd_encoder += enc_diff / ENCODER_PULSES_PER_STEP;
enc_diff %= ENCODER_PULSES_PER_STEP;
lcd_encoder_diff = enc_diff;
Sound_MakeSound(e_SOUND_TYPE_EncoderMove);
} else {
Sound_MakeSound(e_SOUND_TYPE_ButtonEcho);
bool did_rotate = false;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
if (abs(lcd_encoder_diff) >= ENCODER_PULSES_PER_STEP) {
lcd_encoder += lcd_encoder_diff / ENCODER_PULSES_PER_STEP;
lcd_encoder_diff %= ENCODER_PULSES_PER_STEP;
did_rotate = true;
}
}
Sound_MakeSound(did_rotate ? e_SOUND_TYPE_EncoderMove : e_SOUND_TYPE_ButtonEcho);
if (lcd_draw_update == 0) {
// Update LCD rendering at minimum