From 1ad2e00a7c08aa4234e58436530e65e1a37524da Mon Sep 17 00:00:00 2001 From: openshwprojects Date: Wed, 7 Dec 2022 01:10:23 +0100 Subject: [PATCH] sim: BL0942 object with controller (sends BL0942 packets); not finished yet; also fix Text edit cursor --- openBeken_win32_mvsc2017.vcxproj | 2 + openBeken_win32_mvsc2017.vcxproj.filters | 6 ++ src/sim/Controller_BL0942.cpp | 74 ++++++++++++++++++++++++ src/sim/Controller_BL0942.h | 39 +++++++++++++ src/sim/PrefabManager.cpp | 41 +++++++++++++ src/sim/PrefabManager.h | 1 + src/sim/Shape.cpp | 7 ++- src/sim/Shape.h | 6 +- src/sim/Simulation.cpp | 23 +++++++- src/sim/Simulation.h | 4 +- src/sim/Simulator.cpp | 4 +- src/sim/Simulator.h | 2 +- src/sim/Text.cpp | 6 +- src/sim/Text.h | 17 ++++++ src/sim/Tool_Text.cpp | 3 +- src/sim/sim_local.h | 4 ++ 16 files changed, 229 insertions(+), 10 deletions(-) create mode 100644 src/sim/Controller_BL0942.cpp create mode 100644 src/sim/Controller_BL0942.h diff --git a/openBeken_win32_mvsc2017.vcxproj b/openBeken_win32_mvsc2017.vcxproj index 72cf69756..e1d484f56 100644 --- a/openBeken_win32_mvsc2017.vcxproj +++ b/openBeken_win32_mvsc2017.vcxproj @@ -516,6 +516,7 @@ + @@ -567,6 +568,7 @@ + diff --git a/openBeken_win32_mvsc2017.vcxproj.filters b/openBeken_win32_mvsc2017.vcxproj.filters index f86171a88..614f668a2 100644 --- a/openBeken_win32_mvsc2017.vcxproj.filters +++ b/openBeken_win32_mvsc2017.vcxproj.filters @@ -405,6 +405,9 @@ Drv + + Simulator + @@ -485,6 +488,9 @@ Simulator + + Simulator + diff --git a/src/sim/Controller_BL0942.cpp b/src/sim/Controller_BL0942.cpp new file mode 100644 index 000000000..d09badeab --- /dev/null +++ b/src/sim/Controller_BL0942.cpp @@ -0,0 +1,74 @@ +#ifdef WINDOWS +#include "Controller_BL0942.h" +#include "Shape.h" +#include "Junction.h" +#include "Text.h" + +#define BL0942_PACKET_LEN 23 +#define BL0942_READ_COMMAND 0x58 +static float BL0942_PREF = 598; +static float BL0942_UREF = 15188; +static float BL0942_IREF = 251210; + +void CControllerBL0942::onDrawn() { + int currentPending = UART_GetDataSize(); + if (currentPending > 0) { + return; + } + if (1) { + CMD_ExecuteCommand("startDriver BL0942", 0); + } + int i; + byte data[BL0942_PACKET_LEN]; + memset(data, 0, sizeof(data)); + data[0] = 0x55; + byte checksum = BL0942_READ_COMMAND; + float realCurrent = 0.25f; + float realPower = 60.0f; + float realVoltage = 220.0f; + + int bl_current = BL0942_IREF * realCurrent; + int bl_power = BL0942_PREF * realPower; + int bl_voltage = BL0942_UREF * realVoltage; + + data[1] = (byte)(bl_current); + data[2] = (byte)(bl_current >> 8); + data[3] = (byte)(bl_current >> 16); + + data[4] = (byte)(bl_voltage); + data[5] = (byte)(bl_voltage >> 8); + data[6] = (byte)(bl_voltage >> 16); + + data[10] = (byte)(bl_power); + data[11] = (byte)(bl_power >> 8); + data[12] = (byte)(bl_power >> 16); + for (i = 0; i < BL0942_PACKET_LEN - 1; i++) { + checksum += data[i]; + } + checksum ^= 0xFF; + data[BL0942_PACKET_LEN - 1] = checksum; + for (i = 0; i < BL0942_PACKET_LEN; i++) { + UART_AppendByteToCircularBuffer(data[i]); + } +} +class CControllerBase *CControllerBL0942::cloneController(class CShape *origOwner, class CShape *newOwner) { + CControllerBL0942 *r = new CControllerBL0942(); + if (tx) { + r->tx = newOwner->findShapeByName_r(tx->getName())->asJunction(); + } + if (rx) { + r->rx = newOwner->findShapeByName_r(rx->getName())->asJunction(); + } + if (txt_voltage) { + r->txt_voltage = newOwner->findShapeByName_r(txt_voltage->getName())->asText(); + } + if (txt_current) { + r->txt_current = newOwner->findShapeByName_r(txt_current->getName())->asText(); + } + if (txt_power) { + r->txt_power = newOwner->findShapeByName_r(txt_power->getName())->asText(); + } + return r; +} + +#endif diff --git a/src/sim/Controller_BL0942.h b/src/sim/Controller_BL0942.h new file mode 100644 index 000000000..52809a6a5 --- /dev/null +++ b/src/sim/Controller_BL0942.h @@ -0,0 +1,39 @@ +#ifndef __CONTROLLER_BL0942_H__ +#define __CONTROLLER_BL0942_H__ + +#include "sim_local.h" +#include "Controller_Base.h" +#include "Coord.h" + +class CControllerBL0942 : public CControllerBase { + class CJunction *rx, *tx; + class CText *txt_voltage, *txt_current, *txt_power; + + void setShapesActive(bool b); + void setShapesFillColor(const CColor &c); + float realCurrent; + float realPower; + float realVoltage; +public: + CControllerBL0942() { + rx = tx = 0; + txt_voltage = txt_current = txt_power = 0; + realCurrent = 0.25f; + realPower = 60.0f; + realVoltage = 220.0f; + } + CControllerBL0942(class CJunction *_rx, class CJunction *_tx, CText *_txt_voltage, CText *_txt_current, CText *_txt_power) { + rx = _rx; + tx = _tx; + txt_voltage = _txt_voltage; + txt_current = _txt_current; + txt_power = _txt_power; + realCurrent = 0.25f; + realPower = 60.0f; + realVoltage = 220.0f; + } + virtual void onDrawn(); + virtual class CControllerBase *cloneController(class CShape *origOwner, class CShape *newOwner); +}; + +#endif // __CONTROLLER_BL0942_H__ diff --git a/src/sim/PrefabManager.cpp b/src/sim/PrefabManager.cpp index 7cf120aa9..091b7642f 100644 --- a/src/sim/PrefabManager.cpp +++ b/src/sim/PrefabManager.cpp @@ -2,9 +2,11 @@ #include "PrefabManager.h" #include "Shape.h" #include "Wire.h" +#include "Text.h" #include "Controller_Button.h" #include "Controller_Bulb.h" #include "Controller_SimulatorLink.h" +#include "Controller_BL0942.h" #include "Junction.h" class CShape *PrefabManager::generateVDD() { @@ -52,6 +54,44 @@ class CShape *PrefabManager::generateButton() { o->translateEachChild(0, 10); return o; } +class CShape *PrefabManager::generateBL0942() { + + CShape *o = new CShape(); + o->setName("BL0942"); + int w = 40; + int h = 40; + o->addText(-w-5, -h-5, "BL0942"); + o->addText(-w + 5, -h + 15, "U:"); + o->addText(-w + 5, -h + 35, "I:"); + o->addText(-w + 5, -h + 55, "P:"); + CText *tx_voltage = o->addText(-w + 25, -h + 15, "230V", true, false)->asText(); + tx_voltage->setName("text_voltage"); + CText *tx_current = o->addText(-w + 25, -h + 35, "0.24A", true, false)->asText(); + tx_current->setName("text_current"); + CText *tx_power = o->addText(-w + 25, -h + 55, "60W", true, false)->asText(); + tx_power->setName("text_power"); + o->addRect(-w, -h, w * 2, 80); + CJunction *rx = o->addJunction(-w-20, -20, "RX"); + o->addLine(-w-20, -20, -w, -20); + rx->setName("RX"); + rx->addText(-5, -5, "RX"); + CJunction *tx = o->addJunction(-w - 20, 20, "TX"); + o->addLine(-w - 20, 20, -w, 20); + tx->setName("TX"); + tx->addText(-5, -5, "TX"); + + CJunction *vdd = o->addJunction(w + 20, -20, "VDD"); + o->addLine(w + 20, -20, w, -20); + vdd->setName("VDD"); + vdd->addText(-5, -5, "VDD"); + CJunction *gnd = o->addJunction(w + 20, 20, "GND"); + o->addLine(w + 20, 20, w, 20); + gnd->setName("GND"); + gnd->addText(-5, -5, "GND"); + CControllerBL0942 *cntr = new CControllerBL0942(rx, tx, tx_voltage, tx_current, tx_power); + o->setController(cntr); + return o; +} class CShape *PrefabManager::generateTest() { CShape *o = new CShape(); @@ -383,6 +423,7 @@ void PrefabManager::createDefaultPrefabs() { addPrefab(generateTest()); addPrefab(generateGND()); addPrefab(generateVDD()); + addPrefab(generateBL0942()); } diff --git a/src/sim/PrefabManager.h b/src/sim/PrefabManager.h index 85c22214b..f3c06c03c 100644 --- a/src/sim/PrefabManager.h +++ b/src/sim/PrefabManager.h @@ -18,6 +18,7 @@ class PrefabManager { class CShape *generateWB3S(); class CShape *generateButton(); class CShape *generateTest(); + class CShape *generateBL0942(); class CShape *generateGND(); class CShape *generateVDD(); public: diff --git a/src/sim/Shape.cpp b/src/sim/Shape.cpp index 4a5acb9b2..2706d77b5 100644 --- a/src/sim/Shape.cpp +++ b/src/sim/Shape.cpp @@ -31,6 +31,9 @@ class CShape *CShape::findShapeByName_r(const char *name) { class CJunction *CShape::asJunction() { return dynamic_cast(this); } +class CText *CShape::asText() { + return dynamic_cast(this); +} void CShape::snapToGrid() { Coord n = roundToGrid(getPosition()); setPosition(n); @@ -134,8 +137,10 @@ class CShape* CShape::addRect(int x, int y, int w, int h) { addShape(n); return n; } -class CShape* CShape::addText(int x, int y, const char *s) { +class CShape* CShape::addText(int x, int y, const char *s, bool bDeepText, bool bAllowNewLine) { CText *n = new CText(x, y, s); + n->setDeepText(bDeepText); + n->setAllowNewLine(bAllowNewLine); addShape(n); return n; } diff --git a/src/sim/Shape.h b/src/sim/Shape.h index 12d9ddc16..206989c38 100644 --- a/src/sim/Shape.h +++ b/src/sim/Shape.h @@ -47,6 +47,7 @@ public: return name.c_str(); } class CJunction *asJunction(); + class CText *asText(); void snapToGrid(); void cloneShapeTo(CShape *o); virtual CShape *cloneShape(); @@ -132,6 +133,9 @@ public: float getY() const { return pos.getY(); } + virtual bool isDeepText() const { + return false; + } virtual bool isWire() const { return false; } @@ -146,7 +150,7 @@ public: class CShape* addLine(int x, int y, int x2, int y2); class CShape* addRect(int x, int y, int w, int h); class CShape* addCircle(float x, float y, float r); - class CShape* addText(int x, int y, const char *s); + class CShape* addText(int x, int y, const char *s, bool bDeepText = false, bool bAllowNewLine = true); class CJunction* addJunction(int x, int y, const char *name = "", int gpio = -1); class CShape* addShape(CShape *p); void recalcBoundsAll(); diff --git a/src/sim/Simulation.cpp b/src/sim/Simulation.cpp index cb07e56cd..bcd0bdd7c 100644 --- a/src/sim/Simulation.cpp +++ b/src/sim/Simulation.cpp @@ -44,10 +44,27 @@ void CSimulation::drawSim() { wires[i]->drawWire(); } } -class CShape *CSimulation::findShapeByBoundsPoint(const class Coord &p) { +class CShape *CSimulation::findDeepText_r(const class Coord &p, class CShape *cur) { + Coord local = p - cur->getPosition(); + for (int i = 0; i < cur->getShapesCount(); i++) { + CShape *ch = cur->getShape(i); + if (ch->hasWorldPointInside(local)) { + if (ch->isDeepText()) + return ch; + } + } + return 0; +} +class CShape *CSimulation::findShapeByBoundsPoint(const class Coord &p, bool bIncludeDeepText) { for (int i = 0; i < objects.size(); i++) { - if (objects[i]->hasWorldPointInside(p)) + if (objects[i]->hasWorldPointInside(p)) { + if (bIncludeDeepText) { + CShape *r = findDeepText_r(p,objects[i]); + if (r != 0) + return r; + } return objects[i]; + } } for (int i = 0; i < wires.size(); i++) { CShape *r = wires[i]->findSubPart(p); @@ -161,6 +178,8 @@ void CSimulation::createDemo() { CShape *strip2 = addObject(sim->getPfbs()->instantiatePrefab("StripCW")); strip2->setPosition(400, 600); + CShape *bl0942 = addObject(sim->getPfbs()->instantiatePrefab("BL0942")); + bl0942->setPosition(800, 600); //CShape *strip3 = addObject(sim->getPfbs()->instantiatePrefab("StripRGB")); //strip3->setPosition(400, 700); if (0) { diff --git a/src/sim/Simulation.h b/src/sim/Simulation.h index 35844f6da..2c31fd124 100644 --- a/src/sim/Simulation.h +++ b/src/sim/Simulation.h @@ -3,6 +3,7 @@ #include "sim_local.h" + class CSimulation { class CSimulator *sim; // all allocated objects (must be fried later) @@ -18,6 +19,7 @@ class CSimulation { void registerJunction(class CJunction *ju); void registerJunctions(class CWire *w); void registerJunctions(class CShape *s); + class CShape *findDeepText_r(const class Coord &p, class CShape *cur); public: void setSimulator(class CSimulator *ssim) { this->sim = ssim; @@ -50,7 +52,7 @@ public: class CText *addText(const class Coord &p, const char *txt); class CWire *addWire(const class Coord &a, const class Coord &b); class CWire *addWire(float x0, float y0, float x1, float y1); - class CShape *findShapeByBoundsPoint(const class Coord &p); + class CShape *findShapeByBoundsPoint(const class Coord &p, bool bIncludeDeepText = false); void destroyObject(CShape *s); void tryMatchJunction(class CJunction *jn, class CJunction *other); void matchJunction_r(class CShape *sh, class CJunction *j); diff --git a/src/sim/Simulator.cpp b/src/sim/Simulator.cpp index 6fd86047a..5b98cf86a 100644 --- a/src/sim/Simulator.cpp +++ b/src/sim/Simulator.cpp @@ -230,9 +230,9 @@ void CSimulator::beginEditingText(class CText *txt) { void CSimulator::destroyObject(CShape *s) { sim->destroyObject(s); } -class CShape *CSimulator::getShapeUnderCursor() { +class CShape *CSimulator::getShapeUnderCursor(bool bIncludeDeepText) { Coord p = GetMousePos(); - return sim->findShapeByBoundsPoint(p); + return sim->findShapeByBoundsPoint(p,bIncludeDeepText); } bool CSimulator::createSimulation(bool bDemo) { projectPath = ""; diff --git a/src/sim/Simulator.h b/src/sim/Simulator.h index 8434c6e2b..648583b91 100644 --- a/src/sim/Simulator.h +++ b/src/sim/Simulator.h @@ -34,7 +34,7 @@ public: void showExitSaveMessageBox(); void destroyObject(class CShape *s); void beginEditingText(class CText *txt); - class CShape *getShapeUnderCursor(); + class CShape *getShapeUnderCursor(bool bIncludeDeepText = false); class CSimulation*getSim() { return sim; } diff --git a/src/sim/Text.cpp b/src/sim/Text.cpp index 7fead6ae8..0322a594e 100644 --- a/src/sim/Text.cpp +++ b/src/sim/Text.cpp @@ -32,6 +32,8 @@ CShape *CText::cloneShape() { CText *r = new CText(); r->pos = this->pos; r->txt = this->txt; + r->bDeepText = this->bDeepText; + r->bAllowNewLine = this->bAllowNewLine; this->cloneShapeTo(r); return r; } @@ -61,7 +63,9 @@ bool CText::processKeyDown(int keyCode) { return true; } if (keyCode == SDLK_RETURN) { - appendText("\n"); + if (bAllowNewLine) { + appendText("\n"); + } } return false; } diff --git a/src/sim/Text.h b/src/sim/Text.h index 8b26a58ea..405d5ce10 100644 --- a/src/sim/Text.h +++ b/src/sim/Text.h @@ -5,6 +5,8 @@ #include "Shape.h" class CText : public CShape { + bool bDeepText; + bool bAllowNewLine; std::string txt; bool bTextEditMode; int cursorPos; @@ -14,18 +16,27 @@ public: CText() { cursorPos = 0; bTextEditMode = false; + bDeepText = false; + bAllowNewLine = true; } CText(const Coord &np, const char *s) { this->setPosition(np); this->txt = s; cursorPos = strlen(s); bTextEditMode = false; + bDeepText = false; + bAllowNewLine = true; } CText(int _x, int _y, const char *s) { this->setPosition(_x, _y); this->txt = s; cursorPos = strlen(s); bTextEditMode = false; + bDeepText = false; + bAllowNewLine = true; + } + void setAllowNewLine(bool b) { + bAllowNewLine = b; } void setTextEditMode(bool b) { bTextEditMode = b; @@ -37,6 +48,12 @@ public: const char *getText() const { return this->txt.c_str(); } + void setDeepText(bool b) { + bDeepText = b; + } + virtual bool isDeepText() const { + return bDeepText; + } void appendText(const char *s); bool processKeyDown(int keyCode); virtual CShape *cloneShape(); diff --git a/src/sim/Tool_Text.cpp b/src/sim/Tool_Text.cpp index e554043d6..f01e4c618 100644 --- a/src/sim/Tool_Text.cpp +++ b/src/sim/Tool_Text.cpp @@ -30,7 +30,7 @@ void Tool_Text::onMouseDown(const Coord &pos, int button) { currentTarget = 0; return; } - CShape *candid = sim->getShapeUnderCursor(); + CShape *candid = sim->getShapeUnderCursor(true); if (candid != 0) { CText *txt = dynamic_cast(candid); if (txt != 0) { @@ -44,6 +44,7 @@ void Tool_Text::onMouseDown(const Coord &pos, int button) { } } void Tool_Text::drawTool() { + sim->getCursorMgr()->setCursor(SDL_SYSTEM_CURSOR_ARROW); } diff --git a/src/sim/sim_local.h b/src/sim/sim_local.h index 15c69f193..93c56036c 100644 --- a/src/sim/sim_local.h +++ b/src/sim/sim_local.h @@ -15,6 +15,7 @@ #include #include +typedef unsigned char byte; typedef int32_t i32; typedef uint32_t u32; @@ -46,6 +47,9 @@ extern int gridSize; extern "C" { void CMD_ExpandConstantsWithinString(const char *in, char *out, int outLen); + int UART_GetDataSize(); + void UART_AppendByteToCircularBuffer(int rc); + int CMD_ExecuteCommand(const char* s, int cmdFlags); } template