canon_ble #82

Merged
mattmcw merged 149 commits from canon_ble into main 2023-08-01 03:38:52 +00:00
5 changed files with 25 additions and 691 deletions
Showing only changes of commit 4aceeac1c2 - Show all commits

View File

@ -1,308 +0,0 @@
// ArduinoNvs.cpp
// Copyright (c) 2018 Sinai RnD
// Copyright (c) 2016-2017 TridentTD
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "ArduinoNvs.h"
ArduinoNvs::ArduinoNvs()
{
}
bool ArduinoNvs::begin(String namespaceNvs)
{
esp_err_t err = nvs_flash_init();
if (err != ESP_OK)
{
DEBUG_PRINTLN("W: NVS. Cannot init flash mem");
if (err != ESP_ERR_NVS_NO_FREE_PAGES)
return false;
// erase and reinit
DEBUG_PRINTLN("NVS. Try reinit the partition");
const esp_partition_t *nvs_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS, NULL);
if (nvs_partition == NULL)
return false;
err = esp_partition_erase_range(nvs_partition, 0, nvs_partition->size);
esp_err_t err = nvs_flash_init();
if (err)
return false;
DEBUG_PRINTLN("NVS. Partition re-formatted");
}
err = nvs_open(namespaceNvs.c_str(), NVS_READWRITE, &_nvs_handle);
if (err != ESP_OK)
return false;
return true;
}
void ArduinoNvs::close()
{
nvs_close(_nvs_handle);
}
bool ArduinoNvs::eraseAll(bool forceCommit)
{
esp_err_t err = nvs_erase_all(_nvs_handle);
if (err != ESP_OK)
return false;
return forceCommit ? commit() : true;
}
bool ArduinoNvs::erase(String key, bool forceCommit)
{
esp_err_t err = nvs_erase_key(_nvs_handle, key.c_str());
if (err != ESP_OK)
return false;
return forceCommit ? commit() : true;
}
bool ArduinoNvs::commit()
{
esp_err_t err = nvs_commit(_nvs_handle);
if (err != ESP_OK)
return false;
return true;
}
bool ArduinoNvs::setInt(String key, uint8_t value, bool forceCommit)
{
esp_err_t err = nvs_set_u8(_nvs_handle, (char *)key.c_str(), value);
if (err != ESP_OK)
return false;
return forceCommit ? commit() : true;
}
bool ArduinoNvs::setInt(String key, int16_t value, bool forceCommit)
{
esp_err_t err = nvs_set_i16(_nvs_handle, (char *)key.c_str(), value);
if (err != ESP_OK)
return false;
return forceCommit ? commit() : true;
}
bool ArduinoNvs::setInt(String key, uint16_t value, bool forceCommit)
{
esp_err_t err = nvs_set_u16(_nvs_handle, (char *)key.c_str(), value);
if (err != ESP_OK)
return false;
return forceCommit ? commit() : true;
}
bool ArduinoNvs::setInt(String key, int32_t value, bool forceCommit)
{
esp_err_t err = nvs_set_i32(_nvs_handle, (char *)key.c_str(), value);
if (err != ESP_OK)
return false;
return forceCommit ? commit() : true;
}
bool ArduinoNvs::setInt(String key, uint32_t value, bool forceCommit)
{
esp_err_t err = nvs_set_u32(_nvs_handle, (char *)key.c_str(), value);
if (err != ESP_OK)
return false;
return forceCommit ? commit() : true;
}
bool ArduinoNvs::setInt(String key, int64_t value, bool forceCommit)
{
esp_err_t err = nvs_set_i64(_nvs_handle, (char *)key.c_str(), value);
if (err != ESP_OK)
return false;
return forceCommit ? commit() : true;
}
bool ArduinoNvs::setInt(String key, uint64_t value, bool forceCommit)
{
esp_err_t err = nvs_set_u64(_nvs_handle, (char *)key.c_str(), value);
if (err != ESP_OK)
return false;
return forceCommit ? commit() : true;
}
bool ArduinoNvs::setString(String key, String value, bool forceCommit)
{
esp_err_t err = nvs_set_str(_nvs_handle, (char *)key.c_str(), value.c_str());
if (err != ESP_OK)
return false;
return forceCommit ? commit() : true;
}
bool ArduinoNvs::setBlob(String key, uint8_t *blob, size_t length, bool forceCommit)
{
DEBUG_PRINTF("ArduinoNvs::setObjct(): set obj addr = [0x%X], length = [%d]\n", (int32_t)blob, length);
if (length == 0)
return false;
esp_err_t err = nvs_set_blob(_nvs_handle, (char *)key.c_str(), blob, length);
if (err)
{
DEBUG_PRINTF("ArduinoNvs::setObjct(): err = [0x%X]\n", err);
return false;
}
return forceCommit ? commit() : true;
}
bool ArduinoNvs::setBlob(String key, std::vector<uint8_t> &blob, bool forceCommit)
{
return setBlob(key, &blob[0], blob.size(), forceCommit);
}
int64_t ArduinoNvs::getInt(String key, int64_t default_value)
{
uint8_t v_u8;
int16_t v_i16;
uint16_t v_u16;
int32_t v_i32;
uint32_t v_u32;
int64_t v_i64;
uint64_t v_u64;
esp_err_t err;
err = nvs_get_u8(_nvs_handle, (char *)key.c_str(), &v_u8);
if (err == ESP_OK)
return (int64_t)v_u8;
err = nvs_get_i16(_nvs_handle, (char *)key.c_str(), &v_i16);
if (err == ESP_OK)
return (int64_t)v_i16;
err = nvs_get_u16(_nvs_handle, (char *)key.c_str(), &v_u16);
if (err == ESP_OK)
return (int64_t)v_u16;
err = nvs_get_i32(_nvs_handle, (char *)key.c_str(), &v_i32);
if (err == ESP_OK)
return (int64_t)v_i32;
err = nvs_get_u32(_nvs_handle, (char *)key.c_str(), &v_u32);
if (err == ESP_OK)
return (int64_t)v_u32;
err = nvs_get_i64(_nvs_handle, (char *)key.c_str(), &v_i64);
if (err == ESP_OK)
return (int64_t)v_i64;
err = nvs_get_u64(_nvs_handle, (char *)key.c_str(), &v_u64);
if (err == ESP_OK)
return (int64_t)v_u64;
return default_value;
}
bool ArduinoNvs::getString(String key, String &res)
{
size_t required_size;
esp_err_t err;
err = nvs_get_str(_nvs_handle, key.c_str(), NULL, &required_size);
if (err)
return false;
char value[required_size];
err = nvs_get_str(_nvs_handle, key.c_str(), value, &required_size);
if (err)
return false;
res = value;
return true;
}
String ArduinoNvs::getString(String key)
{
String res;
bool ok = getString(key, res);
if (!ok)
return String();
return res;
}
size_t ArduinoNvs::getBlobSize(String key)
{
size_t required_size;
esp_err_t err = nvs_get_blob(_nvs_handle, key.c_str(), NULL, &required_size);
if (err)
{
if (err != ESP_ERR_NVS_NOT_FOUND) // key_not_found is not an error, just return size 0
DEBUG_PRINTF("ArduinoNvs::getBlobSize(): err = [0x%X]\n", err);
return 0;
}
return required_size;
}
bool ArduinoNvs::getBlob(String key, uint8_t *blob, size_t length)
{
if (length == 0)
return false;
size_t required_size = getBlobSize(key);
if (required_size == 0)
return false;
if (length < required_size)
return false;
esp_err_t err = nvs_get_blob(_nvs_handle, key.c_str(), blob, &required_size);
if (err)
{
DEBUG_PRINTF("ArduinoNvs::getBlob(): get object err = [0x%X]\n", err);
return false;
}
return true;
}
bool ArduinoNvs::getBlob(String key, std::vector<uint8_t> &blob)
{
size_t required_size = getBlobSize(key);
if (required_size == 0)
return false;
blob.resize(required_size);
esp_err_t err = nvs_get_blob(_nvs_handle, key.c_str(), &blob[0], &required_size);
if (err)
{
DEBUG_PRINTF("ArduinoNvs::getBlob(): get object err = [0x%X]\n", err);
return false;
}
return true;
}
std::vector<uint8_t> ArduinoNvs::getBlob(String key)
{
std::vector<uint8_t> res;
bool ok = getBlob(key, res);
if (!ok)
res.clear();
return res;
}
bool ArduinoNvs::setFloat(String key, float value, bool forceCommit)
{
return setBlob(key, (uint8_t *)&value, sizeof(float), forceCommit);
}
float ArduinoNvs::getFloat(String key, float default_value)
{
std::vector<uint8_t> res(sizeof(float));
if (!getBlob(key, res))
return default_value;
return *(float *)(&res[0]);
}
ArduinoNvs NVS;

