moved to configurable components, added SSD1306 support

This commit is contained in:
Lurkars
2020-07-15 22:22:54 +02:00
parent 1eedea1b9a
commit eaf1c74faa
35 changed files with 1577 additions and 621 deletions
+9
View File
@@ -0,0 +1,9 @@
idf_component_register(
SRCS
"interface.c"
"interface-datetime.c"
"interface-menu.c"
INCLUDE_DIRS "include"
PRIV_REQUIRES
ena
)
@@ -0,0 +1,31 @@
// Copyright 2020 Lukas Haubaum
//
// Licensed under the GNU Affero General Public License, Version 3;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// https://www.gnu.org/licenses/agpl-3.0.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef _ena_INTERFACE_DATETIME_H_
#define _ena_INTERFACE_DATETIME_H_
typedef enum
{
ENA_INTERFACE_DATETIME_STATE_YEAR = 0,
ENA_INTERFACE_DATETIME_STATE_MONTH,
ENA_INTERFACE_DATETIME_STATE_DAY,
ENA_INTERFACE_DATETIME_STATE_HOUR,
ENA_INTERFACE_DATETIME_STATE_MINUTE,
ENA_INTERFACE_DATETIME_STATE_SECONDS,
} ena_inerface_datetime_state;
void ena_interface_datetime_start(void);
int ena_interface_datetime_state(void);
#endif
@@ -0,0 +1,28 @@
// Copyright 2020 Lukas Haubaum
//
// Licensed under the GNU Affero General Public License, Version 3;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// https://www.gnu.org/licenses/agpl-3.0.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef _ena_INTERFACE_MENU_H_
#define _ena_INTERFACE_MENU_H_
typedef enum
{
ENA_INTERFACE_MENU_STATE_IDLE = 0,
ENA_INTERFACE_MENU_STATE_SELECT_TIME,
ENA_INTERFACE_MENU_STATE_SELECT_INFO,
} ena_interface_menu_state;
void ena_interface_menu_start(void);
int ena_interface_menu_get_state(void);
#endif
@@ -0,0 +1,78 @@
// Copyright 2020 Lukas Haubaum
//
// Licensed under the GNU Affero General Public License, Version 3;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// https://www.gnu.org/licenses/agpl-3.0.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef _ena_INTERFACE_H_
#define _ena_INTERFACE_H_
#define ENA_INTERFACE_LOG "ESP-ENA-interface" // TAG for Logging
#define TOUCHPAD_FILTER_TOUCH_PERIOD (10)
#define TOUCH_PAD_COUNT (4)
#define TOUCH_PAD_ESC (TOUCH_PAD_NUM0)
#define TOUCH_PAD_OK (TOUCH_PAD_NUM6)
#define TOUCH_PAD_UP (TOUCH_PAD_NUM4)
#define TOUCH_PAD_DOWN (TOUCH_PAD_NUM3)
/**
* @brief different interface states
*/
typedef enum
{
ENA_INTERFACE_STATE_IDLE = 0, // ilde state, do nothing
ENA_INTERFACE_STATE_MENU, // main menu
ENA_INTERFACE_STATE_SET_YEAR, // set current year
ENA_INTERFACE_STATE_SET_MONTH, // set current month
ENA_INTERFACE_STATE_SET_DAY, // set current day
ENA_INTERFACE_STATE_SET_HOUR, // set current hour
ENA_INTERFACE_STATE_SET_MINUTE, // set current minute
ENA_INTERFACE_STATE_SET_SECONDS, // set current second
ENA_INTERFACE_STATE_STATUS, // view current status
} ena_interface_state;
/**
* @brief callback function on touch event
*/
typedef void (*ena_interface_touch_callback)(void);
/**
* @brief register a callback function for touch event
*
* @param[in] touch_pad id of the touchpad to listen touch
* @param[in] callback callback function
*/
void ena_interface_register_touch_callback(int touch_pad, ena_interface_touch_callback callback);
/**
* @brief get current interface state
*
* @return
* current state the interface is in
*/
int ena_interface_get_state(void);
/**
* @brief set current interface state
*
* @param[in] state new state to set
*/
void ena_interface_set_state(ena_interface_state state);
/**
* @brief start interface logic
*
* This will initialize the touch controls and start a task to listen to touch
* inputs and calling the callbacks
*/
void ena_interface_start(void);
#endif
@@ -0,0 +1,40 @@
// Copyright 2020 Lukas Haubaum
//
// Licensed under the GNU Affero General Public License, Version 3;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// https://www.gnu.org/licenses/agpl-3.0.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <stdio.h>
#include "esp_log.h"
#include "ena-interface.h"
#include "driver/touch_pad.h"
#include "ena-interface-menu.h"
#include "ena-interface-datetime.h"
static int interface_datetime_state = ENA_INTERFACE_DATETIME_STATE_YEAR;
void ena_interface_datetime_esc(void)
{
ena_interface_menu_start();
}
void ena_interface_datetime_start(void)
{
ena_interface_set_state(ENA_INTERFACE_STATE_SET_YEAR);
ena_interface_register_touch_callback(TOUCH_PAD_ESC, &ena_interface_datetime_esc);
ESP_LOGD(ENA_INTERFACE_LOG, "start datetime interface");
}
int ena_interface_datetime_state(void)
{
return interface_datetime_state;
}
+66
View File
@@ -0,0 +1,66 @@
// Copyright 2020 Lukas Haubaum
//
// Licensed under the GNU Affero General Public License, Version 3;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// https://www.gnu.org/licenses/agpl-3.0.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <stdio.h>
#include "esp_log.h"
#include "driver/touch_pad.h"
#include "ena-interface.h"
#include "ena-interface-datetime.h"
#include "ena-interface-menu.h"
static int interface_menu_state = ENA_INTERFACE_MENU_STATE_IDLE;
void ena_interface_menu_ok(void)
{
if (interface_menu_state == ENA_INTERFACE_MENU_STATE_SELECT_TIME) {
ena_interface_datetime_start();
}
}
void ena_interface_menu_up(void)
{
interface_menu_state--;
if (interface_menu_state < 0)
{
interface_menu_state = sizeof(interface_menu_state) - 1;
}
ESP_LOGD(ENA_INTERFACE_LOG, "menu up to %d", interface_menu_state);
}
void ena_interface_menu_down(void)
{
interface_menu_state++;
if (interface_menu_state == sizeof(interface_menu_state))
{
interface_menu_state = 0;
}
ESP_LOGD(ENA_INTERFACE_LOG, "menu down to %d", interface_menu_state);
}
void ena_interface_menu_start(void)
{
ena_interface_set_state(ENA_INTERFACE_STATE_MENU);
ena_interface_register_touch_callback(TOUCH_PAD_ESC, NULL);
ena_interface_register_touch_callback(TOUCH_PAD_OK, &ena_interface_menu_ok);
ena_interface_register_touch_callback(TOUCH_PAD_UP, &ena_interface_menu_up);
ena_interface_register_touch_callback(TOUCH_PAD_DOWN, &ena_interface_menu_down);
ESP_LOGD(ENA_INTERFACE_LOG, "start menu interface");
}
int ena_interface_menu_get_state(void)
{
return interface_menu_state;
}
+102
View File
@@ -0,0 +1,102 @@
// Copyright 2020 Lukas Haubaum
//
// Licensed under the GNU Affero General Public License, Version 3;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// https://www.gnu.org/licenses/agpl-3.0.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/touch_pad.h"
#include "esp_log.h"
#include "ena-interface.h"
static int interface_state = ENA_INTERFACE_STATE_IDLE;
static int touch_mapping[TOUCH_PAD_COUNT] = {0};
static bool touch_status[TOUCH_PAD_COUNT] = {0};
static ena_interface_touch_callback touch_callbacks[TOUCH_PAD_MAX];
void ena_interface_register_touch_callback(int touch_pad, ena_interface_touch_callback callback)
{
touch_callbacks[touch_pad] = callback;
}
void ena_interface_run(void *pvParameter)
{
uint16_t touch_value;
uint16_t touch_thresh;
bool touch_status_current[4] = {0};
while (1)
{
for (int i = 0; i < TOUCH_PAD_COUNT; i++)
{
int touch_id = touch_mapping[i];
ESP_ERROR_CHECK_WITHOUT_ABORT(touch_pad_read_filtered(touch_id, &touch_value));
ESP_ERROR_CHECK_WITHOUT_ABORT(touch_pad_get_thresh(touch_id, &touch_thresh));
touch_status_current[i] = touch_value < touch_thresh;
if (!touch_status[i] & touch_status_current[i])
{
ESP_LOGD(ENA_INTERFACE_LOG, "touch %u at %d (thresh %u)", touch_value, touch_id, touch_thresh);
if (touch_callbacks[touch_id] != NULL)
{
(*touch_callbacks[touch_id])();
}
}
touch_status[i] = touch_status_current[i];
}
vTaskDelay(10 / portTICK_PERIOD_MS);
}
}
void ena_interface_start(void)
{
ESP_ERROR_CHECK(touch_pad_init());
ESP_ERROR_CHECK(touch_pad_set_voltage(TOUCH_HVOLT_2V7, TOUCH_LVOLT_0V5, TOUCH_HVOLT_ATTEN_1V));
ESP_ERROR_CHECK(touch_pad_set_trigger_mode(TOUCH_TRIGGER_BELOW));
touch_mapping[0] = TOUCH_PAD_ESC;
touch_mapping[1] = TOUCH_PAD_OK;
touch_mapping[2] = TOUCH_PAD_UP;
touch_mapping[3] = TOUCH_PAD_DOWN;
for (int i = 0; i < TOUCH_PAD_COUNT; i++)
{
int touch_id = touch_mapping[i];
ESP_ERROR_CHECK(touch_pad_config(touch_id, 0));
}
ESP_ERROR_CHECK(touch_pad_filter_start(TOUCHPAD_FILTER_TOUCH_PERIOD));
uint16_t touch_value;
for (int i = 0; i < TOUCH_PAD_COUNT; i++)
{
int touch_id = touch_mapping[i];
ESP_ERROR_CHECK(touch_pad_read_filtered(touch_id, &touch_value));
ESP_ERROR_CHECK(touch_pad_set_thresh(touch_id, touch_value * 2 / 3));
ESP_LOGD(ENA_INTERFACE_LOG, "calibrate %u at %u (thresh %u)", touch_id, touch_value, (touch_value * 2 / 3));
}
xTaskCreate(&ena_interface_run, "ena_interface_run", configMINIMAL_STACK_SIZE * 4, NULL, 5, NULL);
}
int ena_interface_get_state(void)
{
return interface_state;
}
void ena_interface_set_state(ena_interface_state state)
{
interface_state = state;
}
+14
View File
@@ -0,0 +1,14 @@
idf_component_register(
SRCS
"beacons.c"
"bluetooth-advertise.c"
"bluetooth-scan.c"
"crypto.c"
"ena.c"
"storage.c"
INCLUDE_DIRS "include"
PRIV_REQUIRES
spi_flash
mbedtls
bt
)
+85
View File
@@ -0,0 +1,85 @@
menu "Exposure Notification API"
menu "Storage"
config ENA_STORAGE_DUMP
bool "Dump storage"
default false
help
Dump storage (stored TEKs, temp. beacons and perm. beacons) to serial output after scan.
config ENA_STORAGE_TEK_MAX
int "Max. TEKs"
default 14
help
Defines the maximum number of TEKs to be stored. (Default 14 [14 * 144 => 14 days])
config ENA_STORAGE_TEMP_BEACONS_MAX
int "Max. temporary beacons"
default 1000
help
Defines the maximum number of temporary beacons to be stored. (Default 1000)
config ENA_STORAGE_START_ADDRESS
int "Storage start address"
default 0
help
Defines the start address on partition. (Default 0)
config ENA_STORAGE_PARTITION_NAME
string "Partition name"
default "ena"
help
Name of the partition used for storage. (Default "ena", see partitions.csv)
config ENA_STORAGE_ERASE
bool "Erase storage (!)"
default false
help
Erases the complete(!) partition on startup and reset counters.
endmenu
menu "Scanning"
config ENA_BEACON_TRESHOLD
int "Contact threshold"
default 300
help
Threshold in seconds after a received beacon is stored permanently. (Default 5 minutes)
config ENA_SCANNING_TIME
int "Scanning time"
default 30
help
Time in seconds how long a scan should run. (Default 30 seconds)
config ENA_SCANNING_INTERVAL
int "Scanning interval"
default 300
help
Interval in seconds for the next scan to happen. (Default 5 minutes)
endmenu
menu "Advertising"
config ENA_BT_ROTATION_TIMEOUT_INTERVAL
int "Rotation timeout interval"
default 900
help
Base rotation timeout interval in seconds for changing BT address and therefore the advertised beacon. (Default 5 minutes)
config ENA_BT_RANDOMIZE_ROTATION_TIMEOUT_INTERVAL
int "Randomize rotation timeout interval"
default 150
help
Range in seconds for randomize the rotation timeout interval. (Default +/- ~2.5 minutes)
config ENA_TEK_ROLLING_PERIOD
int "TEK rolling period"
default 144
help
Defines the TEK rolling period in 10 minute steps. (Default 144 => 24 hours)
endmenu
endmenu
+119
View File
@@ -0,0 +1,119 @@
// Copyright 2020 Lukas Haubaum
//
// Licensed under the GNU Affero General Public License, Version 3;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// https://www.gnu.org/licenses/agpl-3.0.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <string.h>
#include "esp_log.h"
#include "ena-crypto.h"
#include "ena-storage.h"
#include "ena-beacons.h"
static uint32_t temp_beacons_count = 0;
static ena_temp_beacon_t temp_beacons[ENA_STORAGE_TEMP_BEACONS_MAX];
ena_beacon_t ena_beacons_convert(ena_temp_beacon_t temp_beacon)
{
ena_beacon_t beacon;
memcpy(beacon.rpi, temp_beacon.rpi, ENA_KEY_LENGTH);
memcpy(beacon.aem, temp_beacon.aem, ENA_AEM_METADATA_LENGTH);
beacon.timestamp = temp_beacon.timestamp_last;
beacon.rssi = temp_beacon.rssi;
return beacon;
}
int ena_get_temp_beacon_index(uint8_t *rpi, uint8_t *aem)
{
for (int i = 0; i < temp_beacons_count; i++)
{
if (memcmp(temp_beacons[i].rpi, rpi, sizeof(ENA_KEY_LENGTH)) == 0 &&
memcmp(temp_beacons[i].aem, aem, sizeof(ENA_AEM_METADATA_LENGTH)) == 0)
{
return i;
}
}
return -1;
}
void ena_beacons_temp_refresh(uint32_t unix_timestamp)
{
for (int i = temp_beacons_count - 1; i >= 0; i--)
{
// check for treshold and add permanent beacon
if (temp_beacons[i].timestamp_last - temp_beacons[i].timestamp_first >= ENA_BEACON_TRESHOLD)
{
ESP_LOGD(ENA_BEACON_LOG, "create beacon after treshold");
ESP_LOG_BUFFER_HEXDUMP(ENA_BEACON_LOG, temp_beacons[i].rpi, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
ena_beacon_t beacon = ena_beacons_convert(temp_beacons[i]);
ena_storage_add_beacon(&beacon);
ena_storage_remove_temp_beacon(i);
}
else
// delete temp beacons older than two times time window (two times to be safe, one times time window enough?!)
if (unix_timestamp - temp_beacons[i].timestamp_last > (ENA_TIME_WINDOW * 2))
{
ESP_LOGD(ENA_BEACON_LOG, "remove old temporary beacon %u", i);
ena_storage_remove_temp_beacon(i);
}
}
// update beacons
temp_beacons_count = ena_storage_temp_beacons_count();
for (int i = 0; i < temp_beacons_count; i++)
{
ena_storage_get_temp_beacon(i, &temp_beacons[i]);
}
#if (CONFIG_ENA_STORAGE_DUMP)
// DEBUG dump
ena_storage_dump_tek();
ena_storage_dump_temp_beacons();
ena_storage_dump_beacons();
#endif
}
void ena_beacon(uint32_t unix_timestamp, uint8_t *rpi, uint8_t *aem, int rssi)
{
uint32_t beacon_index = ena_get_temp_beacon_index(rpi, aem);
if (beacon_index == -1)
{
temp_beacons[temp_beacons_count].timestamp_first = unix_timestamp;
memcpy(temp_beacons[temp_beacons_count].rpi, rpi, ENA_KEY_LENGTH);
memcpy(temp_beacons[temp_beacons_count].aem, aem, ENA_AEM_METADATA_LENGTH);
temp_beacons[temp_beacons_count].rssi = rssi;
temp_beacons[temp_beacons_count].timestamp_last = unix_timestamp;
beacon_index = ena_storage_add_temp_beacon(&temp_beacons[temp_beacons_count]);
ESP_LOGD(ENA_BEACON_LOG, "New temporary beacon at %d with timestamp %u", temp_beacons_count, unix_timestamp);
ESP_LOG_BUFFER_HEX_LEVEL(ENA_BEACON_LOG, rpi, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
ESP_LOG_BUFFER_HEX_LEVEL(ENA_BEACON_LOG, aem, ENA_AEM_METADATA_LENGTH, ESP_LOG_DEBUG);
ESP_LOGD(ENA_BEACON_LOG, "RSSI %d", rssi);
if (beacon_index != temp_beacons_count)
{
ESP_LOGW(ENA_BEACON_LOG, "last temporary beacon index does not match array index!");
}
temp_beacons_count++;
}
else
{
temp_beacons[beacon_index].rssi = (temp_beacons[beacon_index].rssi + rssi) / 2;
temp_beacons[beacon_index].timestamp_last = unix_timestamp;
ESP_LOGD(ENA_BEACON_LOG, "New Timestamp for temporary beacon %d: %u", beacon_index, unix_timestamp);
ESP_LOG_BUFFER_HEX_LEVEL(ENA_BEACON_LOG, rpi, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
ESP_LOG_BUFFER_HEX_LEVEL(ENA_BEACON_LOG, aem, ENA_AEM_METADATA_LENGTH, ESP_LOG_DEBUG);
ena_storage_set_temp_beacon(temp_beacons_count, &temp_beacons[temp_beacons_count]);
}
}
+88
View File
@@ -0,0 +1,88 @@
// Copyright 2020 Lukas Haubaum
//
// Licensed under the GNU Affero General Public License, Version 3;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// https://www.gnu.org/licenses/agpl-3.0.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "esp_log.h"
#include "esp_bt.h"
#include "esp_gap_ble_api.h"
#include "ena-crypto.h"
#include "ena-bluetooth-advertise.h"
static esp_ble_adv_params_t ena_adv_params = {
.adv_int_min = 0x140, // 200 ms
.adv_int_max = 0x190, // 250 ms
.adv_type = ADV_TYPE_NONCONN_IND,
.own_addr_type = BLE_ADDR_TYPE_RANDOM,
.channel_map = ADV_CHNL_ALL,
.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
};
void ena_bluetooth_advertise_start(void)
{
ESP_ERROR_CHECK(esp_ble_gap_start_advertising(&ena_adv_params));
}
void ena_bluetooth_advertise_set_payload(uint32_t enin, uint8_t *tek)
{
uint8_t rpik[ENA_KEY_LENGTH] = {0};
uint8_t rpi[ENA_KEY_LENGTH] = {0};
uint8_t aemk[ENA_KEY_LENGTH] = {0};
uint8_t aem[ENA_AEM_METADATA_LENGTH] = {0};
ena_crypto_rpik(rpik, tek);
ena_crypto_rpi(rpi, rpik, enin);
ena_crypto_aemk(aemk, tek);
ena_crypto_aem(aem, aemk, rpi, esp_ble_tx_power_get(ESP_BLE_PWR_TYPE_ADV));
uint8_t adv_raw_data[31];
// FLAG??? skipped on sniffed android packages!?
adv_raw_data[0] = 0x02;
adv_raw_data[1] = 0x01;
adv_raw_data[2] = ENA_BLUETOOTH_TAG_DATA;
// SERVICE UUID
adv_raw_data[3] = 0x03;
adv_raw_data[4] = 0x03;
adv_raw_data[5] = 0x6F;
adv_raw_data[6] = 0xFD;
// SERVICE DATA
adv_raw_data[7] = 0x17;
adv_raw_data[8] = 0x16;
adv_raw_data[9] = 0x6F;
adv_raw_data[10] = 0xFD;
for (int i = 0; i < ENA_KEY_LENGTH; i++)
{
adv_raw_data[i + 11] = rpi[i];
}
for (int i = 0; i < ENA_AEM_METADATA_LENGTH; i++)
{
adv_raw_data[i + ENA_KEY_LENGTH + 11] = aem[i];
}
esp_ble_gap_config_adv_data_raw(adv_raw_data, sizeof(adv_raw_data));
ESP_LOGD(ENA_ADVERTISE_LOG, "payload for ENIN %u", enin);
ESP_LOG_BUFFER_HEXDUMP(ENA_ADVERTISE_LOG, adv_raw_data, sizeof(adv_raw_data), ESP_LOG_DEBUG);
}
void ena_bluetooth_advertise_stop(void)
{
ESP_ERROR_CHECK(esp_ble_gap_stop_advertising());
}
+113
View File
@@ -0,0 +1,113 @@
// Copyright 2020 Lukas Haubaum
//
// Licensed under the GNU Affero General Public License, Version 3;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// https://www.gnu.org/licenses/agpl-3.0.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <string.h>
#include <time.h>
#include "esp_log.h"
#include "esp_gap_ble_api.h"
#include "ena-crypto.h"
#include "ena-beacons.h"
#include "ena-bluetooth-scan.h"
static int scan_status = ENA_SCAN_STATUS_NOT_SCANNING;
static const uint16_t ENA_SERVICE_UUID = 0xFD6F;
static esp_ble_scan_params_t ena_scan_params = {
.scan_type = BLE_SCAN_TYPE_ACTIVE,
.own_addr_type = BLE_ADDR_TYPE_RANDOM,
.scan_filter_policy = BLE_SCAN_FILTER_ALLOW_ALL,
.scan_interval = 0x50, // don't know good parameters, just copied
.scan_window = 0x30, // don't know good parameters, just copied
.scan_duplicate = BLE_SCAN_DUPLICATE_ENABLE,
};
void ena_bluetooth_scan_event_callback(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
{
uint32_t unix_timestamp = (uint32_t)time(NULL);
esp_ble_gap_cb_param_t *p = (esp_ble_gap_cb_param_t *)param;
switch (event)
{
case ESP_GAP_BLE_SCAN_START_COMPLETE_EVT:
ESP_LOGD(ENA_SCAN_LOG, "start scanning...");
break;
case ESP_GAP_BLE_SCAN_STOP_COMPLETE_EVT:
ESP_LOGD(ENA_SCAN_LOG, "stopped scanning...");
ena_beacons_temp_refresh(unix_timestamp);
break;
case ESP_GAP_BLE_SCAN_RESULT_EVT:
if (p->scan_rst.search_evt == ESP_GAP_SEARCH_INQ_RES_EVT)
{
uint8_t service_uuid_length = 0;
uint8_t *service_uuid_data = esp_ble_resolve_adv_data(p->scan_rst.ble_adv, 0x03, &service_uuid_length);
// check for ENA Service UUID
if (service_uuid_length == sizeof(ENA_SERVICE_UUID) && memcmp(service_uuid_data, &ENA_SERVICE_UUID, service_uuid_length) == 0)
{
uint8_t service_data_length = 0;
uint8_t *service_data = esp_ble_resolve_adv_data(p->scan_rst.ble_adv, 0x16, &service_data_length);
if (service_data_length != (sizeof(ENA_SERVICE_UUID) + ENA_KEY_LENGTH + ENA_AEM_METADATA_LENGTH))
{
ESP_LOGW(ENA_SCAN_LOG, "received ENA Service with invalid payload");
break;
}
uint8_t *rpi = malloc(ENA_KEY_LENGTH);
memcpy(rpi, &service_data[sizeof(ENA_SERVICE_UUID)], ENA_KEY_LENGTH);
uint8_t *aem = malloc(ENA_AEM_METADATA_LENGTH);
memcpy(aem, &service_data[sizeof(ENA_SERVICE_UUID) + ENA_KEY_LENGTH], ENA_AEM_METADATA_LENGTH);
ena_beacon(unix_timestamp, rpi, aem, p->scan_rst.rssi);
free(rpi);
free(aem);
}
}
else if (p->scan_rst.search_evt == ESP_GAP_SEARCH_INQ_CMPL_EVT)
{
scan_status = ENA_SCAN_STATUS_NOT_SCANNING;
ena_beacons_temp_refresh(unix_timestamp);
ESP_LOGD(ENA_SCAN_LOG, "finished scanning...");
}
break;
default:
// nothing
break;
}
}
void ena_bluetooth_scan_init(void)
{
ESP_ERROR_CHECK(esp_ble_gap_set_scan_params(&ena_scan_params));
ESP_ERROR_CHECK(esp_ble_gap_register_callback(ena_bluetooth_scan_event_callback));
// init temporary beacons
ena_beacons_temp_refresh((uint32_t)time(NULL));
}
void ena_bluetooth_scan_start(uint32_t duration)
{
scan_status = ENA_SCAN_STATUS_SCANNING;
ESP_ERROR_CHECK(esp_ble_gap_start_scanning(duration));
}
void ena_bluetooth_scan_stop(void)
{
scan_status = ENA_SCAN_STATUS_WAITING;
ESP_ERROR_CHECK(esp_ble_gap_stop_scanning());
}
int ena_bluetooth_scan_get_status(void)
{
return scan_status;
}
+96
View File
@@ -0,0 +1,96 @@
// Copyright 2020 Lukas Haubaum
//
// Licensed under the GNU Affero General Public License, Version 3;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// https://www.gnu.org/licenses/agpl-3.0.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "mbedtls/md.h"
#include "mbedtls/aes.h"
#include "mbedtls/hkdf.h"
#include "mbedtls/entropy.h"
#include "mbedtls/ctr_drbg.h"
#include "esp_log.h"
#include "ena-crypto.h"
#define ESP_CRYPTO_LOG "ESP-CRYPTO"
static mbedtls_ctr_drbg_context ctr_drbg;
void ena_crypto_init(void)
{
mbedtls_entropy_context entropy;
uint8_t pers[] = "Exposure Notifcation API esp32";
int ret;
mbedtls_entropy_init(&entropy);
mbedtls_ctr_drbg_init(&ctr_drbg);
if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, pers, sizeof(pers))) != 0)
{
ESP_LOGE(ESP_CRYPTO_LOG, " failed\n ! mbedtls_ctr_drbg_init returned -0x%04x\n", -ret);
}
}
uint32_t ena_crypto_enin(uint32_t unix_epoch_time)
{
return unix_epoch_time / ENA_TIME_WINDOW;
}
void ena_crypto_tek(uint8_t *tek)
{
int ret;
if ((ret = mbedtls_ctr_drbg_random(&ctr_drbg, tek, ENA_KEY_LENGTH)) != 0)
{
ESP_LOGE(ESP_CRYPTO_LOG, " failed\n ! mbedtls_ctr_drbg_random returned -0x%04x\n", -ret);
}
}
void ena_crypto_rpik(uint8_t *rpik, uint8_t *tek)
{
const uint8_t rpik_info[] = "EN-RPIK";
mbedtls_hkdf(mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), NULL, 0, tek, ENA_KEY_LENGTH, rpik_info, sizeof(rpik_info), rpik, ENA_KEY_LENGTH);
}
void ena_crypto_rpi(uint8_t *rpi, uint8_t *rpik, uint32_t enin)
{
uint8_t padded_data[] = "EN-RPI";
padded_data[12] = (enin & 0x000000ff);
padded_data[13] = (enin & 0x0000ff00) >> 8;
padded_data[14] = (enin & 0x00ff0000) >> 16;
padded_data[15] = (enin & 0xff000000) >> 24;
mbedtls_aes_context aes;
mbedtls_aes_init(&aes);
mbedtls_aes_setkey_enc(&aes, rpik, ENA_KEY_LENGTH * 8);
mbedtls_aes_crypt_ecb(&aes, MBEDTLS_AES_ENCRYPT, padded_data, rpi);
mbedtls_aes_free(&aes);
}
void ena_crypto_aemk(uint8_t *aemk, uint8_t *tek)
{
uint8_t aemkInfo[] = "EN-AEMK";
mbedtls_hkdf(mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), NULL, 0, tek, ENA_KEY_LENGTH, aemkInfo, sizeof(aemkInfo), aemk, ENA_KEY_LENGTH);
}
void ena_crypto_aem(uint8_t *aem, uint8_t *aemk, uint8_t *rpi, uint8_t power_level)
{
uint8_t metadata[ENA_AEM_METADATA_LENGTH];
metadata[0] = 0b01000000;
metadata[1] = power_level;
size_t count = 0;
uint8_t sb[16] = {0};
mbedtls_aes_context aes;
mbedtls_aes_init(&aes);
mbedtls_aes_setkey_enc(&aes, aemk, ENA_KEY_LENGTH * 8);
mbedtls_aes_crypt_ctr(&aes, ENA_AEM_METADATA_LENGTH, &count, rpi, sb, metadata, aem);
mbedtls_aes_free(&aes);
}
+176
View File
@@ -0,0 +1,176 @@
// Copyright 2020 Lukas Haubaum
//
// Licensed under the GNU Affero General Public License, Version 3;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// https://www.gnu.org/licenses/agpl-3.0.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <stdio.h>
#include <time.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "esp_system.h"
#include "esp_log.h"
#include "esp_bt.h"
#include "esp_bt_main.h"
#include "esp_gap_ble_api.h"
#include "nvs_flash.h"
#include "ena-crypto.h"
#include "ena-storage.h"
#include "ena-bluetooth-scan.h"
#include "ena-bluetooth-advertise.h"
#include "ena.h"
static ena_tek_t last_tek; // last ENIN
static uint32_t next_rpi_timestamp; // next rpi
void ena_next_rpi_timestamp(uint32_t timestamp)
{
int random_interval = esp_random() % (2 * ENA_BT_RANDOMIZE_ROTATION_TIMEOUT_INTERVAL);
if (random_interval > ENA_BT_RANDOMIZE_ROTATION_TIMEOUT_INTERVAL)
{
random_interval = ENA_BT_RANDOMIZE_ROTATION_TIMEOUT_INTERVAL - random_interval;
}
next_rpi_timestamp = timestamp + ENA_BT_ROTATION_TIMEOUT_INTERVAL + random_interval;
ESP_LOGD(ENA_LOG, "next rpi at %u (%u from %u)", next_rpi_timestamp, (ENA_BT_ROTATION_TIMEOUT_INTERVAL + random_interval), timestamp);
}
void ena_run(void *pvParameter)
{
uint32_t unix_timestamp = 0;
uint32_t current_enin = 0;
while (1)
{
unix_timestamp = (uint32_t)time(NULL);
current_enin = ena_crypto_enin(unix_timestamp);
if (current_enin - last_tek.enin >= last_tek.rolling_period)
{
ena_crypto_tek(last_tek.key_data);
last_tek.enin = current_enin;
// validity only to next day 00:00
last_tek.rolling_period = ENA_TEK_ROLLING_PERIOD - (last_tek.enin % ENA_TEK_ROLLING_PERIOD);
ena_storage_write_tek(&last_tek);
}
// change RPI
if (unix_timestamp >= next_rpi_timestamp)
{
if (ena_bluetooth_scan_get_status() == ENA_SCAN_STATUS_SCANNING)
{
ena_bluetooth_scan_stop();
}
ena_bluetooth_advertise_stop();
ena_bluetooth_advertise_set_payload(current_enin, last_tek.key_data);
ena_bluetooth_advertise_start();
if (ena_bluetooth_scan_get_status() == ENA_SCAN_STATUS_WAITING)
{
ena_bluetooth_scan_start(ENA_SCANNING_TIME);
}
ena_next_rpi_timestamp(unix_timestamp);
}
// scan
if (unix_timestamp % ENA_SCANNING_INTERVAL == 0 && ena_bluetooth_scan_get_status() == ENA_SCAN_STATUS_NOT_SCANNING)
{
ena_bluetooth_scan_start(ENA_SCANNING_TIME);
}
// one second loop correct?!
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
void ena_start(void)
{
#if (CONFIG_ENA_STORAGE_ERASE)
ena_storage_erase();
#endif
// init NVS for BLE
esp_err_t ret;
ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
{
ESP_ERROR_CHECK(nvs_flash_erase());
ESP_ERROR_CHECK(nvs_flash_init());
}
// init BLE
if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE)
{
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_bt_controller_init(&bt_cfg));
while (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE)
{
}
}
if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_INITED)
{
ESP_ERROR_CHECK(esp_bt_controller_enable(ESP_BT_MODE_BLE));
}
if (esp_bluedroid_get_status() == ESP_BLUEDROID_STATUS_UNINITIALIZED)
{
ESP_ERROR_CHECK(esp_bluedroid_init());
}
if (esp_bluedroid_get_status() == ESP_BLUEDROID_STATUS_INITIALIZED)
{
ESP_ERROR_CHECK(esp_bluedroid_enable());
}
// new bluetooth address nesseccary?
uint8_t bt_address[ESP_BD_ADDR_LEN];
esp_fill_random(bt_address, ESP_BD_ADDR_LEN);
bt_address[0] |= 0xC0;
ESP_ERROR_CHECK(esp_ble_gap_set_rand_addr(bt_address));
ESP_ERROR_CHECK(esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_DEFAULT, ESP_PWR_LVL_P9));
ESP_ERROR_CHECK(esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_ADV, ESP_PWR_LVL_P9));
ESP_ERROR_CHECK(esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_SCAN, ESP_PWR_LVL_P9));
ESP_ERROR_CHECK(esp_ble_gap_config_local_privacy(true));
// init ENA
ena_crypto_init();
uint32_t unix_timestamp = (uint32_t)time(NULL);
uint32_t current_enin = ena_crypto_enin(unix_timestamp);
uint32_t tek_count = ena_storage_read_last_tek(&last_tek);
ena_next_rpi_timestamp(unix_timestamp);
// read last TEK or create new
if (tek_count == 0 || (current_enin - last_tek.enin) >= last_tek.rolling_period)
{
ena_crypto_tek(last_tek.key_data);
last_tek.enin = ena_crypto_enin(unix_timestamp);
// validity only to next day 00:00
last_tek.rolling_period = ENA_TEK_ROLLING_PERIOD - (last_tek.enin % ENA_TEK_ROLLING_PERIOD);
ena_storage_write_tek(&last_tek);
}
// init scan
ena_bluetooth_scan_init();
// init and start advertising
ena_bluetooth_advertise_set_payload(current_enin, last_tek.key_data);
ena_bluetooth_advertise_start();
// initial scan on every start
ena_bluetooth_scan_start(ENA_SCANNING_TIME);
// what is a good stack size here?
xTaskCreate(&ena_run, "ena_run", configMINIMAL_STACK_SIZE * 8, NULL, 5, NULL);
}
+45
View File
@@ -0,0 +1,45 @@
// Copyright 2020 Lukas Haubaum
//
// Licensed under the GNU Affero General Public License, Version 3;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// https://www.gnu.org/licenses/agpl-3.0.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef _ena_BEACON_H_
#define _ena_BEACON_H_
#define ENA_BEACON_LOG "ESP-ENA-beacon" // TAG for Logging
#define ENA_BEACON_TRESHOLD (CONFIG_ENA_BEACON_TRESHOLD) // meet for longer than 5 minutes
/**
* @brief check temporary beacon for threshold or expiring
*
* This function checks all current temporary beacons if the contact threshold is
* reached or if the temporary contact can be discarded.
*
* @param[in] unix_timestamp current time as UNIX timestamp to compate
*
*/
void ena_beacons_temp_refresh(uint32_t unix_timestamp);
/**
* @brief handle new beacon received from a BLE scan
*
* This function gets called when a running BLE scan received a new ENA payload.
* On already detected RPI this will update just the timestamp and RSSI.
*
* @param[in] unix_timestamp UNIX timestamp when beacon was made
* @param[in] rpi received RPI from scanned payload
* @param[in] aem received AEM from scanned payload
* @param[in] rssi measured RSSI on scan
*
*/
void ena_beacon(uint32_t unix_timestamp, uint8_t *rpi, uint8_t *aem, int rssi);
#endif
@@ -0,0 +1,46 @@
// Copyright 2020 Lukas Haubaum
//
// Licensed under the GNU Affero General Public License, Version 3;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// https://www.gnu.org/licenses/agpl-3.0.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef _ena_BLUETOOTH_ADVERTISE_H_
#define _ena_BLUETOOTH_ADVERTISE_H_
#define ENA_ADVERTISE_LOG "ESP-ENA-advertise" // TAG for Logging
#define ENA_BLUETOOTH_TAG_DATA (0x1A) // Data for BLE payload TAG
/**
* @brief Start BLE advertising
*/
void ena_bluetooth_advertise_start(void);
/**
* @brief Set payload for BLE advertising
*
* This will set the payload for based on given ENIN and TEK.
*
* Source documents (Section: Advertising Payload)
*
* https://blog.google/documents/70/Exposure_Notification_-_Bluetooth_Specification_v1.2.2.pdf
*
* https://covid19-static.cdn-apple.com/applications/covid19/current/static/detection-tracing/pdf/ExposureNotification-BluetoothSpecificationv1.2.pdf
*
* @param[in] enin ENIN defining the start of the tek vadility. This should be the ENIN for the current timestamp
* @param[in] tek pointer to the TEK used to encrypt the payload.
*/
void ena_bluetooth_advertise_set_payload(uint32_t enin, uint8_t *tek);
/**
* @brief Stop BLE advertising
*/
void ena_bluetooth_advertise_stop(void);
#endif
@@ -0,0 +1,63 @@
// Copyright 2020 Lukas Haubaum
//
// Licensed under the GNU Affero General Public License, Version 3;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// https://www.gnu.org/licenses/agpl-3.0.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef _ena_BLUETOOTH_SCAN_H_
#define _ena_BLUETOOTH_SCAN_H_
#define ENA_SCAN_LOG "ESP-ENA-scan" // TAG for Logging
#define ENA_SCANNING_TIME (CONFIG_ENA_SCANNING_TIME) // time how long a scan should run
#define ENA_SCANNING_INTERVAL (CONFIG_ENA_SCANNING_INTERVAL) // interval for next scan to happen
/**
* @brief status of BLE scan
*/
typedef enum
{
ENA_SCAN_STATUS_SCANNING = 0, // scan is running
ENA_SCAN_STATUS_NOT_SCANNING, // scan is not running
ENA_SCAN_STATUS_WAITING, // scan is not running but stopped manually
} ena_bluetooth_scan_status;
/**
* @brief initialize the BLE scanning
*
*/
void ena_bluetooth_scan_init(void);
/**
* @brief start BLE scanning for a given duration
*
* Source documents (Section: Scanning Behavior)
*
* https://blog.google/documents/70/Exposure_Notification_-_Bluetooth_Specification_v1.2.2.pdf
*
* https://covid19-static.cdn-apple.com/applications/covid19/current/static/detection-tracing/pdf/ExposureNotification-BluetoothSpecificationv1.2.pdf
*
* @param[in] duration duration of the scan in seconds
*/
void ena_bluetooth_scan_start(uint32_t duration);
/**
* @brief stop a running BLE scanning
*/
void ena_bluetooth_scan_stop(void);
/**
* @brief return the current scanning status
*
* @return
* current scan status
*/
int ena_bluetooth_scan_get_status(void);
#endif
+120
View File
@@ -0,0 +1,120 @@
// Copyright 2020 Lukas Haubaum
//
// Licensed under the GNU Affero General Public License, Version 3;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// https://www.gnu.org/licenses/agpl-3.0.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef _ena_CRYPTO_H_
#define _ena_CRYPTO_H_
#define ENA_TIME_WINDOW (600) // time window every 10 minutes
#define ENA_KEY_LENGTH (16) // key length
#define ENA_AEM_METADATA_LENGTH (4) // size of metadata
#define ENA_TEK_ROLLING_PERIOD (CONFIG_ENA_TEK_ROLLING_PERIOD) // TEKRollingPeriod
#include <stdio.h>
/**
* @brief initialize cryptography
*
* This initialize the cryptography by setting up entropy.
*/
void ena_crypto_init(void);
/**
* @brief calculate ENIntervalNumber (ENIN) for given UNIX timestamp
*
* Source documents (Section: ENIntervalNumber)
*
* https://blog.google/documents/69/Exposure_Notification_-_Cryptography_Specification_v1.2.1.pdf
*
* https://covid19-static.cdn-apple.com/applications/covid19/current/static/detection-tracing/pdf/ExposureNotification-CryptographySpecificationv1.2.pdf
*
*
* @param[in] unix_timestamp UNIX Timestamp to calculate ENIN for
*
* @return
* ENIN for given timestamp
*/
uint32_t ena_crypto_enin(uint32_t unix_timestamp);
/**
* @brief calculate a new random Temporary Exposure Key (TEK)
*
* Source documents (Section: Temporary Exposure Key)
*
* https://blog.google/documents/69/Exposure_Notification_-_Cryptography_Specification_v1.2.1.pdf
*
* https://covid19-static.cdn-apple.com/applications/covid19/current/static/detection-tracing/pdf/ExposureNotification-CryptographySpecificationv1.2.pdf
*
* @param[out] tek pointer to the new TEK
*/
void ena_crypto_tek(uint8_t *tek);
/**
* @brief calculate a new Rolling Proximity Identifier Key (RPIK) with given TEK
*
* Source documents (Section: Rolling Proximity Identifier Key)
*
* https://blog.google/documents/69/Exposure_Notification_-_Cryptography_Specification_v1.2.1.pdf
*
* https://covid19-static.cdn-apple.com/applications/covid19/current/static/detection-tracing/pdf/ExposureNotification-CryptographySpecificationv1.2.pdf
*
* @param[out] rpik pointer to the new RPIK
* @param[in] tek TEK for calculating RPIK
*/
void ena_crypto_rpik(uint8_t *rpik, uint8_t *tek);
/**
* @brief calculate a new Rolling Proximity Identifier with given RPIK and ENIN
*
* Source documents (Section: Rolling Proximity Identifier)
*
* https://blog.google/documents/69/Exposure_Notification_-_Cryptography_Specification_v1.2.1.pdf
*
* https://covid19-static.cdn-apple.com/applications/covid19/current/static/detection-tracing/pdf/ExposureNotification-CryptographySpecificationv1.2.pdf
*
* @param[out] rpi pointer to the new RPI
* @param[in] rpik RPIK for encrypting RPI
* @param[in] enin ENIN to encrypt in RPI
*/
void ena_crypto_rpi(uint8_t *rpi, uint8_t *rpik, uint32_t enin);
/**
* @brief calculate a new Associated Encrypted Metadata Key (AEMK) with given TEK
*
* Source documents (Section: Associated Encrypted Metadata Key)
*
* https://blog.google/documents/69/Exposure_Notification_-_Cryptography_Specification_v1.2.1.pdf
*
* https://covid19-static.cdn-apple.com/applications/covid19/current/static/detection-tracing/pdf/ExposureNotification-CryptographySpecificationv1.2.pdf
*
* @param[out] aemk pointer to the new AEMK
* @param[in] tek TEK for calculating AEMK
*/
void ena_crypto_aemk(uint8_t *aemk, uint8_t *tek);
/**
* @brief create Associated Encrypted Metadata (AEM) with given AEMK along the RPI
*
* Source documents (Section: Associated Encrypted Metadata)
*
* https://blog.google/documents/69/Exposure_Notification_-_Cryptography_Specification_v1.2.1.pdf
*
* https://covid19-static.cdn-apple.com/applications/covid19/current/static/detection-tracing/pdf/ExposureNotification-CryptographySpecificationv1.2.pdf
*
* @param[out] aem pointer to the new AEM
* @param[in] aemk AEMK for encrypting AEM
* @param[in] rpi RPI for encrypting AEM
* @param[in] power_level BLE power level to encrypt in AEM
*/
void ena_crypto_aem(uint8_t *aem, uint8_t *aemk, uint8_t *rpi, uint8_t power_level);
#endif
+221
View File
@@ -0,0 +1,221 @@
// Copyright 2020 Lukas Haubaum
//
// Licensed under the GNU Affero General Public License, Version 3;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// https://www.gnu.org/licenses/agpl-3.0.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef _ena_STORAGE_H_
#define _ena_STORAGE_H_
#include "ena-crypto.h"
#define ENA_STORAGE_LOG "ESP-ENA-storage" // TAG for Logging
#define ENA_STORAGE_PARTITION_NAME (CONFIG_ENA_STORAGE_PARTITION_NAME) // name of partition to use for storing
#define ENA_STORAGE_START_ADDRESS (CONFIG_ENA_STORAGE_START_ADDRESS) // start address of storage
#define ENA_STORAGE_TEK_MAX (CONFIG_ENA_STORAGE_TEK_MAX) // Period of storing TEKs // length of a stored beacon -> RPI keysize + AEM size + 4 Bytes for ENIN + 4 Bytes for RSSI
#define ENA_STORAGE_TEMP_BEACONS_MAX (CONFIG_ENA_STORAGE_TEMP_BEACONS_MAX) // Maximum number of temporary stored beacons
/**
* @brief structure for TEK
*/
typedef struct __attribute__((__packed__))
{
uint8_t key_data[ENA_KEY_LENGTH]; // key data for encryption
uint32_t enin; // ENIN marking start of validity
uint8_t rolling_period; // period after validity start to mark key as expired
} ena_tek_t;
/**
* @brief sturcture for storing a temporary beacon
*/
typedef struct __attribute__((__packed__))
{
uint8_t rpi[ENA_KEY_LENGTH]; // received RPI of beacon
uint8_t aem[ENA_AEM_METADATA_LENGTH]; // received AEM of beacon
uint32_t timestamp_first; // timestamp of first recognition
uint32_t timestamp_last; // timestamp of last recognition
int rssi; // average measured RSSI
} ena_temp_beacon_t;
/**
* @brief sturcture for permanently storing a beacon after threshold reached
*/
typedef struct __attribute__((__packed__))
{
uint8_t rpi[ENA_KEY_LENGTH]; // received RPI of beacon
uint8_t aem[ENA_AEM_METADATA_LENGTH]; // received AEM of beacon
uint32_t timestamp; // timestamp of last recognition
int rssi; // average measured RSSI
} ena_beacon_t;
/**
* @brief read bytes at given address
*
* @param[in] address the address to read bytes from
* @param[out] data pointer to write the read data
* @param[in] size how many bytes to read
*/
void ena_storage_read(size_t address, void *data, size_t size);
/**
* @brief store bytes at given address
*
* @param[in] address the address to write bytes to
* @param[in] data pointer to the data to write
* @param[in] size how many bytes to write
*/
void ena_storage_write(size_t address, void *data, size_t size);
/**
* @brief deletes bytes at given address and shift other data back
*
* @param[in] address the address to delete from
* @param[in] end_address the address to mark end of shift
* @param[in] size how many bytes to delete
*/
void ena_storage_shift_delete(size_t address, size_t end_address, size_t size);
/**
* @brief get last stored TEK
*
* @param[out] tek pointer to write last TEK to
*
* @return
* total number of TEKs stored
*/
uint32_t ena_storage_read_last_tek(ena_tek_t *tek);
/**
* @brief store given TEK
*
* This will store the given TEK as new TEK.
*
* @param[in] tek the tek to store
*/
void ena_storage_write_tek(ena_tek_t *tek);
/**
* @brief get number of stored temporary beacons
*
* @return
* total number of temporary beacons stored
*/
uint32_t ena_storage_temp_beacons_count(void);
/**
* @brief get temporary beacon at given index
*
* @param[in] index the index of the temporary beacon to read
* @param[out] beacon pointer to temporary to write to
*/
void ena_storage_get_temp_beacon(uint32_t index, ena_temp_beacon_t *beacon);
/**
* @brief store temporary beacon
*
* @param[in] beacon new temporary beacon to store
*
* @return
* index of new stored beacon
*/
uint32_t ena_storage_add_temp_beacon(ena_temp_beacon_t *beacon);
/**
* @brief store temporary beacon at given index
*
* @param[in] index the index of the temporary beacon to overwrite
* @param[in] beacon temporary beacon to store
*/
void ena_storage_set_temp_beacon(uint32_t index, ena_temp_beacon_t *beacon);
/**
* @brief remove temporary beacon at given index
*
* @param[in] index the index of the temporary beacon to remove
*/
void ena_storage_remove_temp_beacon(uint32_t index);
/**
* @brief get number of permanently stored beacons
*
* @return
* total number of beacons stored
*/
uint32_t ena_storage_beacons_count(void);
/**
* @brief get permanently stored beacon at given index
*
* @param[in] index the index of the beacon to read
* @param[out] beacon pointer to to write to
*/
void ena_storage_get_beacon(uint32_t index, ena_beacon_t *beacon);
/**
* @brief permanently store beacon
*
* @param[in] beacon new beacon to permanently store
*/
void ena_storage_add_beacon(ena_beacon_t *beacon);
/**
* @brief erase the storage
*
* This function completely deletes all stored data and resets the counters
* of TEKs, temporary beacon and beacon to zero.
*/
void ena_storage_erase(void);
/**
* @brief erase all stored TEKs
*
* This function deletes all stored TEKs and resets counter to zero.
*/
void ena_storage_erase_tek(void);
/**
* @brief erase all stored temporary beacons
*
* This function deletes all stored temporary beacons and resets counter to zero.
*/
void ena_storage_erase_temporary_beacon(void);
/**
* @brief erase all permanently stored beacons
*
* This function deletes all stored beacons and resets counter to zero.
*/
void ena_storage_erase_beacon(void);
/**
* @brief dump all stored TEKs to serial output
*
* This function prints all stored TEKs to serial output in
* the following CSV format: #,enin,tek
*/
void ena_storage_dump_tek(void);
/**
* @brief dump all stored temporary beacons to serial output
*
* This function prints all stored temporary beacons to serial output in
* the following CSV format: #,timestamp_first,timestamp_last,rpi,aem,rssi
*/
void ena_storage_dump_temp_beacons(void);
/**
* @brief dump all stored beacons to serial output
*
* This function prints all stored beacons to serial output in
* the following CSV format: #,timestamp,rpi,aem,rssi
*/
void ena_storage_dump_beacons(void);
#endif
+30
View File
@@ -0,0 +1,30 @@
// Copyright 2020 Lukas Haubaum
//
// Licensed under the GNU Affero General Public License, Version 3;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// https://www.gnu.org/licenses/agpl-3.0.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef _ena_H_
#define _ena_H_
#define ENA_LOG "ESP-ENA" // TAG for Logging
#define ENA_BT_ROTATION_TIMEOUT_INTERVAL (CONFIG_ENA_BT_ROTATION_TIMEOUT_INTERVAL) // change advertising payload and therefore the BT address
#define ENA_BT_RANDOMIZE_ROTATION_TIMEOUT_INTERVAL (CONFIG_ENA_BT_RANDOMIZE_ROTATION_TIMEOUT_INTERVAL) // random intervall change for BT address change
/**
* @brief Start Exposure Notification API
*
* This initializes the complete stack of ESP_ENA. It will initialize BLE module and
* starting a task for managing advertising and scanning processes.
*
*/
void ena_start(void);
#endif
+439
View File
@@ -0,0 +1,439 @@
// Copyright 2020 Lukas Haubaum
//
// Licensed under the GNU Affero General Public License, Version 3;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// https://www.gnu.org/licenses/agpl-3.0.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "esp_partition.h"
#include "ena-storage.h"
#include "ena-crypto.h"
#define BLOCK_SIZE (4096)
const int ENA_STORAGE_TEK_COUNT_ADDRESS = (ENA_STORAGE_START_ADDRESS); // starting address for TEK COUNT
const int ENA_STORAGE_TEK_START_ADDRESS = (ENA_STORAGE_TEK_COUNT_ADDRESS + sizeof(uint32_t));
const int ENA_STORAGE_TEMP_BEACONS_COUNT_ADDRESS = (ENA_STORAGE_TEK_START_ADDRESS + sizeof(ena_tek_t) * ENA_STORAGE_TEK_MAX);
const int ENA_STORAGE_TEMP_BEACONS_START_ADDRESS = (ENA_STORAGE_TEMP_BEACONS_COUNT_ADDRESS + sizeof(uint32_t));
const int ENA_STORAGE_BEACONS_COUNT_ADDRESS = (ENA_STORAGE_TEMP_BEACONS_START_ADDRESS + sizeof(ena_temp_beacon_t) * ENA_STORAGE_TEMP_BEACONS_MAX);
const int ENA_STORAGE_BEACONS_START_ADDRESS = (ENA_STORAGE_BEACONS_COUNT_ADDRESS + sizeof(uint32_t));
void ena_storage_read(size_t address, void *data, size_t size)
{
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_read");
const esp_partition_t *partition = esp_partition_find_first(
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, ENA_STORAGE_PARTITION_NAME);
assert(partition);
ESP_ERROR_CHECK(esp_partition_read(partition, address, data, size));
vTaskDelay(1);
ESP_LOGD(ENA_STORAGE_LOG, "read data at %u", address);
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, data, size, ESP_LOG_DEBUG);
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_read");
}
void ena_storage_write(size_t address, void *data, size_t size)
{
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_write");
const int block_num = address / BLOCK_SIZE;
// check for overflow
if (address + size <= (block_num + 1) * BLOCK_SIZE)
{
const esp_partition_t *partition = esp_partition_find_first(
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, ENA_STORAGE_PARTITION_NAME);
assert(partition);
const int block_start = block_num * BLOCK_SIZE;
const int block_address = address - block_start;
void *buffer = malloc(BLOCK_SIZE);
if (buffer == NULL)
{
ESP_LOGE(ENA_STORAGE_LOG, "Warning %s malloc low memory", "buffer");
return;
}
ESP_LOGD(ENA_STORAGE_LOG, "read block %d buffer: start %d size %u", block_num, block_start, BLOCK_SIZE);
ESP_ERROR_CHECK(esp_partition_read(partition, block_start, buffer, BLOCK_SIZE));
vTaskDelay(1);
ESP_ERROR_CHECK(esp_partition_erase_range(partition, block_start, BLOCK_SIZE));
memcpy((buffer + block_address), data, size);
ESP_ERROR_CHECK(esp_partition_write(partition, block_start, buffer, BLOCK_SIZE));
free(buffer);
ESP_LOGD(ENA_STORAGE_LOG, "write data at %u", address);
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, data, size, ESP_LOG_DEBUG);
}
else
{
ESP_LOGD(ENA_STORAGE_LOG, "overflow block at address %u with size %d (block %d)", address, size, block_num);
const size_t block2_address = (block_num + 1) * BLOCK_SIZE;
const size_t data2_size = address + size - block2_address;
const size_t data1_size = size - data2_size;
ESP_LOGD(ENA_STORAGE_LOG, "block1_address %d, block1_size %d (block %d)", address, data1_size, block_num);
ESP_LOGD(ENA_STORAGE_LOG, "block2_address %d, block2_size %d (block %d)", block2_address, data2_size, block_num + 1);
void *data1 = malloc(data1_size);
memcpy(data1, data, data1_size);
ena_storage_write(address, data1, data1_size);
free(data1);
void *data2 = malloc(data2_size);
memcpy(data2, (data + data1_size), data2_size);
ena_storage_write(block2_address, data2, data2_size);
free(data2);
}
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_write");
}
void ena_storage_shift_delete(size_t address, size_t end_address, size_t size)
{
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_shift_delete");
int block_num_start = address / BLOCK_SIZE;
// check for overflow
if (address + size <= (block_num_start + 1) * BLOCK_SIZE)
{
const esp_partition_t *partition = esp_partition_find_first(
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, ENA_STORAGE_PARTITION_NAME);
assert(partition);
int block_num_end = end_address / BLOCK_SIZE;
size_t block_start = address - block_num_start * BLOCK_SIZE;
while (block_num_end >= block_num_start)
{
void *buffer = malloc(BLOCK_SIZE);
ESP_ERROR_CHECK(esp_partition_read(partition, block_num_start * BLOCK_SIZE, buffer, BLOCK_SIZE));
vTaskDelay(1);
// shift inside buffer
ESP_LOGD(ENA_STORAGE_LOG, "shift block %d from %u to %u with size %u", block_num_start, (block_start + size), block_start, (BLOCK_SIZE - block_start - size));
memcpy((buffer + block_start), (buffer + block_start + size), BLOCK_SIZE - block_start - size);
if (block_num_end > block_num_start)
{
void *buffer_next_block = malloc(BLOCK_SIZE);
ESP_ERROR_CHECK(esp_partition_read(partition, (block_num_start + 1) * BLOCK_SIZE, buffer_next_block, BLOCK_SIZE));
vTaskDelay(1);
// shift from next block
ESP_LOGD(ENA_STORAGE_LOG, "shift next block size %u", size);
memcpy((buffer + BLOCK_SIZE - size), buffer_next_block, size);
free(buffer_next_block);
}
ESP_ERROR_CHECK(esp_partition_erase_range(partition, block_num_start * BLOCK_SIZE, BLOCK_SIZE));
ESP_ERROR_CHECK(esp_partition_write(partition, block_num_start * BLOCK_SIZE, buffer, BLOCK_SIZE));
free(buffer);
block_num_start++;
block_start = 0;
}
}
else
{
ESP_LOGD(ENA_STORAGE_LOG, "overflow block at address %u with size %d (block %d)", address, size, block_num_start);
const size_t block1_address = address;
const size_t block2_address = (block_num_start + 1) * BLOCK_SIZE;
const size_t data2_size = address + size - block2_address;
const size_t data1_size = size - data2_size;
ena_storage_shift_delete(block1_address, block2_address, data1_size);
ena_storage_shift_delete(block2_address, end_address - data1_size, data2_size);
}
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_shift_delete");
}
uint32_t ena_storage_read_last_tek(ena_tek_t *tek)
{
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_read_tek");
uint32_t tek_count = 0;
ena_storage_read(ENA_STORAGE_TEK_COUNT_ADDRESS, &tek_count, sizeof(uint32_t));
if (tek_count < 1)
{
return 0;
}
uint8_t index = (tek_count % ENA_STORAGE_TEK_MAX) - 1;
ena_storage_read(ENA_STORAGE_TEK_START_ADDRESS + index * sizeof(ena_tek_t), tek, sizeof(ena_tek_t));
ESP_LOGD(ENA_STORAGE_LOG, "read last tek %u:", tek->enin);
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, tek->key_data, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_read_tek");
return tek_count;
}
void ena_storage_write_tek(ena_tek_t *tek)
{
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_write_tek");
uint32_t tek_count = 0;
ena_storage_read(ENA_STORAGE_TEK_COUNT_ADDRESS, &tek_count, sizeof(uint32_t));
uint8_t index = (tek_count % ENA_STORAGE_TEK_MAX);
ena_storage_write(ENA_STORAGE_TEK_START_ADDRESS + index * sizeof(ena_tek_t), tek, sizeof(ena_tek_t));
tek_count++;
ena_storage_write(ENA_STORAGE_TEK_COUNT_ADDRESS, &tek_count, sizeof(uint32_t));
ESP_LOGD(ENA_STORAGE_LOG, "write tek: ENIN %u", tek->enin);
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, tek->key_data, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_write_tek");
}
uint32_t ena_storage_temp_beacons_count(void)
{
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_temp_beacons_count");
uint32_t count = 0;
ena_storage_read(ENA_STORAGE_TEMP_BEACONS_COUNT_ADDRESS, &count, sizeof(uint32_t));
ESP_LOGD(ENA_STORAGE_LOG, "read temp contancts count: %u", count);
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_temp_beacons_count");
return count;
}
void ena_storage_get_temp_beacon(uint32_t index, ena_temp_beacon_t *beacon)
{
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_read_temp_beacon");
ena_storage_read(ENA_STORAGE_TEMP_BEACONS_START_ADDRESS + index * sizeof(ena_temp_beacon_t), beacon, sizeof(ena_temp_beacon_t));
ESP_LOGD(ENA_STORAGE_LOG, "read temp beacon: first %u, last %u and rssi %d", beacon->timestamp_first, beacon->timestamp_last, beacon->rssi);
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, beacon->rpi, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, beacon->aem, ENA_AEM_METADATA_LENGTH, ESP_LOG_DEBUG);
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_read_temp_beacon");
}
uint32_t ena_storage_add_temp_beacon(ena_temp_beacon_t *beacon)
{
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_add_temp_beacon");
uint32_t count = ena_storage_temp_beacons_count();
// overwrite older temporary beacons?!
uint8_t index = count % ENA_STORAGE_TEMP_BEACONS_MAX;
ena_storage_set_temp_beacon(index, beacon);
ESP_LOGD(ENA_STORAGE_LOG, "add temp beacon at %u: first %u, last %u and rssi %d", index, beacon->timestamp_first, beacon->timestamp_last, beacon->rssi);
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, beacon->rpi, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, beacon->aem, ENA_AEM_METADATA_LENGTH, ESP_LOG_DEBUG);
count++;
ena_storage_write(ENA_STORAGE_TEMP_BEACONS_COUNT_ADDRESS, &count, sizeof(uint32_t));
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_add_temp_beacon");
return count - 1;
}
void ena_storage_set_temp_beacon(uint32_t index, ena_temp_beacon_t *beacon)
{
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_set_temp_beacon");
ena_storage_write(ENA_STORAGE_TEMP_BEACONS_START_ADDRESS + index * sizeof(ena_temp_beacon_t), beacon, sizeof(ena_temp_beacon_t));
ESP_LOGD(ENA_STORAGE_LOG, "set temp beacon at %u: first %u, last %u and rssi %d", index, beacon->timestamp_first, beacon->timestamp_last, beacon->rssi);
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, beacon->rpi, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, beacon->aem, ENA_AEM_METADATA_LENGTH, ESP_LOG_DEBUG);
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_set_temp_beacon");
}
void ena_storage_remove_temp_beacon(uint32_t index)
{
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_remove_temp_beacon");
uint32_t count = ena_storage_temp_beacons_count();
size_t address_from = ENA_STORAGE_TEMP_BEACONS_START_ADDRESS + index * sizeof(ena_temp_beacon_t);
size_t address_to = ENA_STORAGE_TEMP_BEACONS_START_ADDRESS + count * sizeof(ena_temp_beacon_t);
ena_storage_shift_delete(address_from, address_to, sizeof(ena_temp_beacon_t));
count--;
ena_storage_write(ENA_STORAGE_TEMP_BEACONS_COUNT_ADDRESS, &count, sizeof(uint32_t));
ESP_LOGD(ENA_STORAGE_LOG, "remove temp beacon: %u", index);
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_remove_temp_beacon");
}
uint32_t ena_storage_beacons_count(void)
{
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_beacons_count");
uint32_t count = 0;
ena_storage_read(ENA_STORAGE_BEACONS_COUNT_ADDRESS, &count, sizeof(uint32_t));
ESP_LOGD(ENA_STORAGE_LOG, "read contancts count: %u", count);
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_beacons_count");
return count;
}
void ena_storage_get_beacon(uint32_t index, ena_beacon_t *beacon)
{
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_read_beacon");
ena_storage_read(ENA_STORAGE_BEACONS_START_ADDRESS + index * sizeof(ena_beacon_t), beacon, sizeof(ena_beacon_t));
ESP_LOGD(ENA_STORAGE_LOG, "read beacon: timestamp %u and rssi %d", beacon->timestamp, beacon->rssi);
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, beacon->rpi, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, beacon->aem, ENA_AEM_METADATA_LENGTH, ESP_LOG_DEBUG);
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_read_beacon");
}
void ena_storage_add_beacon(ena_beacon_t *beacon)
{
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_write_beacon");
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, beacon->rpi, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
uint32_t count = ena_storage_beacons_count();
ena_storage_write(ENA_STORAGE_BEACONS_START_ADDRESS + count * sizeof(ena_beacon_t), beacon, sizeof(ena_beacon_t));
count++;
ena_storage_write(ENA_STORAGE_BEACONS_COUNT_ADDRESS, &count, sizeof(uint32_t));
ESP_LOGD(ENA_STORAGE_LOG, "write beacon: timestamp %u and rssi %d", beacon->timestamp, beacon->rssi);
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, beacon->rpi, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, beacon->aem, ENA_AEM_METADATA_LENGTH, ESP_LOG_DEBUG);
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_write_beacon");
}
void ena_storage_erase(void)
{
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_erase");
const esp_partition_t *partition = esp_partition_find_first(
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, ENA_STORAGE_PARTITION_NAME);
assert(partition);
ESP_ERROR_CHECK(esp_partition_erase_range(partition, 0, partition->size));
ESP_LOGI(ENA_STORAGE_LOG, "erased partition %s!", ENA_STORAGE_PARTITION_NAME);
uint32_t count = 0;
ena_storage_write(ENA_STORAGE_TEK_COUNT_ADDRESS, &count, sizeof(uint32_t));
ena_storage_write(ENA_STORAGE_TEMP_BEACONS_COUNT_ADDRESS, &count, sizeof(uint32_t));
ena_storage_write(ENA_STORAGE_BEACONS_COUNT_ADDRESS, &count, sizeof(uint32_t));
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_erase");
}
void ena_storage_erase_tek(void)
{
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_erase_teks");
uint32_t tek_count = 0;
ena_storage_read(ENA_STORAGE_TEK_COUNT_ADDRESS, &tek_count, sizeof(uint32_t));
uint8_t stored = ENA_STORAGE_TEK_MAX;
if (tek_count < ENA_STORAGE_TEK_MAX)
{
stored = tek_count;
}
size_t size = sizeof(uint32_t) + stored * sizeof(ena_tek_t);
uint8_t *zeros = calloc(size, sizeof(uint8_t));
ena_storage_write(ENA_STORAGE_TEK_COUNT_ADDRESS, zeros, size);
free(zeros);
ESP_LOGI(ENA_STORAGE_LOG, "erased %d teks (size %u at %u)", stored, size, ENA_STORAGE_TEK_COUNT_ADDRESS);
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_erase_teks");
}
void ena_storage_erase_temporary_beacon(void)
{
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_erase_temporary_beacons");
uint32_t beacon_count = 0;
ena_storage_read(ENA_STORAGE_TEMP_BEACONS_COUNT_ADDRESS, &beacon_count, sizeof(uint32_t));
uint32_t stored = ENA_STORAGE_TEMP_BEACONS_MAX;
if (beacon_count < ENA_STORAGE_TEMP_BEACONS_MAX)
{
stored = beacon_count;
}
size_t size = sizeof(uint32_t) + stored * sizeof(ena_temp_beacon_t);
uint8_t *zeros = calloc(size, sizeof(uint8_t));
ena_storage_write(ENA_STORAGE_TEMP_BEACONS_COUNT_ADDRESS, zeros, size);
free(zeros);
ESP_LOGI(ENA_STORAGE_LOG, "erased %d temporary beacons (size %u at %u)", stored, size, ENA_STORAGE_TEMP_BEACONS_COUNT_ADDRESS);
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_erase_temporary_beacons");
}
void ena_storage_erase_beacon(void)
{
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_erase_beacon");
uint32_t beacon_count = 0;
ena_storage_read(ENA_STORAGE_BEACONS_COUNT_ADDRESS, &beacon_count, sizeof(uint32_t));
size_t size = sizeof(uint32_t) + beacon_count * sizeof(ena_beacon_t);
uint8_t *zeros = calloc(size, sizeof(uint8_t));
ena_storage_write(ENA_STORAGE_BEACONS_COUNT_ADDRESS, zeros, size);
free(zeros);
ESP_LOGI(ENA_STORAGE_LOG, "erased %d beacons (size %u at %u)", beacon_count, size, ENA_STORAGE_BEACONS_COUNT_ADDRESS);
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_erase_beacon");
}
void ena_storage_dump_hash_array(uint8_t *data, size_t size)
{
for (int i = 0; i < size; i++)
{
if (i == 0)
{
printf("%02x", data[i]);
}
else
{
printf(" %02x", data[i]);
}
}
}
void ena_storage_dump_tek(void)
{
ena_tek_t tek;
uint32_t tek_count = 0;
ena_storage_read(ENA_STORAGE_TEK_COUNT_ADDRESS, &tek_count, sizeof(uint32_t));
uint8_t stored = ENA_STORAGE_TEK_MAX;
if (tek_count < ENA_STORAGE_TEK_MAX)
{
stored = tek_count;
}
ESP_LOGD(ENA_STORAGE_LOG, "%u TEKs (%u stored)\n", tek_count, stored);
printf("#,enin,tek,rolling_period\n");
for (int i = 0; i < stored; i++)
{
size_t address = ENA_STORAGE_TEK_START_ADDRESS + i * sizeof(ena_tek_t);
ena_storage_read(address, &tek, sizeof(ena_tek_t));
printf("%d,%u,", i, tek.enin);
ena_storage_dump_hash_array(tek.key_data, ENA_KEY_LENGTH);
printf(",%u\n", tek.rolling_period);
}
}
void ena_storage_dump_temp_beacons(void)
{
ena_temp_beacon_t beacon;
uint32_t beacon_count = 0;
ena_storage_read(ENA_STORAGE_TEMP_BEACONS_COUNT_ADDRESS, &beacon_count, sizeof(uint32_t));
uint32_t stored = ENA_STORAGE_TEMP_BEACONS_MAX;
if (beacon_count < ENA_STORAGE_TEMP_BEACONS_MAX)
{
stored = beacon_count;
}
ESP_LOGD(ENA_STORAGE_LOG, "%u temporary beacons (%u stored)\n", beacon_count, stored);
printf("#,timestamp_first,timestamp_last,rpi,aem,rssi\n");
for (int i = 0; i < stored; i++)
{
ena_storage_get_temp_beacon(i, &beacon);
printf("%d,%u,%u,", i, beacon.timestamp_first, beacon.timestamp_last);
ena_storage_dump_hash_array(beacon.rpi, ENA_KEY_LENGTH);
printf(",");
ena_storage_dump_hash_array(beacon.aem, ENA_AEM_METADATA_LENGTH);
printf(",%d\n", beacon.rssi);
}
}
void ena_storage_dump_beacons(void)
{
ena_beacon_t beacon;
uint32_t beacon_count = 0;
ena_storage_read(ENA_STORAGE_BEACONS_COUNT_ADDRESS, &beacon_count, sizeof(uint32_t));
ESP_LOGD(ENA_STORAGE_LOG, "%u beacons\n", beacon_count);
printf("#,timestamp,rpi,aem,rssi\n");
for (int i = 0; i < beacon_count; i++)
{
ena_storage_get_beacon(i, &beacon);
printf("%d,%u,", i, beacon.timestamp);
ena_storage_dump_hash_array(beacon.rpi, ENA_KEY_LENGTH);
printf(",");
ena_storage_dump_hash_array(beacon.aem, ENA_AEM_METADATA_LENGTH);
printf(",%d\n", beacon.rssi);
}
}
+7
View File
@@ -0,0 +1,7 @@
idf_component_register(
SRCS
"i2c-main.c"
INCLUDE_DIRS "."
PRIV_REQUIRES
driver
)
+39
View File
@@ -0,0 +1,39 @@
// Copyright 2020 Lukas Haubaum
//
// Licensed under the GNU Affero General Public License, Version 3;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// https://www.gnu.org/licenses/agpl-3.0.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <stdio.h>
#include "driver/i2c.h"
#include "i2c-main.h"
static bool i2c_initialized = false;
void i2c_main_init()
{
i2c_config_t i2c_config = {
.mode = I2C_MODE_MASTER,
.sda_io_num = I2C_SDA_PIN,
.scl_io_num = I2C_SCL_PIN,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master.clk_speed = I2C_CLK_SPEED};
ESP_ERROR_CHECK(i2c_param_config(I2C_NUM_0, &i2c_config));
ESP_ERROR_CHECK(i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0));
}
bool i2c_is_initialized()
{
return i2c_initialized;
}
+35
View File
@@ -0,0 +1,35 @@
// Copyright 2020 Lukas Haubaum
//
// Licensed under the GNU Affero General Public License, Version 3;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// https://www.gnu.org/licenses/agpl-3.0.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef _i2c_main_H_
#define _i2c_main_H_
#define I2C_SDA_PIN (21)
#define I2C_SCL_PIN (22)
#define I2C_CLK_SPEED (1000000)
/**
* @brief initialize main I2C interface
*/
void i2c_main_init();
/**
* @brief check if I2C interface already initialized
*
* @return
* - false I2C not initialized
* - true I2C initialized
*/
bool i2c_is_initialized();
#endif
+7
View File
@@ -0,0 +1,7 @@
idf_component_register(
SRCS
"ssd1306.c"
INCLUDE_DIRS "include"
PRIV_REQUIRES
i2c-main
)
+538
View File
@@ -0,0 +1,538 @@
// Copyright 2020 Lukas Haubaum
//
// Licensed under the GNU Affero General Public License, Version 3;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// https://www.gnu.org/licenses/agpl-3.0.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef _ssd1306_FONT_H_
#define _ssd1306_FONT_H_
/**
* @brief constant containing a 5x8 ascii font
*/
const uint8_t ascii_font_5x8[256][5] = {
{0x00, 0x00, 0x00, 0x00, 0x00}, // 0x00
{0x1E, 0x35, 0x31, 0x35, 0x1E}, // 0x01
{0x1E, 0x35, 0x37, 0x35, 0x1E}, // 0x02
{0x0E, 0x1F, 0x3E, 0x1F, 0x0E}, // 0x03
{0x08, 0x1C, 0x7F, 0x1C, 0x08}, // 0x04
{0x18, 0x4A, 0x7F, 0x4A, 0x18}, // 0x05
{0x1C, 0x4E, 0x7F, 0x4E, 0x1C}, // 0x06
{0x00, 0x00, 0x00, 0x00, 0x00}, // 0x07
{0x00, 0x00, 0x00, 0x00, 0x00}, // 0x08
{0x00, 0x00, 0x00, 0x00, 0x00}, // 0x09
{0x00, 0x00, 0x00, 0x00, 0x00}, // 0x0A
{0x38, 0x44, 0x44, 0x47, 0x3B}, // 0x0B
{0x0E, 0x51, 0xF1, 0x51, 0x0E}, // 0x0C
{0x00, 0x00, 0x00, 0x00, 0x00}, // 0x0D
{0x60, 0x7E, 0x02, 0x33, 0x3F}, // 0x0E
{0x2A, 0x1C, 0x36, 0x1C, 0x2A}, // 0x0F
{0x3E, 0x1C, 0x1C, 0x08, 0x08}, // 0x10
{0x08, 0x08, 0x1C, 0x1C, 0x3E}, // 0x11
{0x00, 0x22, 0x7F, 0x22, 0x00}, // 0x12
{0x00, 0x2E, 0x00, 0x2E, 0x00}, // 0x13
{0x06, 0x7F, 0x01, 0x7F, 0x00}, // 0x14
{0x00, 0x4A, 0x55, 0x29, 0x00}, // 0x15
{0x00, 0x18, 0x18, 0x18, 0x18}, // 0x16
{0x00, 0x4A, 0x5F, 0x4A, 0x00}, // 0x17
{0x00, 0x02, 0x7F, 0x02, 0x00}, // 0x18
{0x00, 0x20, 0x7F, 0x20, 0x00}, // 0x19
{0x00, 0x08, 0x08, 0x1C, 0x08}, // 0x1A
{0x00, 0x08, 0x1C, 0x08, 0x08}, // 0x1B
{0x00, 0x3C, 0x20, 0x20, 0x20}, // 0x1C
{0x08, 0x1C, 0x08, 0x1C, 0x08}, // 0x1D
{0x20, 0x38, 0x3E, 0x38, 0x20}, // 0x1E
{0x02, 0x0E, 0x3E, 0x0E, 0x02}, // 0x1F
{0x00, 0x00, 0x00, 0x00, 0x00}, // 0x20 (Space)
{0x00, 0x00, 0x9E, 0x00, 0x00}, // 0x21 !
{0x00, 0x0E, 0x00, 0x0E, 0x00}, // 0x22 "
{0x28, 0xFE, 0x28, 0xFE, 0x28}, // 0x23 #
{0x48, 0x54, 0xFE, 0x54, 0x24}, // 0x24 $
{0x46, 0x26, 0x10, 0xC8, 0xC4}, // 0x25 %
{0x6C, 0x92, 0xAA, 0x44, 0xA0}, // 0x26 &
{0x00, 0x0A, 0x06, 0x00, 0x00}, // 0x27 '
{0x00, 0x38, 0x44, 0x82, 0x00}, // 0x28 (
{0x00, 0x82, 0x44, 0x38, 0x00}, // 0x29 )
{0x10, 0x54, 0x38, 0x54, 0x10}, // 0x2A *
{0x10, 0x10, 0x7C, 0x10, 0x10}, // 0x2B +
{0x00, 0xA0, 0x60, 0x00, 0x00}, // 0x2C ,
{0x10, 0x10, 0x10, 0x10, 0x10}, // 0x2D -
{0x00, 0x60, 0x60, 0x00, 0x00}, // 0x2E .
{0x40, 0x20, 0x10, 0x08, 0x04}, // 0x2F /
{0x7C, 0xA2, 0x92, 0x8A, 0x7C}, // 0x30 0
{0x00, 0x84, 0xFE, 0x80, 0x00}, // 0x31 1
{0x84, 0xC2, 0xA2, 0x92, 0x8C}, // 0x32 2
{0x42, 0x82, 0x8A, 0x96, 0x62}, // 0x33 3
{0x30, 0x28, 0x24, 0xFE, 0x20}, // 0x34 4
{0x4E, 0x8A, 0x8A, 0x8A, 0x72}, // 0x35 5
{0x78, 0x94, 0x92, 0x92, 0x60}, // 0x36 6
{0x02, 0xE2, 0x12, 0x0A, 0x06}, // 0x37 7
{0x6C, 0x92, 0x92, 0x92, 0x6C}, // 0x38 8
{0x0C, 0x92, 0x92, 0x52, 0x3C}, // 0x39 9
{0x00, 0x6C, 0x6C, 0x00, 0x00}, // 0x3A :
{0x00, 0xAC, 0x6C, 0x00, 0x00}, // 0x3B ;
{0x00, 0x10, 0x28, 0x44, 0x82}, // 0x3C <
{0x28, 0x28, 0x28, 0x28, 0x28}, // 0x3D =
{0x82, 0x44, 0x28, 0x10, 0x00}, // 0x3E >
{0x04, 0x02, 0xA2, 0x12, 0x0C}, // 0x3F ?
{0x64, 0x92, 0xF2, 0x82, 0x7C}, // 0x40 @
{0xFC, 0x22, 0x22, 0x22, 0xFC}, // 0x41 A
{0xFE, 0x92, 0x92, 0x92, 0x6C}, // 0x42 B
{0x7C, 0x82, 0x82, 0x82, 0x44}, // 0x43 C
{0xFE, 0x82, 0x82, 0x44, 0x38}, // 0x44 D
{0xFE, 0x92, 0x92, 0x92, 0x82}, // 0x45 E
{0xFE, 0x12, 0x12, 0x02, 0x02}, // 0x46 F
{0x7C, 0x82, 0x82, 0xA2, 0x64}, // 0x47 G
{0xFE, 0x10, 0x10, 0x10, 0xFE}, // 0x48 H
{0x00, 0x82, 0xFE, 0x82, 0x00}, // 0x49 I
{0x40, 0x80, 0x82, 0x7E, 0x02}, // 0x4A J
{0xFE, 0x10, 0x28, 0x44, 0x82}, // 0x4B K
{0xFE, 0x80, 0x80, 0x80, 0x80}, // 0x4C L
{0xFE, 0x04, 0x08, 0x04, 0xFE}, // 0x4D M
{0xFE, 0x08, 0x10, 0x20, 0xFE}, // 0x4E N
{0x7C, 0x82, 0x82, 0x82, 0x7C}, // 0x4F O
{0xFE, 0x12, 0x12, 0x12, 0x0C}, // 0x50 P
{0x7C, 0x82, 0xA2, 0x42, 0xBC}, // 0x51 Q
{0xFE, 0x12, 0x32, 0x52, 0x8C}, // 0x52 R
{0x8C, 0x92, 0x92, 0x92, 0x62}, // 0x53 S
{0x02, 0x02, 0xFE, 0x02, 0x02}, // 0x54 T
{0x7E, 0x80, 0x80, 0x80, 0x7E}, // 0x55 U
{0x3E, 0x40, 0x80, 0x40, 0x3E}, // 0x56 V
{0xFE, 0x40, 0x30, 0x40, 0xFE}, // 0x57 W
{0xC6, 0x28, 0x10, 0x28, 0xC6}, // 0x58 X
{0x06, 0x08, 0xF0, 0x08, 0x06}, // 0x59 Y
{0xC2, 0xA2, 0x92, 0x8A, 0x86}, // 0x5A Z
{0x00, 0x00, 0xFE, 0x82, 0x82}, // 0x5B [
{0x04, 0x08, 0x10, 0x20, 0x40}, // 0x5C "\"
{0x82, 0x82, 0xFE, 0x00, 0x00}, // 0x5D ]
{0x08, 0x04, 0x02, 0x04, 0x08}, // 0x5E ^
{0x80, 0x80, 0x80, 0x80, 0x80}, // 0x5F _
{0x00, 0x02, 0x04, 0x08, 0x00}, // 0x60 `
{0x40, 0xA8, 0xA8, 0xA8, 0xF0}, // 0x61 a
{0xFE, 0x90, 0x88, 0x88, 0x70}, // 0x62 b
{0x70, 0x88, 0x88, 0x88, 0x40}, // 0x63 c
{0x70, 0x88, 0x88, 0x90, 0xFE}, // 0x64 d
{0x70, 0xA8, 0xA8, 0xA8, 0x30}, // 0x65 e
{0x10, 0xFC, 0x12, 0x02, 0x04}, // 0x66 f
{0x10, 0x28, 0xA8, 0xA8, 0x78}, // 0x67 g
{0xFE, 0x10, 0x08, 0x08, 0xF0}, // 0x68 h
{0x00, 0x88, 0xFA, 0x80, 0x00}, // 0x69 i
{0x40, 0x80, 0x88, 0x7A, 0x00}, // 0x6A j
{0x00, 0xFE, 0x20, 0x50, 0x88}, // 0x6B k
{0x00, 0x82, 0xFE, 0x80, 0x00}, // 0x6C l
{0xF8, 0x08, 0x30, 0x08, 0xF0}, // 0x6D m
{0xF8, 0x10, 0x08, 0x08, 0xF0}, // 0x6E n
{0x70, 0x88, 0x88, 0x88, 0x70}, // 0x6F o
{0xF8, 0x28, 0x28, 0x28, 0x10}, // 0x70 p
{0x10, 0x28, 0x28, 0x30, 0xF8}, // 0x71 q
{0xF8, 0x10, 0x08, 0x08, 0x10}, // 0x72 r
{0x90, 0xA8, 0xA8, 0xA8, 0x40}, // 0x73 s
{0x08, 0x7E, 0x88, 0x80, 0x40}, // 0x74 t
{0x78, 0x80, 0x80, 0x40, 0xF8}, // 0x75 u
{0x38, 0x40, 0x80, 0x40, 0x38}, // 0x76 v
{0x78, 0x80, 0x60, 0x80, 0x78}, // 0x77 w
{0x88, 0x50, 0x20, 0x50, 0x88}, // 0x78 x
{0x18, 0xA0, 0xA0, 0xA0, 0x78}, // 0x79 y
{0x88, 0xC8, 0xA8, 0x98, 0x88}, // 0x7A z
{0x00, 0x10, 0x6C, 0x82, 0x00}, // 0x7B {
{0x00, 0x00, 0xFE, 0x00, 0x00}, // 0x7C |
{0x00, 0x82, 0x6C, 0x10, 0x00}, // 0x7D }
{0x20, 0x10, 0x10, 0x20, 0x10}, // 0x7E
{0xF0, 0x88, 0x84, 0x88, 0xF0}, // 0x7F 
{0x28, 0x7C, 0xAA, 0x82, 0x44}, // 0x80 €
{0xF0, 0x29, 0x27, 0x21, 0xFF}, // 0x81 
{0x00, 0xA0, 0x60, 0x00, 0x00}, // 0x82
{0x40, 0x90, 0x7C, 0x12, 0x04}, // 0x83 ƒ
{0xC0, 0xA0, 0x00, 0xC0, 0xA0}, // 0x84 „
{0x80, 0x00, 0x80, 0x00, 0x80}, // 0x85 …
{0x00, 0x04, 0xFE, 0x04, 0x00}, // 0x86 †
{0x00, 0x44, 0xFE, 0x44, 0x00}, // 0x87 ‡
{0x00, 0x04, 0x02, 0x04, 0x00}, // 0x88 ˆ
{0xC3, 0xD3, 0x08, 0xC4, 0xC2}, // 0x89 ‰
{0x4C, 0x93, 0x92, 0x93, 0x64}, // 0x8A Š
{0x00, 0x10, 0x28, 0x00, 0x00}, // 0x8B
{0x7C, 0x82, 0x82, 0x7C, 0x92}, // 0x8C Œ
{0x02, 0xFE, 0x90, 0x90, 0x60}, // 0x8D 
{0xC2, 0xA3, 0x92, 0x8B, 0x86}, // 0x8E Ž
{0x44, 0x92, 0x8A, 0x92, 0x7C}, // 0x8F 
{0x70, 0x88, 0x90, 0x60, 0x98}, // 0x90 
{0x00, 0x02, 0x04, 0x08, 0x00}, // 0x91
{0x00, 0x08, 0x04, 0x02, 0x00}, // 0x92
{0x02, 0x04, 0x0A, 0x04, 0x08}, // 0x93 “
{0x08, 0x04, 0x0A, 0x04, 0x02}, // 0x94 ”
{0x00, 0x38, 0x38, 0x38, 0x00}, // 0x95 •
{0x00, 0x10, 0x10, 0x10, 0x10}, // 0x96
{0x10, 0x10, 0x10, 0x10, 0x10}, // 0x97 —
{0x02, 0x01, 0x02, 0x04, 0x02}, // 0x98 ˜
{0xF1, 0x5B, 0x55, 0x51, 0x51}, // 0x99 ™
{0x90, 0xA9, 0xAA, 0xA9, 0x40}, // 0x9A š
{0x00, 0x88, 0x50, 0x20, 0x00}, // 0x9B
{0x70, 0x88, 0x70, 0xA8, 0xB0}, // 0x9C ϡ
{0x38, 0x7C, 0xF8, 0x7C, 0x38}, // 0x9D 
{0x88, 0xC9, 0xAA, 0x99, 0x88}, // 0x9E ž
{0x1C, 0x21, 0xC0, 0x21, 0x1C}, // 0x9F Ÿ
{0x00, 0x00, 0x00, 0x00, 0x00}, // 0xA0
{0x00, 0x00, 0xF2, 0x00, 0x00}, // 0xA1 ¡
{0x38, 0x44, 0xFE, 0x44, 0x20}, // 0xA2 ¢
{0x90, 0x7C, 0x92, 0x82, 0x40}, // 0xA3 £
{0x44, 0x38, 0x28, 0x38, 0x44}, // 0xA4 ¤
{0x2A, 0x2C, 0xF8, 0x2C, 0x2A}, // 0xA5 ¥
{0x00, 0x00, 0xEE, 0x00, 0x00}, // 0xA6 ¦
{0x40, 0x94, 0xAA, 0x52, 0x04}, // 0xA7 §
{0x00, 0x02, 0x00, 0x02, 0x00}, // 0xA8 ¨
{0xFE, 0x82, 0xBA, 0x92, 0xFE}, // 0xA9 ©
{0x90, 0xAA, 0xAA, 0xAA, 0xBC}, // 0xAA ª
{0x20, 0x50, 0xA8, 0x50, 0x88}, // 0xAB «
{0x20, 0x20, 0x20, 0x20, 0xE0}, // 0xAC ¬
{0x20, 0x20, 0x20, 0x20, 0x20}, // 0xAD ­
{0xFE, 0x82, 0xCA, 0xA2, 0xFE}, // 0xAE ®
{0x02, 0x02, 0x02, 0x02, 0x02}, // 0xAF ¯
{0x0E, 0x11, 0x11, 0x0E, 0x00}, // 0xB0 °
{0x88, 0x88, 0xBE, 0x88, 0x88}, // 0xB1 ±
{0x12, 0x19, 0x15, 0x12, 0x00}, // 0xB2 ²
{0x11, 0x15, 0x15, 0x0A, 0x00}, // 0xB3 ³
{0x00, 0x08, 0x04, 0x02, 0x00}, // 0xB4 ´
{0xFE, 0x20, 0x20, 0x10, 0x3E}, // 0xB5 µ
{0x0C, 0x12, 0x12, 0xFE, 0xFE}, // 0xB6 ¶
{0x00, 0x30, 0x30, 0x00, 0x00}, // 0xB7 ·
{0x00, 0x80, 0xB0, 0x40, 0x00}, // 0xB8 ¸
{0x00, 0x02, 0x0F, 0x00, 0x00}, // 0xB9 ¹
{0x00, 0x02, 0x05, 0x02, 0x00}, // 0xBA º
{0x44, 0x28, 0x54, 0x28, 0x10}, // 0xBB »
{0x22, 0x1F, 0x68, 0x54, 0xFA}, // 0xBC ¼
{0x02, 0x1F, 0x90, 0xC8, 0xB0}, // 0xBD ½
{0x15, 0x1F, 0x60, 0x50, 0xF8}, // 0xBE ¾
{0x60, 0x90, 0x8A, 0x80, 0x40}, // 0xBF ¿
{0xF0, 0x29, 0x26, 0x28, 0xF0}, // 0xC0 À
{0xF0, 0x28, 0x26, 0x29, 0xF0}, // 0xC1 Á
{0xF0, 0x2A, 0x29, 0x2A, 0xF0}, // 0xC2 Â
{0xF2, 0x29, 0x29, 0x2A, 0xF1}, // 0xC3 Ã
{0xF0, 0x29, 0x24, 0x29, 0xF0}, // 0xC4 Ä
{0xF0, 0x2A, 0x2D, 0x2A, 0xF0}, // 0xC5 Å
{0xF8, 0x24, 0xFE, 0x92, 0x92}, // 0xC6 Æ
{0x1E, 0x21, 0xA1, 0xE1, 0x12}, // 0xC7 Ç
{0xF8, 0xA9, 0xAA, 0xA8, 0x88}, // 0xC8 È
{0xF8, 0xA8, 0xAA, 0xA9, 0x88}, // 0xC9 É
{0xF8, 0xAA, 0xA9, 0xAA, 0x88}, // 0xCA Ê
{0xF8, 0xAA, 0xA8, 0xAA, 0x88}, // 0xCB Ë
{0x00, 0x89, 0xFA, 0x88, 0x00}, // 0xCC Ì
{0x00, 0x88, 0xFA, 0x89, 0x00}, // 0xCD Í
{0x00, 0x8A, 0xF9, 0x8A, 0x00}, // 0xCE Î
{0x00, 0x8A, 0xF8, 0x8A, 0x00}, // 0xCF Ï
{0x10, 0xFE, 0x92, 0x82, 0x7C}, // 0xD0 Ð
{0xFA, 0x11, 0x21, 0x42, 0xF9}, // 0xD1 Ñ
{0x78, 0x85, 0x86, 0x84, 0x78}, // 0xD2 Ò
{0x78, 0x84, 0x86, 0x85, 0x78}, // 0xD3 Ó
{0x70, 0x8A, 0x89, 0x8A, 0x70}, // 0xD4 Ô
{0x72, 0x89, 0x89, 0x8A, 0x71}, // 0xD5 Õ
{0x78, 0x85, 0x84, 0x85, 0x78}, // 0xD6 Ö
{0x44, 0x28, 0x10, 0x28, 0x44}, // 0xD7 ×
{0x10, 0xAA, 0xFE, 0xAA, 0x10}, // 0xD8 Ø
{0x7C, 0x81, 0x82, 0x80, 0x7C}, // 0xD9 Ù
{0x7C, 0x80, 0x82, 0x81, 0x7C}, // 0xDA Ú
{0x78, 0x82, 0x81, 0x82, 0x78}, // 0xDB Û
{0x7C, 0x81, 0x80, 0x81, 0x7C}, // 0xDC Ü
{0x04, 0x08, 0xF2, 0x09, 0x04}, // 0xDD Ý
{0x81, 0xFF, 0x24, 0x24, 0x18}, // 0xDE Þ
{0x80, 0x7C, 0x92, 0x92, 0x6C}, // 0xDF ß
{0x40, 0xA9, 0xAA, 0xA8, 0xF0}, // 0xE0 à
{0x40, 0xA8, 0xAA, 0xA9, 0xF0}, // 0xE1 á
{0x40, 0xAA, 0xA9, 0xAA, 0xF0}, // 0xE2 â
{0x42, 0xA9, 0xA9, 0xAA, 0xF1}, // 0xE3 ã
{0x40, 0xAA, 0xA8, 0xAA, 0xF0}, // 0xE4 ä
{0x40, 0xAA, 0xAD, 0xAA, 0xF0}, // 0xE5 å
{0x64, 0x94, 0x78, 0x94, 0x58}, // 0xE6 æ
{0x18, 0x24, 0xA4, 0xE4, 0x10}, // 0xE7 ç
{0x70, 0xA9, 0xAA, 0xA8, 0x30}, // 0xE8 è
{0x70, 0xA8, 0xAA, 0xA9, 0x30}, // 0xE9 é
{0x70, 0xAA, 0xA9, 0xAA, 0x30}, // 0xEA ê
{0x70, 0xAA, 0xA8, 0xAA, 0x30}, // 0xEB ë
{0x00, 0x91, 0xFA, 0x80, 0x00}, // 0xEC ì
{0x00, 0x90, 0xFA, 0x81, 0x00}, // 0xED í
{0x00, 0x92, 0xF9, 0x82, 0x00}, // 0xEE î
{0x00, 0x92, 0xF8, 0x82, 0x00}, // 0xEF ï
{0x4A, 0xA4, 0xAA, 0xB0, 0x60}, // 0xF0 ð
{0xFA, 0x11, 0x09, 0x0A, 0xF1}, // 0xF1 ñ
{0x70, 0x89, 0x8A, 0x88, 0x70}, // 0xF2 ò
{0x70, 0x88, 0x8A, 0x89, 0x70}, // 0xF3 ó
{0x60, 0x94, 0x92, 0x94, 0x60}, // 0xF4 ô
{0x64, 0x92, 0x92, 0x94, 0x62}, // 0xF5 õ
{0x70, 0x8A, 0x88, 0x8A, 0x70}, // 0xF6 ö
{0x10, 0x10, 0x54, 0x10, 0x10}, // 0xF7 ÷
{0x10, 0xA8, 0x7C, 0x2A, 0x10}, // 0xF8 ø
{0x78, 0x81, 0x82, 0x40, 0xF8}, // 0xF9 ù
{0x78, 0x80, 0x82, 0x41, 0xF8}, // 0xFA ú
{0x78, 0x82, 0x81, 0x42, 0xF8}, // 0xFB û
{0x78, 0x82, 0x80, 0x42, 0xF8}, // 0xFC ü
{0x18, 0xA0, 0xA4, 0xA2, 0x78}, // 0xFD v
{0x00, 0x82, 0xFE, 0xA8, 0x10}, // 0xFE þ
{0x18, 0xA2, 0xA0, 0xA2, 0x78} // 0xFF ÿ
};
const uint8_t ascii_font_8x8[256][8] = {
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x00
{0x7E, 0x81, 0x95, 0xB1, 0xB1, 0x95, 0x81, 0x7E}, // 0x01
{0x7E, 0xFF, 0xEB, 0xCF, 0xCF, 0xEB, 0xFF, 0x7E}, // 0x02
{0x0E, 0x1F, 0x3F, 0x7E, 0x3F, 0x1F, 0x0E, 0x00}, // 0x03
{0x08, 0x1C, 0x3E, 0x7F, 0x3E, 0x1C, 0x08, 0x00}, // 0x04
{0x38, 0x3A, 0x9F, 0xFF, 0x9F, 0x3A, 0x38, 0x00}, // 0x05
{0x10, 0x38, 0xBC, 0xFF, 0xBC, 0x38, 0x10, 0x00}, // 0x06
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x07
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x08
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x09
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x0A
{0x70, 0xF8, 0x88, 0x88, 0xFD, 0x7F, 0x07, 0x0F}, // 0x0B
{0x00, 0x4E, 0x5F, 0xF1, 0xF1, 0x5F, 0x4E, 0x00}, // 0x0C
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x0D
{0xC0, 0xFF, 0x7F, 0x05, 0x05, 0x65, 0x7F, 0x3F}, // 0x0E
{0x99, 0x5A, 0x3C, 0xE7, 0xE7, 0x3C, 0x5A, 0x99}, // 0x0F
{0x7F, 0x3E, 0x3E, 0x1C, 0x1C, 0x08, 0x08, 0x00}, // 0x10
{0x08, 0x08, 0x1C, 0x1C, 0x3E, 0x3E, 0x7F, 0x00}, // 0x11
{0x00, 0x24, 0x66, 0xFF, 0xFF, 0x66, 0x24, 0x00}, // 0x12
{0x00, 0x5F, 0x5F, 0x00, 0x00, 0x5F, 0x5F, 0x00}, // 0x13
{0x06, 0x0F, 0x09, 0x7F, 0x7F, 0x01, 0x7F, 0x7F}, // 0x14
{0xDA, 0xBF, 0xA5, 0xA5, 0xFD, 0x59, 0x03, 0x02}, // 0x15
{0x00, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x00}, // 0x16
{0x80, 0x94, 0xB6, 0xFF, 0xFF, 0xB6, 0x94, 0x80}, // 0x17
{0x00, 0x04, 0x06, 0x7F, 0x7F, 0x06, 0x04, 0x00}, // 0x18
{0x00, 0x10, 0x30, 0x7F, 0x7F, 0x30, 0x10, 0x00}, // 0x19
{0x08, 0x08, 0x08, 0x2A, 0x3E, 0x1C, 0x08, 0x00}, // 0x1A
{0x08, 0x1C, 0x3E, 0x2A, 0x08, 0x08, 0x08, 0x00}, // 0x1B
{0x3C, 0x3C, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00}, // 0x1C
{0x08, 0x1C, 0x3E, 0x08, 0x08, 0x3E, 0x1C, 0x08}, // 0x1D
{0x30, 0x38, 0x3C, 0x3E, 0x3E, 0x3C, 0x38, 0x30}, // 0x1E
{0x06, 0x0E, 0x1E, 0x3E, 0x3E, 0x1E, 0x0E, 0x06}, // 0x1F
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x20
{0x00, 0x06, 0x5F, 0x5F, 0x06, 0x00, 0x00, 0x00}, // 0x21
{0x00, 0x07, 0x07, 0x00, 0x07, 0x07, 0x00, 0x00}, // 0x22
{0x14, 0x7F, 0x7F, 0x14, 0x7F, 0x7F, 0x14, 0x00}, // 0x23
{0x24, 0x2E, 0x6B, 0x6B, 0x3A, 0x12, 0x00, 0x00}, // 0x24
{0x46, 0x66, 0x30, 0x18, 0x0C, 0x66, 0x62, 0x00}, // 0x25
{0x30, 0x7A, 0x4F, 0x5D, 0x37, 0x7A, 0x48, 0x00}, // 0x26
{0x04, 0x07, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x27
{0x00, 0x1C, 0x3E, 0x63, 0x41, 0x00, 0x00, 0x00}, // 0x28
{0x00, 0x41, 0x63, 0x3E, 0x1C, 0x00, 0x00, 0x00}, // 0x29
{0x08, 0x2A, 0x3E, 0x1C, 0x1C, 0x3E, 0x2A, 0x08}, // 0x2A
{0x08, 0x08, 0x3E, 0x3E, 0x08, 0x08, 0x00, 0x00}, // 0x2B
{0x00, 0xA0, 0xE0, 0x60, 0x00, 0x00, 0x00, 0x00}, // 0x2C
{0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00}, // 0x2D
{0x00, 0x00, 0x60, 0x60, 0x00, 0x00, 0x00, 0x00}, // 0x2E
{0x60, 0x30, 0x18, 0x0C, 0x06, 0x03, 0x01, 0x00}, // 0x2F
{0x3E, 0x7F, 0x59, 0x4D, 0x7F, 0x3E, 0x00, 0x00}, // 0x30
{0x42, 0x42, 0x7F, 0x7F, 0x40, 0x40, 0x00, 0x00}, // 0x31
{0x62, 0x73, 0x59, 0x49, 0x6F, 0x66, 0x00, 0x00}, // 0x32
{0x22, 0x63, 0x49, 0x49, 0x7F, 0x36, 0x00, 0x00}, // 0x33
{0x18, 0x1C, 0x16, 0x13, 0x7F, 0x7F, 0x10, 0x00}, // 0x34
{0x27, 0x67, 0x45, 0x45, 0x7D, 0x39, 0x00, 0x00}, // 0x35
{0x3C, 0x7E, 0x4B, 0x49, 0x79, 0x30, 0x00, 0x00}, // 0x36
{0x03, 0x63, 0x71, 0x19, 0x0F, 0x07, 0x00, 0x00}, // 0x37
{0x36, 0x7F, 0x49, 0x49, 0x7F, 0x36, 0x00, 0x00}, // 0x38
{0x06, 0x4F, 0x49, 0x69, 0x3F, 0x1E, 0x00, 0x00}, // 0x39
{0x00, 0x00, 0x6C, 0x6C, 0x00, 0x00, 0x00, 0x00}, // 0x3A
{0x00, 0xA0, 0xEC, 0x6C, 0x00, 0x00, 0x00, 0x00}, // 0x3B
{0x08, 0x1C, 0x36, 0x63, 0x41, 0x00, 0x00, 0x00}, // 0x3C
{0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x00, 0x00}, // 0x3D
{0x00, 0x41, 0x63, 0x36, 0x1C, 0x08, 0x00, 0x00}, // 0x3E
{0x02, 0x03, 0x51, 0x59, 0x0F, 0x06, 0x00, 0x00}, // 0x3F
{0x3E, 0x7F, 0x41, 0x5D, 0x5D, 0x1F, 0x1E, 0x00}, // 0x40
{0x7C, 0x7E, 0x13, 0x13, 0x7E, 0x7C, 0x00, 0x00}, // 0x41
{0x41, 0x7F, 0x7F, 0x49, 0x49, 0x7F, 0x36, 0x00}, // 0x42
{0x1C, 0x3E, 0x63, 0x41, 0x41, 0x63, 0x22, 0x00}, // 0x43
{0x41, 0x7F, 0x7F, 0x41, 0x63, 0x7F, 0x1C, 0x00}, // 0x44
{0x41, 0x7F, 0x7F, 0x49, 0x5D, 0x41, 0x63, 0x00}, // 0x45
{0x41, 0x7F, 0x7F, 0x49, 0x1D, 0x01, 0x03, 0x00}, // 0x46
{0x1C, 0x3E, 0x63, 0x41, 0x51, 0x73, 0x72, 0x00}, // 0x47
{0x7F, 0x7F, 0x08, 0x08, 0x7F, 0x7F, 0x00, 0x00}, // 0x48
{0x00, 0x41, 0x7F, 0x7F, 0x41, 0x00, 0x00, 0x00}, // 0x49
{0x30, 0x70, 0x40, 0x41, 0x7F, 0x3F, 0x01, 0x00}, // 0x4A
{0x41, 0x7F, 0x7F, 0x08, 0x1C, 0x77, 0x63, 0x00}, // 0x4B
{0x41, 0x7F, 0x7F, 0x41, 0x40, 0x60, 0x70, 0x00}, // 0x4C
{0x7F, 0x7F, 0x06, 0x0C, 0x06, 0x7F, 0x7F, 0x00}, // 0x4D
{0x7F, 0x7F, 0x06, 0x0C, 0x18, 0x7F, 0x7F, 0x00}, // 0x4E
{0x1C, 0x3E, 0x63, 0x41, 0x63, 0x3E, 0x1C, 0x00}, // 0x4F
{0x41, 0x7F, 0x7F, 0x49, 0x09, 0x0F, 0x06, 0x00}, // 0x50
{0x1E, 0x3F, 0x21, 0x71, 0x7F, 0x5E, 0x00, 0x00}, // 0x51
{0x41, 0x7F, 0x7F, 0x19, 0x39, 0x6F, 0x46, 0x00}, // 0x52
{0x26, 0x67, 0x4D, 0x59, 0x7B, 0x32, 0x00, 0x00}, // 0x53
{0x03, 0x41, 0x7F, 0x7F, 0x41, 0x03, 0x00, 0x00}, // 0x54
{0x7F, 0x7F, 0x40, 0x40, 0x7F, 0x7F, 0x00, 0x00}, // 0x55
{0x1F, 0x3F, 0x60, 0x60, 0x3F, 0x1F, 0x00, 0x00}, // 0x56
{0x7F, 0x7F, 0x30, 0x18, 0x30, 0x7F, 0x7F, 0x00}, // 0x57
{0x63, 0x77, 0x1C, 0x08, 0x1C, 0x77, 0x63, 0x00}, // 0x58
{0x07, 0x4F, 0x78, 0x78, 0x4F, 0x07, 0x00, 0x00}, // 0x59
{0x67, 0x73, 0x59, 0x4D, 0x47, 0x63, 0x71, 0x00}, // 0x5A
{0x00, 0x7F, 0x7F, 0x41, 0x41, 0x00, 0x00, 0x00}, // 0x5B
{0x01, 0x03, 0x06, 0x0C, 0x18, 0x30, 0x60, 0x00}, // 0x5C
{0x00, 0x41, 0x41, 0x7F, 0x7F, 0x00, 0x00, 0x00}, // 0x5D
{0x08, 0x0C, 0x06, 0x03, 0x06, 0x0C, 0x08, 0x00}, // 0x5E
{0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80}, // 0x5F
{0x00, 0x00, 0x03, 0x07, 0x04, 0x00, 0x00, 0x00}, // 0x60
{0x20, 0x74, 0x54, 0x54, 0x3C, 0x78, 0x40, 0x00}, // 0x61
{0x41, 0x3F, 0x7F, 0x44, 0x44, 0x7C, 0x38, 0x00}, // 0x62
{0x38, 0x7C, 0x44, 0x44, 0x6C, 0x28, 0x00, 0x00}, // 0x63
{0x30, 0x78, 0x48, 0x49, 0x3F, 0x7F, 0x40, 0x00}, // 0x64
{0x38, 0x7C, 0x54, 0x54, 0x5C, 0x18, 0x00, 0x00}, // 0x65
{0x48, 0x7E, 0x7F, 0x49, 0x03, 0x02, 0x00, 0x00}, // 0x66
{0x98, 0xBC, 0xA4, 0xA4, 0xF8, 0x7C, 0x04, 0x00}, // 0x67
{0x41, 0x7F, 0x7F, 0x08, 0x04, 0x7C, 0x78, 0x00}, // 0x68
{0x00, 0x44, 0x7D, 0x7D, 0x40, 0x00, 0x00, 0x00}, // 0x69
{0x40, 0xC4, 0x84, 0xFD, 0x7D, 0x00, 0x00, 0x00}, // 0x6A
{0x41, 0x7F, 0x7F, 0x10, 0x38, 0x6C, 0x44, 0x00}, // 0x6B
{0x00, 0x41, 0x7F, 0x7F, 0x40, 0x00, 0x00, 0x00}, // 0x6C
{0x7C, 0x7C, 0x0C, 0x18, 0x0C, 0x7C, 0x78, 0x00}, // 0x6D
{0x7C, 0x7C, 0x04, 0x04, 0x7C, 0x78, 0x00, 0x00}, // 0x6E
{0x38, 0x7C, 0x44, 0x44, 0x7C, 0x38, 0x00, 0x00}, // 0x6F
{0x84, 0xFC, 0xF8, 0xA4, 0x24, 0x3C, 0x18, 0x00}, // 0x70
{0x18, 0x3C, 0x24, 0xA4, 0xF8, 0xFC, 0x84, 0x00}, // 0x71
{0x44, 0x7C, 0x78, 0x44, 0x1C, 0x18, 0x00, 0x00}, // 0x72
{0x48, 0x5C, 0x54, 0x54, 0x74, 0x24, 0x00, 0x00}, // 0x73
{0x00, 0x04, 0x3E, 0x7F, 0x44, 0x24, 0x00, 0x00}, // 0x74
{0x3C, 0x7C, 0x40, 0x40, 0x3C, 0x7C, 0x40, 0x00}, // 0x75
{0x1C, 0x3C, 0x60, 0x60, 0x3C, 0x1C, 0x00, 0x00}, // 0x76
{0x3C, 0x7C, 0x60, 0x30, 0x60, 0x7C, 0x3C, 0x00}, // 0x77
{0x44, 0x6C, 0x38, 0x10, 0x38, 0x6C, 0x44, 0x00}, // 0x78
{0x9C, 0xBC, 0xA0, 0xA0, 0xFC, 0x7C, 0x00, 0x00}, // 0x79
{0x4C, 0x64, 0x74, 0x5C, 0x4C, 0x64, 0x00, 0x00}, // 0x7A
{0x08, 0x08, 0x3E, 0x77, 0x41, 0x41, 0x00, 0x00}, // 0x7B
{0x00, 0x00, 0x00, 0x77, 0x77, 0x00, 0x00, 0x00}, // 0x7C
{0x41, 0x41, 0x77, 0x3E, 0x08, 0x08, 0x00, 0x00}, // 0x7D
{0x02, 0x03, 0x01, 0x03, 0x02, 0x03, 0x01, 0x00}, // 0x7E
{0x78, 0x7C, 0x46, 0x43, 0x46, 0x7C, 0x78, 0x00}, // 0x7F
{0x1E, 0xBF, 0xE1, 0x61, 0x33, 0x12, 0x00, 0x00}, // 0x80
{0x3A, 0x7A, 0x40, 0x40, 0x7A, 0x7A, 0x40, 0x00}, // 0x81
{0x38, 0x7C, 0x56, 0x57, 0x5D, 0x18, 0x00, 0x00}, // 0x82
{0x02, 0x23, 0x75, 0x55, 0x55, 0x7D, 0x7B, 0x42}, // 0x83
{0x21, 0x75, 0x54, 0x54, 0x7D, 0x79, 0x40, 0x00}, // 0x84
{0x20, 0x75, 0x57, 0x56, 0x7C, 0x78, 0x40, 0x00}, // 0x85
{0x00, 0x22, 0x77, 0x55, 0x55, 0x7F, 0x7A, 0x40}, // 0x86
{0x1C, 0xBE, 0xE2, 0x62, 0x36, 0x14, 0x00, 0x00}, // 0x87
{0x02, 0x3B, 0x7D, 0x55, 0x55, 0x5D, 0x1B, 0x02}, // 0x88
{0x39, 0x7D, 0x54, 0x54, 0x5D, 0x19, 0x00, 0x00}, // 0x89
{0x38, 0x7D, 0x57, 0x56, 0x5C, 0x18, 0x00, 0x00}, // 0x8A
{0x01, 0x45, 0x7C, 0x7C, 0x41, 0x01, 0x00, 0x00}, // 0x8B
{0x02, 0x03, 0x45, 0x7D, 0x7D, 0x43, 0x02, 0x00}, // 0x8C
{0x00, 0x45, 0x7F, 0x7E, 0x40, 0x00, 0x00, 0x00}, // 0x8D
{0x79, 0x7D, 0x26, 0x26, 0x7D, 0x79, 0x00, 0x00}, // 0x8E
{0x70, 0x7A, 0x2D, 0x2D, 0x7A, 0x70, 0x00, 0x00}, // 0x8F
{0x44, 0x7C, 0x7E, 0x57, 0x55, 0x44, 0x00, 0x00}, // 0x90
{0x20, 0x74, 0x54, 0x54, 0x7C, 0x7C, 0x54, 0x54}, // 0x91
{0x7C, 0x7E, 0x0B, 0x09, 0x7F, 0x7F, 0x49, 0x00}, // 0x92
{0x32, 0x7B, 0x49, 0x49, 0x7B, 0x32, 0x00, 0x00}, // 0x93
{0x32, 0x7A, 0x48, 0x48, 0x7A, 0x32, 0x00, 0x00}, // 0x94
{0x30, 0x79, 0x4B, 0x4A, 0x78, 0x30, 0x00, 0x00}, // 0x95
{0x3A, 0x7B, 0x41, 0x41, 0x7B, 0x7A, 0x40, 0x00}, // 0x96
{0x38, 0x79, 0x43, 0x42, 0x78, 0x78, 0x40, 0x00}, // 0x97
{0xBA, 0xBA, 0xA0, 0xA0, 0xFA, 0x7A, 0x00, 0x00}, // 0x98
{0x39, 0x7D, 0x44, 0x44, 0x44, 0x7D, 0x39, 0x00}, // 0x99
{0x3D, 0x7D, 0x40, 0x40, 0x7D, 0x3D, 0x00, 0x00}, // 0x9A
{0x38, 0x7C, 0x64, 0x54, 0x4C, 0x7C, 0x38, 0x00}, // 0x9B
{0x68, 0x7E, 0x7F, 0x49, 0x43, 0x66, 0x20, 0x00}, // 0x9C
{0x5C, 0x3E, 0x73, 0x49, 0x67, 0x3E, 0x1D, 0x00}, // 0x9D
{0x44, 0x6C, 0x38, 0x38, 0x6C, 0x44, 0x00, 0x00}, // 0x9E
{0x40, 0xC8, 0x88, 0xFE, 0x7F, 0x09, 0x0B, 0x02}, // 0x9F
{0x20, 0x74, 0x56, 0x57, 0x7D, 0x78, 0x40, 0x00}, // 0xA0
{0x00, 0x44, 0x7E, 0x7F, 0x41, 0x00, 0x00, 0x00}, // 0xA1
{0x30, 0x78, 0x48, 0x4A, 0x7B, 0x31, 0x00, 0x00}, // 0xA2
{0x38, 0x78, 0x40, 0x42, 0x7B, 0x79, 0x40, 0x00}, // 0xA3
{0x7A, 0x7B, 0x09, 0x0B, 0x7A, 0x73, 0x01, 0x00}, // 0xA4
{0x7A, 0x7B, 0x19, 0x33, 0x7A, 0x7B, 0x01, 0x00}, // 0xA5
{0x00, 0x26, 0x2F, 0x29, 0x2F, 0x2F, 0x28, 0x00}, // 0xA6
{0x00, 0x26, 0x2F, 0x29, 0x29, 0x2F, 0x26, 0x00}, // 0xA7
{0x30, 0x78, 0x4D, 0x45, 0x60, 0x20, 0x00, 0x00}, // 0xA8
{0x1C, 0x22, 0x7D, 0x4B, 0x5B, 0x65, 0x22, 0x1C}, // 0xA9
{0x08, 0x08, 0x08, 0x08, 0x38, 0x38, 0x00, 0x00}, // 0xAA
{0x61, 0x3F, 0x1F, 0xCC, 0xEE, 0xAB, 0xB9, 0x90}, // 0xAB
{0x61, 0x3F, 0x1F, 0x4C, 0x66, 0x73, 0xD9, 0xF8}, // 0xAC
{0x00, 0x00, 0x60, 0xFA, 0xFA, 0x60, 0x00, 0x00}, // 0xAD
{0x08, 0x1C, 0x36, 0x22, 0x08, 0x1C, 0x36, 0x22}, // 0xAE
{0x22, 0x36, 0x1C, 0x08, 0x22, 0x36, 0x1C, 0x08}, // 0xAF
{0xAA, 0x00, 0x55, 0x00, 0xAA, 0x00, 0x55, 0x00}, // 0xB0
{0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55}, // 0xB1
{0x55, 0xFF, 0xAA, 0xFF, 0x55, 0xFF, 0xAA, 0xFF}, // 0xB2
{0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00}, // 0xB3
{0x10, 0x10, 0x10, 0xFF, 0xFF, 0x00, 0x00, 0x00}, // 0xB4
{0x70, 0x78, 0x2C, 0x2E, 0x7B, 0x71, 0x00, 0x00}, // 0xB5
{0x72, 0x79, 0x2D, 0x2D, 0x79, 0x72, 0x00, 0x00}, // 0xB6
{0x71, 0x7B, 0x2E, 0x2C, 0x78, 0x70, 0x00, 0x00}, // 0xB7
{0x1C, 0x22, 0x5D, 0x55, 0x55, 0x41, 0x22, 0x1C}, // 0xB8
{0x14, 0x14, 0xF7, 0xF7, 0x00, 0xFF, 0xFF, 0x00}, // 0xB9
{0x00, 0x00, 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0x00}, // 0xBA
{0x14, 0x14, 0xF4, 0xF4, 0x04, 0xFC, 0xFC, 0x00}, // 0xBB
{0x14, 0x14, 0x17, 0x17, 0x10, 0x1F, 0x1F, 0x00}, // 0xBC
{0x18, 0x3C, 0x24, 0xE7, 0xE7, 0x24, 0x24, 0x00}, // 0xBD
{0x2B, 0x2F, 0xFC, 0xFC, 0x2F, 0x2B, 0x00, 0x00}, // 0xBE
{0x10, 0x10, 0x10, 0xF0, 0xF0, 0x00, 0x00, 0x00}, // 0xBF
{0x00, 0x00, 0x00, 0x1F, 0x1F, 0x10, 0x10, 0x10}, // 0xC0
{0x10, 0x10, 0x10, 0x1F, 0x1F, 0x10, 0x10, 0x10}, // 0xC1
{0x10, 0x10, 0x10, 0xF0, 0xF0, 0x10, 0x10, 0x10}, // 0xC2
{0x00, 0x00, 0x00, 0xFF, 0xFF, 0x10, 0x10, 0x10}, // 0xC3
{0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10}, // 0xC4
{0x10, 0x10, 0x10, 0xFF, 0xFF, 0x10, 0x10, 0x10}, // 0xC5
{0x22, 0x77, 0x55, 0x57, 0x7E, 0x7B, 0x41, 0x00}, // 0xC6
{0x72, 0x7B, 0x2D, 0x2F, 0x7A, 0x73, 0x01, 0x00}, // 0xC7
{0x00, 0x00, 0x1F, 0x1F, 0x10, 0x17, 0x17, 0x14}, // 0xC8
{0x00, 0x00, 0xFC, 0xFC, 0x04, 0xF4, 0xF4, 0x14}, // 0xC9
{0x14, 0x14, 0x17, 0x17, 0x10, 0x17, 0x17, 0x14}, // 0xCA
{0x14, 0x14, 0xF4, 0xF4, 0x04, 0xF4, 0xF4, 0x14}, // 0xCB
{0x00, 0x00, 0xFF, 0xFF, 0x00, 0xF7, 0xF7, 0x14}, // 0xCC
{0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14}, // 0xCD
{0x14, 0x14, 0xF7, 0xF7, 0x00, 0xF7, 0xF7, 0x14}, // 0xCE
{0x66, 0x3C, 0x3C, 0x24, 0x3C, 0x3C, 0x66, 0x00}, // 0xCF
{0x05, 0x27, 0x72, 0x57, 0x7D, 0x38, 0x00, 0x00}, // 0xD0
{0x49, 0x7F, 0x7F, 0x49, 0x63, 0x7F, 0x1C, 0x00}, // 0xD1
{0x46, 0x7D, 0x7D, 0x55, 0x55, 0x46, 0x00, 0x00}, // 0xD2
{0x45, 0x7D, 0x7C, 0x54, 0x55, 0x45, 0x00, 0x00}, // 0xD3
{0x44, 0x7D, 0x7F, 0x56, 0x54, 0x44, 0x00, 0x00}, // 0xD4
{0x0A, 0x0E, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0xD5
{0x00, 0x44, 0x7E, 0x7F, 0x45, 0x00, 0x00, 0x00}, // 0xD6
{0x02, 0x45, 0x7D, 0x7D, 0x45, 0x02, 0x00, 0x00}, // 0xD7
{0x01, 0x45, 0x7C, 0x7C, 0x45, 0x01, 0x00, 0x00}, // 0xD8
{0x10, 0x10, 0x10, 0x1F, 0x1F, 0x00, 0x00, 0x00}, // 0xD9
{0x00, 0x00, 0x00, 0xF0, 0xF0, 0x10, 0x10, 0x10}, // 0xDA
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, // 0xDB
{0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0}, // 0xDC
{0x00, 0x00, 0x00, 0x77, 0x77, 0x00, 0x00, 0x00}, // 0xDD
{0x00, 0x45, 0x7F, 0x7E, 0x44, 0x00, 0x00, 0x00}, // 0xDE
{0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F}, // 0xDF
{0x38, 0x7C, 0x46, 0x47, 0x45, 0x7C, 0x38, 0x00}, // 0xE0
{0xFC, 0xFE, 0x2A, 0x2A, 0x3E, 0x14, 0x00, 0x00}, // 0xE1
{0x3A, 0x7D, 0x45, 0x45, 0x45, 0x7D, 0x3A, 0x00}, // 0xE2
{0x38, 0x7C, 0x45, 0x47, 0x46, 0x7C, 0x38, 0x00}, // 0xE3
{0x32, 0x7B, 0x49, 0x4B, 0x7A, 0x33, 0x01, 0x00}, // 0xE4
{0x3A, 0x7F, 0x45, 0x47, 0x46, 0x7F, 0x39, 0x00}, // 0xE5
{0x80, 0xFE, 0x7E, 0x20, 0x20, 0x3E, 0x1E, 0x00}, // 0xE6
{0x42, 0x7E, 0x7E, 0x54, 0x1C, 0x08, 0x00, 0x00}, // 0xE7
{0x41, 0x7F, 0x7F, 0x55, 0x14, 0x1C, 0x08, 0x00}, // 0xE8
{0x3C, 0x7C, 0x42, 0x43, 0x7D, 0x3C, 0x00, 0x00}, // 0xE9
{0x3A, 0x79, 0x41, 0x41, 0x79, 0x3A, 0x00, 0x00}, // 0xEA
{0x3C, 0x7D, 0x43, 0x42, 0x7C, 0x3C, 0x00, 0x00}, // 0xEB
{0xB8, 0xB8, 0xA2, 0xA3, 0xF9, 0x78, 0x00, 0x00}, // 0xEC
{0x0C, 0x5C, 0x72, 0x73, 0x5D, 0x0C, 0x00, 0x00}, // 0xED
{0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x00}, // 0xEE
{0x00, 0x00, 0x02, 0x03, 0x01, 0x00, 0x00, 0x00}, // 0xEF
{0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00}, // 0xF0
{0x44, 0x44, 0x5F, 0x5F, 0x44, 0x44, 0x00, 0x00}, // 0xF1
{0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x00, 0x00}, // 0xF2
{0x71, 0x35, 0x1F, 0x4C, 0x66, 0x73, 0xD9, 0xF8}, // 0xF3
{0x06, 0x0F, 0x09, 0x7F, 0x7F, 0x01, 0x7F, 0x7F}, // 0xF4
{0xDA, 0xBF, 0xA5, 0xA5, 0xFD, 0x59, 0x03, 0x02}, // 0xF5
{0x08, 0x08, 0x6B, 0x6B, 0x08, 0x08, 0x00, 0x00}, // 0xF6
{0x00, 0x80, 0xC0, 0x40, 0x00, 0x00, 0x00, 0x00}, // 0xF7
{0x00, 0x06, 0x0F, 0x09, 0x0F, 0x06, 0x00, 0x00}, // 0xF8
{0x02, 0x02, 0x00, 0x00, 0x02, 0x02, 0x00, 0x00}, // 0xF9
{0x00, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00, 0x00}, // 0xFA
{0x00, 0x12, 0x13, 0x1F, 0x1F, 0x10, 0x10, 0x00}, // 0xFB
{0x00, 0x11, 0x15, 0x15, 0x1F, 0x1F, 0x0A, 0x00}, // 0xFC
{0x00, 0x19, 0x1D, 0x15, 0x17, 0x12, 0x00, 0x00}, // 0xFD
{0x00, 0x00, 0x3C, 0x3C, 0x3C, 0x3C, 0x00, 0x00}, // 0xFE
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // 0xFF
};
#endif
+103
View File
@@ -0,0 +1,103 @@
// Copyright 2020 Lukas Haubaum
//
// Licensed under the GNU Affero General Public License, Version 3;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// https://www.gnu.org/licenses/agpl-3.0.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef _ssd1306_H_
#define _ssd1306_H_
#define SSD1306_ADDRESS (0x3C)
#define SSD1306_COLUMNS (128)
#define SSD1306_PAGES (8)
// Write mode for I2C https://robotcantalk.blogspot.com/2015/03/interfacing-arduino-with-ssd1306-driven.html
#define SSD1306_CONTROL_CMD_BYTE (0x80)
#define SSD1306_CONTROL_CMD_STREAM (0x00)
#define SSD1306_CONTROL_DATA_BYTE (0xC0)
#define SSD1306_CONTROL_DATA_STREAM (0x40)
// 1. Fundamental Command Table
#define SSD1306_CMD_CONTRAST (0x81)
#define SSD1306_CMD_RAM (0xA4)
#define SSD1306_CMD_ALL_ON (0xA5)
#define SSD1306_CMD_NORMAL (0xA6)
#define SSD1306_CMD_INVERSE (0xA7)
#define SSD1306_CMD_OFF (0xAE)
#define SSD1306_CMD_ON (0xAF)
// 2. Scrolling Command Table
#define SSD1306_CMD_SCROLL_HORI_RIGHT (0x26)
#define SSD1306_CMD_SCROLL_HORI_LEFT (0x27)
#define SSD1306_CMD_SCROLL_VERT_RIGHT (0x29)
#define SSD1306_CMD_SCROLL_VERT_LEFT (0x2A)
#define SSD1306_CMD_SCROLL_STOP (0x2E)
#define SSD1306_CMD_SCROLL_START (0x2F)
#define SSD1306_CMD_SCROLL_VERT_AREA (0xA3)
// 3. Addressing Setting Command Table
#define SSD1306_CMD_COLUMN_LOW (0x00)
#define SSD1306_CMD_COLUMN_HIGH (0x10)
#define SSD1306_CMD_MEMORY_MODE (0x20)
#define SSD1306_CMD_COLUMN_ADDRESS (0x21)
#define SSD1306_CMD_PAGE_ADDRESS (0x22)
#define SSD1306_CMD_PAGE (0xB0)
// 4. Hardware Configuration (Panel resolution & layout related) Command Table
#define SSD1306_CMD_START_LINE (0x40)
#define SSD1306_CMD_SEGMENT_LOW (0xA0)
#define SSD1306_CMD_SEGMENT_HIGH (0xA1)
#define SSD1306_CMD_MULTIPLEX_RATIO (0xA8)
#define SSD1306_CMD_SCAN_DIRECTION_NORMAL (0xC0)
#define SSD1306_CMD_SCAN_DIRECTION_REMAPPED (0xC8)
#define SSD1306_CMD_OFFSET (0xD3)
#define SSD1306_CMD_COM_PINS (0xDA)
// 5. Timing & Driving Scheme Setting Command Table
#define SSD1306_CMD_CLOCK (0xD5)
#define SSD1306_CMD_PRE_CHARGE_PERIOD (0xD9)
#define SSD1306_CMD_VCOMH (0xDB)
#define SSD1306_CMD_NOP (0xE3)
// 1. Charge Pump Command Table
#define SSD1306_CMD_CHARGE_PUMP (0x8D)
/**
* @brief initalize SSD1306 with I2C at given address
*
* @param[in] i2address I2C address of SSD1306
*/
void ssd1306_start(uint8_t i2address);
/**
* @brief clear the display
*
* @param[in] i2address I2C address of SSD1306
* @param[in] line the line to clear
*/
void ssd1306_clear_line(uint8_t i2address, uint8_t line, bool invert);
/**
* @brief clear the display
*
* @param[in] i2address I2C address of SSD1306
*/
void ssd1306_clear(uint8_t i2address);
/**
* @brief write text to display
*
* @param[in] i2address I2C address of SSD1306
* @param[in] text text to display
* @param[in] line the line to write to
*/
void ssd1306_text_line(uint8_t i2address, char *text, uint8_t line, bool invert);
#endif
+193
View File
@@ -0,0 +1,193 @@
// Copyright 2020 Lukas Haubaum
//
// Licensed under the GNU Affero General Public License, Version 3;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// https://www.gnu.org/licenses/agpl-3.0.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <string.h>
#include "driver/i2c.h"
#include "esp_log.h"
#include "i2c-main.h"
#include "ssd1306-ascii.h"
#include "ssd1306.h"
void ssd1306_start(uint8_t i2address)
{
if (!i2c_is_initialized())
{
i2c_main_init();
}
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
// Begin the I2C comm with SSD1306's address (SLA+Write)
i2c_master_write_byte(cmd, (i2address << 1) | I2C_MASTER_WRITE, true);
// Tell the SSD1306 that a command stream is incoming
i2c_master_write_byte(cmd, SSD1306_CONTROL_CMD_STREAM, true);
// Turn the Display OFF
i2c_master_write_byte(cmd, SSD1306_CMD_OFF, true);
// Set mux ration tp select max number of rows - 64
i2c_master_write_byte(cmd, SSD1306_CMD_MULTIPLEX_RATIO, true);
i2c_master_write_byte(cmd, 0x3F, true);
// Set the display offset to 0
i2c_master_write_byte(cmd, SSD1306_CMD_OFFSET, true);
i2c_master_write_byte(cmd, 0x00, true);
// Display start line to 0
i2c_master_write_byte(cmd, SSD1306_CMD_START_LINE, true);
// Mirror the x-axis. In case you set it up such that the pins are north.
i2c_master_write_byte(cmd, SSD1306_CMD_SEGMENT_HIGH, true);
// Mirror the y-axis. In case you set it up such that the pins are north.
i2c_master_write_byte(cmd, SSD1306_CMD_SCAN_DIRECTION_REMAPPED, true);
// Default - alternate COM pin map
i2c_master_write_byte(cmd, SSD1306_CMD_COM_PINS, true);
i2c_master_write_byte(cmd, 0x12, true);
// set contrast
i2c_master_write_byte(cmd, SSD1306_CMD_CONTRAST, true);
i2c_master_write_byte(cmd, 0xFF, true);
// Set display to enable rendering from GDDRAM (Graphic Display Data RAM)
i2c_master_write_byte(cmd, SSD1306_CMD_RAM, true);
// Normal mode!
i2c_master_write_byte(cmd, SSD1306_CMD_NORMAL, true);
// Default oscillator clock
i2c_master_write_byte(cmd, SSD1306_CMD_CLOCK, true);
i2c_master_write_byte(cmd, 0x80, true);
// Enable the charge pump
i2c_master_write_byte(cmd, SSD1306_CMD_CHARGE_PUMP, true);
i2c_master_write_byte(cmd, 0x14, true);
// Set precharge cycles to high cap type
i2c_master_write_byte(cmd, SSD1306_CMD_PRE_CHARGE_PERIOD, true);
i2c_master_write_byte(cmd, 0x22, true);
// Set the V_COMH deselect volatage to max
i2c_master_write_byte(cmd, SSD1306_CMD_VCOMH, true);
i2c_master_write_byte(cmd, 0x30, true);
// Horizonatal addressing mode to page addressing
i2c_master_write_byte(cmd, SSD1306_CMD_MEMORY_MODE, true);
i2c_master_write_byte(cmd, 0x02, true);
//i2c_master_write_byte(cmd, 0x00, true);
// i2c_master_write_byte(cmd, 0x10, true);
// i2c_master_write_byte(cmd, SSD1306_CMD_SCROLL_STOP, true);
// Turn the Display ON
i2c_master_write_byte(cmd, SSD1306_CMD_ON, true);
i2c_master_stop(cmd);
ESP_ERROR_CHECK(i2c_master_cmd_begin(I2C_NUM_0, cmd, 10 / portTICK_PERIOD_MS));
i2c_cmd_link_delete(cmd);
}
void ssd1306_init_data(uint8_t i2address)
{
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
// Begin the I2C comm with SSD1306's address (SLA+Write)
i2c_master_write_byte(cmd, (i2address << 1) | I2C_MASTER_WRITE, true);
// Tell the SSD1306 that a command stream is incoming
i2c_master_write_byte(cmd, SSD1306_CONTROL_CMD_STREAM, true);
// set column start + end
i2c_master_write_byte(cmd, SSD1306_CMD_COLUMN_LOW, true);
i2c_master_write_byte(cmd, SSD1306_CMD_COLUMN_HIGH, true);
// set page
i2c_master_write_byte(cmd, SSD1306_CMD_PAGE, true);
i2c_master_stop(cmd);
ESP_ERROR_CHECK(i2c_master_cmd_begin(I2C_NUM_0, cmd, 10 / portTICK_PERIOD_MS));
i2c_cmd_link_delete(cmd);
}
void ssd1306_clear_line(uint8_t i2address, uint8_t line, bool invert)
{
i2c_cmd_handle_t cmd;
uint8_t *zeros = calloc(SSD1306_COLUMNS, sizeof(uint8_t));
// set line
cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (i2address << 1) | I2C_MASTER_WRITE, true);
i2c_master_write_byte(cmd, SSD1306_CONTROL_CMD_STREAM, true);
i2c_master_write_byte(cmd, SSD1306_CMD_COLUMN_LOW, true);
i2c_master_write_byte(cmd, SSD1306_CMD_COLUMN_HIGH, true);
i2c_master_write_byte(cmd, SSD1306_CMD_PAGE | line, true);
i2c_master_stop(cmd);
i2c_master_cmd_begin(I2C_NUM_0, cmd, 10 / portTICK_PERIOD_MS);
i2c_cmd_link_delete(cmd);
// fill line with zeros
cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (i2address << 1) | I2C_MASTER_WRITE, true);
i2c_master_write_byte(cmd, SSD1306_CONTROL_DATA_STREAM, true);
i2c_master_write(cmd, zeros, SSD1306_COLUMNS, true);
i2c_master_stop(cmd);
i2c_master_cmd_begin(I2C_NUM_0, cmd, 10 / portTICK_PERIOD_MS);
i2c_cmd_link_delete(cmd);
free(zeros);
}
void ssd1306_clear(uint8_t i2address)
{
for (uint8_t i = 0; i < SSD1306_PAGES; i++)
{
ssd1306_clear_line(i2address, i, false);
}
}
void ssd1306_text_line(uint8_t i2address, char *text, uint8_t line, bool invert)
{
ssd1306_init_data(i2address);
i2c_cmd_handle_t cmd;
// set line
cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (i2address << 1) | I2C_MASTER_WRITE, true);
i2c_master_write_byte(cmd, SSD1306_CONTROL_CMD_STREAM, true);
i2c_master_write_byte(cmd, SSD1306_CMD_COLUMN_LOW, true);
i2c_master_write_byte(cmd, SSD1306_CMD_COLUMN_HIGH, true);
i2c_master_write_byte(cmd, SSD1306_CMD_PAGE | line, true);
i2c_master_stop(cmd);
i2c_master_cmd_begin(I2C_NUM_0, cmd, 10 / portTICK_PERIOD_MS);
i2c_cmd_link_delete(cmd);
uint8_t *linedata = calloc(SSD1306_COLUMNS, sizeof(uint8_t));
uint8_t font_width = sizeof(ascii_font_5x8[0]);
for (uint8_t i = 0; i < strlen(text); i++)
{
memcpy(&linedata[i * font_width], ascii_font_5x8[(uint8_t)text[i]], font_width);
}
if (invert)
{
for (uint8_t i = 0; i < SSD1306_COLUMNS; i++)
{
linedata[i] = ~linedata[i];
}
}
cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (i2address << 1) | I2C_MASTER_WRITE, true);
i2c_master_write_byte(cmd, SSD1306_CONTROL_DATA_STREAM, true);
i2c_master_write(cmd, linedata, SSD1306_COLUMNS, true);
i2c_master_stop(cmd);
i2c_master_cmd_begin(I2C_NUM_0, cmd, 10 / portTICK_PERIOD_MS);
i2c_cmd_link_delete(cmd);
free(linedata);
}