diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2025-10-24 16:55:59 -0400 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2025-10-24 16:55:59 -0400 |
| commit | 01bd9cbc6fe64f236a1b467f011e56dcae0306e7 (patch) | |
| tree | dc74cfa742d77befcbe58715e3ae7a2727c89781 /fw/table.h | |
| parent | a0c6bbb21c5e7d9e0090e66c316cf88ef7f0726b (diff) | |
| download | can-gauge-interface-01bd9cbc6fe64f236a1b467f011e56dcae0306e7.zip | |
table module
Diffstat (limited to 'fw/table.h')
| -rw-r--r-- | fw/table.h | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/fw/table.h b/fw/table.h new file mode 100644 index 0000000..91e981a --- /dev/null +++ b/fw/table.h @@ -0,0 +1,41 @@ +/* Table datastructure for storing calibrations in the EEPROM. + * + * A table has a fixed number of rows that define a key/value mapping. + * Keys and values are each U16. + * T: U16->U16 + * Numbers are stored little-endian. + * + * Keys must be monotonically increasing. + * + * Device: PIC16F1459 + * Compiler: XC8 v3.00 + * + * Usage: + * + * #include <stdint.h> + * #include "types.h" + * #include "table.h" + */ + +enum { + TAB_KEY_SIZE = 2, // U16 + TAB_VAL_SIZE = 2, // U16 + TAB_ROWS = 32, + TAB_ROW_SIZE = TAB_KEY_SIZE + TAB_VAL_SIZE, + TAB_SIZE = TAB_ROWS * TAB_ROW_SIZE, +}; + +typedef struct { + U16 offset; // starting address +} Table; + +// Set the key and value of row k. +Status tabWrite(const Table *tab, U8 k, U16 key, U16 val); + +// Read row k. +Status tabRead(const Table *tab, U8 k, U16 *key, U16 *val); + +// Lookup the value associated with given key. +// If key falls between two rows, the value is interpolated +// from the two adjacent. +Status tabLookup(const Table *tab, U16 key, U16 *val); |