Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
joeinman committed Aug 5, 2023
1 parent e29968a commit af54891
Showing 1 changed file with 14 additions and 54 deletions.
68 changes: 14 additions & 54 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,104 +11,64 @@ FlashKV is a lightweight, hardware agnostic key-value store for flash memory. It

## Basic Example:

A basic example of using the library with an RP2040 microcontroller is shown below.

```cpp
// Includes
#include <hardware/flash.h>
#include <pico/stdlib.h>
#include <tusb.h>

#include <FlashKV/FlashKV.h>

#include <iostream>
#include <optional>
#include <iomanip>
#include <vector>

// Address Of The FlashKV Store In Flash Memory
#define FLASHKV_BASE XIP_BASE + (1024 * 1040)
#define FLASHKV_SIZE FLASH_SECTOR_SIZE * 2
// Define the size of the FlashKV store
#define FLASHKV_SIZE 4096
#define FLASHKV_BASE 0

int main()
{
stdio_init_all();
while (!tud_cdc_connected())
tight_loop_contents();

// Create a FlashKV object.
// Create FlashKV Object
FlashKV::FlashKV flashKV(
[](uint32_t flashAddress, const uint8_t *data, size_t count) -> bool
{
flash_range_program(flashAddress - XIP_BASE, data, count);
[](uint32_t flashAddress, const uint8_t *data, size_t count) -> bool {
// Implement Flash Write Function
return true;
},
[](uint32_t flashAddress, uint8_t *data, size_t count) -> bool
{
memcpy(data, reinterpret_cast<uint8_t *>(flashAddress), count);
[](uint32_t flashAddress, uint8_t *data, size_t count) -> bool {
// Implement Flash Read Function
return true;
},
[](uint32_t flashAddress, size_t count) -> bool
{
sleep_ms(100);
flash_range_erase(flashAddress - XIP_BASE, count);
[](uint32_t flashAddress, size_t count) -> bool {
// Implement Flash Erase Function
return true;
},
FLASH_PAGE_SIZE,
FLASH_SECTOR_SIZE,
FLASHKV_BASE,
FLASHKV_SIZE);
);

// Load The Store From Flash
int loadStatus = flashKV.loadStore();
if (loadStatus == 0)
std::cout << "FlashKV Store Loaded Successfully" << std::endl;
else if (loadStatus == 1)
std::cout << "No FlashKV Store Found. A New One Will Be Created Upon Saving" << std::endl;
else
std::cout << "Error Occurred While Loading The FlashKV Store" << std::endl;

// Try To Read A Value From The Store
std::string key = "test";
std::optional<std::vector<uint8_t>> value = flashKV.readKey(key);
if (value)
{
std::cout << "Read Key: [" << key << "], Value: [";
for (auto it = value->begin(); it != value->end(); ++it)
{
if (it != value->begin())
std::cout << " ";
std::cout << std::hex << std::setw(2) << std::setfill('0') << (int)*it;
}
std::cout << "]" << std::endl;
}
std::cout << "Key [" << key << "] Found." << std::endl;
else
{
std::cout << "Key [" << key << "] Not Found." << std::endl;
}

// Write A Value To The Store
std::vector<uint8_t> data = {0x01, 0x02, 0x03, 0x04};
flashKV.writeKey(key, data);

std::cout << "Writing Key: [" << key << "], Value: [";
for (auto it = data.begin(); it != data.end(); ++it)
{
if (it != data.begin())
std::cout << " ";
std::cout << std::hex << std::setw(2) << std::setfill('0') << (int)*it;
}
std::cout << "]" << std::endl;
std::cout << "Writing Key: [" << key << "]" << std::endl;

// Save The Store To Flash
if (flashKV.saveStore())
std::cout << "FlashKV Store Saved Successfully" << std::endl;
else
std::cout << "Error Occurred While Saving The FlashKV Store" << std::endl;

while (1)
{
tight_loop_contents();
}
return 0;
}
```

0 comments on commit af54891

Please sign in to comment.