// https://github.com/siara-cc/sqlite_micro_logger_arduino/issues/7
// ver o negocio do a+
#include "ulog_sqlite.h"
#include <SPI.h>
#include "SD.h"
#include "SdFat.h"
#include <stdio.h>
//SdFat SD;
// Set the CS Pin connected to the MicroSD
// 8 on most shields such as Sparkfun Micro SD Shield
// 4 on WeMos D1 Mini
#define SD_CS_PIN 4
#define DB_FILENAME "database.db"
//rewind DB_FILENAME "/sd/data/database.db"
//INÍCIO Sckech Debug
#define pinBotaoDebug 13
#define habilitaDebugSerial true //define se envia informações do funcionamento para o monitor serial. "true" envia e "false" não envia. Utilizado apenas para identificar problemas de funcionamento atraves do monitor serial do IDE Arduino. Em situações normais, definir este parametro como "false". Quando usar o monitor, ele deve ser configurado para a velocidade de 115200.
#if habilitaDebugSerial == true
void debug(int pontoParada, String nomeVariavel, String valorVariavel, int tempoParada = -1) { //TempoParada faz delay. Com -1, para até porta 13 mudar de nível
Serial.print("(");
Serial.print(pontoParada);
Serial.print(") ");
Serial.print(nomeVariavel);
Serial.print(":");
Serial.print(valorVariavel);
Serial.println();
if (tempoParada == -1) {
static bool estadoBotaoAnt = digitalRead(pinBotaoDebug);
static unsigned long delayDebounce;
bool estadoBotao;
bool aguarda = true;
while (aguarda) {
estadoBotao = digitalRead(pinBotaoDebug);
if ( (estadoBotao != estadoBotaoAnt) && !estadoBotao ) {
if ((millis() - delayDebounce) > 100) {
aguarda = false;
delayDebounce = millis();
}
} else if (estadoBotao != estadoBotaoAnt) {
delayDebounce = millis();
}
estadoBotaoAnt = estadoBotao;
}
} else if (tempoParada > 0) {
delay(tempoParada);
}
}
#endif
//FIM Sckech Debug
FILE *fp;
int32_t read_fn(struct dblog_write_context *ctx, void *buf, uint32_t pos, size_t len) {
if (fseek(fp, pos, SEEK_SET)) {
ctx->err_no = ferror(fp);
return DBLOG_RES_SEEK_ERR;
}
size_t ret = fread(buf, 1, len, fp);
if (ret != len) {
ctx->err_no = ferror(fp);
return DBLOG_RES_READ_ERR;
}
return ret;
}
int32_t read_fn_rctx(struct dblog_read_context *ctx, void *buf, uint32_t pos, size_t len) {
if (fseek(fp, pos, SEEK_SET))
return DBLOG_RES_SEEK_ERR;
size_t ret = fread(buf, 1, len, fp);
if (ret != len)
return DBLOG_RES_READ_ERR;
return ret;
}
int32_t write_fn(struct dblog_write_context *ctx, void *buf, uint32_t pos, size_t len) {
if (fseek(fp, pos, SEEK_SET)) {
ctx->err_no = ferror(fp);
return DBLOG_RES_SEEK_ERR;
}
size_t ret = fwrite(buf, 1, len, fp);
if (ret != len) {
ctx->err_no = ferror(fp);
return DBLOG_RES_WRITE_ERR;
}
return ret;
}
int flush_fn(struct dblog_write_context *ctx) {
int ret = fflush(fp);
if (ret) {
ctx->err_no = ferror(fp);
return DBLOG_RES_FLUSH_ERR;
}
return DBLOG_RES_OK;
}
void setup() {
// INÍCIO Sckech Debug
#if habilitaDebugSerial == true
Serial.begin(115200);
pinMode(pinBotaoDebug, INPUT_PULLUP);
#endif
// FIM Sckech Debug
File test;
SD.begin(SD_CS_PIN);
Serial.print("SD Card init...");
//test if wiring is correct
if (!SD.begin(SD_CS_PIN)) {
Serial.println("init failed..");
while (1);
}
Serial.println("init ok");
//test if file exist
if (SD.exists("testing.db")) {
Serial.println("Yes that is exist..");
} else {
Serial.println("Nope there is not..");
}
//create file
Serial.println("Creating file..");
test = SD.open("testing.db", FILE_WRITE);
test.close();
//test again if file exist to verify
if (SD.exists("testing.db")) {
Serial.println("Yes that is exist..");
} else {
Serial.println("Nope there is not..");
}
//Delete file
Serial.println("Deleting file...");
SD.remove("testing.db");
//test again
if (SD.exists("testing.dbt")) {
Serial.println("Yes that is exist..");
} else {
Serial.println("Nope there is not..");
}
delay(1000);
Serial.println("Testing done...");
}
FILE * fopen ( const char * filename, const char * r);
void loop()
{
}