CPS-UI: Make channels, zones and contacts 1-based to fix zone bound

This commit is contained in:
Federico Amedeo Izzo 2021-02-02 20:52:37 +01:00
parent 08a44375fc
commit 61ad0879c5
6 changed files with 41 additions and 30 deletions

View File

@ -40,7 +40,9 @@ void state_init()
state.v_bat = platform_getVbat();
state.charge = battery_getCharge(state.v_bat);
state.rssi = rtx_getRssi();
// Set default channel index (it is 1-based)
state.channel_index = 1;
// Set VFO channel default settings
state.channelInfoUpdated = true;
state.channel.mode = FM;

View File

@ -450,10 +450,11 @@ int _ui_fsm_loadChannel(uint16_t zone_index, bool *sync_rtx) {
{
// Calculate zone size
const uint8_t zone_size = sizeof(state.zone.member)/sizeof(state.zone.member[0]);
if(zone_index >= zone_size)
if((zone_index <= 0) || (zone_index > zone_size))
return -1;
else
channel_index = state.zone.member[zone_index];
// Channel index is 1-based while zone array access is 0-based
channel_index = state.zone.member[zone_index - 1];
}
int result = nvm_readChannelData(&channel, channel_index);
// Read successful and channel is valid
@ -847,20 +848,23 @@ void ui_updateFSM(event_t event, bool *sync_rtx)
if(state.ui_screen == MENU_ZONE)
{
zone_t zone;
// The index is -1 because the first zone "All channels" is not read from flash
if(nvm_readZoneData(&zone, ui_state.menu_selected) != -1)
// menu_selected is 0-based while channels are 1-based
// menu_selected == 0 corresponds to "All Channels" zone
if(nvm_readZoneData(&zone, ui_state.menu_selected + 1) != -1)
ui_state.menu_selected += 1;
}
else if(state.ui_screen == MENU_CHANNEL)
{
channel_t channel;
if(nvm_readChannelData(&channel, ui_state.menu_selected + 1) != -1)
// menu_selected is 0-based while channels are 1-based
if(nvm_readChannelData(&channel, ui_state.menu_selected + 2) != -1)
ui_state.menu_selected += 1;
}
else if(state.ui_screen == MENU_CONTACTS)
{
contact_t contact;
if(nvm_readContactData(&contact, ui_state.menu_selected + 1) != -1)
// menu_selected is 0-based while channels are 1-based
if(nvm_readContactData(&contact, ui_state.menu_selected + 2) != -1)
ui_state.menu_selected += 1;
}
}
@ -876,8 +880,7 @@ void ui_updateFSM(event_t event, bool *sync_rtx)
else
{
state.zone_enabled = true;
// The index is -1 because the first zone "All channels" is not read from flash
result = nvm_readZoneData(&newzone, ui_state.menu_selected - 1);
result = nvm_readZoneData(&newzone, ui_state.menu_selected);
}
if(result != -1)
{
@ -886,7 +889,7 @@ void ui_updateFSM(event_t event, bool *sync_rtx)
if(ui_state.last_main_state == MAIN_VFO)
state.vfo_channel = state.channel;
// Load zone first channel
_ui_fsm_loadChannel(0, sync_rtx);
_ui_fsm_loadChannel(1, sync_rtx);
// Switch to MEM screen
state.ui_screen = MAIN_MEM;
}
@ -896,7 +899,7 @@ void ui_updateFSM(event_t event, bool *sync_rtx)
// If we were in VFO mode, save VFO channel
if(ui_state.last_main_state == MAIN_VFO)
state.vfo_channel = state.channel;
_ui_fsm_loadChannel(ui_state.menu_selected, sync_rtx);
_ui_fsm_loadChannel(ui_state.menu_selected + 1, sync_rtx);
// Switch to MEM screen
state.ui_screen = MAIN_MEM;
}

View File