View File

@ -1,93 +0,0 @@
// ArduinoNvs.h
// Copyright (c) 2018 Sinai RnD
// Copyright (c) 2016-2017 TridentTD
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#ifndef __ARDUINO_NVS_H__
#define __ARDUINO_NVS_H__
#include <Arduino.h>
#include <vector>
extern "C" {
#include "esp_partition.h"
#include "esp_err.h"
#include "nvs_flash.h"
#include "nvs.h"
}
#ifndef ARDUINONVS_SILENT
#define ARDUINONVS_SILENT 0
#endif
#if ARDUINONVS_SILENT
#define DEBUG_PRINT(...) { }
#define DEBUG_PRINTLN(...) { }
#define DEBUG_PRINTF(fmt, args...) { }
#else
#define DEBUG_PRINTER Serial
#define DEBUG_PRINT(...) { DEBUG_PRINTER.print(__VA_ARGS__); }
#define DEBUG_PRINTLN(...) { DEBUG_PRINTER.println(__VA_ARGS__); }
#define DEBUG_PRINTF(fmt, args...) { DEBUG_PRINTER.printf(fmt,## args); }
#endif
class ArduinoNvs {
public:
ArduinoNvs();
bool begin(String namespaceNvs = "storage");
void close();
bool eraseAll(bool forceCommit = true);
bool erase(String key, bool forceCommit = true);
bool setInt(String key, uint8_t value, bool forceCommit = true);
bool setInt(String key, int16_t value, bool forceCommit = true);
bool setInt(String key, uint16_t value, bool forceCommit = true);
bool setInt(String key, int32_t value, bool forceCommit = true);
bool setInt(String key, uint32_t value, bool forceCommit = true);
bool setInt(String key, int64_t value, bool forceCommit = true);
bool setInt(String key, uint64_t value, bool forceCommit = true);
bool setFloat(String key, float value, bool forceCommit = true);
bool setString(String key, String value, bool forceCommit = true);
bool setBlob(String key, uint8_t* blob, size_t length, bool forceCommit = true);
bool setBlob(String key, std::vector<uint8_t>& blob, bool forceCommit = true);
int64_t getInt(String key, int64_t default_value = 0); // In case of error, default_value will be returned
float getFloat(String key, float default_value = 0);
bool getString(String key, String& res);
String getString(String key);
size_t getBlobSize(String key); /// Returns the size of the stored blob
bool getBlob(String key, uint8_t* blob, size_t length); /// User should proivde enought memory to store the loaded blob. If length < than required size to store blob, function fails.
bool getBlob(String key, std::vector<uint8_t>& blob);
std::vector<uint8_t> getBlob(String key); /// Less eficient but more simple in usage implemetation of `getBlob()`
bool commit();
protected:
nvs_handle _nvs_handle;
};
extern ArduinoNvs NVS;
#endif

