Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
openshwprojects committed Sep 23, 2024
2 parents e25bf8a + 266954a commit db6d81d
Show file tree
Hide file tree
Showing 22 changed files with 351 additions and 64 deletions.
2 changes: 2 additions & 0 deletions openBeken_win32_mvsc2017.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,7 @@
<ClCompile Include="src\sim\Controller_BL0942.cpp" />
<ClCompile Include="src\sim\Controller_Bulb.cpp" />
<ClCompile Include="src\sim\Controller_Button.cpp" />
<ClCompile Include="src\sim\Controller_DHT11.cpp" />
<ClCompile Include="src\sim\Controller_Pot.cpp" />
<ClCompile Include="src\sim\Controller_SimulatorLink.cpp" />
<ClCompile Include="src\sim\Controller_Switch.cpp" />
Expand Down Expand Up @@ -1074,6 +1075,7 @@
<ClInclude Include="src\sim\Controller_BL0942.h" />
<ClInclude Include="src\sim\Controller_Bulb.h" />
<ClInclude Include="src\sim\Controller_Button.h" />
<ClInclude Include="src\sim\Controller_DHT11.h" />
<ClInclude Include="src\sim\Controller_Pot.h" />
<ClInclude Include="src\sim\Controller_SimulatorLink.h" />
<ClInclude Include="src\sim\Controller_Switch.h" />
Expand Down
2 changes: 2 additions & 0 deletions openBeken_win32_mvsc2017.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@
<ClCompile Include="src\driver\drv_charts.c" />
<ClCompile Include="src\driver\drv_test_charts.c" />
<ClCompile Include="src\sim\Controller_Switch.cpp" />
<ClCompile Include="src\sim\Controller_DHT11.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\base64\base64.h" />
Expand Down Expand Up @@ -495,6 +496,7 @@
<ClInclude Include="src\sim\Controller_WS2812.h" />
<ClInclude Include="src\driver\drv_spiLED.h" />
<ClInclude Include="src\sim\Controller_Switch.h" />
<ClInclude Include="src\sim\Controller_DHT11.h" />
</ItemGroup>
<ItemGroup>
<CustomBuild Include="..\..\platforms\bk7231t\bk7231t_os\beken378\func\include\net_param_pub.h" />
Expand Down
86 changes: 57 additions & 29 deletions src/cmnds/cmd_if.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,19 @@ const char *CMD_FindOperator(const char *s, const char *stop, byte *oCode) {

retVal = 0;
bestPriority = 0;

int level = 0;
while (s[0] && s[1] && (s < stop || stop == 0)) {
if (s != signSkip) {
if (*s == '(') {
level++;
} else if (*s == ')') {
level--;
} else if (s != signSkip) {
for (o = 0; o < g_numOperators; o++) {
if (!strncmp(s, g_operators[o].txt, g_operators[o].len)) {
if (g_operators[o].prio >= bestPriority) {
bestPriority = g_operators[o].prio;
int prio = g_operators[o].prio;
prio -= level * 1000;
if (prio >= bestPriority) {
bestPriority = prio;
retVal = s;
*oCode = o;
signSkip = s + g_operators[o].len;
Expand Down Expand Up @@ -675,35 +681,35 @@ const char *CMD_ExpandConstantString(const char *s, const char *stop, char *out,
#endif

const char *CMD_ExpandConstantToString(const char *constant, char *out, char *stop) {
int outLen;
float value;
int valueInt;
const char *after;
float delta;
int outLen;
float value;
int valueInt;
const char *after;
float delta;

outLen = (stop - out) - 1;
outLen = (stop - out) - 1;

after = CMD_ExpandConstant(constant, 0, &value);
after = CMD_ExpandConstant(constant, 0, &value);
#if WINDOWS
if (after == 0) {
after = CMD_ExpandConstantString(constant, 0, out, outLen);
return after;
}
if (after == 0) {
after = CMD_ExpandConstantString(constant, 0, out, outLen);
return after;
}
#endif
if (after == 0)
return 0;
if (after == 0)
return 0;

valueInt = (int)value;
delta = valueInt - value;
if (delta < 0)
delta = -delta;
if (delta < 0.001f) {
snprintf(out, outLen, "%i", valueInt);
}
else {
snprintf(out, outLen, "%f", value);
}
return after;
valueInt = (int)value;
delta = valueInt - value;
if (delta < 0)
delta = -delta;
if (delta < 0.001f) {
snprintf(out, outLen, "%i", valueInt);
}
else {
snprintf(out, outLen, "%f", value);
}
return after;
}
void CMD_ExpandConstantsWithinString(const char *in, char *out, int outLen) {
char *outStop;
Expand Down Expand Up @@ -771,6 +777,24 @@ char *CMD_ExpandingStrdup(const char *in) {
CMD_ExpandConstantsWithinString(in, ret, realLen);
return ret;
}
const char *CMD_FindMatchingBrace(const char *s) {
if (*s != '(')
return s;
int level = 1;
s++;
while (*s) {
if (*s == '(')
level++;
else if (*s == ')') {
level--;
if (level == 0)
return s;
}
s++;
}
return s;

}
float CMD_EvaluateExpression(const char *s, const char *stop) {
byte opCode;
const char *op;
Expand All @@ -789,12 +813,17 @@ float CMD_EvaluateExpression(const char *s, const char *stop) {
while (stop > s && isspace(((int)stop[-1]))) {
stop--;
}
// cull whitespaces at the start
while (isspace(((int)*s))) {
s++;
if (s >= stop) {
return 0;
}
}
while (*s == '(' && stop[-1] == ')' && CMD_FindMatchingBrace(s) == (stop-1)) {
s++;
stop--;
}
if (g_expDebugBuffer == 0) {
g_expDebugBuffer = malloc(EXPRESSION_DEBUG_BUFFER_SIZE);
}
Expand All @@ -804,7 +833,6 @@ float CMD_EvaluateExpression(const char *s, const char *stop) {
g_expDebugBuffer[idx] = 0;
ADDLOG_IF_MATHEXP_DBG(LOG_FEATURE_EVENT, "CMD_EvaluateExpression: will run '%s'", g_expDebugBuffer);
}

op = CMD_FindOperator(s, stop, &opCode);
if (op) {
const char *p2;
Expand Down
5 changes: 4 additions & 1 deletion src/cmnds/cmd_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,10 @@ void CMD_RegisterCommand(const char* name, commandHandler_t handler, void* conte
// check
newCmd = CMD_Find(name);
if (newCmd != 0) {
ADDLOG_ERROR(LOG_FEATURE_CMD, "command with name %s already exists!", name);
// it happens very often in Simulator due to the lack of the ability to remove commands
if (newCmd->handler != handler) {
ADDLOG_ERROR(LOG_FEATURE_CMD, "command with name %s already exists!", name);
}
return;
}
ADDLOG_DEBUG(LOG_FEATURE_CMD, "Adding command %s", name);
Expand Down
20 changes: 2 additions & 18 deletions src/driver/drv_dht.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,9 @@
#include "drv_local.h"
#include "drv_dht_internal.h"

// test device
static dht_t *test = 0;
// per-pin devices
static dht_t **g_dhts = 0;

// simplest demo
void DHT_DoMyDHTTest() {
if (test == 0) {
test = DHT_Create(24, DHT11);
}
if (test) {
float temp, humid;

humid = DHT_readHumidity(test, false);
temp = DHT_readTemperature(test, false, false);

addLogAdv(LOG_INFO, LOG_FEATURE_ENERGYMETER, "DHT says %f %f", humid, temp);
}
}
int translateDHTType(int role) {
if (role == IOR_DHT11)
return DHT11;
Expand Down Expand Up @@ -101,8 +85,8 @@ void DHT_OnEverySecond() {
if (g_dhts[i]->_lastresult != 0) {
// don't want to loose accuracy, so multiply by 10
// We have a channel types to handle that
CHANNEL_Set(g_cfg.pins.channels[i], (int)(temp * 10), 0);
CHANNEL_Set(g_cfg.pins.channels2[i], (int)(humid), 0);
CHANNEL_Set(g_cfg.pins.channels[i], (int)(temp * 10), CHANNEL_SET_FLAG_SILENT);
CHANNEL_Set(g_cfg.pins.channels2[i], (int)(humid), CHANNEL_SET_FLAG_SILENT);
}
}
}
Expand Down
13 changes: 5 additions & 8 deletions src/driver/drv_dht_internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ float DHT_computeHeatIndexInternal(dht_t *dht, float temperature, float percentH
* @param force
* true if using force mode
* @return float value
*/
*/
bool DHT_read(dht_t *dht, bool force) {
byte *data = dht->data;
// Check if sensor was read less than two seconds ago and return early
Expand All @@ -288,14 +288,11 @@ bool DHT_read(dht_t *dht, bool force) {
dht->_lastreadtime = currenttime;

#if WINDOWS
bool SIM_ReadDHT11(int pin, byte *data);

dht->_lastresult = true;
data[0] = data[1] = data[2] = data[3] = data[4] = 0;
// temp 19
data[2] = 19;
// humidity 67.8
data[0] = 67;
data[1] = 0;
return true;

return SIM_ReadDHT11(dht->_pin, data);
#endif
// Reset 40 bits of received data to zero.
data[0] = data[1] = data[2] = data[3] = data[4] = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/mqtt/new_mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ int channelGet(obk_mqtt_request_t* request) {
// atoi won't parse any non-decimal chars, so it should skip over the rest of the topic.
channel = atoi(p);

addLogAdv(LOG_INFO, LOG_FEATURE_MQTT, "channelGet channel %i", channel);
//addLogAdv(LOG_INFO, LOG_FEATURE_MQTT, "channelGet channel %i", channel);

// if channel out of range, stop here.
if ((channel < 0) || (channel > 32)) {
Expand Down
50 changes: 50 additions & 0 deletions src/selftest/selftest_expressions.c
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,60 @@ void Test_Expressions_RunTests_Basic() {
SELFTEST_ASSERT((3 - 6 % 4 + 2) == 3);
SELFTEST_ASSERT_EXPRESSION("3 - 6 % 4 + 2", 3)

// try with braces
SELFTEST_ASSERT(((3 - 6) % 4 + 2) == -1);
SELFTEST_ASSERT_EXPRESSION("(3 - 6) % 4 + 2", -1)

SELFTEST_ASSERT(((3 - 6) % (4 + 2)) == -3);
SELFTEST_ASSERT_EXPRESSION("(3 - 6) % (4 + 2)", -3)

//CHANNEL_Set(18, 15, 0);
//SELFTEST_ASSERT_EXPRESSION("15.0+$CH18+1000\n\r", 30.0f + 1000);
//SELFTEST_ASSERT_EXPRESSION("15.0/$CH18+1000\n\r", 1.0f + 1000);
//SELFTEST_ASSERT_EXPRESSION("1.50/$CH18+1000\n\r", 0.1f + 1000);

}



void Test_Expressions_RunTests_Braces() {
SELFTEST_ASSERT_EXPRESSION("2*(3+3)", 12);
SELFTEST_ASSERT_EXPRESSION("2*((3+3))", 12);
SELFTEST_ASSERT_EXPRESSION("(2*((3+3)))", 12);
SELFTEST_ASSERT_EXPRESSION("((2)*((3+3)))", 12);
SELFTEST_ASSERT_EXPRESSION("(((2)*((3+3))))", 12);
SELFTEST_ASSERT_EXPRESSION("(((2)*(((3)+(3)))))", 12);
SELFTEST_ASSERT_EXPRESSION("2*(3+7)", 20);
SELFTEST_ASSERT_EXPRESSION("2*(3+4)", 14);
SELFTEST_ASSERT_EXPRESSION("4*(3+4)", 28);
SELFTEST_ASSERT_EXPRESSION("4*(3+4+3)", 40);
SELFTEST_ASSERT_EXPRESSION("9*(2+6+1)", 81);
SELFTEST_ASSERT_EXPRESSION("2*(8+7)", 30);
SELFTEST_ASSERT_EXPRESSION("10*(3+5)", 80);
SELFTEST_ASSERT_EXPRESSION("3*(7+7)", 42);
SELFTEST_ASSERT_EXPRESSION("11*(2+3)", 55);
SELFTEST_ASSERT_EXPRESSION("12*(3+3+3)", 108);
SELFTEST_ASSERT_EXPRESSION("5*(4+5)", 45);
SELFTEST_ASSERT_EXPRESSION("2*((3+5)*2)", 32);
SELFTEST_ASSERT_EXPRESSION("4*(3+(2*5))", 52);
SELFTEST_ASSERT_EXPRESSION("(3*(2+3))*(4+1)", 75);
SELFTEST_ASSERT_EXPRESSION("(2+3)*(4+(5*2))", 70);
SELFTEST_ASSERT_EXPRESSION("((3+2)*(2+1))*2", 30);
SELFTEST_ASSERT_EXPRESSION("4*((2+5)+(3*2))", 52);
SELFTEST_ASSERT_EXPRESSION("2*(3+((4*2)+1))", 24);
SELFTEST_ASSERT_EXPRESSION("((3+4)*(2+2))*2", 56);
SELFTEST_ASSERT_EXPRESSION("(5*(3+(2*2)))+10", 45);
SELFTEST_ASSERT_EXPRESSION("(6+4)*(3+(2*3))", 90);
SELFTEST_ASSERT_EXPRESSION("2*(3+5)+((2+3)*4)", 36);
SELFTEST_ASSERT_EXPRESSION("((4*2)+(3*(2+3)))*2", 46);
SELFTEST_ASSERT_EXPRESSION("((3+4)*(5+6))+((2*3)+4)", 87);
CMD_ExecuteCommand("setChannel 1 4",0);
SELFTEST_ASSERT_EXPRESSION("((3+$CH1)*(5+6))+((2.0*3.0)+$CH1)", 87);
CMD_ExecuteCommand("setChannel 2 3", 0);
SELFTEST_ASSERT_EXPRESSION("(($CH2+$CH1)*(5+6))+((2.0*$CH2)+$CH1)", 87);



}

#endif
2 changes: 2 additions & 0 deletions src/selftest/selftest_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ void Test_ChargeLimitDriver();
void Test_WS2812B();
void Test_DoorSensor();
void Test_Enums();
void Test_Expressions_RunTests_Basic();
void Test_Expressions_RunTests_Braces();

void Test_GetJSONValue_Setup(const char *text);
void Test_FakeHTTPClientPacket_GET(const char *tg);
Expand Down
65 changes: 65 additions & 0 deletions src/sim/Controller_DHT11.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#ifdef WINDOWS
#include "Simulator.h"
#include "Simulation.h"
#include "Junction.h"
#include "Solver.h"
#include "sim_local.h"
#include "Controller_DHT11.h"
#include "Controller_SimulatorLink.h"
#include "Shape.h"
#include "Junction.h"
#include "Text.h"

extern "C" bool SIM_ReadDHT11(int pin, byte *data) {
data[0] = data[1] = data[2] = data[3] = data[4] = 0;
// temp 19
data[2] = 19;
// humidity 67
data[0] = 67;
data[1] = 0;
if (g_sim == 0)
return true;
CSimulation *s = g_sim->getSim();
if (s == 0)
return true;
TArray<CControllerDHT11 *> dhts = s->findControllersOfType<CControllerDHT11>();
for (int d = 0; d < dhts.size(); d++) {
CControllerDHT11 *dht = dhts[d];
CControllerSimulatorLink *wifi = s->findFirstControllerOfType<CControllerSimulatorLink>();
if (wifi == 0)
return true;
CJunction *p = wifi->findJunctionByGPIOIndex(pin);
if (p == 0)
return true;
data[2] = dht->getTemperature();
data[0] = dht->getHumidity();
if (g_sim->getSolver()->hasPath(dht->getDataPin(), p)) {
return true;
}
}
return true;
}

void CControllerDHT11::onDrawn() {
if (txt_temperature->isBeingEdited() == false) {
realTemperature = txt_temperature->getFloat();
}
if (txt_humidity->isBeingEdited() == false) {
realHumidity = txt_humidity->getFloat();
}
}
class CControllerBase *CControllerDHT11::cloneController(class CShape *origOwner, class CShape *newOwner) {
CControllerDHT11 *r = new CControllerDHT11();
if (dat) {
r->dat = newOwner->findShapeByName_r(dat->getName())->asJunction();
}
if (txt_temperature) {
r->txt_temperature = newOwner->findShapeByName_r(txt_temperature->getName())->asText();
}
if (txt_humidity) {
r->txt_humidity = newOwner->findShapeByName_r(txt_humidity->getName())->asText();
}
return r;
}

#endif
Loading

0 comments on commit db6d81d

Please sign in to comment.