[{"content":"This post introduce a uni project at 2024 S2 in which we designed and implemented a full-stack medical information monitoring system combining hardware and software. The final product utilizes various sensors to monitor data from medical instruments and consolidates the information into a dashboard web application for convenient viewing. The source code is open-sourced and available on GitHub and the live demo is also available on https://vitaltrack.dzx1025.com.\nMy primary responsibilities in this project included developing the dashboard application and designing the APIs. As such, this article will primarily focus on the software development aspects of the project:\nSystem Architecture Due to budget constraints for the school project, we only used infusion bag sensors for the hardware component. Also considering the requirements for high hardware scalability and low power consumption, the specific hardware configuration is as follows:\nHardwares Number TTGO T-Beam v1.2 ESP32 - 915MHZ Version 1 Jumper Wires 10 Mini External USB Stereo Speaker 1 1kg Beam Load Cell 1 Buttons 1 LED of different colours 5 The hardware structure after assembly and the functions of each component are shown in the diagram below:\nIn this project, we primarily used the MQTT protocol for communication, with all communication data stored in InfluxDB. Ideally, these services would be deployed on hospital servers. Following a client/server (C/S) architecture, the topic-based features of MQTT enable a convenient management model for different departments and floors.\nPlatform Setup For the MQTT protocol, I chose the open-source implementation Mosquitto. When Mosquitto is installed and running on the host machine, the host acts as the MQTT broker, providing relay services for all monitoring nodes, i.e., ESP32 devices. In our demo, both Mosquitto and InfluxDB are installed on a personal PC.\nWe use topics to tag hospital staff, monitoring data, and patient identities while utilizing an Access-Control List (ACL) file to ensure proper authorization. For example, public/ivbag/patientId represents public data about an IV bag associated with a patient identified by patientId. For a specific IV bag sensor, the payload might be:\n1 2 3 4 5 6 7 8 { \u0026#34;level\u0026#34;: 43, \u0026#34;rate\u0026#34;: 23.52, \u0026#34;timeLeft\u0026#34;: { \u0026#34;hour\u0026#34;: 0, \u0026#34;minute\u0026#34;: 30 } } For more detailed Mosquitto configuration, refer to GitHub MQTT Configuration.\nSoftware usage Connecting Because the dashboard application is hosted on the Vercel platform, if you want to set up your own version, you may need to provide your own environment variable MQTT_BROKER_URL.\nIn the application, simply input the patient’s monitoring ID and reconnect. If logging in as hospital staff, you need to enter the corresponding administrator username and password in the Advanced Settings. Note these staff credentials need to be preconfigured in Mosquitto. When logged in as an staff, the topic used will be private, and there will be permission to modify the configuration of the ESP32 hardware, not just limited to the web app side.\nThese values will be cached in local storage to avoid repeated inputs.\nMonitoring For regular patient users, they can monitor commonly used data via the dashboard and adjust the local Alert Threshold by dragging the slider.\nWhen the remaining IV bag capacity falls below the alert threshold, both the app and the ESP32 emit a warning sound to alert the user. Clicking the Stop button will stop the app’s warning sound.\nAt the same time, the ESP32 board will send the data to the InfluxDB for logging.\nScalability Additionally, since the application is developed using Next.js, it can be conveniently converted into a React Native app for use on mobile platforms such as Apple devices. Here is a short demonstration video: https://youtube.com/shorts/qPmOdOjlymg?si=D9SlY9jT2-UZ5Gxz.\n","date":"2025-01-10T10:03:05Z","permalink":"https://www.dzx1025.com/p/iot-project-vital-track/","title":"IoT Project: Vital Track"},{"content":"While using Black Formatter in VSCode, we can set the maximum line length to prevent some code to be wrapped lines by reformat operation.\nAdd these in setting.json.\n1 2 3 4 5 6 { \u0026#34;black-formatter.args\u0026#34;: [ \u0026#34;--line-length\u0026#34;, \u0026#34;90\u0026#34; ] } Before:\nAfter:\nAbout the best line length number, refer to Black document.\nIn general, 90-ish seems like the wise choice.\n","date":"2024-04-02T15:47:59Z","permalink":"https://www.dzx1025.com/p/python-formatter-setting-in-vscode/","title":"Python Formatter setting in VSCode"},{"content":"First, let\u0026rsquo;s distinguish different cases of const 1 2 3 4 5 6 7 8 // Variables const int x; // top-level const // References const int\u0026amp; r1 = x; // low-level const int\u0026amp; const r2 = x; // this syntax doesn\u0026#39;t exist // Pointers const int* p1 = \u0026amp;x; // low-level const int* const p2 = \u0026amp;x; // top-level const For top-level const, it indicates that the variable itself cannot be modified. As the name suggests, \u0026ldquo;top-level\u0026rdquo; refers to the topmost layer, which is the variable itself.\nFor low-level const, it indicates that the space pointed to by the reference/pointer cannot be modified.\nWhy can\u0026rsquo;t references be declared with top-level const? This is because all references are inherently top-level const - once a reference is initialized, it cannot be changed to reference another object. Since references already have the top-level const property, there\u0026rsquo;s no need for us to explicitly declare it. As we can see, the most common references/pointers we encounter are low-level const, meaning they modify the immutability of the variables they point to.\nWith this understanding, let\u0026rsquo;s look at template type deduction.\nType Deduction First, let\u0026rsquo;s introduce common terminology. In template function calls using f(expr), we have arguments and parameters. Here, we\u0026rsquo;ll mainly study the parameters and the typename\u0026rsquo;s value type, which are T and ParamType.\nC++ has three forms of ParamType declarations, representing three different deduction rules:\nParamType is a Reference/Pointer, but not a Universal Reference 1 2 3 4 template \u0026lt;typename T\u0026gt; void f(T\u0026amp; param); // For pointer ParamType: void f(T* param); First, if the type of expr is a reference, the reference part is ignored, so int\u0026amp; and int should yield the same deduction result. Since ParamType already specifies that the function parameter is a reference, when both expr and paramtype are references, expr\u0026rsquo;s reference part is ignored.\nThe same principle applies when both are pointers - int* and int are treated equally because ParamType already indicates that this template wants to use a reference/pointer type parameter.\nNote that when ParamType is a reference type and expr is a pointer type, the pointer part isn\u0026rsquo;t ignored - T\u0026rsquo;s type will match expr, as shown below. However, in normal circumstances, why would you pass a pointer to a reference? Conversely, when ParamType is a pointer and expr is a reference type, calling f(expr) requires passing the address of the reference to match, which is essentially the same as passing a variable\u0026rsquo;s address.\n1 2 3 4 5 int x=27; const int cx=x; const int\u0026amp; rx=x; const int * p1=\u0026amp;x; int* const p2=\u0026amp;x; 1 2 3 4 5 f(x); // T-\u0026gt; int f(int\u0026amp;) f(cx); // T-\u0026gt; const int f(const int\u0026amp;) f(rx); // T-\u0026gt; const int f(const int\u0026amp;) f(p1); // T-\u0026gt; const int * f(const int *\u0026amp;) f(p2); // T-\u0026gt; int *const f(int *const \u0026amp;) For callers, when passing a const object to a reference parameter, they expect the object to maintain its immutability. If the passed parameter cannot modify the content it points to, the template should obviously also protect that space from modification.\nTherefore, the parameter will also be reference-to-const. This makes it safe to pass a const object to a template with a T\u0026amp; type parameter: the object\u0026rsquo;s constness is preserved as part of T.\nWe can see that T preserves both top-level and low-level const attributes from the argument expr, while ParamType simply adds a \u0026amp; after the deduced typename T, becoming const T\u0026amp;.\nParamType is a Universal Reference 1 2 template\u0026lt;typename T\u0026gt; void f(T\u0026amp;\u0026amp; param); In this case, ParamType is called a universal reference. For universal references, we must discuss two situations based on expr\u0026rsquo;s value category:\nWhen expr is an lvalue: Both T and ParamType are deduced as lvalue references. This is the only case in template type deduction where T is deduced as a reference.\nIn this case, ParamType\u0026rsquo;s final result is actually consistent with scenario 1\u0026rsquo;s deduction result. The only difference is that in scenario 1, \u0026amp; is added to the parameter after T is deduced as a base type, while for universal references, \u0026amp; is added when deducing T - only T differs.\n1 2 3 int x=27; const int cx=x; const int\u0026amp; rx=cx; 1 2 3 f(x); //x is lvalue T-\u0026gt; int\u0026amp;, f(int\u0026amp;) f(cx); //cx is lvalue T-\u0026gt; const int\u0026amp; f(const int\u0026amp;) f(rx); //rx is lvalue T-\u0026gt; const int\u0026amp; f(const int\u0026amp;) When expr is an rvalue: Use normal deduction rules (same as scenario 1)\nHere, T will be the \u0026ldquo;base\u0026rdquo; type, while ParamType will be an rvalue reference, i.e., T\u0026amp;\u0026amp;\n1 f(27); // T-\u0026gt; int f(int\u0026amp;\u0026amp;) ParamType is Neither a Pointer/Reference Nor a Universal Reference 1 2 template\u0026lt;typename T\u0026gt; void f(T param); This situation is similar to pass-by-value handling. This means the template actually generates a complete new object copy.\nAs before, if expr\u0026rsquo;s type is a reference, ignore the reference part Unlike scenario 1, expr\u0026rsquo;s const and volatile attributes are also ignored here, which is very reasonable because the original object\u0026rsquo;s attributes shouldn\u0026rsquo;t affect the copy\u0026rsquo;s attributes - after all, the template is only responsible for passing types Therefore, the above x cx rx will all be deduced as int\nHowever, note that const is only ignored when passing by value to parameters. For reference-to-const and pointer-to-const parameters, besides themselves, they also contain the space they point to. What happens to this pointed space?\n1 2 3 4 5 const char* const ptr=\u0026#34;Hello\u0026#34;; // ptr is a const pointer pointing to const object const int x=12; f(ptr); // T-\u0026gt; const char * In this case, only the top-level const of expr is ignored, while the low-level const is preserved. Therefore, when we call f(\u0026quot;hello\u0026quot;), T is deduced as const char *. It\u0026rsquo;s like we copied a pointer - the template is still responsible for ensuring that the data pointed to by two identical pointers cannot be modified.\nArray Function Decay to Pointer 1 2 3 4 5 template\u0026lt;typename T\u0026gt; void f(T param); const char name[] = \u0026#34;J. P. Briggs\u0026#34;; // name\u0026#39;s type is const char[13] f(name) // T-\u0026gt; const char * i.e., f(const char*) But if we make a small change\u0026hellip;\n1 2 3 4 template\u0026lt;typename T\u0026gt; void f(T\u0026amp; param); f(name) // T-\u0026gt; const char[13] f(const char(\u0026amp;)[13]) This type includes the array\u0026rsquo;s size. In this example, T is deduced as const char[13], and the type of f\u0026rsquo;s parameter (reference to this array) is const char (\u0026amp;)[13]\nBut since you\u0026rsquo;re writing in C++, it\u0026rsquo;s recommended to use array instead\u0026hellip;.\n","date":"2023-03-12T12:18:59Z","permalink":"https://www.dzx1025.com/p/template-type-deduction/","title":"Template Type Deduction"},{"content":"RAII(Resource Acquisition Is Initialization) is a C++ programming idiom that ties resource management to object lifetime.\nWhen Objects Should Not Be Stack-Allocated Objects should be heap-allocated when:\nObject size is very large Object size cannot be determined at compile time Object is a function return value but should not be returned by value due to specific reasons Resource Management RAII uses stack-based objects and destructors to effectively manage system resources (including heap memory). For objects created with new inside stack scope, they should be wrapped in a class to ensure automatic destruction and resource release when the function returns.\nNote: For any new-allocated objects on the stack, they should be wrapped in a class to ensure automatic cleanup through the destructor when going out of scope.\n","date":"2023-02-22T12:18:59Z","permalink":"https://www.dzx1025.com/p/c-raii/","title":"C++ RAII"},{"content":"\nTypes of Values lvalue: ++x, x = 1, cout \u0026lt;\u0026lt; '...', \u0026quot;hello world\u0026quot; xvalue (expiring value): std::move(ptr1) can be considered as a named rvalue, distinguished from unnamed pure rvalue (prvalue) prvalue: smart_ptr ptr1{new circle()}, return circle() Value Categories C++ has unique concepts of value categories - lvalue/rvalue:\nValue and Reference Types Comparison Language Value Types Reference Types C++ All primitive types, enums, structs, unions, classes References (\u0026amp;) and pointers (*) Java Primitive types (numbers, etc.) Classes Python None All C++ primarily uses value semantics - parameters are passed by value (copied) by default.\n","date":"2023-02-22T12:18:59Z","permalink":"https://www.dzx1025.com/p/c-value-categories-and-semantics/","title":"C++ Value Categories and Semantics"}]