Fixed EEPROM write and state swtich when filament runs out.
Fixed EEPROM detection
This commit is contained in:
parent
b9039f7af6
commit
2bbbeec873
39
code/main.c
39
code/main.c
@ -315,11 +315,20 @@ int main()
|
|||||||
|
|
||||||
if (count_value_fine < 0)
|
if (count_value_fine < 0)
|
||||||
{
|
{
|
||||||
|
// Write the zero to EEPROM
|
||||||
|
++eeprom_idx;
|
||||||
|
eeprom_idx %= EEPROM_SIZE;
|
||||||
|
write_eeprom_val(eeprom_idx, 0, move_dir);
|
||||||
|
|
||||||
count_value_fine = 0;
|
count_value_fine = 0;
|
||||||
state = STATE_SETTING;
|
|
||||||
display_enable(1);
|
display_enable(1);
|
||||||
needs_update = 1;
|
|
||||||
|
// Switch State
|
||||||
sleep_when_ms = 0;
|
sleep_when_ms = 0;
|
||||||
|
long_press_when_ms = 0;
|
||||||
|
highlight = 6;
|
||||||
|
needs_update = 1;
|
||||||
|
state = STATE_SETTING;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update EEPROM When Meter Value Changes
|
// Update EEPROM When Meter Value Changes
|
||||||
@ -339,6 +348,7 @@ int main()
|
|||||||
|
|
||||||
if ((0 != long_press_when_ms) && (long_press_when_ms < ms))
|
if ((0 != long_press_when_ms) && (long_press_when_ms < ms))
|
||||||
{
|
{
|
||||||
|
// Switch State
|
||||||
sleep_when_ms = 0;
|
sleep_when_ms = 0;
|
||||||
long_press_when_ms = 0;
|
long_press_when_ms = 0;
|
||||||
highlight = 6;
|
highlight = 6;
|
||||||
@ -428,15 +438,15 @@ int main()
|
|||||||
{
|
{
|
||||||
dir_highlight = 1;
|
dir_highlight = 1;
|
||||||
}
|
}
|
||||||
needs_update = 1;
|
|
||||||
}
|
}
|
||||||
else if (0 != dir_highlight)
|
else if (0 != dir_highlight)
|
||||||
{
|
{
|
||||||
|
// Switch state
|
||||||
|
sleep_when_ms = ms + DISPLAY_DELAY;
|
||||||
dir_highlight = 0;
|
dir_highlight = 0;
|
||||||
state = STATE_COUNTING;
|
state = STATE_COUNTING;
|
||||||
sleep_when_ms = ms + DISPLAY_DELAY;
|
|
||||||
needs_update = 1;
|
|
||||||
|
|
||||||
|
// Write starting value to EEPROM
|
||||||
++eeprom_idx;
|
++eeprom_idx;
|
||||||
eeprom_idx %= EEPROM_SIZE;
|
eeprom_idx %= EEPROM_SIZE;
|
||||||
write_eeprom_val(eeprom_idx, count_value, move_dir);
|
write_eeprom_val(eeprom_idx, count_value, move_dir);
|
||||||
@ -449,6 +459,7 @@ int main()
|
|||||||
// STATE_SETTING
|
// STATE_SETTING
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (needs_update)
|
if (needs_update)
|
||||||
{
|
{
|
||||||
update_display(count_value, highlight, move_dir, dir_highlight);
|
update_display(count_value, highlight, move_dir, dir_highlight);
|
||||||
@ -821,10 +832,10 @@ uint16_t measure_voltage()
|
|||||||
|
|
||||||
uint16_t find_eeprom_idx()
|
uint16_t find_eeprom_idx()
|
||||||
{
|
{
|
||||||
// TODO: double check everything
|
|
||||||
uint16_t idx = 0;
|
uint16_t idx = 0;
|
||||||
uint8_t found = 0;
|
uint8_t found = 0;
|
||||||
|
|
||||||
|
// FF is the erased byte value and is used as prefix
|
||||||
for (idx = 0; idx < EEPROM_SIZE; ++idx)
|
for (idx = 0; idx < EEPROM_SIZE; ++idx)
|
||||||
{
|
{
|
||||||
if (0xFF != eeprom_read_byte((uint8_t *) idx))
|
if (0xFF != eeprom_read_byte((uint8_t *) idx))
|
||||||
@ -839,9 +850,22 @@ uint16_t find_eeprom_idx()
|
|||||||
return EEPROM_SIZE;
|
return EEPROM_SIZE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FF XX ...
|
||||||
|
if (1 == idx)
|
||||||
|
{
|
||||||
|
// Check Last byte for value
|
||||||
|
// FF XX ... FF ??
|
||||||
|
if (0xFF != eeprom_read_byte((uint8_t *) EEPROM_SIZE - 1))
|
||||||
|
{
|
||||||
|
idx = EEPROM_SIZE - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// XX ...
|
||||||
if (0 == idx)
|
if (0 == idx)
|
||||||
{
|
{
|
||||||
for (idx = EEPROM_SIZE - 3; idx < EEPROM_SIZE; ++idx)
|
// XX ... FF ?? ??
|
||||||
|
for (idx = EEPROM_SIZE - 2; idx < EEPROM_SIZE; ++idx)
|
||||||
{
|
{
|
||||||
if (0xFF != eeprom_read_byte((uint8_t *) idx))
|
if (0xFF != eeprom_read_byte((uint8_t *) idx))
|
||||||
{
|
{
|
||||||
@ -850,6 +874,7 @@ uint16_t find_eeprom_idx()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// idx here is the byte that's not the FF prefix so reverse it by 1
|
||||||
idx += EEPROM_SIZE;
|
idx += EEPROM_SIZE;
|
||||||
--idx;
|
--idx;
|
||||||
idx %= EEPROM_SIZE;
|
idx %= EEPROM_SIZE;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user