Skip to content

Commit

Permalink
Changed wifi led to blink faster. Simplified menu timing code by addi…
Browse files Browse the repository at this point in the history
…ng a helper function. Changed default camera duration to 200ms. Added a new file to put new menus in and added instructions for adding new menus.
  • Loading branch information
mribble committed Oct 26, 2017
1 parent 0f7d9c2 commit 98d628f
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 39 deletions.
2 changes: 1 addition & 1 deletion Esp8266/Esp8266.ino
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const char* gIntervalFilename = "/data/interval";
const char* gStartLocation = "/data/startLocation";

// Assumes PID_CAM_SETTINGS is 5
String gCamSettingDefaults = "5~0~0~0~0~1~0~0~0~255~0~&5~1~0~0~0~1~0~0~0~255~0~&5~2~0~0~0~1~0~0~0~255~0~&5~3~0~0~0~1~0~0~0~255~0~&5~4~0~0~0~1~0~0~0~255~0~&5~5~0~0~0~1~0~0~0~255~0~&5~6~0~0~0~1~0~0~0~255~0~&5~7~0~0~0~1~0~0~0~255~0~&";
String gCamSettingDefaults = "5~0~0~0~0~0~200000000~0~0~255~0~&5~1~0~0~0~0~200000000~0~0~255~0~&5~2~0~0~0~0~200000000~0~0~255~0~&5~3~0~0~0~0~200000000~0~0~255~0~&5~4~0~0~0~0~200000000~0~0~255~0~&5~5~0~0~0~0~200000000~0~0~255~0~&5~6~0~0~0~0~200000000~0~0~255~0~&5~7~0~0~0~0~200000000~0~0~255~0~&";

