#ifndef CARD_H #define CARD_H #include "flash.h" #include class CardSlot { private: static constexpr size_t FLASH_SIZE_MB = 2; public: CardSlot(lib::Flash& flash, uint16_t card_index); void mark_unused(lib::Flash& flash); void set_name(const uint8_t* name, uint8_t length); void save_data(lib::Flash& flash); void erase(lib::Flash& flash); uint16_t get_image_start_page_index() const; bool is_used() const; uint32_t times_erased() const; const uint8_t* get_name() const; uint8_t get_name_length() const; bool operator<=(const CardSlot&) const; bool operator>=(const CardSlot&) const; void each_pixel(lib::Flash& flash, void (*fn)(void*, uint8_t), void* userdata); static int32_t get_unused(lib::Flash& flash); private: static constexpr size_t IMAGE_LENGTH = 320 * 240 * 3; static constexpr size_t NAME_BITS = 5; static constexpr size_t MAX_NAME_LENGTH = 1 << NAME_BITS; struct { bool in_use : 1; uint32_t times_erased : 26; uint8_t name_length : NAME_BITS; } status; uint8_t name[MAX_NAME_LENGTH]; uint16_t card_index; void load_data(lib::Flash& flash); public: static constexpr size_t IMAGE_PAGE_COUNT = IMAGE_LENGTH / lib::Flash::Page::SIZE; // round up to nearest multiple of 16 since // that's how many pages there are per sector static constexpr size_t TOTAL_PAGE_COUNT = 16 * ((IMAGE_PAGE_COUNT + 16) / 16); // based on flash size static constexpr uint16_t MAX_CARDS = FLASH_SIZE_MB * 1024 * 1024 / (lib::Flash::Page::SIZE * TOTAL_PAGE_COUNT); }; #endif // CARD_H