diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2025-10-27 20:25:28 -0400 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2025-10-27 20:25:28 -0400 |
| commit | 3907711d2abca4277e1dbe01b98e0c7f3cc1304e (patch) | |
| tree | 51bf124b217ac5f5f29314832a9937046ad04198 /fw/table.c | |
| parent | 8b7701ed93ac19ac386c4de460e1c3424a260262 (diff) | |
| download | can-gauge-interface-3907711d2abca4277e1dbe01b98e0c7f3cc1304e.zip | |
use builtin int types
Diffstat (limited to 'fw/table.c')
| -rw-r--r-- | fw/table.c | 21 |
1 files changed, 10 insertions, 11 deletions
@@ -15,8 +15,10 @@ tabWrite(const Table *tab, U8 k, U16 key, U16 val) { return FAIL; } - U16 addr = addU16U8(tab->offset, k*TAB_ROW_SIZE); - U8 row[4u] = {key.lo, key.hi, val.lo, val.hi}; + U16 addr = tab->offset + k*TAB_ROW_SIZE; + U8 row[4u] = { + (key>>0u)&0xFF, (key>>8u)&0xFF, + (val>>0u)&0xFF, (val>>8u)&0xFF}; return eepromWrite(addr, row, sizeof(row)); } @@ -30,10 +32,10 @@ tabRead(const Table *tab, U8 k, U16 *key, U16 *val) { return FAIL; } - addr = addU16U8(tab->offset, k*TAB_ROW_SIZE); + addr = tab->offset + k*TAB_ROW_SIZE; status = eepromRead(addr, row, sizeof(row)); - *key = (U16){.lo=row[0u], .hi=row[1u]}; - *val = (U16){.lo=row[2u], .hi=row[3u]}; + *key = ((U16)row[0u]<<0u) | ((U16)row[1u]<<8u); + *val = ((U16)row[2u]<<0u) | ((U16)row[3u]<<8u); return status; } @@ -42,7 +44,6 @@ tabLookup(const Table *tab, U16 key, U16 *val) { U8 k; U16 tkey, tval1, tval2; Status status; - I8 ord; // Search for key for (k = 0u; (k < TAB_ROWS-1u); k++) { @@ -50,17 +51,15 @@ tabLookup(const Table *tab, U16 key, U16 *val) { if (status != OK) { return FAIL; } - ord = cmpU16(key, tkey); - if (ord == 0u) { // found exact key + if (key == tkey) { // found exact key *val = tval1; return OK; - } else if (ord > 0u) { // interpolate + } else if (key > tkey) { // interpolate status = tabRead(tab, k+1u, &tkey, &tval2); if (status != OK) { return FAIL; } - // Mean: (tval1+tval2)/2 - *val = rshiftU16(addU16(tval1, tval2), 1u); + *val = (tval1 + tval2) / 2u; return OK; } else { // less // continue |