aboutsummaryrefslogtreecommitdiffstats
path: root/fw/table.h
blob: c1cc03d7ec090e7f84451b53cabacd507b0827ed (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/* 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 32-bit and 16-bit respectively.
 *
 * Keys may be signed or unsigned; whether they are interpreted as
 * signed or unsigned when they are read depends on the context. They
 * must be monotonically increasing down the table.
 *
 * Values are unsigned 16-bit integers.
 *
 * Numbers are stored big-endian.
 *
 * See also: `doc/datafmt.pdf'
 *
 * Device: PIC16F1459
 * Compiler: XC8 v3.00
 *
 * Usage:
 *
 * #include <stdbool.h>
 * #include <stdint.h>
 * #include "types.h"
 * #include "can.h"
 * #include "eeprom.h"
 * #include "table.h"
 */

enum {
	TAB_KEY_SIZE = sizeof(U32),
	TAB_VAL_SIZE = sizeof(U16),
	TAB_ROWS = 32,
	TAB_ROW_SIZE = TAB_KEY_SIZE + TAB_VAL_SIZE,
	TAB_SIZE = TAB_ROWS * TAB_ROW_SIZE,
};

typedef struct {
	EepromAddr offset; // starting address
} Table;

// Set the key and value of row k.
Status tabWrite(const Table *tab, U8 k, U32 key, U16 val);

// Read row k.
Status tabRead(const Table *tab, U8 k, U32 *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, Number key, U16 *val);