Skip to content

Commit

Permalink
fix #3 #4: code optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
dhineshkumarmcci committed Jul 5, 2023
1 parent 9dd00c1 commit 14d3911
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 66 deletions.
34 changes: 29 additions & 5 deletions src/FED3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ void FED3::Feed(int pulse, bool pixelsoff) {
leftInterval = (millis()-leftPokeTime);
UpdateDisplay();
Event = "LeftWithPellet";
if (LoRaTransmit)
fed3wan.run(pointerToFED3); //Tx data via uart

logdata();
}

Expand All @@ -196,7 +199,10 @@ void FED3::Feed(int pulse, bool pixelsoff) {
rightInterval = (millis()-rightPokeTime);
UpdateDisplay();
Event = "RightWithPellet";
logdata();
if (LoRaTransmit)
fed3wan.run(pointerToFED3); //Tx data via uart

logdata();
}
}

Expand All @@ -212,6 +218,9 @@ void FED3::Feed(int pulse, bool pixelsoff) {
leftInterval = (millis()-leftPokeTime);
UpdateDisplay();
Event = "LeftWithPellet";
if (LoRaTransmit)
fed3wan.run(pointerToFED3); //Tx data via uart

logdata();
}

Expand All @@ -223,7 +232,10 @@ void FED3::Feed(int pulse, bool pixelsoff) {
rightInterval = (millis()-rightPokeTime);
UpdateDisplay();
Event = "RightWithPellet";
logdata();
if (LoRaTransmit)
fed3wan.run(pointerToFED3); //Tx data via uart

logdata();
}
}

Expand Down Expand Up @@ -349,6 +361,9 @@ bool FED3::RotateDisk(int steps) {
leftInterval = (millis() - leftPokeTime);
UpdateDisplay();
Event = "LeftDuringDispense";
if (LoRaTransmit)
fed3wan.run(pointerToFED3); //Tx data via uart

logdata();
}

Expand All @@ -360,6 +375,9 @@ bool FED3::RotateDisk(int steps) {
rightInterval = (millis() - rightPokeTime);
UpdateDisplay();
Event = "RightDuringDispense";
if (LoRaTransmit)
fed3wan.run(pointerToFED3); //Tx data via uart

logdata();
}

Expand Down Expand Up @@ -422,6 +440,9 @@ void FED3::Timeout(int seconds) {
leftInterval = (millis() - leftPokeTime);
UpdateDisplay();
Event = "LeftinTimeOut";
if (LoRaTransmit)
fed3wan.run(pointerToFED3); //Tx data via uart

logdata();
}