@ -155,7 +155,7 @@ int _ui_getZoneName(char *buf, uint8_t max_len, uint8_t index)
else
{
zone_t zone;
result = nvm_readZoneData(&zone, index - 1);
result = nvm_readZoneData(&zone, index);
if(result != -1)
snprintf(buf, max_len, "%s", zone.name);
}
@ -165,7 +165,7 @@ int _ui_getZoneName(char *buf, uint8_t max_len, uint8_t index)
int _ui_getChannelName(char *buf, uint8_t max_len, uint8_t index)
{
channel_t channel;
int result = nvm_readChannelData(&channel, index);
int result = nvm_readChannelData(&channel, index + 1);
if(result != -1)
snprintf(buf, max_len, "%s", channel.name);
return result;
@ -174,7 +174,7 @@ int _ui_getChannelName(char *buf, uint8_t max_len, uint8_t index)
int _ui_getContactName(char *buf, uint8_t max_len, uint8_t index)
{
contact_t contact;
int result = nvm_readContactData(&contact, index);
int result = nvm_readContactData(&contact, index + 1);
if(result != -1)
snprintf(buf, max_len, "%s", contact.name);
return result;

View File

@ -264,13 +264,14 @@ void nvm_loadHwInfo(hwInfo_t *info)
int nvm_readChannelData(channel_t *channel, uint16_t pos)
{
if(pos > maxNumChannels) return -1;
if((pos <= 0) || (pos > maxNumChannels)) return -1;
W25Qx_wakeup();
delayUs(5);
md3x0Channel_t chData;
uint32_t readAddr = chDataBaseAddr + pos * sizeof(md3x0Channel_t);
// Note: pos is 1-based because an empty slot in a zone contains index 0
uint32_t readAddr = chDataBaseAddr + (pos - 1) * sizeof(md3x0Channel_t);
W25Qx_readData(readAddr, ((uint8_t *) &chData), sizeof(md3x0Channel_t));
W25Qx_sleep();
@ -342,13 +343,14 @@ int nvm_readChannelData(channel_t *channel, uint16_t pos)
int nvm_readZoneData(zone_t *zone, uint16_t pos)
{
if(pos >= maxNumZones) return -1;
if((pos <= 0) || (pos > maxNumZones)) return -1;
W25Qx_wakeup();
delayUs(5);
md3x0Zone_t zoneData;
uint32_t zoneAddr = zoneBaseAddr + pos * sizeof(md3x0Zone_t);
// Note: pos is 1-based to be consistent with channels
uint32_t zoneAddr = zoneBaseAddr + (pos - 1) * sizeof(md3x0Zone_t);
W25Qx_readData(zoneAddr, ((uint8_t *) &zoneData), sizeof(md3x0Zone_t));
W25Qx_sleep();
@ -373,13 +375,14 @@ int nvm_readZoneData(zone_t *zone, uint16_t pos)
int nvm_readContactData(contact_t *contact, uint16_t pos)
{
if(pos >= maxNumContacts) return -1;
if((pos <= 0) || (pos > maxNumContacts)) return -1;
W25Qx_wakeup();
delayUs(5);
md3x0Contact_t contactData;
uint32_t contactAddr = contactBaseAddr + pos * sizeof(md3x0Contact_t);
// Note: pos is 1-based to be consistent with channels
uint32_t contactAddr = contactBaseAddr + (pos - 1) * sizeof(md3x0Contact_t);
W25Qx_readData(contactAddr, ((uint8_t *) &contactData), sizeof(md3x0Contact_t));
W25Qx_sleep();

View File

@ -286,13 +286,14 @@ void nvm_loadHwInfo(hwInfo_t *info)
int nvm_readChannelData(channel_t *channel, uint16_t pos)
{
if(pos >= maxNumChannels) return -1;
if((pos <= 0) || (pos > maxNumChannels)) return -1;
W25Qx_wakeup();
delayUs(5);
mduv3x0Channel_t chData;
uint32_t readAddr = chDataBaseAddr + pos * sizeof(mduv3x0Channel_t);
// Note: pos is 1-based because an empty slot in a zone contains index 0
uint32_t readAddr = chDataBaseAddr + (pos - 1) * sizeof(mduv3x0Channel_t);
W25Qx_readData(readAddr, ((uint8_t *) &chData), sizeof(mduv3x0Channel_t));
W25Qx_sleep();
@ -379,15 +380,16 @@ int nvm_readChannelData(channel_t *channel, uint16_t pos)
int nvm_readZoneData(zone_t *zone, uint16_t pos)
{
if(pos >= maxNumZones) return -1;
if((pos <= 0) || (pos > maxNumZones)) return -1;
W25Qx_wakeup();
delayUs(5);
mduv3x0Zone_t zoneData;
mduv3x0ZoneExt_t zoneExtData;
uint32_t zoneAddr = zoneBaseAddr + pos * sizeof(mduv3x0Zone_t);
uint32_t zoneExtAddr = zoneExtBaseAddr + pos * sizeof(mduv3x0ZoneExt_t);
// Note: pos is 1-based to be consistent with channels
uint32_t zoneAddr = zoneBaseAddr + (pos - 1) * sizeof(mduv3x0Zone_t);
uint32_t zoneExtAddr = zoneExtBaseAddr + (pos - 1) * sizeof(mduv3x0ZoneExt_t);
W25Qx_readData(zoneAddr, ((uint8_t *) &zoneData), sizeof(mduv3x0Zone_t));
W25Qx_readData(zoneExtAddr, ((uint8_t *) &zoneExtData), sizeof(mduv3x0ZoneExt_t));
W25Qx_sleep();
@ -418,13 +420,14 @@ int nvm_readZoneData(zone_t *zone, uint16_t pos)
int nvm_readContactData(contact_t *contact, uint16_t pos)
{
if(pos >= maxNumContacts) return -1;
if((pos <= 0) || (pos > maxNumContacts)) return -1;
W25Qx_wakeup();
delayUs(5);
mduv3x0Contact_t contactData;
uint32_t contactAddr = contactBaseAddr + pos * sizeof(mduv3x0Contact_t);
// Note: pos is 1-based to be consistent with channels
uint32_t contactAddr = contactBaseAddr + (pos - 1) * sizeof(mduv3x0Contact_t);
W25Qx_readData(contactAddr, ((uint8_t *) &contactData), sizeof(mduv3x0Contact_t));
W25Qx_sleep();

View File

@ -44,7 +44,7 @@ void nvm_loadHwInfo(hwInfo_t *info)
int nvm_readChannelData(channel_t *channel, uint16_t pos)
{
if(pos >= maxNumChannels) return -1;
if((pos <= 0) || (pos > maxNumChannels)) return -1;
/* Generate dummy channel name */
snprintf(channel->name, 16, "Channel %d", pos);
@ -57,7 +57,7 @@ int nvm_readChannelData(channel_t *channel, uint16_t pos)
int nvm_readZoneData(zone_t *zone, uint16_t pos)
{
if(pos >= maxNumZones) return -1;
if((pos <= 0) || (pos > maxNumZones)) return -1;
/* Generate dummy zone name */
snprintf(zone->name, 16, "Zone %d", pos);
@ -71,7 +71,7 @@ int nvm_readZoneData(zone_t *zone, uint16_t pos)
int nvm_readContactData(contact_t *contact, uint16_t pos)
{
if(pos >= maxNumContacts) return -1;
if((pos <= 0) || (pos > maxNumContacts)) return -1;
/* Generate dummy contact name */
snprintf(contact->name, 16, "Contact %d", pos);