summaryrefslogtreecommitdiff
path: root/src/storage.h
blob: fd7b0ad75ca17b9f91981e155cc6dce98700f330 (plain)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <stdlib.h>

#define I2C_EEPROM_ADDR 0x50

class Storage {
	private:
		uint8_t num_anims;
		uint8_t first_free_page;
		void i2c_start_write(void);
		void i2c_start_read(void);
		void i2c_stop(void);
		int8_t i2c_send(uint8_t len, uint8_t *data);
		int8_t i2c_receive(uint8_t len, uint8_t *data);
		int8_t i2c_read(uint16_t addr, uint8_t len, uint8_t *data);
		int8_t i2c_write(uint16_t addr, uint8_t len, uint8_t *data);
	public:
		Storage() { num_anims = 0; first_free_page = 0;};

		/**
		 * Enable the storage hardware: Configures the internal I2C
		 * module and reads the number of stored animations from the
		 * EEPROM.
		 */
		void enable();

		/**
		 * Prepares the storage for a complete overwrite by setting the
		 * number of stored animations to zero. The next save operation
		 * will get pattern id 0 and overwrite the first stored pattern.
		 *
		 * This function itself does not write anything to the EEPROM.
		 */
		void reset();

		/**
		 * Load pattern from EEPROM.
		 *
		 * @param idx pattern index
		 * @param data pointer to data structure for the pattern. Must be
		 *        at least 256 Bytes
		 */
		void load(uint16_t idx, uint8_t *data);

		/**
		 * Save (possibly partial) pattern on the EEPROM. 64 bytes of
		 * dattern data will be read and stored, regardless of the
		 * pattern header.
		 *
		 * @param data pattern data. Must be at least 64 Bytes
		 */
		void save(uint8_t *data);

		/**
		 * Continue saving a pattern on the EEPROM. Appends 64 bytes of
		 * pattern data after the most recently written block of data
		 * (i.e., to the pattern which is currently being saved).
		 *
		 * @param data pattern data. Must be at least 64 Bytes
		 */
		void append(uint8_t *data);
};

extern Storage storage;