Skip to content

Commit

Permalink
2019-11-13 add read single data from csv file
Browse files Browse the repository at this point in the history
2019-11-13 add read single data from csv file
  • Loading branch information
xuhongv committed Nov 14, 2019
1 parent d7bf29d commit 9cc2cdf
Show file tree
Hide file tree
Showing 8 changed files with 640 additions and 0 deletions.
6 changes: 6 additions & 0 deletions 14_ReadFromCsvSingle/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# The following four lines of boilerplate have to be in your project's CMakeLists
# in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(project_template)
9 changes: 9 additions & 0 deletions 14_ReadFromCsvSingle/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
# project subdirectory.
#

PROJECT_NAME := project_template

include $(IDF_PATH)/make/project.mk

3 changes: 3 additions & 0 deletions 14_ReadFromCsvSingle/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
set(COMPONENT_SRCS "user_main.c")

register_component()
5 changes: 5 additions & 0 deletions 14_ReadFromCsvSingle/main/component.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#
# "main" pseudo-component makefile.
#
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)

142 changes: 142 additions & 0 deletions 14_ReadFromCsvSingle/main/user_main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
* @Author: your name
* @Date: 2019-10-13 12:49:47
* @LastEditTime: 2019-11-14 21:33:00
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: \ESP8266_RTOS_SDK\examples\get-started\project_template\main\user_main.c
*/
#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"

#include "esp_system.h"
#include "esp_err.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "nvs.h"

//这个宏的数值是从 自定义分区表获取
#define MFG_PARTITION_NAME "fctry"
//这个宏的数值是从 csv文件定义获取
#define NVS_PRODUCT "user-info"

static const char *TAG = "wrapper_product";

/**
* @description: 读取数据进去nvs里面的任务
* @param {type} null
* @return:
*/
static void Task_NVS_Read(void *pvParameters)
{

ESP_LOGI(TAG, "--------------------------- Start Task_NVS_Read --------------------------");

nvs_handle mHandleNvsRead;
uint8_t nvs_i8 = 0;
esp_err_t err;

//初始化分区
if ((err = nvs_flash_init_partition(MFG_PARTITION_NAME)) != ESP_OK)
{
ESP_LOGE(TAG, "NVS Flash init %s failed, Please check that you have flashed fctry partition!!!", MFG_PARTITION_NAME);
vTaskDelete(NULL);
}

//NVS操作的句柄,类似于 rtos系统的任务创建返回的句柄!
err = nvs_open_from_partition(MFG_PARTITION_NAME, NVS_PRODUCT, NVS_READONLY, &mHandleNvsRead);

//打开数据库,打开一个数据库就相当于会返回一个句柄
if (err != ESP_OK)
{
ESP_LOGE(TAG, "Open NVS Table fail");
vTaskDelete(NULL);
}
else
{
ESP_LOGI(TAG, "Open NVS Table ok.");
}

//读取 字符串 --> 名字
char data[65];
uint32_t len = sizeof(data);
err = nvs_get_str(mHandleNvsRead, "Name", data, &len);

if (err == ESP_OK)
ESP_LOGI(TAG, "get str data = %s ", data);
else
ESP_LOGI(TAG, "get str data error");

//读取 u8 年龄
err = nvs_get_u8(mHandleNvsRead, "Age", &nvs_i8);
if (err == ESP_OK)
ESP_LOGI(TAG, "get nvs_i8 = %d ", nvs_i8);
else
ESP_LOGI(TAG, "get nvs_i8 error");

//读取 字符串 --> Csdn博客地址
char Csdn[65];
uint32_t lenCsdn = sizeof(Csdn);
err = nvs_get_str(mHandleNvsRead, "Csdn", Csdn, &lenCsdn);

if (err == ESP_OK)
ESP_LOGI(TAG, "get str data = %s ", Csdn);
else
ESP_LOGI(TAG, "get Csdn data error");

//读取 字符串 --> GitHub地址
char GitHub[65];
uint32_t lenGitHub = sizeof(GitHub);
err = nvs_get_str(mHandleNvsRead, "GitHub", GitHub, &lenGitHub);

if (err == ESP_OK)
ESP_LOGI(TAG, "get GitHub data = %s ", GitHub);
else
ESP_LOGI(TAG, "get GitHub data error");

//关闭数据库,关闭面板!
nvs_close(mHandleNvsRead);

ESP_LOGI(TAG, "--------------------------- End Task_NVS_Read --------------------------");
vTaskDelete(NULL);
}

/******************************************************************************
* FunctionName : app_main
* Description : entry of user application, init user function here
* Parameters : none
* Returns : none
*******************************************************************************/
void app_main(void)
{

//Initialize NVS
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES)
{
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);

printf("\n\n-------------------------------- Get Systrm Info------------------------------------------\n");
//获取IDF版本
printf(" SDK version:%s\n", esp_get_idf_version());
//获取芯片可用内存
printf(" esp_get_free_heap_size : %d \n", esp_get_free_heap_size());
//获取从未使用过的最小内存
printf(" esp_get_minimum_free_heap_size : %d \n", esp_get_minimum_free_heap_size());
//获取芯片的内存分布,返回值具体见结构体 flash_size_map
printf(" system_get_flash_size_map(): %d \n", system_get_flash_size_map());
//获取mac地址(station模式)
uint8_t mac[6];
esp_read_mac(mac, ESP_MAC_WIFI_STA);
printf("esp_read_mac(): %02x:%02x:%02x:%02x:%02x:%02x \n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
printf("--------------------------------------------------------------------------\n\n");

//创建一个读取的任务
xTaskCreate(Task_NVS_Read, "Task_NVS_Read", 1024, NULL, 5, NULL);
}
8 changes: 8 additions & 0 deletions 14_ReadFromCsvSingle/partitions_esp32.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Name, Type, SubType, Offset, Size, Flags
# Note: if you change the phy_init or app partition offset, make sure to change the offset in Kconfig.projbuild
nvs, data, nvs, , 0x4000,
otadata, data, ota, , 0x2000,
phy_init, data, phy, , 0x1000,
ota_0, app, ota_0, , 1M,
ota_1, app, ota_1, , 1M,
fctry, data, nvs, , 0x4000
Loading

0 comments on commit 9cc2cdf

Please sign in to comment.