Skip to content

Commit 29c4f3b

Browse files
initial commit for testing
0 parents  commit 29c4f3b

31 files changed

+15770
-0
lines changed

Firmware/main/AudioDriver.ino

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
Copyright (c) 2019 Andy England
3+
The Infinite Pyramid
4+
*/
5+
#include "arduinoFFT.h"
6+
#include <driver/adc.h>
7+
8+
arduinoFFT FFT = arduinoFFT();
9+
10+
uint8_t avgLowEnd = 3;
11+
uint8_t avgHighEnd = 8;
12+
13+
float audioScale = 5.0;
14+
15+
#define MIC_PIN 34
16+
#define LOWEST_HZ_BIN 2
17+
#define GAMMA_CORRECTION 1.6
18+
/*
19+
These are the input and output vectors
20+
Input vectors receive computed results from FFT
21+
*/
22+
#define MAX_SAMPLES 256
23+
#define NUM_SAMPLES 128
24+
#define NUM_SAMPLES_TO_AVERAGE 2
25+
#define NUM_BINS 12
26+
double vReal[MAX_SAMPLES];
27+
double vImag[MAX_SAMPLES];
28+
double vReal1[MAX_SAMPLES];
29+
double vImag1[MAX_SAMPLES];
30+
double avgOfBins[NUM_BINS];
31+
32+
#define SCL_INDEX 0x00
33+
#define SCL_TIME 0x01
34+
#define SCL_FREQUENCY 0x02
35+
#define SCL_PLOT 0x03
36+
37+
/* @brief computeFFT - computes our fourier transform
38+
@param uint16_t sample - number of samples taken to compute FFT, must be a power of 2
39+
*/
40+
void computeFFT (uint16_t samples = NUM_SAMPLES)
41+
{
42+
for (uint16_t i = 0; i < samples; i++)
43+
{
44+
int temp = 0;
45+
for (uint8_t avgNum = 0; avgNum < 12; avgNum++)
46+
{
47+
temp += analogRead(MIC_PIN);
48+
}
49+
temp /= 12;
50+
vReal[i] = map(temp, 910, 2900, -127, 127);
51+
//Serial.println(vReal[i]);
52+
vImag[i] = 0.0;
53+
}
54+
FFT.Windowing(vReal, samples, FFT_WIN_TYP_HAMMING, FFT_FORWARD); /* Weigh data */
55+
FFT.Compute(vReal, vImag, samples, FFT_FORWARD); /* Compute FFT */
56+
FFT.ComplexToMagnitude(vReal, vImag, samples); /* Compute magnitudes */
57+
for (uint16_t i = 0; i < samples; i++)
58+
{
59+
//vReal[i] *= i;
60+
//vReal[i] /= audioScale;
61+
constrain(vReal[i], 0, 255);
62+
}
63+
}
64+
65+
uint8_t fftAvg()
66+
{
67+
for (uint8_t bin = 0; bin < NUM_BINS; bin++)
68+
{
69+
int temp = 0;
70+
for (uint8_t sampleNum = 0; sampleNum < NUM_SAMPLES_TO_AVERAGE; sampleNum++)
71+
{
72+
temp += vReal[LOWEST_HZ_BIN + (bin * NUM_SAMPLES_TO_AVERAGE) + sampleNum];
73+
}
74+
avgOfBins[bin] = temp / NUM_SAMPLES_TO_AVERAGE;
75+
}
76+
}

