diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2025-11-01 20:32:55 -0400 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2025-11-01 20:32:55 -0400 |
| commit | b71dd676b5ebe3feec1eb2194e20453a3d01705d (patch) | |
| tree | 10e19b6fa21d27c97376cdaf2bcfa03c05b2a75a /fw/table.c | |
| parent | 96a85a2bb8e52cb8b15745bca617e3cc788d02ad (diff) | |
| download | can-gauge-interface-b71dd676b5ebe3feec1eb2194e20453a3d01705d.zip | |
lookup output value in table
Diffstat (limited to 'fw/table.c')
| -rw-r--r-- | fw/table.c | 40 |
1 files changed, 26 insertions, 14 deletions
@@ -8,22 +8,22 @@ #include "table.h" Status -tabWrite(const Table *tab, U8 k, U16 key, U16 val) { +tabWrite(const Table *tab, U8 k, U32 key, U16 val) { if (k >= TAB_ROWS) { return FAIL; } U16 addr = tab->offset + k*TAB_ROW_SIZE; - U8 row[4u] = { - (key>>0u)&0xFF, (key>>8u)&0xFF, - (val>>0u)&0xFF, (val>>8u)&0xFF}; + U8 row[sizeof(key) + sizeof(val)] = { + (key>>0u)&0xFF, (key>>8u)&0xFF, (key>>16u)&0xFF, (key>>24u)&0xFF, // LE + (val>>0u)&0xFF, (val>>8u)&0xFF}; // LE return eepromWrite(addr, row, sizeof(row)); } Status -tabRead(const Table *tab, U8 k, U16 *key, U16 *val) { +tabRead(const Table *tab, U8 k, U32 *key, U16 *val) { U16 addr; - U8 row[4u]; + U8 row[sizeof(*key) + sizeof(*val)]; Status status; if (k >= TAB_ROWS) { @@ -32,28 +32,40 @@ tabRead(const Table *tab, U8 k, U16 *key, U16 *val) { addr = tab->offset + k*TAB_ROW_SIZE; status = eepromRead(addr, row, sizeof(row)); - *key = ((U16)row[0u]<<0u) | ((U16)row[1u]<<8u); - *val = ((U16)row[2u]<<0u) | ((U16)row[3u]<<8u); + *key = ((U32)row[0u]<<0u) | ((U32)row[1u]<<8u) | ((U32)row[2u]<<16u) | ((U32)row[3u]<<24u); // LE + *val = ((U16)row[4u]<<0u) | ((U16)row[5u]<<8u); // LE return status; } Status -tabLookup(const Table *tab, U16 key, U16 *val) { +tabLookup(const Table *tab, Number key, U16 *val) { U8 k; - U16 tkey, tval1, tval2; + Number tkey; + U16 tval1, tval2; Status status; + int ord; // Search for key for (k = 0u; (k < TAB_ROWS-1u); k++) { - status = tabRead(tab, k, &tkey, &tval1); + status = tabRead(tab, k, &tkey.u32, &tval1); if (status != OK) { return FAIL; } - if (key == tkey) { // found exact key + + status = u32ToNum(tkey.u32, key.type, &tkey); + if (status != OK) { + return FAIL; + } + + status = cmpNum(key, tkey, &ord); + if (status != OK) { + return FAIL; + } + if (ord == 0) { // found exact key *val = tval1; return OK; - } else if (key > tkey) { // interpolate - status = tabRead(tab, k+1u, &tkey, &tval2); + } else if (ord > 0) { // interpolate + status = tabRead(tab, k+1u, &tkey.u32, &tval2); if (status != OK) { return FAIL; } |