View File

@ -1,115 +0,0 @@
/* Ticker library code is placed under the MIT license
* Copyright (c) 2018 Stefan Staub
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "TickTwo.h"
TickTwo::TickTwo(fptr callback, uint32_t timer, uint32_t repeat, resolution_t resolution) {
this->resolution = resolution;
if (resolution == MICROS) timer = timer * 1000;
this->timer = timer;
this->repeat = repeat;
this->callback = callback;
enabled = false;
lastTime = 0;
counts = 0;
}
TickTwo::~TickTwo() {}
void TickTwo::start() {
if (callback == NULL) return;
if (resolution == MILLIS) lastTime = millis();
else lastTime = micros();
enabled = true;
counts = 0;
status = RUNNING;
}
void TickTwo::resume() {
if (callback == NULL) return;
if (resolution == MILLIS) lastTime = millis() - diffTime;
else lastTime = micros() - diffTime;
if (status == STOPPED) counts = 0;
enabled = true;
status = RUNNING;
}
void TickTwo::stop() {
enabled = false;
counts = 0;
status = STOPPED;
}
void TickTwo::pause() {
if (resolution == MILLIS) diffTime = millis() - lastTime;
else diffTime = micros() - lastTime;
enabled = false;
status = PAUSED;
}
void TickTwo::update() {
if (tick()) callback();
}
bool TickTwo::tick() {
if (!enabled) return false;
uint32_t currentTime = (resolution == MILLIS) ? millis() : micros();
if ((currentTime - lastTime) >= timer) {
lastTime = currentTime;
if (repeat - counts == 1 && counts != 0xFFFFFFFF) {
enabled = false;
status = STOPPED;
}
counts++;
return true;
}
return false;
}
void TickTwo::interval(uint32_t timer) {
if (resolution == MICROS) timer *= 1000;
this->timer = timer;
}
uint32_t TickTwo::interval() {
if (resolution == MILLIS) return timer / 1000;
else return timer;
}
uint32_t TickTwo::elapsed() {
if (resolution == MILLIS) return millis() - lastTime;
else return micros() - lastTime;
}
uint32_t TickTwo::remaining() {
return timer - elapsed();
}
status_t TickTwo::state() {
return status;
}
uint32_t TickTwo::counter() {
return counts;
}

View File

@ -1,160 +0,0 @@
/* Ticker library code is placed under the MIT license
* Copyright (c) 2018 Stefan Staub
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef TICKTWO_H
#define TICKTWO_H
#include "Arduino.h"
/** Ticker internal resolution
*
* @param MICROS default, the resoöution is in micro seconds, max is 70 minutes, the real resoltuion is 4 microseconds at 16MHz CPU cycle
* @param MILLIS set the resolution to millis, for longer cycles over 70 minutes
*
*/
enum resolution_t {
MICROS,
MILLIS,
MICROS_MICROS
};
/** Ticker status
*
* @param STOPPED default, ticker is stopped
* @param RUNNIBG ticker is running
* @param PAUSED ticker is paused
*
*/
enum status_t {
STOPPED,
RUNNING,
PAUSED};
#if defined(__arm__) || defined(ESP8266) || defined(ESP32)
#include <functional>
using fptr = std::function<void()>;
#else
typedef void (*fptr)();
#endif
class TickTwo {
public:
/** create a Ticker object
*
* @param callback the name of the function to call
* @param timer interval length in ms or us
* @param repeat default 0 -> endless, repeat > 0 -> number of repeats
* @param resolution default MICROS for tickers under 70min, use MILLIS for tickers over 70 min
*
*/
TickTwo(fptr callback, uint32_t timer, uint32_t repeat = 0, resolution_t resolution = MICROS);
/** destructor for the Ticker object
*
*/
~TickTwo();
/** start the ticker
*
*/
void start();
/** resume the ticker. If not started, it will start it.
*
*/
void resume();
/** pause the ticker
*
*/
void pause();
/** stops the ticker
*
*/
void stop();
/** must to be called in the main loop(), it will check the Ticker, and if necessary, will run the callback
*
*/
void update();
/**
* @brief set the interval timer
*
* @param timer interval length in ms or us
*/
void interval(uint32_t timer);
/**
* @brief get the interval time
*
* @returns the interval time
*/
uint32_t interval();
/** actual ellapsed time
*
* @returns the elapsed time after the last tick
*
*/
uint32_t elapsed();
/** time remaining to the next tick
*
* @returns the remaining time to the next tick in ms or us depending from mode
*
*/
uint32_t remaining();
/** get the state of the ticker
*
* @returns the state of the ticker: STOPPED, RUNNING or PAUSED
*/
status_t state();
/** get the numbers of executed repeats
*
* @returns the number of executed repeats
*
*/
uint32_t counter();
private:
bool tick();
bool enabled;
uint32_t timer;
uint32_t repeat;
resolution_t resolution = MICROS;
uint32_t counts;
status_t status;
fptr callback;
uint32_t lastTime;
uint32_t diffTime;
};
#endif