Firmware/main/BLE.cpp

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
Copyright (c) 2019 Andy England
3+
Header: BLE.h
4+
*/
5+
6+
#include "BLE.h"
7+
8+
#define SERVICE_UUID_INFINITEPYRAMID "e804b643-6ce7-4e81-9f8a-ce0f699085eb"
9+
#define CHARACTERISTIC_UUID_IP_ID "e804b644-6ce7-4e81-9f8a-ce0f699085eb"
10+
11+
#define SERVICE_UUID_OTA "b8659210-af91-4ad3-a995-a58d6fd26145" // UART service UUID
12+
#define CHARACTERISTIC_UUID_FW "b8659211-af91-4ad3-a995-a58d6fd26145"
13+
#define CHARACTERISTIC_UUID_HW_VERSION "b8659212-af91-4ad3-a995-a58d6fd26145"
14+
#define CHARACTERISTIC_UUID_READY "b8659213-af91-4ad3-a995-a58d6fd26145"
15+
16+
#define SOFTWARE_VERSION_MAJOR 0
17+
#define SOFTWARE_VERSION_MINOR 1
18+
#define SOFTWARE_VERSION_PATCH 0
19+
#define HARDWARE_VERSION_MAJOR 1
20+
#define HARDWARE_VERSION_MINOR 3
21+
22+
#define FULL_PACKET 512
23+
#define CHARPOS_UPDATE_FLAG 5
24+
25+
esp_ota_handle_t otaHandler = 0;
26+
27+
bool updateFlag = false;
28+
bool readyFlag = false;
29+
int bytesReceived = 0;
30+
int timesWritten = 0;
31+
32+
void otaCallback::onWrite(BLECharacteristic *pCharacteristic)
33+
{
34+
std::string rxData = pCharacteristic->getValue();
35+
if (!updateFlag) { //If it's the first packet of OTA since bootup, begin OTA
36+
Serial.println("BeginOTA");
37+
esp_ota_begin(esp_ota_get_next_update_partition(NULL), OTA_SIZE_UNKNOWN, &otaHandler);
38+
updateFlag = true;
39+
}
40+
if (_p_ble != NULL)
41+
{
42+
if (rxData.length() > 0)
43+
{
44+
esp_ota_write(otaHandler, rxData.c_str(), rxData.length());
45+
if (rxData.length() != FULL_PACKET)
46+
{
47+
esp_ota_end(otaHandler);
48+
Serial.println("EndOTA");
49+
if (ESP_OK == esp_ota_set_boot_partition(esp_ota_get_next_update_partition(NULL))) {
50+
esp_restart();
51+
}
52+
else {
53+
Serial.println("Upload Error");
54+
}
55+
}
56+
}
57+
}
58+
59+
uint8_t txData[5] = {1, 2, 3, 4, 5};
60+
//delay(1000);
61+
pCharacteristic->setValue((uint8_t*)txData, 5);
62+
pCharacteristic->notify();
63+
}
64+
65+
//
66+
// Constructor
67+
BLE::BLE(void) {
68+
69+
}
70+
71+
//
72+
// Destructor
73+
BLE::~BLE(void)
74+
{
75+
76+
}
77+
78+
//
79+
// begin
80+
bool BLE::begin(const char* localName = "UART Service") {
81+
// Create the BLE Device
82+
BLEDevice::init(localName);
83+
84+
// Create the BLE Server
85+
pServer = BLEDevice::createServer();
86+
pServer->setCallbacks(new BLECustomServerCallbacks());
87+
88+
// Create the BLE Service
89+
pInfinitePyramidService = pServer->createService(SERVICE_UUID_INFINITEPYRAMID);
90+
pService = pServer->createService(SERVICE_UUID_OTA);
91+
92+
// Create a BLE Characteristic
93+
pInfinitePyramidIdCharacteristic = pInfinitePyramidService->createCharacteristic(
94+
CHARACTERISTIC_UUID_IP_ID,
95+
BLECharacteristic::PROPERTY_READ
96+
);
97+
98+
pVersionCharacteristic = pService->createCharacteristic(
99+
CHARACTERISTIC_UUID_HW_VERSION,
100+
BLECharacteristic::PROPERTY_READ
101+
);
102+
103+
pOtaCharacteristic = pService->createCharacteristic(
104+
CHARACTERISTIC_UUID_FW,
105+
BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_WRITE
106+
);
107+
108+
pOtaCharacteristic->addDescriptor(new BLE2902());
109+
pOtaCharacteristic->setCallbacks(new otaCallback(this));
110+
111+
// Start the service(s)
112+
pInfinitePyramidService->start();
113+
pService->start();
114+
115+
// Start advertising
116+
pServer->getAdvertising()->addServiceUUID(SERVICE_UUID_INFINITEPYRAMID);
117+
pServer->getAdvertising()->start();
118+
119+
uint8_t hardwareVersion[5] = {HARDWARE_VERSION_MAJOR, HARDWARE_VERSION_MINOR, SOFTWARE_VERSION_MAJOR, SOFTWARE_VERSION_MINOR, SOFTWARE_VERSION_PATCH};
120+
pVersionCharacteristic->setValue((uint8_t*)hardwareVersion, 5);
121+
return true;
122+
}

