canon_ble #82
|
@ -0,0 +1,308 @@
|
|||
// 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;
|
|
@ -0,0 +1,93 @@
|
|||
// 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
|
||||
|
|
@ -0,0 +1,298 @@
|
|||
#include "CanonBLERemote.h"
|
||||
#include <Arduino.h>
|
||||
#include <BLEDevice.h>
|
||||
|
||||
static const char *LOG_TAG = "MySecurity";
|
||||
|
||||
advdCallback::advdCallback(BLEUUID service_uuid, bool *ready_to_connect, BLEAddress *address_to_connect)
|
||||
{
|
||||
service_uuid_wanted = service_uuid;
|
||||
pready_to_connect = ready_to_connect;
|
||||
paddress_to_connect = address_to_connect;
|
||||
}
|
||||
|
||||
void advdCallback::onResult(BLEAdvertisedDevice advertisedDevice)
|
||||
{
|
||||
if (advertisedDevice.haveServiceUUID())
|
||||
{ //Check if device has service UUID available.
|
||||
if (service_uuid_wanted.equals(advertisedDevice.getServiceUUID()))
|
||||
{
|
||||
*paddress_to_connect = advertisedDevice.getAddress();
|
||||
*pready_to_connect = true;
|
||||
advertisedDevice.getScan()->stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ConnectivityState::onConnect(BLEClient *pclient)
|
||||
{
|
||||
// Serial.println("Device is connected");
|
||||
connected = true;
|
||||
}
|
||||
|
||||
void ConnectivityState::onDisconnect(BLEClient *pclient)
|
||||
{
|
||||
// Serial.println("Device disconnnect");
|
||||
connected = false;
|
||||
}
|
||||
|
||||
bool ConnectivityState::isConnected()
|
||||
{
|
||||
return connected;
|
||||
}
|
||||
|
||||
class SecurityCallback : public BLESecurityCallbacks
|
||||
{
|
||||
|
||||
uint32_t onPassKeyRequest()
|
||||
{
|
||||
return 123456;
|
||||
}
|
||||
void onPassKeyNotify(uint32_t pass_key)
|
||||
{
|
||||
ESP_LOGE(LOG_TAG, "The passkey Notify number:%d", pass_key);
|
||||
}
|
||||
bool onConfirmPIN(uint32_t pass_key)
|
||||
{
|
||||
ESP_LOGI(LOG_TAG, "The passkey YES/NO number:%d", pass_key);
|
||||
vTaskDelay(5000);
|
||||
return true;
|
||||
}
|
||||
bool onSecurityRequest()
|
||||
{
|
||||
ESP_LOGI(LOG_TAG, "Security Request");
|
||||
return true;
|
||||
}
|
||||
void onAuthenticationComplete(esp_ble_auth_cmpl_t auth_cmpl)
|
||||
{
|
||||
if (auth_cmpl.success)
|
||||
{
|
||||
ESP_LOGI(LOG_TAG, "remote BD_ADDR:");
|
||||
esp_log_buffer_hex(LOG_TAG, auth_cmpl.bd_addr, sizeof(auth_cmpl.bd_addr));
|
||||
ESP_LOGI(LOG_TAG, "address type = %d", auth_cmpl.addr_type);
|
||||
}
|
||||
ESP_LOGI(LOG_TAG, "pair status = %s", auth_cmpl.success ? "success" : "fail");
|
||||
}
|
||||
};
|
||||
|
||||
CanonBLERemote::CanonBLERemote(String name) : SERVICE_UUID("00050000-0000-1000-0000-d8492fffa821"),
|
||||
PAIRING_SERVICE("00050002-0000-1000-0000-d8492fffa821"),
|
||||
SHUTTER_CONTROL_SERVICE("00050003-0000-1000-0000-d8492fffa821")
|
||||
{
|
||||
device_name = name;
|
||||
// Add our connection callback for state tracking.
|
||||
pclient->setClientCallbacks(pconnection_state);
|
||||
}
|
||||
|
||||
void CanonBLERemote::init()
|
||||
{
|
||||
BLEDevice::init(device_name.c_str());
|
||||
BLEDevice::setEncryptionLevel(ESP_BLE_SEC_ENCRYPT_NO_MITM);
|
||||
BLEDevice::setSecurityCallbacks(new SecurityCallback());
|
||||
|
||||
if (nvs.begin())
|
||||
{
|
||||
log_e("Initialize NVS Success");
|
||||
String address = nvs.getString("cameraaddr");
|
||||
|
||||
if (address.length() == 17)
|
||||
{
|
||||
// Serial.printf("Paired camera address: %s\n", address.c_str());
|
||||
camera_address = BLEAddress(address.c_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
// Serial.println("No camera has been paired yet.");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
log_e("Initialize NVS Failed");
|
||||
}
|
||||
}
|
||||
|
||||
// Purpose : Scanning for new BLE devices around.
|
||||
// When found -> advdCallback::OnResult
|
||||
void CanonBLERemote::scan(unsigned int scan_duration)
|
||||
{
|
||||
|
||||
log_i("Start BLE scan");
|
||||
BLEScan *pBLEScan = BLEDevice::getScan();
|
||||
advdCallback *advert_dev_callback = new advdCallback(SERVICE_UUID, &ready_to_connect, &camera_address);
|
||||
|
||||
pBLEScan->setAdvertisedDeviceCallbacks(advert_dev_callback); // Retrieve a Scanner and set the callback we want to use to be informed when we have detected a new device.
|
||||
pBLEScan->setActiveScan(true);
|
||||
pBLEScan->start(scan_duration); // Specify that we want active scanning and start the scan to run for 30 seconds.
|
||||
}
|
||||
|
||||
BLEAddress CanonBLERemote::getPairedAddress()
|
||||
{
|
||||
return camera_address;
|
||||
}
|
||||
|
||||
String CanonBLERemote::getPairedAddressString()
|
||||
{
|
||||
return String(camera_address.toString().c_str());
|
||||
}
|
||||
|
||||
/**
|
||||
* Scan and pair camera
|
||||
*
|
||||
* This function should be called only when you want to pair with the new camera, and the camera is in remote paring screen.
|
||||
* After paired, camera mac address will be stored in ESP32 NVS for later connection use.
|
||||
*
|
||||
* @param scan_duration : Scan duration in seconds
|
||||
*/
|
||||
|
||||
bool CanonBLERemote::pair(unsigned int scan_duration)
|
||||
{
|
||||
BLEDevice::setEncryptionLevel(ESP_BLE_SEC_ENCRYPT_MITM);
|
||||
log_i("Scanning for camera...");
|
||||
scan(scan_duration);
|
||||
unsigned long start_ms = millis();
|
||||
while (!ready_to_connect && millis() - start_ms < scan_duration * 1000)
|
||||
{
|
||||
}
|
||||
|
||||
if (ready_to_connect)
|
||||
{
|
||||
log_i("Canon device found");
|
||||
// Serial.println(camera_address.toString().c_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
log_i("Camera not found");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Pair camera
|
||||
log_i("Pairing..");
|
||||
if (pclient->connect(camera_address))
|
||||
{
|
||||
// Acquire reference to main service
|
||||
pRemoteService = pclient->getService(SERVICE_UUID);
|
||||
if (pRemoteService != nullptr)
|
||||
{
|
||||
// Acquire reference to BLE characteristics
|
||||
pRemoteCharacteristic_Pairing = pRemoteService->getCharacteristic(PAIRING_SERVICE);
|
||||
if ((pRemoteCharacteristic_Pairing != nullptr))
|
||||
{
|
||||
// Send request on pairing service from external device
|
||||
String device_name_ = " " + device_name + " "; //Pairing message to send
|
||||
byte cmdPress[device_name_.length()]; // Stocking list of Bytes char of the message
|
||||
device_name_.getBytes(cmdPress, device_name_.length()); // message Parser
|
||||
cmdPress[0] = {0x03};
|
||||
pRemoteCharacteristic_Pairing->writeValue(cmdPress, sizeof(cmdPress), false); // Writing to Canon_pairing_service
|
||||
log_e("Camera paring success");
|
||||
delay(200);
|
||||
disconnect();
|
||||
BLEDevice::setEncryptionLevel(ESP_BLE_SEC_ENCRYPT_NO_MITM);
|
||||
delay(200);
|
||||
connect();
|
||||
nvs.setString("cameraaddr", String(camera_address.toString().c_str()));
|
||||
if (nvs.commit())
|
||||
{
|
||||
log_i("Saving camera's address to NVS success");
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
log_e("Storing camera's address to NVS failed");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
log_e("Couldn't acquire the pairing or shutter service");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
log_e("Couldn't acquire the remote main service");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
log_e("Couldn't connect the BLEClient to the device");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CanonBLERemote::connect()
|
||||
{
|
||||
if (pclient->connect(camera_address))
|
||||
{
|
||||
pRemoteService = pclient->getService(SERVICE_UUID);
|
||||
if (pRemoteService != nullptr)
|
||||
{
|
||||
// Serial.println("Get remote service OK");
|
||||
pRemoteCharacteristic_Trigger = pRemoteService->getCharacteristic(SHUTTER_CONTROL_SERVICE);
|
||||
if (pRemoteCharacteristic_Trigger != nullptr)
|
||||
{
|
||||
log_i("Camera connection Success");
|
||||
// disconnect(); // Disconnect remote from the camera every time after action, as the real canon remote did.
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
log_e("Get trigger service failed");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
log_e("Couldn't acquire the remote main service");
|
||||
}
|
||||
disconnect();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void CanonBLERemote::disconnect()
|
||||
{
|
||||
pclient->disconnect();
|
||||
}
|
||||
|
||||
bool CanonBLERemote::isConnected()
|
||||
{
|
||||
return pconnection_state->isConnected();
|
||||
}
|
||||
|
||||
/** Trigger Camera
|
||||
* If the camera is in photo mode, it will take a single picture.
|
||||
* If the camera is in movie mode, it will start/stop movie recording.
|
||||
*/
|
||||
|
||||
bool CanonBLERemote::trigger()
|
||||
{
|
||||
|
||||
if (!isConnected())
|
||||
{
|
||||
if (!connect())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
byte cmdByte = {MODE_IMMEDIATE | BUTTON_RELEASE}; // Binary OR : Concatenate Mode and Button
|
||||
pRemoteCharacteristic_Trigger->writeValue(cmdByte, false); // Set the characteristic's value to be the array of bytes that is actually a string.
|
||||
delay(200);
|
||||
pRemoteCharacteristic_Trigger->writeValue(MODE_IMMEDIATE, false);
|
||||
delay(50);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CanonBLERemote::focus()
|
||||
{
|
||||
if (!isConnected())
|
||||
{
|
||||
if (!connect())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
byte cmdByte[] = {MODE_IMMEDIATE | BUTTON_FOCUS}; // Binary OR : Concatenate Mode and Button
|
||||
pRemoteCharacteristic_Trigger->writeValue(cmdByte, sizeof(cmdByte)); // Set the characteristic's value to be the array of bytes that is actually a string.
|
||||
delay(200);
|
||||
pRemoteCharacteristic_Trigger->writeValue(MODE_IMMEDIATE, sizeof(MODE_IMMEDIATE));
|
||||
return true;
|
||||
}
|
|
@ -0,0 +1,75 @@
|
|||
#ifndef CANON_BLE_REMOTE_H_
|
||||
#define CANON_BLE_REMOTE_H_
|
||||
|
||||
#include <BLEDevice.h>
|
||||
#include <Arduino.h>
|
||||
#include "ArduinoNvs.h"
|
||||
|
||||
class advdCallback : public BLEAdvertisedDeviceCallbacks
|
||||
{
|
||||
private:
|
||||
BLEAddress *paddress_to_connect;
|
||||
BLEUUID service_uuid_wanted;
|
||||
bool *pready_to_connect;
|
||||
|
||||
public:
|
||||
advdCallback(BLEUUID service_uuid, bool *ready_to_connect, BLEAddress *address_to_connect);
|
||||
void onResult(BLEAdvertisedDevice advertisedDevice);
|
||||
};
|
||||
|
||||
class ConnectivityState : public BLEClientCallbacks
|
||||
{
|
||||
private:
|
||||
bool connected = false;
|
||||
|
||||
public:
|
||||
void onConnect(BLEClient *pclient) override;
|
||||
void onDisconnect(BLEClient *pclient) override;
|
||||
bool isConnected();
|
||||
};
|
||||
|
||||
class CanonBLERemote
|
||||
{
|
||||
private:
|
||||
// Trigger options
|
||||
const byte BUTTON_RELEASE = 0b10000000;
|
||||
const byte BUTTON_FOCUS = 0b01000000;
|
||||
const byte BUTTON_TELE = 0b00100000;
|
||||
const byte BUTTON_WIDE = 0b00010000;
|
||||
const byte MODE_IMMEDIATE = 0b00001100;
|
||||
const byte MODE_DELAY = 0b00000100;
|
||||
const byte MODE_MOVIE = 0b00001000;
|
||||
|
||||
const BLEUUID SERVICE_UUID;
|
||||
const BLEUUID PAIRING_SERVICE;
|
||||
const BLEUUID SHUTTER_CONTROL_SERVICE;
|
||||
|
||||
BLEClient *pclient = BLEDevice::createClient();
|
||||
ConnectivityState *pconnection_state = new ConnectivityState();
|
||||
BLEAddress camera_address = BLEAddress("");
|
||||
BLERemoteService *pRemoteService;
|
||||
BLERemoteCharacteristic *pRemoteCharacteristic_Pairing;
|
||||
BLERemoteCharacteristic *pRemoteCharacteristic_Trigger;
|
||||
ArduinoNvs nvs;
|
||||
|
||||
bool ready_to_connect = false;
|
||||
String device_name = "";
|
||||
void scan(unsigned int scan_duration);
|
||||
bool connect();
|
||||
void disconnect();
|
||||
|
||||
|
||||
public:
|
||||
CanonBLERemote(String name);
|
||||
void init();
|
||||
bool pair(unsigned int scan_duration);
|
||||
bool isConnected();
|
||||
|
||||
bool trigger();
|
||||
bool focus();
|
||||
|
||||
BLEAddress getPairedAddress();
|
||||
String getPairedAddressString();
|
||||
};
|
||||
|
||||
#endif
|
|
@ -0,0 +1,115 @@
|
|||
/* 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;
|
||||
}
|
|
@ -0,0 +1,160 @@
|
|||
/* 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
|
|
@ -0,0 +1,14 @@
|
|||
# SPDX-License-Identifier: GPL-2.0-or-later
|
||||
#
|
||||
# Example OpenOCD configuration file for ESP32-WROVER-KIT board.
|
||||
#
|
||||
# For example, OpenOCD can be started for ESP32 debugging on
|
||||
#
|
||||
# openocd -f board/esp32-wrover-kit-3.3v.cfg
|
||||
#
|
||||
|
||||
# Source the JTAG interface configuration file
|
||||
source [find interface/ftdi/esp32_devkitj_v1.cfg]
|
||||
set ESP32_FLASH_VOLTAGE 3.3
|
||||
# Source the ESP32 configuration file
|
||||
source [find target/esp32.cfg]
|
|
@ -0,0 +1,19 @@
|
|||
{
|
||||
"name":"Arduino on ESP32",
|
||||
"toolchainPrefix":"xtensa-esp32-elf",
|
||||
"svdFile":"esp32.svd",
|
||||
"request":"attach",
|
||||
"postAttachCommands":[
|
||||
"set remote hardware-watchpoint-limit 2",
|
||||
"monitor reset halt",
|
||||
"monitor gdb_sync",
|
||||
"thb setup",
|
||||
"c"
|
||||
],
|
||||
"overrideRestartCommands":[
|
||||
"monitor reset halt",
|
||||
"monitor gdb_sync",
|
||||
"thb setup",
|
||||
"c"
|
||||
]
|
||||
}
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,14 @@
|
|||
#!/bin/bash
|
||||
|
||||
if [ -d "../../../ArduinoNvs" ]; then
|
||||
cp "../../../ArduinoNvs/src/"* ./
|
||||
fi
|
||||
|
||||
if [ -d "../../../ESP32-Canon-BLE-Remote" ]; then
|
||||
cp "../../../ESP32-Canon-BLE-Remote/src/"* ./
|
||||
fi
|
||||
|
||||
if [ -d "../../../TickTwo" ]; then
|
||||
cp "../../../TockTwo/"*.cpp ./
|
||||
cp "../../../TockTwo/"*.h ./
|
||||
fi
|
|
@ -0,0 +1,72 @@
|
|||
#include "CanonBLERemote.h"
|
||||
#include <Arduino.h>
|
||||
#include "TickTwo.h"
|
||||
|
||||
#define LOG_LOCAL_LEVEL ESP_LOG_INFO
|
||||
#include "esp_log.h"
|
||||
#include <esp32-hal-log.h>
|
||||
|
||||
#define SHUTTTER_BTN 14
|
||||
#define FOCUS_BTN 12
|
||||
#define LED 2
|
||||
|
||||
void blink();
|
||||
bool ledState;
|
||||
|
||||
String name_remote = "mcopy";
|
||||
CanonBLERemote canon_ble(name_remote);
|
||||
TickTwo blinker(blink, 500);
|
||||
|
||||
void blink(){
|
||||
digitalWrite(LED, ledState);
|
||||
ledState = !ledState;
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(57600);
|
||||
esp_log_level_set("*", ESP_LOG_INFO);
|
||||
|
||||
pinMode(SHUTTTER_BTN, INPUT_PULLUP);
|
||||
pinMode(FOCUS_BTN, INPUT_PULLUP);
|
||||
pinMode(LED, OUTPUT);
|
||||
|
||||
canon_ble.init();
|
||||
delay(1000);
|
||||
blinker.start();
|
||||
|
||||
do {
|
||||
Serial.println("Pairing...");
|
||||
}
|
||||
while(!canon_ble.pair(10));
|
||||
|
||||
blinker.stop();
|
||||
delay(1000);
|
||||
Serial.println("Camera paired");
|
||||
Serial.println(canon_ble.getPairedAddressString());
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
// Shutter
|
||||
if (digitalRead(SHUTTTER_BTN) == LOW){
|
||||
blinker.pause();
|
||||
Serial.println("Shutter pressed");
|
||||
digitalWrite(LED, LOW);
|
||||
if(!canon_ble.trigger()){
|
||||
Serial.println("Trigger Failed");
|
||||
}
|
||||
blinker.resume();
|
||||
}
|
||||
// Focus
|
||||
else if (digitalRead(FOCUS_BTN) == LOW){
|
||||
blinker.pause();
|
||||
Serial.println("Focus pressed");
|
||||
digitalWrite(LED, LOW);
|
||||
if(!canon_ble.focus()){
|
||||
Serial.println("Focus failed");
|
||||
}
|
||||
blinker.resume();
|
||||
}
|
||||
blinker.update();
|
||||
}
|
Loading…
Reference in New Issue