View File

@ -25,7 +25,7 @@
#define SHUTTTER_BTN 12 #define SHUTTTER_BTN 12
#define RELAY_PIN 14 #define RELAY_PIN 14
#define RED_LED 23 #define RED_LED 23
#define GREEN_LED 22 #define GREEN_LED 22
void blink(); void blink();
volatile bool greenLEDstate; volatile bool greenLEDstate;
@ -40,27 +40,30 @@ volatile boolean connected = false;
volatile long now; volatile long now;
volatile long last = -1; volatile long last = -1;
volatile byte cmd; volatile char cmdChar = 'z';
void blink(){
digitalWrite(GREEN_LED, greenLEDstate);
greenLEDstate = !greenLEDstate;
}
void setup() void setup()
{ {
esp_log_level_set("*", ESP_LOG_INFO); esp_log_level_set("*", ESP_LOG_INFO);
pinMode(SHUTTTER_BTN, INPUT_PULLUP); pins();
pinMode(GREEN_LED, OUTPUT);
mc.begin(mc.CAMERA_IDENTIFIER); mc.begin(mc.CAMERA_IDENTIFIER);
digitalWrite(GREEN_LED, HIGH); digitalWrite(RED_LED, HIGH);
canon_ble.init(); canon_ble.init();
delay(1000); delay(1000);
} }
void pins () {
pinMode(SHUTTTER_BTN, INPUT_PULLUP);
pinMode(RED_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
digitalWrite(RED_LED, LOW);
digitalWrite(GREEN_LED, HIGH);
}
void connectBLE () { void connectBLE () {
do { do {
Serial.println("Pairing..."); Serial.println("Pairing...");
@ -69,6 +72,7 @@ void connectBLE () {
connected = true; connected = true;
digitalWrite(RED_LED, LOW);
digitalWrite(GREEN_LED, HIGH); digitalWrite(GREEN_LED, HIGH);
delay(1000); delay(1000);
@ -79,11 +83,9 @@ void connectBLE () {
void loop() void loop()
{ {
now = millis(); now = millis();
cmd = mc.loop(); cmdChar = mc.loop();
if (cmd == mc.CAMERA && last + 1000 < now) { cmd(cmdChar);
shutter();
}
// Shutter // Shutter
if (digitalRead(SHUTTTER_BTN) == LOW && last + 1000 < now){ if (digitalRead(SHUTTTER_BTN) == LOW && last + 1000 < now){
@ -95,8 +97,15 @@ void loop()
} }
} }
void cmd (char val) {
if (cmd == mc.CAMERA && connected) {
shutter();
}
}
void shutter () { void shutter () {
digitalWrite(GREEN_LED, LOW); digitalWrite(GREEN_LED, HIGH);
digitalWrite(RED_LED, HIGH);
mc.log("Shutter pressed"); mc.log("Shutter pressed");
if(!canon_ble.trigger()){ if(!canon_ble.trigger()){
@ -104,6 +113,7 @@ void shutter () {
} }
digitalWrite(GREEN_LED, HIGH); digitalWrite(GREEN_LED, HIGH);
digitalWrite(RED_LED, LOW);
last = millis(); last = millis();
mc.confirm(mc.CAMERA); mc.confirm(mc.CAMERA);
} }