Firmware/main/BLE.h

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
Copyright (c) 2019 Andy England
3+
4+
A BLE driver for Over the Air Updates
5+
6+
Built on top of the BLE utilities by Neil Kolban
7+
https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleNotify.cpp
8+
*/
9+
10+
11+
#ifndef _BLE_H_
12+
#define _BLE_H_
13+
14+
#include "Arduino.h"
15+
16+
#include <BLEDevice.h>
17+
#include <BLEServer.h>
18+
#include <BLEUtils.h>
19+
#include <BLE2902.h>
20+
21+
#include "esp_ota_ops.h"
22+
23+
24+
class BLE; // forward declaration
25+
26+
class BLECustomServerCallbacks: public BLEServerCallbacks {
27+
void onConnect(BLEServer* pServer) {
28+
// deviceConnected = true;
29+
// // code here for when device connects
30+
};
31+
32+
void onDisconnect(BLEServer* pServer) {
33+
// deviceConnected = false;
34+
// // code here for when device disconnects
35+
}
36+
};
37+
38+
class otaCallback: public BLECharacteristicCallbacks {
39+
public:
40+
otaCallback(BLE* ble) {
41+
_p_ble = ble;
42+
}
43+
BLE* _p_ble;
44+
45+
void onWrite(BLECharacteristic *pCharacteristic);
46+
};
47+
48+
class BLE
49+
{
50+
public:
51+
52+
BLE(void);
53+
~BLE(void);
54+
55+
bool begin(const char* localName);
56+
57+
private:
58+
String local_name;
59+
60+
BLEServer *pServer = NULL;
61+
62+
BLEService *pInfinitePyramidService = NULL;
63+
BLECharacteristic * pInfinitePyramidIdCharacteristic = NULL;
64+
65+
BLEService *pService = NULL;
66+
BLECharacteristic * pVersionCharacteristic = NULL;
67+
BLECharacteristic * pOtaCharacteristic = NULL;
68+
};
69+
70+
#endif

Firmware/main/battery.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Firmware/main/battery.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <SparkFunBQ27441.h>
2+
3+
// Set BATTERY_CAPACITY to the design capacity of your battery.
4+
const unsigned int BATTERY_CAPACITY = 6000; // e.g. 850mAh battery
5+
6+
void initializeBattery(void)
7+
{
8+
// Use lipo.begin() to initialize the BQ27441-G1A and confirm that it's
9+
// connected and communicating.
10+
if (!lipo.begin()) // begin() will return true if communication is successful
11+
{
12+
// If communication fails, print an error message and loop forever.
13+
//Serial.println("Error: Unable to communicate with BQ27441.");
14+
//Serial.println(" Check wiring and try again.");
15+
//Serial.println(" (Battery must be plugged into Battery Babysitter!)");
16+
while (1) ;
17+
}
18+
//Serial.println("Connected to BQ27441!");
19+
20+
lipo.setCapacity(BATTERY_CAPACITY);
21+
}
22+
23+
void printBatteryStats()
24+
{
25+
// Read battery stats from the BQ27441-G1A
26+
unsigned int soc = lipo.soc(); // Read state-of-charge (%)
27+
unsigned int volts = lipo.voltage(); // Read battery voltage (mV)
28+
int current = lipo.current(AVG); // Read average current (mA)
29+
unsigned int fullCapacity = lipo.capacity(FULL); // Read full capacity (mAh)
30+
unsigned int capacity = lipo.capacity(REMAIN); // Read remaining capacity (mAh)
31+
int power = lipo.power(); // Read average power draw (mW)
32+
int health = lipo.soh(); // Read state-of-health (%)
33+
34+
// Now print out those values:
35+
String toPrint = String(soc) + "% | ";
36+
toPrint += String(volts) + " mV | ";
37+
toPrint += String(current) + " mA | ";
38+
toPrint += String(capacity) + " / ";
39+
toPrint += String(fullCapacity) + " mAh | ";
40+
toPrint += String(power) + " mW | ";
41+
toPrint += String(health) + "%";
42+
43+
Serial.println(toPrint);
44+
}

0 commit comments

Comments
 (0)