Expand All @@ -433,6 +454,9 @@ void FED3::Timeout(int seconds) {
rightInterval = (millis() - rightPokeTime);
UpdateDisplay();
Event = "RightinTimeout";
if (LoRaTransmit)
fed3wan.run(pointerToFED3); //Tx data via uart

logdata();
}
}
Expand Down Expand Up @@ -1188,16 +1212,16 @@ void FED3::logdata() {
// If any errors are detected with the SD card upon boot this function
// will blink both LEDs on the Feather M0, turn the NeoPixel into red wipe pattern,
// and display "Check SD Card" on the screen
void FED3::error(uint8_t errno) {
void FED3::error(uint8_t fed3_errno) {
if (suppressSDerrors == false){
DisplaySDError();
while (1) {
uint8_t i;
for (i = 0; i < errno; i++) {
for (i = 0; i < fed3_errno; i++) {
Blink(GREEN_LED, 25, 2);
colorWipe(strip.Color(5, 0, 0), 25); // RED
}
for (i = errno; i < 10; i++) {
for (i = fed3_errno; i < 10; i++) {
colorWipe(strip.Color(0, 0, 0), 25); // clear
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/FED3.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ This device includes hardware and code from:
#include <Fonts/Org_01.h>
#include <Adafruit_NeoPixel.h>
#include <Adafruit_AHTX0.h>
#include <stdint.h>

// Pin definitions
#define NEOPIXEL A1
Expand Down Expand Up @@ -92,7 +93,7 @@ class FED3 {
void writeHeader();
void writeConfigFile();
void writeFEDmode();
void error(uint8_t errno);
void error(uint8_t fed3_errno);
void getFilename(char *filename);
bool suppressSDerrors = false; //set to true to suppress SD card errors at startup

Expand Down
153 changes: 93 additions & 60 deletions src/FED3WAN.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Name: FED3WAN.h
#include "RTClib.h" //Include RTC library
#include <stdint.h>

#include <map>

#define HEADER_LENGTH 0x4

// real-tiem clock
Expand Down Expand Up @@ -56,11 +58,11 @@ class FED3WAN:FED3 {
BYTE_CNT = int(SerialMessageOffset::BYTE_CNT), //!< Index of byte counter
};

uint8_t buf[42]; // this sets the largest buffer size
uint8_t buf[44]; // this sets the largest buffer size
uint8_t *p;
uint16_t u16timeOut;
uint32_t u32time, u32timeOut;
uint8_t au8Buffer[42];
uint8_t au8Buffer[44];
uint8_t u8BufferSize;
uint16_t u16OutCnt;

Expand Down Expand Up @@ -257,40 +259,49 @@ void FED3WAN::putV(float V)

uint8_t FED3WAN::sessionType(String fed3_session)
{
uint8_t sessionIndex;

if (fed3_session == "ClassicFED3")
sessionIndex = 1;
else if (fed3_session == "ClosedEconomy_PR1")
sessionIndex = 2;
else if (fed3_session == "Dispenser")
sessionIndex = 3;
else if (fed3_session == "Extinction")
sessionIndex = 4;
else if (fed3_session == "FixedRatio1")
sessionIndex = 5;
else if (fed3_session == "FR_Customizable")
sessionIndex = 6;
else if (fed3_session == "FreeFeeding")
sessionIndex = 7;
else if (fed3_session == "MenuExample")
sessionIndex = 8;
else if (fed3_session == "Optogenetic_Self_Stim")
sessionIndex = 9;
else if (fed3_session == "Pavlovian")
sessionIndex = 10;
else if (fed3_session == "ProbReversalTask")
sessionIndex = 11;
else if (fed3_session == "ProgressiveRatio")
sessionIndex = 12;
else if (fed3_session == "RandomRatio")
sessionIndex = 13;
uint8_t sessionIndex = 0;

std::map<String, int> session {
{"Classic", 1},
{"ClosedEcon_PR1", 2},
{"Dispenser", 3},
{"Ext", 4},
{"FR1", 5},
{"FRCustom", 6},
{"FreeFeed", 7},
{"Menu", 8},
{"OptoStim", 9},
{"Pavlov", 10},
{"Prob_Reversal", 11},
{"ProgRat", 12},
{"RndRatio", 13},
};

auto search = session.find(fed3_session);

if (search != session.end())
sessionIndex = search->second;
else
sessionIndex = 0;

return sessionIndex;
}

enum class Events : uint8_t
{
left = 1,
leftshort,
leftwithpellet,
leftintimeout,
leftduringdispense,
right,
rightshort,
rightwithpellet,
rightintimeout,
rightduringdispense,
pellet,
};

void FED3WAN::run(FED3 *fed3)
{
this->p = this->buf;
Expand Down Expand Up @@ -329,48 +340,70 @@ void FED3WAN::run(FED3 *fed3)
uint8_t session_type = this->sessionType(fed3->sessiontype);
put(session_type);

// battery voltage
this->putV(fed3->measuredvbat);

// number of motor turns
this->put4u(fed3->numMotorTurns+1);

// FixedRatio
this->put2u(fed3->FR);

// Active event
uint8_t event_active;
uint16_t pokeTime;
uint16_t retrievalTime;
uint16_t event_with_time;

if (fed3->Event == "Left")
{
pokeTime = fed3->leftInterval / 4;
pokeTime &= 0x3FFF;
event_active = 1;
event_with_time = (pokeTime << 2) | event_active;
}
else if (fed3->Event == "Right")
{
pokeTime = fed3->rightInterval / 4;
pokeTime &= 0x3FFF;
event_active = 2;
event_with_time = (pokeTime << 2) | event_active;
}
else if (fed3->Event == "Pellet")
{
retrievalTime = fed3->retInterval / 4;
retrievalTime &= 0x3FFF;
event_active = 3;
event_with_time = (retrievalTime << 2) | event_active;
}
std::map<String, int> event {
{"Left", (uint8_t) Events::left},
{"LeftShort", (uint8_t) Events::leftshort},
{"LeftWithPellet", (uint8_t) Events::leftwithpellet},
{"LeftinTimeout", (uint8_t) Events::leftintimeout},
{"LeftDuringDispense", (uint8_t) Events::leftduringdispense},
{"Right", (uint8_t) Events::right},
{"RightShort", (uint8_t) Events::rightshort},
{"RightWithPellet", (uint8_t) Events::rightwithpellet},
{"RightinTimeout", (uint8_t) Events::rightintimeout},
{"RightDuringDispense", (uint8_t) Events::rightduringdispense},
{"Pellet", (uint8_t) Events::pellet},
};

auto search = event.find(fed3->Event);

if (search != event.end())
event_active = search->second;
else
{
pokeTime = 0;
retrievalTime = 0;
event_active = 0;
}
event_active = 0;

this->put2u(event_with_time);
this->put(event_active);

// poke time / retrieval time
uint16_t pokeTime = 0;
uint16_t retrievalTime = 0;
uint16_t event_time = 0;

if (event_active > 0 && event_active < 6)
{
pokeTime = fed3->leftInterval / 4;
event_time = pokeTime;
}
else if (event_active > 5 && event_active < 11)
{
pokeTime = fed3->rightInterval / 4;
event_time = pokeTime;
}
else if (event_active == 11)
{
retrievalTime = fed3->retInterval / 4;
event_time = retrievalTime;
}

this->put2u(event_time);

// poke counts
this->put4u(fed3->LeftCount);
this->put4u(fed3->RightCount);
this->put4u(fed3->PelletCount);

// block pellet count
this->put2u(fed3->BlockPelletCount);

auto pMessage = this->getbase();
Expand Down

0 comments on commit 14d3911

Please sign in to comment.