// Assumes PID_INTERVALOMETER is 6
String gIntervalometerDefaults = "6~0~0~0~1~0~4~";
Expand Down
4 changes: 2 additions & 2 deletions Libs/CALed/src/CALed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ CALed::CALed(uint8_t greenPin, uint8_t redPin) {
mGreenPin = greenPin;
mRedPin = redPin;
mState = ALL_OFF;
mOnTime = 1000;
mOffTime = 1000;
mOnTime = 100;
mOffTime = 100;
mNextTime = 0;
mWriteVal = LOW;

Expand Down
1 change: 1 addition & 0 deletions Sam3x/CA6/CA6.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <CAEsp8266.h>
#include "Context.h"
#include "Menus.h"
#include "NewMenu.h"
#include "PacketProcessor.h"
#include "Terminal.h"
#include "Tests.h"
Expand Down
69 changes: 33 additions & 36 deletions Sam3x/CA6/Menus.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,22 @@
#include "Context.h"
#include "PacketProcessor.h"

//////////////////////////////
// executeLimitAt - Helper function to limit how often code executes
//////////////////////////////

bool executeLimitAt(uint32_t updateFrequency) {
uint32_t curTime = millis();
static uint32_t nextUpdate = millis();
bool ret = false;

if ((curTime >= nextUpdate) || (curTime-nextUpdate < updateFrequency*1024)) { // Handles wraparounds
ret = true;
nextUpdate = curTime + updateFrequency;
}
return ret;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Dev Menu - A menu that demonstrates how to make a menu
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -28,15 +44,10 @@ void dev_PhotoInit() {
}

void dev_MenuRun() {
uint32_t updateFrequency = 1000; // 1000 ms
uint32_t curTime = millis();
static uint32_t nextUpdate = millis();
static uint32_t count = 0;

// Handle outgoing packets
if ((curTime >= nextUpdate) && (curTime-nextUpdate < updateFrequency*256)) { // Handles wraparounds
if (executeLimitAt(1000)) {
g_ctx.packetHelper.writePacketString(0, String(count++).c_str());
nextUpdate = curTime + updateFrequency;
}

// Handle incoming packets
Expand Down Expand Up @@ -69,9 +80,9 @@ void dev_PhotoRun() {
// Sound Menu - Detects sound
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
typedef struct {
hwPortPin ppPin;
CASensorFilter sf;
uint32_t triggerVal;
hwPortPin ppPin; // This is the port and in where the analog sound value comes from
CASensorFilter sf; // This helps filter incoming values for a cleaner display
uint32_t triggerVal; // This stores the amount of sound change required to trigger the CA6
} SoundData;

SoundData gSoundData;
Expand All @@ -81,47 +92,44 @@ const char* sound_Name() {
}

void sound_MenuInit() {
gSoundData.ppPin = CAU::getModulePin(0, 0);
gSoundData.ppPin = CAU::getModulePin(0, 0); // Module 0 pin 0 is where the analog sound values are
CAU::pinMode(gSoundData.ppPin, ANALOG_INPUT);
gSoundData.sf.init(gSoundData.ppPin, CASensorFilter::ANALOG_THRESHOLD, 2000); // Update display ever 2000 ms
gSoundData.sf.setThreshold(2048);
gSoundData.sf.setThreshold(2048); // Analog sound can range from 0 to 4095. No sound is at a 2048 value
}

void sound_PhotoInit() {
gSoundData.ppPin = CAU::getModulePin(0, 0);
gSoundData.ppPin = CAU::getModulePin(0, 0); // Same settings as menu init since it's possible to skip right to photo mode and skip menu mode
CAU::pinMode(gSoundData.ppPin, ANALOG_INPUT);
}

void sound_MenuRun() {
uint32_t updateFrequency = 500; // 500 ms
uint32_t curTime = millis();
static uint32_t nextUpdate = millis();
uint16_t val = gSoundData.sf.getSensorData();

// Handle outgoing packets
if ((curTime >= nextUpdate) && (curTime-nextUpdate < updateFrequency*1000)) { // Handles wraparounds
if (executeLimitAt(500)) {
// Every 500 ms send a packet to the webserver so it can display the filtered current value
g_ctx.packetHelper.writePacketString(1, String(val).c_str());
nextUpdate = curTime + updateFrequency;
}

// Handle incoming packets
// Handle incoming packets from webserver
CAPacketElement *packet = processIncomingPacket();
packet = incomingPacketCheckUint32(packet, 0, gSoundData.triggerVal);
packet = incomingPacketCheckUint32(packet, 0, gSoundData.triggerVal); // Store the trigger value user set on webpage here
incomingPacketFinish(packet);
}

void sound_PhotoRun() {
while (g_ctx.state == CA_STATE_PHOTO_MODE) {
// Handle triggering
uint16_t val = CAU::analogRead(gSoundData.ppPin);
val = (val >= 2048) ? (val-2048) : (2048-val);
uint8_t trigger = (val >= gSoundData.triggerVal) ? true : false;
val = (val >= 2048) ? (val-2048) : (2048-val); // Bias value to have a center at 2048 (no sound for this sensor)

bool trigger = (val >= gSoundData.triggerVal) ? true : false;
if (trigger) {
triggerCameras();
}

// Handle incoming packets
// Handle incoming packets (needed so user can exit photo mode)
CAPacketElement *packet = processIncomingPacket();
incomingPacketFinish(packet);
}
Expand Down Expand Up @@ -154,16 +162,12 @@ void vibration_PhotoInit() {
}

void vibration_MenuRun() {
uint32_t updateFrequency = 500; // 500 ms
uint32_t curTime = millis();
static uint32_t nextUpdate = millis();
uint32_t val = gVibrationData.sf.getSensorData();

// Handle outgoing packets
if ((curTime >= nextUpdate) && (curTime-nextUpdate < updateFrequency*1000)) { // Handles wraparounds
if (executeLimitAt(1000)) {
val = map(val, 1, 4095, 1, 1000); // Convert the values used on the micro to values on webpage
g_ctx.packetHelper.writePacketString(1, String(val).c_str());
nextUpdate = curTime + updateFrequency;
}

// Handle incoming packets
Expand Down Expand Up @@ -338,20 +342,16 @@ void lightning_PhotoInit() {
}

void lightning_MenuRun() {
uint32_t curTimeMS = millis();
static uint32_t timeToDisplayMS = curTimeMS;

// Handle incoming packets
CAPacketElement *packet = processIncomingPacket();
packet = incomingPacketCheckUint32(packet, 0, gLightningData.triggerDiffThreshold);
packet = incomingPacketCheckUint32(packet, 1, gLightningData.updateRefPeriodMS);
incomingPacketFinish(packet);

// Handle outgoing packets
if ((curTimeMS >= timeToDisplayMS) && (curTimeMS - timeToDisplayMS < DISPLAYFREQMS * 1000)) { // Handles wraparounds
if (executeLimitAt(DISPLAYFREQMS)) {
gLightningData.sensorVal = CAU::analogRead(gLightningData.ppLight);
g_ctx.packetHelper.writePacketString(2, String(gLightningData.sensorVal).c_str());
timeToDisplayMS = curTimeMS + DISPLAYFREQMS;
}

// Still may need to deal with Camera settings, e.g. want to default to Focus active, no delay, etc.
Expand Down Expand Up @@ -379,7 +379,6 @@ void LTGDisplayPhotoMode() {

void lightning_PhotoRun() {
uint32_t curTimeMS = millis();
static uint32_t timeToDisplayMS = curTimeMS;
int16_t currentDif = 0;
uint32_t strikeDurUS = 0;
uint32_t strikeDurMS = 0;
Expand All @@ -389,7 +388,6 @@ void lightning_PhotoRun() {
gLightningData.referenceSensorVal = CAU::analogRead(gLightningData.ppLight); // initialize reference base
CA_LOG("Ref value starting Photo mode=%u\n", gLightningData.referenceSensorVal);
gLightningData.referenceUpdateTimeMS = curTimeMS + gLightningData.updateRefPeriodMS; // initialize the update timer
timeToDisplayMS = curTimeMS + DISPLAYFREQMS; // Update the display only once per second because it takes ~50-70 ms to do

while (g_ctx.state == CA_STATE_PHOTO_MODE) {
// Loop checking for a strike (curval - ref > trigger)
Expand Down Expand Up @@ -425,10 +423,9 @@ void lightning_PhotoRun() {
}

//Is it time to display current values?
if ((curTimeMS >= timeToDisplayMS) && (curTimeMS - timeToDisplayMS < DISPLAYFREQMS * 1000)) { // Handles wraparounds
if (executeLimitAt(DISPLAYFREQMS)) {
CA_LOG("Display\n");
LTGDisplayPhotoMode();
timeToDisplayMS = curTimeMS + DISPLAYFREQMS;
}

} // End of loop looking for start of a strike
Expand Down
26 changes: 26 additions & 0 deletions Sam3x/CA6/NewMenu.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef NEW_MENUS_H
#define NEW_MENUS_H

#include <CASensorFilter.h>
#include "Context.h"
#include "PacketProcessor.h"

/*
Here are the steps to add a new menu.
1) Add a new menu to the webpage
A) Start with an example at CameraAxe6/Esp8266/data/menus and modify it to be what you want
B) The name of the file is the name that will show up in the menu
2) Add menu logic to this file
A) Take a menu from Menus.h, copy it here, and modify it to fit your needs
B) A pretty good example to start with is Sound Menu
C) The string returned by *_Name must match the file name used for the menu in part 1
D) Update the names of the functions
E) Update the logic in the menu
3) Add the function names you added to CtxProcTable in Context.h
4) Add the function names you added to MenuData.h
5) Once the code is fully working and ready to check in move code from this file to Menus.h
6) Create a pull request on github if you'd like this to be part of the official CA6 releases
*/

#endif //NEW_MENUS_H

0 comments on commit 98d628f

Please sign in to comment.