Esprit.h
4.19 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/****************************************************************************
* Arduino Helper Library for Freematics Esprit Development Board
* Distributed under BSD license
* Visit http://freematics.com/products/freematics-esprit for more information
* Developed by Stanley Huang <stanley@freematics.com.au>
****************************************************************************/
#ifndef Esprit_h
#define Esprit_h
#define PIN_LED 4
#define D0 16
#define D1 17
#define D2 2
#define D3 27
#define D4 26
#define D5 25
#define D6 15
#define D7 13
#define D8 12
#define D9 14
#define D10 5
#define D11 23
#define D12 19
#define D13 18
#define A0 36
#define A1 35
#define A2 34
#define A3 39
#define A4 32
#define A5 33
#ifndef _SIM
#include "esp_system.h"
#include "esp_partition.h"
#include "nvs_flash.h"
#include "nvs.h"
#include "esp_spi_flash.h"
#include "FreematicsPlus.h"
extern HardwareSerial Serial1;
extern HardwareSerial Serial2;
#else
#define SIM_FLASH_SIZE 4 * 1024 * 1024
#endif
#define FLASH_STORAGE_BASE_ADDR 0x100000
class CNVS
{
public:
#ifdef _SIM
CNVS(const char* flashFilePath, size_t flashSize)
{
FILE* fp = fopen(flashFilePath, "rb+");
size_t size = 0;
if (fp) {
fseek(fp, 0, SEEK_END);
size = ftell(fp);
}
if (size != flashSize) {
if (fp) fclose(fp);
fp = fopen(flashFilePath, "wb+");
char c = 0;
for (size_t t = 0; t < flashSize; t++) {
fputc(c, fp);
}
}
if (fp) {
fseek(fp, 0, SEEK_SET);
m_flashSize = flashSize;
m_fpFlash = fp;
}
else {
m_fpFlash = 0;
m_flashSize = 0;
}
}
#endif
bool init(const char* ns = "storage")
{
#ifndef _SIM
m_flashSize = spi_flash_get_chip_size();
return nvs_open(ns, NVS_READWRITE, &m_handle) == ESP_OK;
#endif
}
#ifndef _SIM
bool set(const char* key, int8_t value) { return nvs_set_i8(m_handle, key, value) == ESP_OK; }
bool set(const char* key, uint8_t value) { return nvs_set_u8(m_handle, key, value) == ESP_OK; }
bool set(const char* key, int16_t value) { return nvs_set_i16(m_handle, key, value) == ESP_OK; }
bool set(const char* key, uint16_t value) { return nvs_set_u16(m_handle, key, value) == ESP_OK; }
bool set(const char* key, int32_t value) { return nvs_set_i32(m_handle, key, value) == ESP_OK; }
bool set(const char* key, uint32_t value) { return nvs_set_u32(m_handle, key, value) == ESP_OK; }
bool set(const char* key, void* value, size_t len) { return nvs_set_blob(m_handle, key, value, len) == ESP_OK; }
bool set(const char* key, const char* value) { return nvs_set_str(m_handle, key, value) == ESP_OK; }
bool get(const char* key, int8_t& value) { return nvs_get_i8(m_handle, key, &value) == ESP_OK; }
bool get(const char* key, uint8_t& value) { return nvs_get_u8(m_handle, key, &value) == ESP_OK; }
bool get(const char* key, int16_t& value) { return nvs_get_i16(m_handle, key, &value) == ESP_OK; }
bool get(const char* key, uint16_t& value) { return nvs_get_u16(m_handle, key, &value) == ESP_OK; }
bool get(const char* key, int32_t& value) { return nvs_get_i32(m_handle, key, &value) == ESP_OK; }
bool get(const char* key, uint32_t& value) { return nvs_get_u32(m_handle, key, &value) == ESP_OK; }
bool get(const char* key, void* buffer, size_t& len) { return nvs_get_blob(m_handle, key, buffer, &len) == ESP_OK; }
bool get(const char* key, char* buffer, size_t& len) { return nvs_get_str(m_handle, key, buffer, &len) == ESP_OK; }
bool erase() { return nvs_erase_all(m_handle); }
bool erase(const char* key) { return nvs_erase_key(m_handle, key); }
bool commit() { return nvs_commit(m_handle); }
#endif
bool flashRead(void* buffer, size_t bufsize, size_t offet = -1);
bool flashWrite(void* buffer, size_t bufsize, size_t offet = -1);
size_t getFlashSize() { return m_flashSize; }
private:
#ifndef _SIM
nvs_handle m_handle;
uint32_t m_offset;
#else
FILE* m_fpFlash;
#endif
size_t m_flashSize;
};
#ifndef _SIM
class GATTServer
{
public:
bool init(const char* deviceName = 0);
bool send(uint8_t* data, size_t len);
virtual size_t onRequest(uint8_t* buffer, size_t len)
{
// being requested for data
buffer[0] = 'O';
buffer[1] = 'K';
return 2;
}
virtual void onReceive(uint8_t* buffer, size_t len)
{
// data received is in buffer
}
};
#endif
#endif