From cbc972073860e4e944ab68a0998220dfa7ccdfdf Mon Sep 17 00:00:00 2001 From: Victor Date: Mon, 19 Dec 2022 20:16:02 +0300 Subject: [PATCH 1/2] New plugin: Unitemp --- applications/plugins/unitemp/LICENSE.md | 674 ++++++++++++++++++ applications/plugins/unitemp/README.md | 10 + applications/plugins/unitemp/Sensors.c | 645 +++++++++++++++++ applications/plugins/unitemp/Sensors.h | 324 +++++++++ applications/plugins/unitemp/application.fam | 19 + applications/plugins/unitemp/assets/README.MD | 3 + .../unitemp/assets/flipper_happy_2_60x38.png | Bin 0 -> 564 bytes .../unitemp/assets/flipper_happy_60x38.png | Bin 0 -> 563 bytes .../unitemp/assets/flipper_sad_60x38.png | Bin 0 -> 560 bytes .../plugins/unitemp/assets/hum_9x15.png | Bin 0 -> 198 bytes .../plugins/unitemp/assets/in_hg_15x15.png | Bin 0 -> 219 bytes .../plugins/unitemp/assets/mm_hg_15x15.png | Bin 0 -> 207 bytes .../plugins/unitemp/assets/pressure_7x13.png | Bin 0 -> 169 bytes .../plugins/unitemp/assets/repo_qr_50x50.png | Bin 0 -> 700 bytes .../plugins/unitemp/assets/sherlok_53x45.png | Bin 0 -> 695 bytes .../plugins/unitemp/assets/temp_C_11x14.png | Bin 0 -> 214 bytes .../plugins/unitemp/assets/temp_F_11x14.png | Bin 0 -> 210 bytes applications/plugins/unitemp/icon.png | Bin 0 -> 1826 bytes .../plugins/unitemp/interfaces/I2CSensor.c | 131 ++++ .../plugins/unitemp/interfaces/I2CSensor.h | 128 ++++ .../unitemp/interfaces/OneWireSensor.c | 490 +++++++++++++ .../unitemp/interfaces/OneWireSensor.h | 223 ++++++ .../unitemp/interfaces/SingleWireSensor.c | 279 ++++++++ .../unitemp/interfaces/SingleWireSensor.h | 92 +++ applications/plugins/unitemp/sensors/AM2320.c | 106 +++ applications/plugins/unitemp/sensors/AM2320.h | 62 ++ applications/plugins/unitemp/sensors/BMx280.c | 352 +++++++++ applications/plugins/unitemp/sensors/BMx280.h | 102 +++ applications/plugins/unitemp/sensors/DHT20.c | 65 ++ applications/plugins/unitemp/sensors/DHT20.h | 62 ++ applications/plugins/unitemp/sensors/LM75.c | 87 +++ applications/plugins/unitemp/sensors/LM75.h | 62 ++ .../plugins/unitemp/sensors/Sensors.xlsx | Bin 0 -> 11864 bytes applications/plugins/unitemp/unitemp.c | 310 ++++++++ applications/plugins/unitemp/unitemp.h | 148 ++++ .../plugins/unitemp/views/General_view.c | 562 +++++++++++++++ .../plugins/unitemp/views/MainMenu_view.c | 99 +++ .../plugins/unitemp/views/Popup_view.c | 50 ++ .../unitemp/views/SensorActions_view.c | 125 ++++ .../plugins/unitemp/views/SensorEdit_view.c | 376 ++++++++++ .../unitemp/views/SensorNameEdit_view.c | 46 ++ .../plugins/unitemp/views/SensorsList_view.c | 162 +++++ .../plugins/unitemp/views/Settings_view.c | 152 ++++ .../plugins/unitemp/views/UnitempViews.h | 94 +++ .../plugins/unitemp/views/Widgets_view.c | 204 ++++++ 45 files changed, 6244 insertions(+) create mode 100644 applications/plugins/unitemp/LICENSE.md create mode 100644 applications/plugins/unitemp/README.md create mode 100644 applications/plugins/unitemp/Sensors.c create mode 100644 applications/plugins/unitemp/Sensors.h create mode 100644 applications/plugins/unitemp/application.fam create mode 100644 applications/plugins/unitemp/assets/README.MD create mode 100644 applications/plugins/unitemp/assets/flipper_happy_2_60x38.png create mode 100644 applications/plugins/unitemp/assets/flipper_happy_60x38.png create mode 100644 applications/plugins/unitemp/assets/flipper_sad_60x38.png create mode 100644 applications/plugins/unitemp/assets/hum_9x15.png create mode 100644 applications/plugins/unitemp/assets/in_hg_15x15.png create mode 100644 applications/plugins/unitemp/assets/mm_hg_15x15.png create mode 100644 applications/plugins/unitemp/assets/pressure_7x13.png create mode 100644 applications/plugins/unitemp/assets/repo_qr_50x50.png create mode 100644 applications/plugins/unitemp/assets/sherlok_53x45.png create mode 100644 applications/plugins/unitemp/assets/temp_C_11x14.png create mode 100644 applications/plugins/unitemp/assets/temp_F_11x14.png create mode 100644 applications/plugins/unitemp/icon.png create mode 100644 applications/plugins/unitemp/interfaces/I2CSensor.c create mode 100644 applications/plugins/unitemp/interfaces/I2CSensor.h create mode 100644 applications/plugins/unitemp/interfaces/OneWireSensor.c create mode 100644 applications/plugins/unitemp/interfaces/OneWireSensor.h create mode 100644 applications/plugins/unitemp/interfaces/SingleWireSensor.c create mode 100644 applications/plugins/unitemp/interfaces/SingleWireSensor.h create mode 100644 applications/plugins/unitemp/sensors/AM2320.c create mode 100644 applications/plugins/unitemp/sensors/AM2320.h create mode 100644 applications/plugins/unitemp/sensors/BMx280.c create mode 100644 applications/plugins/unitemp/sensors/BMx280.h create mode 100644 applications/plugins/unitemp/sensors/DHT20.c create mode 100644 applications/plugins/unitemp/sensors/DHT20.h create mode 100644 applications/plugins/unitemp/sensors/LM75.c create mode 100644 applications/plugins/unitemp/sensors/LM75.h create mode 100644 applications/plugins/unitemp/sensors/Sensors.xlsx create mode 100644 applications/plugins/unitemp/unitemp.c create mode 100644 applications/plugins/unitemp/unitemp.h create mode 100644 applications/plugins/unitemp/views/General_view.c create mode 100644 applications/plugins/unitemp/views/MainMenu_view.c create mode 100644 applications/plugins/unitemp/views/Popup_view.c create mode 100644 applications/plugins/unitemp/views/SensorActions_view.c create mode 100644 applications/plugins/unitemp/views/SensorEdit_view.c create mode 100644 applications/plugins/unitemp/views/SensorNameEdit_view.c create mode 100644 applications/plugins/unitemp/views/SensorsList_view.c create mode 100644 applications/plugins/unitemp/views/Settings_view.c create mode 100644 applications/plugins/unitemp/views/UnitempViews.h create mode 100644 applications/plugins/unitemp/views/Widgets_view.c diff --git a/applications/plugins/unitemp/LICENSE.md b/applications/plugins/unitemp/LICENSE.md new file mode 100644 index 000000000..f288702d2 --- /dev/null +++ b/applications/plugins/unitemp/LICENSE.md @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. diff --git a/applications/plugins/unitemp/README.md b/applications/plugins/unitemp/README.md new file mode 100644 index 000000000..baae10ee9 --- /dev/null +++ b/applications/plugins/unitemp/README.md @@ -0,0 +1,10 @@ +![Flipper usage](https://user-images.githubusercontent.com/10090793/206618263-c1e212e4-58dc-432e-87a8-5c19fd835b35.png) +# Unitemp - Universal temperature sensor reader +[![GitHub release](https://img.shields.io/github/release/quen0n/unitemp-flipperzero?include_prereleases=&sort=semver&color=blue)](https://github.com/quen0n/unitemp-flipperzero/releases/) +[![GitHub all releases](https://img.shields.io/github/downloads/quen0n/unitemp-flipperzero/total)]() +[![GitHub](https://img.shields.io/github/license/quen0n/unitemp-flipperzero)](https://github.com/quen0n/unitemp-flipperzero/blob/dev/LICENSE.md) +[Flipper Zero](https://flipperzero.one/) application for reading temperature, pressure and pressure sensors using Onewire, Singlewire, I2C protocols. +## List of supported sensors (supplemented) +![image](https://user-images.githubusercontent.com/10090793/208480561-e98a6192-d44d-4ad9-8692-a91ccaae47c7.png) +## Installation +Copy the contents of the repository to the `applications/plugins/unitemp` folder and build the project. Flash FZ along with resources. [More...](https://github.com/flipperdevices/flipperzero-firmware/blob/dev/documentation/fbt.md) diff --git a/applications/plugins/unitemp/Sensors.c b/applications/plugins/unitemp/Sensors.c new file mode 100644 index 000000000..d35e31931 --- /dev/null +++ b/applications/plugins/unitemp/Sensors.c @@ -0,0 +1,645 @@ +/* + Unitemp - Universal temperature reader + Copyright (C) 2022 Victor Nikitchuk (https://github.com/quen0n) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +#include "Sensors.h" +#include +#include + +//Порты ввода/вывода, которые не были обозначены в общем списке +const GpioPin SWC_10 = {.pin = LL_GPIO_PIN_14, .port = GPIOA}; +const GpioPin SIO_12 = {.pin = LL_GPIO_PIN_13, .port = GPIOA}; +const GpioPin TX_13 = {.pin = LL_GPIO_PIN_6, .port = GPIOB}; +const GpioPin RX_14 = {.pin = LL_GPIO_PIN_7, .port = GPIOB}; + +//Количество доступных портов ввода/вывода +#define GPIO_ITEMS (sizeof(GPIOList) / sizeof(GPIO)) +//Количество интерфейсов +#define INTERFACES_TYPES_COUNT (int)(sizeof(interfaces) / sizeof(const Interface*)) +//Количество типов датчиков +#define SENSOR_TYPES_COUNT (int)(sizeof(sensorTypes) / sizeof(const SensorType*)) + +//Перечень достуных портов ввода/вывода +static const GPIO GPIOList[] = { + {2, "2 (A7)", &gpio_ext_pa7}, + {3, "3 (A6)", &gpio_ext_pa6}, + {4, "4 (A4)", &gpio_ext_pa4}, + {5, "5 (B3)", &gpio_ext_pb3}, + {6, "6 (B2)", &gpio_ext_pb2}, + {7, "7 (C3)", &gpio_ext_pc3}, + {10, " 10(SWC) ", &SWC_10}, + {12, "12 (SIO)", &SIO_12}, + {13, "13 (TX)", &TX_13}, + {14, "14 (RX)", &RX_14}, + {15, "15 (C1)", &gpio_ext_pc1}, + {16, "16 (C0)", &gpio_ext_pc0}, + {17, "17 (1W)", &ibutton_gpio}}; + +//Список интерфейсов, которые прикреплены к GPIO (определяется индексом) +//NULL - порт свободен, указатель на интерфейс - порт занят этим интерфейсом +static const Interface* gpio_interfaces_list[GPIO_ITEMS] = {0}; + +const Interface SINGLE_WIRE = { + .name = "Single wire", + .allocator = unitemp_singlewire_alloc, + .mem_releaser = unitemp_singlewire_free, + .updater = unitemp_singlewire_update}; +const Interface I2C = { + .name = "I2C", + .allocator = unitemp_I2C_sensor_alloc, + .mem_releaser = unitemp_I2C_sensor_free, + .updater = unitemp_I2C_sensor_update}; +const Interface ONE_WIRE = { + .name = "One wire", + .allocator = unitemp_onewire_sensor_alloc, + .mem_releaser = unitemp_onewire_sensor_free, + .updater = unitemp_onewire_sensor_update}; + +//Перечень интерфейсов подключения +//static const Interface* interfaces[] = {&SINGLE_WIRE, &I2C, &ONE_WIRE}; +//Перечень датчиков +static const SensorType* sensorTypes[] = + {&DHT11, &DHT12_SW, &DHT21, &DHT22, &AM2320_SW, &AM2320_I2C, &LM75, &BMP280, &BME280, &Dallas}; + +const SensorType* unitemp_sensors_getTypeFromInt(uint8_t index) { + if(index > SENSOR_TYPES_COUNT) return NULL; + return sensorTypes[index]; +} + +const SensorType* unitemp_sensors_getTypeFromStr(char* str) { + UNUSED(str); + if(str == NULL) return NULL; + for(uint8_t i = 0; i < unitemp_sensors_getTypesCount(); i++) { + if(!strcmp(str, sensorTypes[i]->typename)) { + return sensorTypes[i]; + } + } + return NULL; +} + +uint8_t unitemp_sensors_getTypesCount(void) { + return SENSOR_TYPES_COUNT; +} +const SensorType** unitemp_sensors_getTypes(void) { + return sensorTypes; +} + +int unitemp_getIntFromType(const SensorType* type) { + for(int i = 0; i < SENSOR_TYPES_COUNT; i++) { + if(!strcmp(type->typename, sensorTypes[i]->typename)) { + return i; + } + } + return 255; +} +const GPIO* unitemp_gpio_getFromInt(uint8_t name) { + for(uint8_t i = 0; i < GPIO_ITEMS; i++) { + if(GPIOList[i].num == name) { + return &GPIOList[i]; + } + } + return NULL; +} + +const GPIO* unitemp_gpio_getFromIndex(uint8_t index) { + return &GPIOList[index]; +} + +uint8_t unitemp_gpio_toInt(const GPIO* gpio) { + if(gpio == NULL) return 255; + for(uint8_t i = 0; i < GPIO_ITEMS; i++) { + if(GPIOList[i].pin->pin == gpio->pin->pin && GPIOList[i].pin->port == gpio->pin->port) { + return GPIOList[i].num; + } + } + return 255; +} + +uint8_t unitemp_gpio_to_index(const GpioPin* gpio) { + if(gpio == NULL) return 255; + for(uint8_t i = 0; i < GPIO_ITEMS; i++) { + if(GPIOList[i].pin->pin == gpio->pin && GPIOList[i].pin->port == gpio->port) { + return i; + } + } + return 255; +} + +uint8_t unitemp_gpio_getAviablePortsCount(const Interface* interface, const GPIO* extraport) { + uint8_t aviable_ports_count = 0; + for(uint8_t i = 0; i < GPIO_ITEMS; i++) { + //Проверка для one wire + if(interface == &ONE_WIRE) { + if(((gpio_interfaces_list[i] == NULL || gpio_interfaces_list[i] == &ONE_WIRE) && + (i != 12)) || //Почему-то не работает на 17 порте + (unitemp_gpio_getFromIndex(i) == extraport)) { + aviable_ports_count++; + } + } + + //Проверка для single wire + if(interface == &SINGLE_WIRE) { + if(gpio_interfaces_list[i] == NULL || (unitemp_gpio_getFromIndex(i) == extraport)) { + aviable_ports_count++; + } + } + + if(interface == &I2C) { + //У I2C два фиксированых порта + return 0; + } + } + return aviable_ports_count; +} + +void unitemp_gpio_lock(const GPIO* gpio, const Interface* interface) { + uint8_t i = unitemp_gpio_to_index(gpio->pin); + if(i == 255) return; + gpio_interfaces_list[i] = interface; +} + +void unitemp_gpio_unlock(const GPIO* gpio) { + uint8_t i = unitemp_gpio_to_index(gpio->pin); + if(i == 255) return; + gpio_interfaces_list[i] = NULL; +} + +const GPIO* + unitemp_gpio_getAviablePort(const Interface* interface, uint8_t index, const GPIO* extraport) { + //Проверка для I2C + if(interface == &I2C) { + if((gpio_interfaces_list[10] == NULL || gpio_interfaces_list[10] == &I2C) && + (gpio_interfaces_list[11] == NULL || gpio_interfaces_list[11] == &I2C)) { + //Возврат истины + return unitemp_gpio_getFromIndex(0); + } else { + //Возврат лжи + return NULL; + } + } + + uint8_t aviable_index = 0; + for(uint8_t i = 0; i < GPIO_ITEMS; i++) { + //Проверка для one wire + if(interface == &ONE_WIRE) { + //Почему-то не работает на 17 порте + if(((gpio_interfaces_list[i] == NULL || gpio_interfaces_list[i] == &ONE_WIRE) && + (i != 12)) || //Почему-то не работает на 17 порте + (unitemp_gpio_getFromIndex(i) == extraport)) { + if(aviable_index == index) { + return unitemp_gpio_getFromIndex(i); + } else { + aviable_index++; + } + } + } + //Проверка для single wire + if(interface == &SINGLE_WIRE) { + if(gpio_interfaces_list[i] == NULL || unitemp_gpio_getFromIndex(i) == extraport) { + if(aviable_index == index) { + return unitemp_gpio_getFromIndex(i); + } else { + aviable_index++; + } + } + } + } + + return NULL; +} + +void unitemp_sensor_delete(Sensor* sensor) { + for(uint8_t i = 0; i < app->sensors_count; i++) { + if(app->sensors[i] == sensor) { + app->sensors[i]->status = UT_SENSORSTATUS_INACTIVE; + unitemp_sensors_save(); + unitemp_sensors_reload(); + return; + } + } +} + +Sensor* unitemp_sensor_getActive(uint8_t index) { + uint8_t aviable_index = 0; + for(uint8_t i = 0; i < app->sensors_count; i++) { + if(app->sensors[i]->status != UT_SENSORSTATUS_INACTIVE) { + if(aviable_index == index) { + return app->sensors[i]; + } else { + aviable_index++; + } + } + } + return NULL; +} + +uint8_t unitemp_sensors_getCount(void) { + if(app->sensors == NULL) return 0; + return app->sensors_count; +} + +uint8_t unitemp_sensors_getActiveCount(void) { + if(app->sensors == NULL) return 0; + uint8_t counter = 0; + for(uint8_t i = 0; i < unitemp_sensors_getCount(); i++) { + if(app->sensors[i]->status != UT_SENSORSTATUS_INACTIVE) counter++; + } + return counter; +} + +void unitemp_sensors_add(Sensor* sensor) { + app->sensors = + (Sensor**)realloc(app->sensors, (unitemp_sensors_getCount() + 1) * sizeof(Sensor*)); + app->sensors[unitemp_sensors_getCount()] = sensor; + app->sensors_count++; +} + +bool unitemp_sensors_load(void) { +#ifdef UNITEMP_DEBUG + FURI_LOG_D(APP_NAME, "Loading sensors..."); +#endif + + //Выделение памяти на поток + app->file_stream = file_stream_alloc(app->storage); + + //Переменная пути к файлу + FuriString* filepath = furi_string_alloc(); + //Составление пути к файлу + furi_string_printf(filepath, "%s/%s", APP_PATH_FOLDER, APP_FILENAME_SENSORS); + + //Открытие потока к файлу с датчиками + if(!file_stream_open( + app->file_stream, furi_string_get_cstr(filepath), FSAM_READ_WRITE, FSOM_OPEN_EXISTING)) { + if(file_stream_get_error(app->file_stream) == FSE_NOT_EXIST) { + FURI_LOG_W(APP_NAME, "Missing sensors file"); + //Закрытие потока и освобождение памяти + file_stream_close(app->file_stream); + stream_free(app->file_stream); + return false; + } else { + FURI_LOG_E( + APP_NAME, + "An error occurred while loading the sensors file: %d", + file_stream_get_error(app->file_stream)); + //Закрытие потока и освобождение памяти + file_stream_close(app->file_stream); + stream_free(app->file_stream); + return false; + } + } + + //Вычисление размера файла + uint16_t file_size = stream_size(app->file_stream); + //Если файл пустой, то: + if(file_size == (uint8_t)0) { + FURI_LOG_W(APP_NAME, "Sensors file is empty"); + //Закрытие потока и освобождение памяти + file_stream_close(app->file_stream); + stream_free(app->file_stream); + return false; + } + //Выделение памяти под загрузку файла + uint8_t* file_buf = malloc(file_size); + //Опустошение буфера файла + memset(file_buf, 0, file_size); + //Загрузка файла + if(stream_read(app->file_stream, file_buf, file_size) != file_size) { + //Выход при ошибке чтения + FURI_LOG_E(APP_NAME, "Error reading sensors file"); + //Закрытие потока и освобождение памяти + file_stream_close(app->file_stream); + stream_free(app->file_stream); + free(file_buf); + return false; + } + + //Указатель на начало строки + FuriString* file = furi_string_alloc_set_str((char*)file_buf); + //Сколько байт до конца строки + size_t line_end = 0; + + while(line_end != STRING_FAILURE && line_end != (size_t)(file_size - 1)) { + //Имя датчика + char name[11] = {0}; + //Тип датчика + char type[11] = {0}; + //Смещение по температуре + int temp_offset = 0; + //Смещение по строке для отделения аргументов + int offset = 0; + //Чтение из строки + sscanf(((char*)(file_buf + line_end)), "%s %s %d %n", name, type, &temp_offset, &offset); + //Ограничение длины имени + name[10] = '\0'; + + //Замена ? на пробел + for(uint8_t i = 0; i < 10; i++) { + if(name[i] == '?') name[i] = ' '; + } + + char* args = ((char*)(file_buf + line_end + offset)); + const SensorType* stype = unitemp_sensors_getTypeFromStr(type); + + //Проверка типа датчика + if(stype != NULL && sizeof(name) > 0 && sizeof(name) <= 11) { + Sensor* sensor = + unitemp_sensor_alloc(name, unitemp_sensors_getTypeFromStr(type), args); + if(sensor != NULL) { + sensor->temp_offset = temp_offset; + unitemp_sensors_add(sensor); + } else { + FURI_LOG_E(APP_NAME, "Failed sensor (%s:%s) mem allocation", name, type); + } + } else { + FURI_LOG_E(APP_NAME, "Unsupported sensor name (%s) or sensor type (%s)", name, type); + } + //Вычисление конца строки + line_end = furi_string_search_char(file, '\n', line_end + 1); + } + + free(file_buf); + file_stream_close(app->file_stream); + stream_free(app->file_stream); + + FURI_LOG_I(APP_NAME, "Sensors have been successfully loaded"); + return true; +} + +bool unitemp_sensors_save(void) { +#ifdef UNITEMP_DEBUG + FURI_LOG_D(APP_NAME, "Saving sensors..."); +#endif + + //Выделение памяти для потока + app->file_stream = file_stream_alloc(app->storage); + + //Переменная пути к файлу + FuriString* filepath = furi_string_alloc(); + //Составление пути к файлу + furi_string_printf(filepath, "%s/%s", APP_PATH_FOLDER, APP_FILENAME_SENSORS); + //Создание папки плагина + storage_common_mkdir(app->storage, APP_PATH_FOLDER); + //Открытие потока + if(!file_stream_open( + app->file_stream, furi_string_get_cstr(filepath), FSAM_READ_WRITE, FSOM_CREATE_ALWAYS)) { + FURI_LOG_E( + APP_NAME, + "An error occurred while saving the sensors file: %d", + file_stream_get_error(app->file_stream)); + //Закрытие потока и освобождение памяти + file_stream_close(app->file_stream); + stream_free(app->file_stream); + return false; + } + + //Сохранение датчиков + for(uint8_t i = 0; i < unitemp_sensors_getActiveCount(); i++) { + Sensor* sensor = unitemp_sensor_getActive(i); + //Замена пробела на ? + for(uint8_t i = 0; i < 10; i++) { + if(sensor->name[i] == ' ') sensor->name[i] = '?'; + } + + stream_write_format( + app->file_stream, + "%s %s %d ", + sensor->name, + sensor->type->typename, + sensor->temp_offset); + + if(sensor->type->interface == &SINGLE_WIRE) { + stream_write_format( + app->file_stream, "%d\n", unitemp_singlewire_sensorGetGPIO(sensor)->num); + } + if(sensor->type->interface == &I2C) { + stream_write_format( + app->file_stream, "%X\n", ((I2CSensor*)sensor->instance)->currentI2CAdr); + } + if(sensor->type->interface == &ONE_WIRE) { + stream_write_format( + app->file_stream, + "%d %02X%02X%02X%02X%02X%02X%02X%02X\n", + ((OneWireSensor*)sensor->instance)->bus->gpio->num, + ((OneWireSensor*)sensor->instance)->deviceID[0], + ((OneWireSensor*)sensor->instance)->deviceID[1], + ((OneWireSensor*)sensor->instance)->deviceID[2], + ((OneWireSensor*)sensor->instance)->deviceID[3], + ((OneWireSensor*)sensor->instance)->deviceID[4], + ((OneWireSensor*)sensor->instance)->deviceID[5], + ((OneWireSensor*)sensor->instance)->deviceID[6], + ((OneWireSensor*)sensor->instance)->deviceID[7]); + } + } + + //Закрытие потока и освобождение памяти + file_stream_close(app->file_stream); + stream_free(app->file_stream); + + FURI_LOG_I(APP_NAME, "Sensors have been successfully saved"); + return true; +} +void unitemp_sensors_reload(void) { + unitemp_sensors_deInit(); + unitemp_sensors_free(); + + unitemp_sensors_load(); + unitemp_sensors_init(); +} + +bool unitemp_sensor_isContains(Sensor* sensor) { + for(uint8_t i = 0; i < unitemp_sensors_getCount(); i++) { + if(app->sensors[i] == sensor) return true; + } + return false; +} + +Sensor* unitemp_sensor_alloc(char* name, const SensorType* type, char* args) { + if(name == NULL || type == NULL) return NULL; + bool status = false; + //Выделение памяти под датчик + Sensor* sensor = malloc(sizeof(Sensor)); + if(sensor == NULL) { + FURI_LOG_E(APP_NAME, "Sensor %s allocation error", name); + return false; + } + + //Выделение памяти под имя + sensor->name = malloc(11); + if(sensor->name == NULL) { + FURI_LOG_E(APP_NAME, "Sensor %s name allocation error", name); + return false; + } + //Запись имени датчка + strcpy(sensor->name, name); + //Тип датчика + sensor->type = type; + //Статус датчика по умолчанию - ошибка + sensor->status = UT_SENSORSTATUS_ERROR; + //Время последнего опроса + sensor->lastPollingTime = + furi_get_tick() - 10000; //чтобы первый опрос произошёл как можно раньше + + sensor->temp = -128.0f; + sensor->hum = -128.0f; + sensor->pressure = -128.0f; + sensor->temp_offset = 0; + //Выделение памяти под инстанс датчика в зависимости от его интерфейса + status = sensor->type->interface->allocator(sensor, args); + + //Выход если датчик успешно развёрнут + if(status) { + FURI_LOG_I(APP_NAME, "Sensor %s allocated", name); + return sensor; + } + //Выход с очисткой если память для датчика не была выделена + free(sensor->name); + free(sensor); + FURI_LOG_E(APP_NAME, "Sensor %s(%s) allocation error", name, type->typename); + return NULL; +} + +void unitemp_sensor_free(Sensor* sensor) { + if(sensor == NULL) { + FURI_LOG_E(APP_NAME, "Null pointer sensor releasing"); + return; + } + if(sensor->type == NULL) { + FURI_LOG_E(APP_NAME, "Sensor type is null"); + return; + } + if(sensor->type->mem_releaser == NULL) { + FURI_LOG_E(APP_NAME, "Sensor releaser is null"); + return; + } + bool status = false; + //Высвобождение памяти под инстанс + status = sensor->type->interface->mem_releaser(sensor); + UNUSED(status); +#ifdef UNITEMP_DEBUG + + if(status) { + FURI_LOG_D(APP_NAME, "Sensor %s memory successfully released", sensor->name); + } else { + FURI_LOG_E(APP_NAME, "Sensor %s memory is not released", sensor->name); + } +#endif + free(sensor->name); + //free(sensor); +} + +void unitemp_sensors_free(void) { + for(uint8_t i = 0; i < unitemp_sensors_getCount(); i++) { + unitemp_sensor_free(app->sensors[i]); + } + app->sensors_count = 0; +} + +bool unitemp_sensors_init(void) { + bool result = true; + + //Перебор датчиков из списка + for(uint8_t i = 0; i < unitemp_sensors_getCount(); i++) { + //Включение 5V если на порту 1 FZ его нет + //Может пропасть при отключении USB + if(furi_hal_power_is_otg_enabled() != true) { + furi_hal_power_enable_otg(); +#ifdef UNITEMP_DEBUG + FURI_LOG_D(APP_NAME, "OTG enabled"); +#endif + } + if(!(*app->sensors[i]->type->initializer)(app->sensors[i])) { + FURI_LOG_E( + APP_NAME, + "An error occurred during sensor initialization %s", + app->sensors[i]->name); + result = false; + } +#ifdef UNITEMP_DEBUG + FURI_LOG_D(APP_NAME, "Sensor %s successfully initialized", app->sensors[i]->name); +#endif + } + app->sensors_ready = true; + return result; +} + +bool unitemp_sensors_deInit(void) { + bool result = true; + //Выключение 5 В если до этого оно не было включено + if(app->settings.lastOTGState != true) { + furi_hal_power_disable_otg(); +#ifdef UNITEMP_DEBUG + FURI_LOG_D(APP_NAME, "OTG disabled"); +#endif + } + + //Перебор датчиков из списка + for(uint8_t i = 0; i < unitemp_sensors_getCount(); i++) { + if(!(*app->sensors[i]->type->deinitializer)(app->sensors[i])) { + FURI_LOG_E( + APP_NAME, + "An error occurred during sensor deinitialization %s", + app->sensors[i]->name); + result = false; + } + } + return result; +} + +UnitempStatus unitemp_sensor_updateData(Sensor* sensor) { + if(sensor == NULL) return UT_SENSORSTATUS_ERROR; + + //Проверка на допустимость опроса датчика + if(furi_get_tick() - sensor->lastPollingTime < sensor->type->pollingInterval) { + //Возврат ошибки если последний опрос датчика был неудачным + if(sensor->status == UT_SENSORSTATUS_TIMEOUT) { + return UT_SENSORSTATUS_TIMEOUT; + } + return UT_SENSORSTATUS_EARLYPOOL; + } + + sensor->lastPollingTime = furi_get_tick(); + + if(!furi_hal_power_is_otg_enabled()) { + furi_hal_power_enable_otg(); + } + + sensor->status = sensor->type->interface->updater(sensor); + +#ifdef UNITEMP_DEBUG + if(sensor->status != UT_SENSORSTATUS_OK && sensor->status != UT_SENSORSTATUS_POLLING) + FURI_LOG_D(APP_NAME, "Sensor %s update status %d", sensor->name, sensor->status); +#endif + + if(app->settings.temp_unit == UT_TEMP_FAHRENHEIT && sensor->status == UT_SENSORSTATUS_OK) + uintemp_celsiumToFarengate(sensor); + if(sensor->status == UT_SENSORSTATUS_OK) { + sensor->temp += sensor->temp_offset / 10.f; + if(app->settings.pressure_unit == UT_PRESSURE_MM_HG) { + unitemp_pascalToMmHg(sensor); + } else if(app->settings.pressure_unit == UT_PRESSURE_IN_HG) { + unitemp_pascalToInHg(sensor); + } else if(app->settings.pressure_unit == UT_PRESSURE_KPA) { + unitemp_pascalToKPa(sensor); + } + } + return sensor->status; +} + +void unitemp_sensors_updateValues(void) { + for(uint8_t i = 0; i < unitemp_sensors_getCount(); i++) { + unitemp_sensor_updateData(unitemp_sensor_getActive(i)); + } +} diff --git a/applications/plugins/unitemp/Sensors.h b/applications/plugins/unitemp/Sensors.h new file mode 100644 index 000000000..52e6165f5 --- /dev/null +++ b/applications/plugins/unitemp/Sensors.h @@ -0,0 +1,324 @@ +/* + Unitemp - Universal temperature reader + Copyright (C) 2022 Victor Nikitchuk (https://github.com/quen0n) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +#ifndef UNITEMP_SENSORS +#define UNITEMP_SENSORS +#include +#include + +//Маски бит для определения типов возвращаемых значений +#define UT_TEMPERATURE 0b00000001 +#define UT_HUMIDITY 0b00000010 +#define UT_PRESSURE 0b00000100 + +//Статусы опроса датчика +typedef enum { + UT_DATA_TYPE_TEMP = UT_TEMPERATURE, + UT_DATA_TYPE_TEMP_HUM = UT_TEMPERATURE | UT_HUMIDITY, + UT_DATA_TYPE_TEMP_PRESS = UT_TEMPERATURE | UT_PRESSURE, + UT_DATA_TYPE_TEMP_HUM_PRESS = UT_TEMPERATURE | UT_HUMIDITY | UT_PRESSURE, +} SensorDataType; + +//Типы возвращаемых данных +typedef enum { + UT_SENSORSTATUS_OK, //Всё хорошо, опрос успешен + UT_SENSORSTATUS_TIMEOUT, //Датчик не отозвался + UT_SENSORSTATUS_EARLYPOOL, //Опрос раньше положенной задержки + UT_SENSORSTATUS_BADCRC, //Неверная контрольная сумма + UT_SENSORSTATUS_ERROR, //Прочие ошибки + UT_SENSORSTATUS_POLLING, //В датчике происходит преобразование + UT_SENSORSTATUS_INACTIVE, //Датчик на редактировании или удалён + +} UnitempStatus; + +//Порт ввода/вывода Flipper Zero +typedef struct GPIO { + const uint8_t num; + const char* name; + const GpioPin* pin; +} GPIO; + +typedef struct Sensor Sensor; + +/** + * @brief Указатель функции выделения памяти и подготовки экземпляра датчика + */ +typedef bool(SensorAllocator)(Sensor* sensor, char* args); +/** + * @brief Указатель на функцию высвобождении памяти датчика + */ +typedef bool(SensorFree)(Sensor* sensor); +/** + * @brief Указатель функции инициализации датчика + */ +typedef bool(SensorInitializer)(Sensor* sensor); +/** + * @brief Указатель функции деинициализации датчика + */ +typedef bool(SensorDeinitializer)(Sensor* sensor); +/** + * @brief Указатель функции обновления значения датчика + */ +typedef UnitempStatus(SensorUpdater)(Sensor* sensor); + +//Типы подключения датчиков +typedef struct Interface { + //Имя интерфейса + const char* name; + //Функция выделения памяти интерфейса + SensorAllocator* allocator; + //Функция высвыбождения памяти интерфейса + SensorFree* mem_releaser; + //Функция обновления значения датчика по интерфейсу + SensorUpdater* updater; +} Interface; + +//Типы датчиков +typedef struct { + //Модель датчика + const char* typename; + //Полное имя с аналогами + const char* altname; + //Тип возвращаемых данных + SensorDataType datatype; + //Интерфейс подключения + const Interface* interface; + //Интервал опроса датчика + uint16_t pollingInterval; + //Функция выделения памяти для датчика + SensorAllocator* allocator; + //Функция высвыбождения памяти для датчика + SensorFree* mem_releaser; + //Функция инициализации датчика + SensorInitializer* initializer; + //Функция деинициализация датчика + SensorDeinitializer* deinitializer; + //Функция обновления значения датчка + SensorUpdater* updater; +} SensorType; + +//Датчик +typedef struct Sensor { + //Имя датчика + char* name; + //Температура + float temp; + //Относительная влажность + float hum; + //Атмосферное давление + float pressure; + //Тип датчика + const SensorType* type; + //Статус последнего опроса датчика + UnitempStatus status; + //Время последнего опроса датчика + uint32_t lastPollingTime; + //Смещение по температуре (x10) + int8_t temp_offset; + //Экземпляр датчика + void* instance; +} Sensor; + +extern const Interface SINGLE_WIRE; //Собственный однопроводной протокол датчиков DHTXX и AM23XX +extern const Interface ONE_WIRE; //Однопроводной протокол Dallas +extern const Interface I2C; //I2C_2 (PC0, PC1) +//extern const Interface SPI; + +/* ============================= Датчик(и) ============================= */ +/** + * @brief Выделение памяти под датчик + * + * @param name Имя датчика + * @param type Тип датчика + * @param args Указатель на строку с парамерами датчика + * @return Указатель на датчик в случае успешного выделения памяти, NULL при ошибке + */ +Sensor* unitemp_sensor_alloc(char* name, const SensorType* type, char* args); + +/** + * @brief Высвыбождение памяти конкретного датчка + * @param sensor Указатель на датчик + */ +void unitemp_sensor_free(Sensor* sensor); + +/** + * @brief Обновление данных указанного датчика + * @param sensor Указатель на датчик + * @return Статус опроса датчика + */ +UnitempStatus unitemp_sensor_updateData(Sensor* sensor); + +/** + * @brief Проверка наличия датчика в памяти + * + * @param sensor Указатель на датчик + * @return Истина если этот датчик уже загружен, ложь если это новый датчик + */ +bool unitemp_sensor_isContains(Sensor* sensor); + +/** + * @brief Получить датчик из списка по индексу + * + * @param index Индекс датчика (0 - unitemp_sensors_getCount()) + * @return Указатель на датчик при успехе, NULL при неудаче + */ +Sensor* unitemp_sensor_getActive(uint8_t index); + +/** + * @brief Загрузка датчиков с SD-карты + * @return Истина если загрузка прошла успешно + */ +bool unitemp_sensors_load(); + +/** + * @brief Функция перезагрузки датчиков с SD-карты +*/ +void unitemp_sensors_reload(void); + +/** + * @brief Сохранение датчиков на SD-карту + * @return Истина если сохранение прошло успешно + */ +bool unitemp_sensors_save(void); + +/** + * @brief Удаление датчика + * + * @param sensor Указатель на датчик + */ +void unitemp_sensor_delete(Sensor* sensor); + +/** + * @brief Инициализация загруженных датчиков + * @return Истина если всё прошло успешно + */ +bool unitemp_sensors_init(void); + +/** + * @brief Деинициализация загруженных датчиков + * @return Истина если всё прошло успешно + */ +bool unitemp_sensors_deInit(void); + +/** + * @brief Высвыбождение памяти всех датчиков + */ +void unitemp_sensors_free(void); + +/** + * @brief Обновить данные всех датчиков + */ +void unitemp_sensors_updateValues(void); + +/** + * @brief Получить количество загруженных датчиков + * @return Количество датчиков + */ +uint8_t unitemp_sensors_getCount(void); + +/** + * @brief Добавить датчик в общий список + * @param sensor Указатель на датчик + */ +void unitemp_sensors_add(Sensor* sensor); + +/** +* @brief Получить списк доступных типов датчиков +* @return Указатель на список датчиков +*/ +const SensorType** unitemp_sensors_getTypes(void); + +/** +* @brief Получить количество доступных типов датчиков +* @return Количество доступных типов датчиков +*/ +uint8_t unitemp_sensors_getTypesCount(void); + +/** + * @brief Получить тип сенсора по его индексу + * @param index Индекс типа датчика (от 0 до SENSOR_TYPES_COUNT) + * @return const SensorType* + */ +const SensorType* unitemp_sensors_getTypeFromInt(uint8_t index); + +/** + * @brief Преобразовать строчное название датчка в указатель + * + * @param str Имя датчика в виде строки + * @return Указатель на тип датчика при успехе, иначе NULL + */ +const SensorType* unitemp_sensors_getTypeFromStr(char* str); + +/** + * @brief Получить количество активных датчиков + * + * @return Количество активных датчиков + */ +uint8_t unitemp_sensors_getActiveCount(void); + +/* ============================= GPIO ============================= */ +/** + * @brief Конвертация номера порта на корпусе FZ в GPIO + * @param name Номер порта на корпусе FZ + * @return Указатель на GPIO при успехе, NULL при ошибке + */ +const GPIO* unitemp_gpio_getFromInt(uint8_t name); +/** + * @brief Конвертация GPIO в номер на корпусе FZ + * @param gpio Указатель на порт + * @return Номер порта на корпусе FZ + */ +uint8_t unitemp_gpio_toInt(const GPIO* gpio); + +/** + * @brief Блокировка GPIO указанным интерфейсом + * @param gpio Указатель на порт + * @param interface Указатель на интерфейс, которым порт будет занят + */ +void unitemp_gpio_lock(const GPIO* gpio, const Interface* interface); + +/** + * @brief Разблокировка порта + * @param gpio Указатель на порт + */ +void unitemp_gpio_unlock(const GPIO* gpio); +/** + * @brief Получить количество доступных портов для указанного интерфейса + * @param interface Указатель на интерфейс + * @return Количество доступных портов + */ +uint8_t unitemp_gpio_getAviablePortsCount(const Interface* interface, const GPIO* extraport); +/** + * @brief Получить указатель на доступный для интерфейса порт по индексу + * @param interface Указатель на интерфейс + * @param index Номер порта (от 0 до unitemp_gpio_getAviablePortsCount()) + * @param extraport Указатель на дополнительный порт, который будет принудительно считаться доступным. Можно указать NULL если не требуется + * @return Указатель на доступный порт + */ +const GPIO* + unitemp_gpio_getAviablePort(const Interface* interface, uint8_t index, const GPIO* extraport); + +/* Датчики */ +//DHTxx и их производные +#include "./interfaces/SingleWireSensor.h" +//DS18x2x +#include "./interfaces/OneWireSensor.h" +#include "./sensors/LM75.h" +//BMP280, BME280 +#include "./sensors/BMx280.h" +#include "./sensors/AM2320.h" +#endif diff --git a/applications/plugins/unitemp/application.fam b/applications/plugins/unitemp/application.fam new file mode 100644 index 000000000..89292e504 --- /dev/null +++ b/applications/plugins/unitemp/application.fam @@ -0,0 +1,19 @@ +App( + appid="unitemp", + name="Temp sensors reader", + apptype=FlipperAppType.EXTERNAL, + entry_point="unitemp_app", + cdefines=["UNITEMP_APP"], + requires=[ + "gui", + ], + stack_size=2 * 1024, + order=100, + fap_description = "Universal temperature sensors reader", + fap_author = "Quenon", + fap_weburl = "https://github.com/quen0n/Unitemp-Flipper-Zero-Plugin", + fap_category="GPIO", + fap_icon="icon.png", + fap_icon_assets="assets", + fap_libs=["assets"], +) \ No newline at end of file diff --git a/applications/plugins/unitemp/assets/README.MD b/applications/plugins/unitemp/assets/README.MD new file mode 100644 index 000000000..7c359a052 --- /dev/null +++ b/applications/plugins/unitemp/assets/README.MD @@ -0,0 +1,3 @@ +# Unitemp assets + +Created by [@Svaarich](https://github.com/Svaarich) diff --git a/applications/plugins/unitemp/assets/flipper_happy_2_60x38.png b/applications/plugins/unitemp/assets/flipper_happy_2_60x38.png new file mode 100644 index 0000000000000000000000000000000000000000..c4322c237da0a6995009e08bda5774b83515283e GIT binary patch literal 564 zcmV-40?Yl0P)Px#1ZP1_K>z@;j|==^1poj532;bRa{vGi!~g&e!~vBn4jTXf0m(^3K~!i%?UwIp4fD3v?U6hc{w7a_p9=5K?JB(bhsPdOz?Qi@rFoBtm9Sw72!@A1PMVW#EfI~O zTbe?)i1`N8NaGeo!?~-@#L+fNG#^_{&UE&c#-hyh<+1d;75;Uz0Qz;M%?isPJ*^X5 z39mr1c!^SNh9c3 zCX~5|6-V@bR$d*WiNpZJE#A_RMHXC(1AV}AIR@l-2}={kWImwx$T7dABLY0QR_CEO zD|gMFT!RDs8b-rwH@2`0c;afz?nUy_s)2^k>OKyd<6JGgcrvF2V=Ts>mZbz1HESa1 zypermjMoP^pKocLToG!s^l@C8Dv|toShd&?u@&xbY0>+6S{Y}pH~$`biGCe;OI}(q ztFL5Bm~ZKpO}k%9G~J4E_!vD6AH@f@1=DYvUH1>7Px#1ZP1_K>z@;j|==^1poj532;bRa{vGi!~g&e!~vBn4jTXf0mw;2K~!i%?U##| zf-nq3VgLW1cNt{Zm?mk;LlC&t;aoiI{ZnV4nG~ z)GbXRYs7p4YNTgE)1`gYz}G7Q7i|(k1BP zyq0qk8H;LS4Gn5I@70iS|JzgAN}556k|y*Vk?SKYO_f_rryinDSv z)&g_kXx&Rj!^`os7=K!}76wc9WYBpt zb>tfF9N>DsrEzgasLj&Hd1Px#1ZP1_K>z@;j|==^1poj532;bRa{vGi!~g&e!~vBn4jTXf0mVr~K~!i%?U#$P zf-nq3!T+979?8?;GkH4vba;QB4xhF{0z5Z7C^7`Lq@|bnI)EK%nWOv#WiNN zgte4vt|j$>``K_x)a>5kfR`6*Dbf1Oi81>gt~EkYQXDC*YakXEbfp*POE4BJ3^QpB zOm5{VtxFiI#dDr!Jh*>xN^42i!aSCU(t3)f33GC9G#{8x={?K>=CSgX);*{bSJa?n zG`yv_2gH=FtH=(mT407<;05IU7Sr6v8WxPvcx^4NVF}TMM(S!Ym}3r-=H5`8=HmK|C!E8oeM yP*3TWO`Bg!G~J4E_!vD6FU1G81=IgFyY3B5#<5PdI{%~q0000Nn{1`ISV`@iy0XBj({-ZRBb+KpkTD8i(`mJ@YIRXd<=>l%-!Gjf6l61s+3n! z*TJHudMmskA+11nNnCwoTU%>rrE#WM{%&(u@s+vUatHGwv!d_0-KgKKw}~h1Lx=7T p`77mrA588va5J6zX^Y(i#yg*NWhVXHI~!;XgQu&X%Q~loCIATmKg<9C literal 0 HcmV?d00001 diff --git a/applications/plugins/unitemp/assets/in_hg_15x15.png b/applications/plugins/unitemp/assets/in_hg_15x15.png new file mode 100644 index 0000000000000000000000000000000000000000..a2778248220fda613a9437a07245b84c7dbfcfa9 GIT binary patch literal 219 zcmeAS@N?(olHy`uVBq!ia0vp^{2vE3q zyT1b2o{o3V^!e$Z-(h{G@Kt)*qb3#S7Z+A74|ViiaiyawD#QEF^)nw!OrKV55DnU- zcIMKQz1IC@OTxZJ8~>lUU}09u`x~MveQs>?_jPGc2v0V)d_H4tf1jJw+#6qa1MOz; MboFyt=akR{0DT=%E&u=k literal 0 HcmV?d00001 diff --git a/applications/plugins/unitemp/assets/mm_hg_15x15.png b/applications/plugins/unitemp/assets/mm_hg_15x15.png new file mode 100644 index 0000000000000000000000000000000000000000..66b5dc6eab5f8231184eef708e409cf18ad1c97b GIT binary patch literal 207 zcmeAS@N?(olHy`uVBq!ia0vp^{2H#7 literal 0 HcmV?d00001 diff --git a/applications/plugins/unitemp/assets/pressure_7x13.png b/applications/plugins/unitemp/assets/pressure_7x13.png new file mode 100644 index 0000000000000000000000000000000000000000..43aaa7bbcaa1510b55086b12a4ede0fe85ac3ad1 GIT binary patch literal 169 zcmeAS@N?(olHy`uVBq!ia0vp^>>xG=8<0#rS+W&KF%}28J29*~C-V}>VM%xNb!1@J z*w6hZkrl}2EbxddW?X?_wfUrhf_9!Rjv*Ss$$$7+cmfWbIB}xkNNPgUrHF$K z9!)VFybVWYI{3MVGkg|`OMckcutr);;K)v~Wm0a<403aMcoiv~ z_k$W==8o#aM{mCHtfuA;eV$wZZR zIRO()V)h(9DJ`PXt5TQ_6nywdR^@c!jyJqrJ2#uJ`}X3`2AQ_g_ZG~su6~@gw*KSu z7amf(XRNPoPO?1EnOmaL8++#ZUdN4x?Hgh*SnW{C+_FjboM*E57QS^JQfqT8^X)dD z-J2UQ%Vv^{lzr0d>e~xUDpf2Ma#eblOf-qPu^>TwOW*OS@*Yy2%9%ShI3Co?ko|g! z@jZ9-?){sj@44Knh`(|B@6@|jjOC;GlrvRCD?^@KEcki$+>q#cD%3s@xZqJ zY42>N?|bbZoZkZsF`#1=Ue2@s+{P`fSKZvCvPAUvS-oRdizeLFFVdQ&MBb@0K(Z;Y5)KL literal 0 HcmV?d00001 diff --git a/applications/plugins/unitemp/assets/sherlok_53x45.png b/applications/plugins/unitemp/assets/sherlok_53x45.png new file mode 100644 index 0000000000000000000000000000000000000000..1f258737e351cc742771d8ddaa160184670cbad4 GIT binary patch literal 695 zcmV;o0!aOdP)Px#1ZP1_K>z@;j|==^1poj532;bRa{vGi!~g&e!~vBn4jTXf0!&FnK~!i%-Ia}w z;xG(^;okq9cNw&U=DfCJI{}XSq=+{0&rDsF>(`CTVXoKf&sEG5+CDgDN{yf-?!D67 zxA7<0Hbrrn0g@R7n^GS9`T1=FAr!eQhBa7om(iqXCL@a3$*t0!)NaEHsE5&lg3z zQ>y0JI2y3Ix?io`niPuaIst1zEQRaR{VL^2={1S=I%xfjOot^jrO8<#8wa9TMPqpl zY_2`iei6rBKQ>!6R64PQ#xlyE^sEqLB38LosggJ)MV8h=h;J8v3kfy{-<^X zS|C=}gtn=jX0Kv2snulc`DBvFnZ5xnQ*3l%Qad$}n;CBXPenpBu7k}}DPAqBC!#&6 z2sWxN=2X!fUPqKnp$G*~6c@0KR$}eB*5TTr^!kygnHLw7y~A^@$9UVr+s|qP*VIas z62(^cRV=VMJjcFXXN*4SiZ+|znb0+1+f;;S7WdIp#=dvPy)4&q#1xuMpQ;s$Yh515 z$@(3glx<{1+575)U>+X;SCs$jL!p7@XbE$f=QXuw)gFlVV!i#pOxb{_Py~e%FSioo zk!i7KkJryj!o3xd!F>GMT13yH7)xfz~eqoR!ycNJY&;39eq#x zp8{xP*97Ew4+waMpK9tPo-yA4+P|$^e0{X<#u}hm)%Y=!A{5VG8d9?#XR5+Sn~J9O dA5Q9W`3ATiP9a&X@v#5^002ovPDHLkV1h6FKidER literal 0 HcmV?d00001 diff --git a/applications/plugins/unitemp/assets/temp_C_11x14.png b/applications/plugins/unitemp/assets/temp_C_11x14.png new file mode 100644 index 0000000000000000000000000000000000000000..19cf423b4b18ec8724843dd2ee48bd7290662a38 GIT binary patch literal 214 zcmeAS@N?(olHy`uVBq!ia0vp^+(697!3HFMCO(@Aq!^2X+?^QKos)S9a~60+76UaL0b$0e+I-SL!E8?##}JL+)`^^a2NXD14*vhYv$QjLW#G&u zayxp|zAjIeEx(?UEM0x=f=R)G{Oc~dhnk$yrk*iaCTz8TmhsY=uPgmB!#FNp?V0-Z z>YTN+(j(UJ&RCiK#`pethMt)Q>kkU=PY>C0w{-WcnK!w==ex`7DxRAJw3fls)z4*} HQ$iB}?4eE& literal 0 HcmV?d00001 diff --git a/applications/plugins/unitemp/assets/temp_F_11x14.png b/applications/plugins/unitemp/assets/temp_F_11x14.png new file mode 100644 index 0000000000000000000000000000000000000000..5ed469337e7b98392d9b65e0e156a41b5b4a68c9 GIT binary patch literal 210 zcmeAS@N?(olHy`uVBq!ia0vp^+(697!3HFMCO(@Aq!^2X+?^QKos)S9a~60+76UaL0b$0e+I-SL!E{d-#}JL+wS9qn2NXD*yT9+>y!ePzieLRV zA@!`v!@HP|pV^Tp^LR~^i-rA_2`Wo^Yj>{MFf+St^I9jt>;KaVP18?aToV~8e|A>r zSDwkY6gI#6`E}Vpu2VBOi+Srd_7pCDTep2Ta|65b0w)mzHW_cAl?CG$boEJ%AX`jCW1e9%DP% zO$0(h0;#H?r=Aev)C)+FP`ROs3%zh4HzW@H0bD?cLoY~WylZa@YSUb>wDyem&HLWG z@AEg;R#z^aoPKdy5QLNM<<=U1mgDKE`Q`h{A$$MC^q~zJj`Ml5f)H+7^fU92p8suDZ-ny z5Chtcl7{rhci&55L>kf+(^vg;k@lkH-Hfj9u5|G3CU%K5e^IwkvCaWg^=>FG3YUMYR-72b%2} zmZuqFbxAy2MnZ3`^;#8-&l*y%C{j;R27`e-&}Ei&70q>BMTH84fFnS@lN4wOlKgbd z(4slcqO^!uB9@FOU|U5);!?+Zh|>vel2<-)$CM#T6-`#lJ=H+pKNuRv6LelIZSyFV z-bWJWot=~_YcywD8Kz6yG$~Hk!O|WpSl(lg(Kh+}UkVAH1WdQG{>UDJ722mU=gv7+ z8|h2E#f+kYWgW))b*5J9h$7TwQ#^AeN(dX|XG=xZ0@^|aZAfL-fa(Ix>}a}Yn4aOB zQ*}>O$55Xa0zm~j9GH7f3@tfeYas)CMXAyWr9#106cfq+B95KsiMV;Y8yZC4T>#9d^4midfu zcXdf5*b7+}BVIpIjJi}wlWtkfoSpAtIm&tJcB=1VoxWF%`jI#)IuGHp)*BKolT3+J zy^bE`-l24kX$L*ZNe@W=7?!iJ7@&+cyWHs~7MJok@j2Rlg7iZr{(qES*@}B8=~CV~ z6{*~eQnr|JQk4JZ>p{NjnrZTCF0+|eHY}j-#E^@1F(ITA7h(i#%h7=g4I4P28URc} zNT97@JG91e`%$f6Or#tfaisO3_E<0GJ+~ZAx;>vv&I_9Gt!Df*xc=_U=lmpDYqy%6 zGwUDUxIQbKnt95;e}CV3^UoJ#bo<-4hI<=#e?9y2m(N@~@!ZkRX4HHB)L&{ab?+wp z>g_q&x%0~Jr{-QIWbT94-@gUE{&4=2=Wl(X-#+@|<@3kZe^K|#gQ~r>(%M^i=c9iB DwLd-Z literal 0 HcmV?d00001 diff --git a/applications/plugins/unitemp/interfaces/I2CSensor.c b/applications/plugins/unitemp/interfaces/I2CSensor.c new file mode 100644 index 000000000..72d959e27 --- /dev/null +++ b/applications/plugins/unitemp/interfaces/I2CSensor.c @@ -0,0 +1,131 @@ +/* + Unitemp - Universal temperature reader + Copyright (C) 2022 Victor Nikitchuk (https://github.com/quen0n) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +#include "I2CSensor.h" + +static uint8_t sensors_count = 0; + +void unitemp_i2c_acquire(FuriHalI2cBusHandle* handle) { + furi_hal_i2c_acquire(handle); + LL_GPIO_SetPinPull(gpio_ext_pc1.port, gpio_ext_pc1.pin, LL_GPIO_PULL_UP); + LL_GPIO_SetPinPull(gpio_ext_pc0.port, gpio_ext_pc0.pin, LL_GPIO_PULL_UP); +} + +bool unitemp_i2c_isDeviceReady(I2CSensor* i2c_sensor) { + unitemp_i2c_acquire(i2c_sensor->i2c); + bool status = furi_hal_i2c_is_device_ready(i2c_sensor->i2c, i2c_sensor->currentI2CAdr, 10); + furi_hal_i2c_release(i2c_sensor->i2c); + return status; +} + +uint8_t unitemp_i2c_readReg(I2CSensor* i2c_sensor, uint8_t reg) { + //Блокировка шины + unitemp_i2c_acquire(i2c_sensor->i2c); + uint8_t buff[1] = {0}; + furi_hal_i2c_read_mem(i2c_sensor->i2c, i2c_sensor->currentI2CAdr, reg, buff, 1, 10); + furi_hal_i2c_release(i2c_sensor->i2c); + return buff[0]; +} + +bool unitemp_i2c_readArray(I2CSensor* i2c_sensor, uint8_t len, uint8_t* data) { + unitemp_i2c_acquire(i2c_sensor->i2c); + bool status = furi_hal_i2c_rx(i2c_sensor->i2c, i2c_sensor->currentI2CAdr, data, len, 10); + furi_hal_i2c_release(i2c_sensor->i2c); + return status; +} + +bool unitemp_i2c_readRegArray(I2CSensor* i2c_sensor, uint8_t startReg, uint8_t len, uint8_t* data) { + unitemp_i2c_acquire(i2c_sensor->i2c); + bool status = + furi_hal_i2c_read_mem(i2c_sensor->i2c, i2c_sensor->currentI2CAdr, startReg, data, len, 10); + furi_hal_i2c_release(i2c_sensor->i2c); + return status; +} + +bool unitemp_i2c_writeReg(I2CSensor* i2c_sensor, uint8_t reg, uint8_t value) { + //Блокировка шины + unitemp_i2c_acquire(i2c_sensor->i2c); + uint8_t buff[1] = {value}; + bool status = + furi_hal_i2c_write_mem(i2c_sensor->i2c, i2c_sensor->currentI2CAdr, reg, buff, 1, 10); + furi_hal_i2c_release(i2c_sensor->i2c); + return status; +} + +bool unitemp_i2c_writeArray(I2CSensor* i2c_sensor, uint8_t len, uint8_t* data) { + unitemp_i2c_acquire(i2c_sensor->i2c); + bool status = furi_hal_i2c_tx(i2c_sensor->i2c, i2c_sensor->currentI2CAdr, data, len, 10); + furi_hal_i2c_release(i2c_sensor->i2c); + return status; +} + +bool unitemp_i2c_writeRegArray(I2CSensor* i2c_sensor, uint8_t startReg, uint8_t len, uint8_t* data) { + //Блокировка шины + unitemp_i2c_acquire(i2c_sensor->i2c); + bool status = furi_hal_i2c_write_mem( + i2c_sensor->i2c, i2c_sensor->currentI2CAdr, startReg, data, len, 10); + furi_hal_i2c_release(i2c_sensor->i2c); + return status; +} + +bool unitemp_I2C_sensor_alloc(Sensor* sensor, char* args) { + bool status = false; + I2CSensor* instance = malloc(sizeof(I2CSensor)); + if(instance == NULL) { + FURI_LOG_E(APP_NAME, "Sensor %s instance allocation error", sensor->name); + return false; + } + instance->i2c = &furi_hal_i2c_handle_external; + sensor->instance = instance; + + //Указание функций инициализации, деинициализации и обновления данных, а так же адреса на шине I2C + status = sensor->type->allocator(sensor, args); + int i2c_addr; + sscanf(args, "%X", &i2c_addr); + + //Установка адреса шины I2C + if(i2c_addr >= instance->minI2CAdr && i2c_addr <= instance->maxI2CAdr) { + instance->currentI2CAdr = i2c_addr; + } else { + instance->currentI2CAdr = instance->minI2CAdr; + } + + //Блокировка портов GPIO + sensors_count++; + unitemp_gpio_lock(unitemp_gpio_getFromInt(15), &I2C); + unitemp_gpio_lock(unitemp_gpio_getFromInt(16), &I2C); + + return status; +} + +bool unitemp_I2C_sensor_free(Sensor* sensor) { + bool status = sensor->type->mem_releaser(sensor); + free(sensor->instance); + if(--sensors_count == 0) { + unitemp_gpio_unlock(unitemp_gpio_getFromInt(15)); + unitemp_gpio_unlock(unitemp_gpio_getFromInt(16)); + } + + return status; +} + +UnitempStatus unitemp_I2C_sensor_update(Sensor* sensor) { + if(sensor->status != UT_SENSORSTATUS_OK) { + sensor->type->initializer(sensor); + } + return sensor->type->updater(sensor); +} \ No newline at end of file diff --git a/applications/plugins/unitemp/interfaces/I2CSensor.h b/applications/plugins/unitemp/interfaces/I2CSensor.h new file mode 100644 index 000000000..3df709d9a --- /dev/null +++ b/applications/plugins/unitemp/interfaces/I2CSensor.h @@ -0,0 +1,128 @@ +/* + Unitemp - Universal temperature reader + Copyright (C) 2022 Victor Nikitchuk (https://github.com/quen0n) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +#ifndef UNITEMP_I2C +#define UNITEMP_I2C + +#include "../unitemp.h" + +#include + +//Структура I2C датчика +typedef struct I2CSensor { + //Указатель на интерфейс I2C + FuriHalI2cBusHandle* i2c; + //Минимальный адрес устройства на шине I2C + uint8_t minI2CAdr; + //Максимальный адрес устройства на шине I2C + uint8_t maxI2CAdr; + //Текущий адрес устройства на шине I2C + uint8_t currentI2CAdr; + //Указатель на собственный экземпляр датчика + void* sensorInstance; +} I2CSensor; + +/** + * @brief Заблокировать шину I2C + * + * @param handle Указатель на шину + */ +void unitemp_i2c_acquire(FuriHalI2cBusHandle* handle); + +/** + * @brief Проверить наличие датчика на шине + * + * @param i2c_sensor Указатель на датчик + * @return Истина если устройство отозвалось + */ +bool unitemp_i2c_isDeviceReady(I2CSensor* i2c_sensor); + +/** + * @brief Выделение памяти для датчика на шине I2C + * @param sensor Указатель на датчик + * @param st Тип датчика + * @return Истина если всё ок + */ +bool unitemp_I2C_sensor_alloc(Sensor* sensor, char* args); + +/** + * @brief Высвобождение памяти инстанса датчика + * @param sensor Указатель на датчик + */ +bool unitemp_I2C_sensor_free(Sensor* sensor); + +/** + * @brief Обновить значение с датчка + * @param sensor Указатель на датчик + * @return Статус обновления + */ +UnitempStatus unitemp_I2C_sensor_update(Sensor* sensor); +/** + * @brief Прочитать значение регистра reg + * @param i2c_sensor Указатель на инстанс датчика + * @param reg Номер регистра + * @return Значение регистра + */ +uint8_t unitemp_i2c_readReg(I2CSensor* i2c_sensor, uint8_t reg); + +/** + * @brief Прочитать масссив значений из памяти + * @param i2c_sensor Указатель на инстанс датчика + * @param startReg Адрес регистра с которого начнётся чтение + * @param len Количество байт для считывания из регистра + * @param data Указатель на массив куда будут считаны данные + * @return Истина если устройство вернуло данные + */ +bool unitemp_i2c_readRegArray(I2CSensor* i2c_sensor, uint8_t startReg, uint8_t len, uint8_t* data); + +/** + * @brief Записать значение в регистр + * @param i2c_sensor Указатель на инстанс датчика + * @param reg Номер регистра + * @param value Значение для записи + * @return Истина если значение записано + */ +bool unitemp_i2c_writeReg(I2CSensor* i2c_sensor, uint8_t reg, uint8_t value); + +/** + * @brief Записать масссив значений в память + * @param i2c_sensor Указатель на инстанс датчика + * @param startReg Адрес регистра с которого начнётся запись + * @param len Количество байт для считывания из регистра + * @param data Указатель на массив откуда будут записаны данные + * @return Истина если устройство вернуло данные + */ +bool unitemp_i2c_writeRegArray(I2CSensor* i2c_sensor, uint8_t startReg, uint8_t len, uint8_t* data); + +/** + * @brief Прочитать массив данных по шине I2C + * @param i2c_sensor Указатель на инстанс датчика + * @param startReg Адрес регистра с которого начнётся чтение + * @param data Указатель на массив куда будут считаны данные + * @return Истина если устройство вернуло данные + */ +bool unitemp_i2c_readArray(I2CSensor* i2c_sensor, uint8_t len, uint8_t* data); + +/** + * @brief Записать масссив данных по шине I2C + * @param i2c_sensor Указатель на инстанс датчика + * @param len Количество байт для считывания из регистра + * @param data Указатель на массив откуда будут записаны данные + * @return Истина если устройство вернуло данные + */ +bool unitemp_i2c_writeArray(I2CSensor* i2c_sensor, uint8_t len, uint8_t* data); +#endif \ No newline at end of file diff --git a/applications/plugins/unitemp/interfaces/OneWireSensor.c b/applications/plugins/unitemp/interfaces/OneWireSensor.c new file mode 100644 index 000000000..740ba3365 --- /dev/null +++ b/applications/plugins/unitemp/interfaces/OneWireSensor.c @@ -0,0 +1,490 @@ +/* + Unitemp - Universal temperature reader + Copyright (C) 2022 Victor Nikitchuk (https://github.com/quen0n) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +//Использован код Дмитрия Погребняка: https://aterlux.ru/article/1wire + +#include "OneWireSensor.h" +#include +#include +#include + +const SensorType Dallas = { + .typename = "Dallas", + .altname = "Dallas (DS18x2x)", + .interface = &ONE_WIRE, + .datatype = UT_DATA_TYPE_TEMP, + .pollingInterval = 1000, + .allocator = unitemp_onewire_sensor_alloc, + .mem_releaser = unitemp_onewire_sensor_free, + .initializer = unitemp_onewire_sensor_init, + .deinitializer = unitemp_onewire_sensor_deinit, + .updater = unitemp_onewire_sensor_update}; + +// Переменные для хранения промежуточного результата сканирования шины +// найденный восьмибайтовый адрес +static uint8_t onewire_enum[8] = {0}; +// последний нулевой бит, где была неоднозначность (нумеруя с единицы) +static uint8_t onewire_enum_fork_bit = 65; + +OneWireBus* uintemp_onewire_bus_alloc(const GPIO* gpio) { + if(gpio == NULL) { + return NULL; + } + + //Проверка на наличие шины на этом порте + for(uint8_t i = 0; i < unitemp_sensors_getActiveCount(); i++) { + if(unitemp_sensor_getActive(i)->type->interface == &ONE_WIRE && + ((OneWireSensor*)unitemp_sensor_getActive(i)->instance)->bus->gpio->num == gpio->num) { + //Если шина на этом порту уже есть, то возврат указателя на шину + return ((OneWireSensor*)unitemp_sensor_getActive(i)->instance)->bus; + } + } + + OneWireBus* bus = malloc(sizeof(OneWireBus)); + bus->device_count = 0; + bus->gpio = gpio; + bus->powerMode = PWR_PASSIVE; +#ifdef UNITEMP_DEBUG + FURI_LOG_D(APP_NAME, "one wire bus (port %d) allocated", gpio->num); +#endif + + return bus; +} + +bool unitemp_onewire_bus_init(OneWireBus* bus) { + if(bus == NULL) return false; + bus->device_count++; + //Выход если шина уже была инициализирована + if(bus->device_count > 1) return true; + + unitemp_gpio_lock(bus->gpio, &ONE_WIRE); + //Высокий уровень по умолчанию + furi_hal_gpio_write(bus->gpio->pin, true); + //Режим работы - OpenDrain, подтяжка включается на всякий случай + furi_hal_gpio_init( + bus->gpio->pin, //Порт FZ + GpioModeOutputOpenDrain, //Режим работы - открытый сток + GpioPullUp, //Принудительная подтяжка линии данных к питанию + GpioSpeedVeryHigh); //Скорость работы - максимальная + + return true; +} +bool unitemp_onewire_bus_deinit(OneWireBus* bus) { +#ifdef UNITEMP_DEBUG + FURI_LOG_D(APP_NAME, "devices on wire %d: %d", bus->gpio->num, bus->device_count); +#endif + bus->device_count--; + if(bus->device_count <= 0) { + bus->device_count = 0; + unitemp_gpio_unlock(bus->gpio); + //Режим работы - аналог, подтяжка выключена + furi_hal_gpio_init( + bus->gpio->pin, //Порт FZ + GpioModeAnalog, //Режим работы - аналог + GpioPullNo, //Подтяжка выключена + GpioSpeedLow); //Скорость работы - минимальная + //Низкий уровень по умолчанию + furi_hal_gpio_write(bus->gpio->pin, false); + return true; + } else { + return false; + } +} +bool unitemp_onewire_bus_start(OneWireBus* bus) { + furi_hal_gpio_write(bus->gpio->pin, false); + furi_delay_us(500); + + furi_hal_gpio_write(bus->gpio->pin, true); + + //Ожидание подъёма шины + uint32_t t = furi_get_tick(); + while(!furi_hal_gpio_read(bus->gpio->pin)) { + //Выход если шина не поднялась + if(furi_get_tick() - t > 10) return false; + } + + furi_delay_us(100); + bool status = !furi_hal_gpio_read(bus->gpio->pin); + furi_delay_us(400); + return status; +} + +void unitemp_onewire_bus_send_bit(OneWireBus* bus, bool state) { + //Необходимо для стабильной работы при пассивном питании + if(bus->powerMode == PWR_PASSIVE) furi_delay_us(100); + + if(state) { + // write 1 + furi_hal_gpio_write(bus->gpio->pin, false); + furi_delay_us(1); + furi_hal_gpio_write(bus->gpio->pin, true); + furi_delay_us(90); + } else { + furi_hal_gpio_write(bus->gpio->pin, false); + furi_delay_us(90); + furi_hal_gpio_write(bus->gpio->pin, true); + //Ожидание подъёма шины + uint32_t t = furi_get_tick(); + while(!furi_hal_gpio_read(bus->gpio->pin)) { + //Выход если шина не поднялась + if(furi_get_tick() - t > 10) return; + } + } +} + +void unitemp_onewire_bus_send_byte(OneWireBus* bus, uint8_t data) { + for(int i = 0; i < 8; i++) { + unitemp_onewire_bus_send_bit(bus, (data & (1 << i)) != 0); + } +} + +void unitemp_onewire_bus_send_byteArray(OneWireBus* bus, uint8_t* data, uint8_t len) { + for(uint8_t i = 0; i < len; i++) { + unitemp_onewire_bus_send_byte(bus, data[i]); + } +} + +bool unitemp_onewire_bus_read_bit(OneWireBus* bus) { + furi_delay_ms(1); + furi_hal_gpio_write(bus->gpio->pin, false); + furi_delay_us(2); // Длительность низкого уровня, минимум 1 мкс + furi_hal_gpio_write(bus->gpio->pin, true); + furi_delay_us(8); // Пауза до момента сэмплирования, всего не более 15 мкс + bool r = furi_hal_gpio_read(bus->gpio->pin); + furi_delay_us(80); // Ожидание до следующего тайм-слота, минимум 60 мкс с начала низкого уровня + return r; +} + +uint8_t unitemp_onewire_bus_read_byte(OneWireBus* bus) { + uint8_t r = 0; + for(uint8_t p = 8; p; p--) { + r >>= 1; + if(unitemp_onewire_bus_read_bit(bus)) r |= 0x80; + } + return r; +} + +void unitemp_onewire_bus_read_byteArray(OneWireBus* bus, uint8_t* data, uint8_t len) { + for(uint8_t i = 0; i < len; i++) { + data[i] = unitemp_onewire_bus_read_byte(bus); + } +} + +static uint8_t onewire_CRC_update(uint8_t crc, uint8_t b) { + for(uint8_t p = 8; p; p--) { + crc = ((crc ^ b) & 1) ? (crc >> 1) ^ 0b10001100 : (crc >> 1); + b >>= 1; + } + return crc; +} + +bool unitemp_onewire_CRC_check(uint8_t* data, uint8_t len) { + uint8_t crc = 0; + for(uint8_t i = 0; i < len; i++) { + crc = onewire_CRC_update(crc, data[i]); + } + return !crc; +} + +char* unitemp_onewire_sensor_getModel(Sensor* sensor) { + OneWireSensor* ow_sensor = sensor->instance; + switch(ow_sensor->deviceID[0]) { + case FC_DS18B20: + return "DS18B20"; + case FC_DS18S20: + return "DS18S20"; + case FC_DS1822: + return "DS1822"; + default: + return "unknown"; + } +} + +bool unitemp_onewire_sensor_readID(OneWireSensor* instance) { + if(!unitemp_onewire_bus_start(instance->bus)) return false; + unitemp_onewire_bus_send_byte(instance->bus, 0x33); // Чтение ПЗУ + unitemp_onewire_bus_read_byteArray(instance->bus, instance->deviceID, 8); + if(!unitemp_onewire_CRC_check(instance->deviceID, 8)) { + memset(instance->deviceID, 0, 8); + return false; + } + instance->familyCode = instance->deviceID[0]; + return true; +} + +void unitemp_onewire_bus_enum_init(void) { + for(uint8_t p = 0; p < 8; p++) { + onewire_enum[p] = 0; + } + onewire_enum_fork_bit = 65; // правее правого +} + +uint8_t* unitemp_onewire_bus_enum_next(OneWireBus* bus) { + furi_delay_ms(10); + if(!onewire_enum_fork_bit) { // Если на предыдущем шаге уже не было разногласий +#ifdef UNITEMP_DEBUG + FURI_LOG_D(APP_NAME, "All devices on wire %d is found", unitemp_gpio_toInt(bus->gpio)); +#endif + return 0; // то просто выходим ничего не возвращая + } + if(!unitemp_onewire_bus_start(bus)) { +#ifdef UNITEMP_DEBUG + FURI_LOG_D(APP_NAME, "Wire %d is empty", unitemp_gpio_toInt(bus->gpio)); +#endif + return 0; + } + uint8_t bp = 8; + uint8_t* pprev = &onewire_enum[0]; + uint8_t prev = *pprev; + uint8_t next = 0; + + uint8_t p = 1; + unitemp_onewire_bus_send_byte(bus, 0xF0); + uint8_t newfork = 0; + for(;;) { + uint8_t not0 = unitemp_onewire_bus_read_bit(bus); + uint8_t not1 = unitemp_onewire_bus_read_bit(bus); + if(!not0) { // Если присутствует в адресах бит ноль + if(!not1) { // Но также присустствует бит 1 (вилка) + if(p < + onewire_enum_fork_bit) { // Если мы левее прошлого правого конфликтного бита, + if(prev & 1) { + next |= 0x80; // то копируем значение бита из прошлого прохода + } else { + newfork = p; // если ноль, то запомним конфликтное место + } + } else if(p == onewire_enum_fork_bit) { + next |= + 0x80; // если на этом месте в прошлый раз был правый конфликт с нулём, выведем 1 + } else { + newfork = p; // правее - передаём ноль и запоминаем конфликтное место + } + } // в противном случае идём, выбирая ноль в адресе + } else { + if(!not1) { // Присутствует единица + next |= 0x80; + } else { // Нет ни нулей ни единиц - ошибочная ситуация +#ifdef UNITEMP_DEBUG + FURI_LOG_D(APP_NAME, "Wrong wire %d situation", unitemp_gpio_toInt(bus->gpio)); +#endif + return 0; + } + } + unitemp_onewire_bus_send_bit(bus, next & 0x80); + bp--; + if(!bp) { + *pprev = next; + if(p >= 64) break; + next = 0; + pprev++; + prev = *pprev; + bp = 8; + } else { + if(p >= 64) break; + prev >>= 1; + next >>= 1; + } + p++; + } + onewire_enum_fork_bit = newfork; + return &onewire_enum[0]; +} + +void unitemp_onewire_bus_select_sensor(OneWireSensor* instance) { + unitemp_onewire_bus_send_byte(instance->bus, 0x55); + unitemp_onewire_bus_send_byteArray(instance->bus, instance->deviceID, 8); +} + +bool unitemp_onewire_sensor_alloc(Sensor* sensor, char* args) { + OneWireSensor* instance = malloc(sizeof(OneWireSensor)); + if(instance == NULL) { + FURI_LOG_E(APP_NAME, "Sensor %s instance allocation error", sensor->name); + return false; + } + sensor->instance = instance; + //Очистка адреса + memset(instance->deviceID, 0, 8); + + int gpio, addr_0, addr_1, addr_2, addr_3, addr_4, addr_5, addr_6, addr_7; + sscanf( + args, + "%d %2X%2X%2X%2X%2X%2X%2X%2X", + &gpio, + &addr_0, + &addr_1, + &addr_2, + &addr_3, + &addr_4, + &addr_5, + &addr_6, + &addr_7); + instance->deviceID[0] = addr_0; + instance->deviceID[1] = addr_1; + instance->deviceID[2] = addr_2; + instance->deviceID[3] = addr_3; + instance->deviceID[4] = addr_4; + instance->deviceID[5] = addr_5; + instance->deviceID[6] = addr_6; + instance->deviceID[7] = addr_7; + + instance->familyCode = instance->deviceID[0]; + + instance->bus = uintemp_onewire_bus_alloc(unitemp_gpio_getFromInt(gpio)); + + if(instance != NULL) { + return true; + } + FURI_LOG_E(APP_NAME, "Sensor %s bus allocation error", sensor->name); + free(instance); + return false; +} + +bool unitemp_onewire_sensor_free(Sensor* sensor) { + if(((OneWireSensor*)sensor->instance)->bus != NULL) { + if(((OneWireSensor*)sensor->instance)->bus->device_count == 0) { + free(((OneWireSensor*)sensor->instance)->bus); + } + } + + free(sensor->instance); + + return true; +} + +bool unitemp_onewire_sensor_init(Sensor* sensor) { + OneWireSensor* instance = sensor->instance; + if(instance == NULL || instance->bus == NULL) { + FURI_LOG_E(APP_NAME, "Sensor pointer is null!"); + return false; + } + + unitemp_onewire_bus_init(instance->bus); + furi_delay_ms(1); + + if(instance->familyCode == FC_DS18B20 || instance->familyCode == FC_DS1822) { + //Установка разрядности в 10 бит + if(!unitemp_onewire_bus_start(instance->bus)) return false; + unitemp_onewire_bus_select_sensor(instance); + unitemp_onewire_bus_send_byte(instance->bus, 0x4E); // Запись в память + uint8_t buff[3]; + //Значения тревоги + buff[0] = 0x4B; //Значение нижнего предела температуры + buff[1] = 0x46; //Значение верхнего предела температуры + //Конфигурация + buff[2] = 0b01111111; //12 бит разрядность преобразования + unitemp_onewire_bus_send_byteArray(instance->bus, buff, 3); + + //Сохранение значений в EEPROM для автоматического восстановления после сбоев питания + if(!unitemp_onewire_bus_start(instance->bus)) return false; + unitemp_onewire_bus_select_sensor(instance); + unitemp_onewire_bus_send_byte(instance->bus, 0x48); // Запись в EEPROM + } + + return true; +} + +bool unitemp_onewire_sensor_deinit(Sensor* sensor) { + OneWireSensor* instance = sensor->instance; + if(instance == NULL || instance->bus == NULL) return false; + unitemp_onewire_bus_deinit(instance->bus); + + return true; +} + +UnitempStatus unitemp_onewire_sensor_update(Sensor* sensor) { + //Снятие особого статуса с датчика при пассивном режиме питания + if(sensor->status == UT_SENSORSTATUS_EARLYPOOL) { + return UT_SENSORSTATUS_POLLING; + } + + OneWireSensor* instance = sensor->instance; + uint8_t buff[9] = {0}; + if(sensor->status != UT_SENSORSTATUS_POLLING) { + //Если датчик в прошлый раз не отозвался, проверка его наличия на шине + if(sensor->status == UT_SENSORSTATUS_TIMEOUT || sensor->status == UT_SENSORSTATUS_BADCRC) { + if(!unitemp_onewire_bus_start(instance->bus)) return UT_SENSORSTATUS_TIMEOUT; + unitemp_onewire_bus_select_sensor(instance); + unitemp_onewire_bus_send_byte(instance->bus, 0xBE); // Read Scratch-pad + unitemp_onewire_bus_read_byteArray(instance->bus, buff, 9); + if(!unitemp_onewire_CRC_check(buff, 9)) { +#ifdef UNITEMP_DEBUG + FURI_LOG_D(APP_NAME, "Sensor %s is not found", sensor->name); +#endif + return UT_SENSORSTATUS_TIMEOUT; + } + } + + if(!unitemp_onewire_bus_start(instance->bus)) return UT_SENSORSTATUS_TIMEOUT; + //Запуск преобразования на всех датчиках в режиме пассивного питания + if(instance->bus->powerMode == PWR_PASSIVE) { + unitemp_onewire_bus_send_byte(instance->bus, 0xCC); // skip addr + //Установка на всех датчиках этой шины особого статуса, чтобы не запускать преобразование ещё раз + for(uint8_t i = 0; i < unitemp_sensors_getActiveCount(); i++) { + if(unitemp_sensor_getActive(i)->type->interface == &ONE_WIRE && + ((OneWireSensor*)unitemp_sensor_getActive(i)->instance)->bus == instance->bus) { + unitemp_sensor_getActive(i)->status = UT_SENSORSTATUS_EARLYPOOL; + } + } + + } else { + unitemp_onewire_bus_select_sensor(instance); + } + + unitemp_onewire_bus_send_byte(instance->bus, 0x44); // convert t + if(instance->bus->powerMode == PWR_PASSIVE) { + furi_hal_gpio_write(instance->bus->gpio->pin, true); + furi_hal_gpio_init( + instance->bus->gpio->pin, GpioModeOutputPushPull, GpioPullUp, GpioSpeedVeryHigh); + } + return UT_SENSORSTATUS_POLLING; + } else { + if(instance->bus->powerMode == PWR_PASSIVE) { + furi_hal_gpio_write(instance->bus->gpio->pin, true); + furi_hal_gpio_init( + instance->bus->gpio->pin, GpioModeOutputOpenDrain, GpioPullUp, GpioSpeedVeryHigh); + } + if(!unitemp_onewire_bus_start(instance->bus)) return UT_SENSORSTATUS_TIMEOUT; + unitemp_onewire_bus_select_sensor(instance); + unitemp_onewire_bus_send_byte(instance->bus, 0xBE); // Read Scratch-pad + unitemp_onewire_bus_read_byteArray(instance->bus, buff, 9); + if(!unitemp_onewire_CRC_check(buff, 9)) { +#ifdef UNITEMP_DEBUG + FURI_LOG_D(APP_NAME, "Failed CRC check: %s", sensor->name); +#endif + return UT_SENSORSTATUS_BADCRC; + } + int16_t raw = buff[0] | ((int16_t)buff[1] << 8); + if(instance->familyCode == FC_DS18S20) { + //Песевдо-12-бит. Отключено из-за неестественности и нестабильности показаний по сравнению с DS18B20 + //sensor->temp = ((float)raw / 2.0f) - 0.25f + (16.0f - buff[6]) / 16.0f; + //Честные 9 бит + sensor->temp = ((float)raw / 2.0f); + } else { + sensor->temp = (float)raw / 16.0f; + } + } + + return UT_SENSORSTATUS_OK; +} + +bool unitemp_onewire_id_compare(uint8_t* id1, uint8_t* id2) { + if(id1 == NULL || id2 == NULL) return false; + for(uint8_t i = 0; i < 8; i++) { + if(id1[i] != id2[i]) return false; + } + return true; +} \ No newline at end of file diff --git a/applications/plugins/unitemp/interfaces/OneWireSensor.h b/applications/plugins/unitemp/interfaces/OneWireSensor.h new file mode 100644 index 000000000..f2b148535 --- /dev/null +++ b/applications/plugins/unitemp/interfaces/OneWireSensor.h @@ -0,0 +1,223 @@ +/* + Unitemp - Universal temperature reader + Copyright (C) 2022 Victor Nikitchuk (https://github.com/quen0n) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +#ifndef UNITEMP_OneWire +#define UNITEMP_OneWire + +#include "../unitemp.h" + +//Коды семейства устройств +typedef enum DallasFamilyCode { + FC_DS18S20 = 0x10, + FC_DS1822 = 0x22, + FC_DS18B20 = 0x28, +} DallasFamilyCode; + +//Режим питания датчка +typedef enum PowerMode { + PWR_PASSIVE, //Питание от линии данных + PWR_ACTIVE //Питание от источника питания +} PowerMode; + +//Инстанс шины one wire +typedef struct { + //Порт подключения датчика + const GPIO* gpio; + //Количество устройств на шине + //Обновляется при ручном добавлении датчика на эту шину + int8_t device_count; + //Режим питания датчиков на шине + PowerMode powerMode; +} OneWireBus; + +//Инстанс датчика one wire +typedef struct OneWireSensor { + //Указатель на шину OneWire + OneWireBus* bus; + //Текущий адрес устройства на шине OneWire + uint8_t deviceID[8]; + //Код семейства устройств + DallasFamilyCode familyCode; +} OneWireSensor; + +/** + * @brief Выделение памяти для датчика на шине OneWire + * @param sensor Указатель на датчик + * @param args Указатель на массив аргументов с параметрами датчика + * @return Истина если всё ок + */ +bool unitemp_onewire_sensor_alloc(Sensor* sensor, char* args); + +/** + * @brief Высвобождение памяти инстанса датчика + * @param sensor Указатель на датчик + */ +bool unitemp_onewire_sensor_free(Sensor* sensor); + +/** + * @brief Инициализации датчика на шине one wire + * @param sensor Указатель на датчик + * @return Истина если инициализация упспешная + */ +bool unitemp_onewire_sensor_init(Sensor* sensor); + +/** + * @brief Деинициализация датчика + * @param sensor Указатель на датчик + */ +bool unitemp_onewire_sensor_deinit(Sensor* sensor); + +/** + * @brief Обновить значение с датчка + * @param sensor Указатель на датчик + * @return Статус обновления + */ +UnitempStatus unitemp_onewire_sensor_update(Sensor* sensor); + +/** + * @brief Выделение памяти для шины one wire и её инициализация + * @param gpio Порт на котором необходимо создать шину + * @return При успехе возвращает указатель на шину one wire + */ +OneWireBus* uintemp_onewire_bus_alloc(const GPIO* gpio); + +/** + * @brief Инициализация шины one wire + * + * @param bus Указатель на шину + * @return Истина если инициализация успешна + */ +bool unitemp_onewire_bus_init(OneWireBus* bus); + +/** + * @brief Деинициализация шины one wire + * + * @param bus Указатель на шину + * @return Истина если шина была деинициализирована, ложь если на шине остались устройства + */ +bool unitemp_onewire_bus_deinit(OneWireBus* bus); + +/** + * @brief Запуск общения с датчиками на шине one wire + * @param bus Указатель на шину + * @return Истина если хотя бы одно устройство отозвалось + */ +bool unitemp_onewire_bus_start(OneWireBus* bus); + +/** + * @brief Отправить 1 бит данных на шину one wire + * @param bus Указатель на шину + * @param state Логический уровень + */ +void unitemp_onewire_bus_send_bit(OneWireBus* bus, bool state); + +/** + * @brief Запись байта на шину one wire + * + * @param bus Указатель на шину one wire + * @param data Записываемый байт + */ +void unitemp_onewire_bus_send_byte(OneWireBus* bus, uint8_t data); + +/** + * @brief Запись массива байт на шину one wire + * + * @param bus Указатель на шину one wire + * @param data Указатель на массив, откуда будут записаны данные + * @param len Количество байт + */ +void unitemp_onewire_bus_send_byteArray(OneWireBus* bus, uint8_t* data, uint8_t len); + +/** + * @brief Чтение бита на шине one wire + * + * @param bus Указатель на шину one wire + * @return Логический уровень бита + */ +bool unitemp_onewire_bus_read_bit(OneWireBus* bus); + +/** + * @brief Чтение байта с шины One Wire + * + * @param bus Указатель на шину one wire + * @return Байт информации + */ +uint8_t unitemp_onewire_bus_read_byte(OneWireBus* bus); + +/** + * @brief Чтение массива байт с шины One Wire + * + * @param bus Указатель на шину one wire + * @param data Указатель на массив, куда будут записаны данные + * @param len Количество байт + */ +void unitemp_onewire_bus_read_byteArray(OneWireBus* bus, uint8_t* data, uint8_t len); + +/** + * @brief Проверить контрольную сумму массива данных + * + * @param data Указатель на массив данных + * @param len Длина массива (включая байт CRC) + * @return Истина если контрольная сумма корректная + */ +bool unitemp_onewire_CRC_check(uint8_t* data, uint8_t len); + +/** + * @brief Получить имя модели датчика на шине One Wire + * + * @param sensor Указатель на датчик + * @return Указатель на строку с названием + */ +char* unitemp_onewire_sensor_getModel(Sensor* sensor); + +/** + * @brief Чтение индификатора единственного датчика. ID запишется в инстанс датчика + * + * @param instance Указатель на инстанс датчика + * @return Истина, если код успешно прочитан, ложь если устройство отсутствует или устройств на шине больше одного + */ +bool unitemp_oneWire_sensor_readID(OneWireSensor* instance); + +/** + * @brief Команда выбора определённого датчка по его ID + * @param instance Указатель на датчик one wire + */ +void unitemp_onewire_bus_select_sensor(OneWireSensor* instance); + +/** + * @brief Инициализация процесса поиска адресов на шине one wire + */ +void unitemp_onewire_bus_enum_init(void); + +/** + * @brief Перечисляет устройства на шине one wire и получает очередной адрес + * @param bus Указатель на шину one wire + * @return Возвращает указатель на буфер, содержащий восьмибайтовое значение адреса, либо NULL, если поиск завешён + */ +uint8_t* unitemp_onewire_bus_enum_next(OneWireBus* bus); + +/** + * @brief Сравнить ID датчиков + * + * @param id1 Указатель на адрес первого датчика + * @param id2 Указатель на адрес второго датчика + * @return Истина если ID индентичны + */ +bool unitemp_onewire_id_compare(uint8_t* id1, uint8_t* id2); + +extern const SensorType Dallas; +#endif \ No newline at end of file diff --git a/applications/plugins/unitemp/interfaces/SingleWireSensor.c b/applications/plugins/unitemp/interfaces/SingleWireSensor.c new file mode 100644 index 000000000..183bfc872 --- /dev/null +++ b/applications/plugins/unitemp/interfaces/SingleWireSensor.c @@ -0,0 +1,279 @@ +/* + Unitemp - Universal temperature reader + Copyright (C) 2022 Victor Nikitchuk (https://github.com/quen0n) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +#include "SingleWireSensor.h" + +//Максимальное количество попугаев ожидания датчика +#define POLLING_TIMEOUT_TICKS 500 + +/* Типы датчиков и их параметры */ +const SensorType DHT11 = { + .typename = "DHT11", + .interface = &SINGLE_WIRE, + .datatype = UT_DATA_TYPE_TEMP_HUM, + .pollingInterval = 2000, + .allocator = unitemp_singlewire_alloc, + .mem_releaser = unitemp_singlewire_free, + .initializer = unitemp_singlewire_init, + .deinitializer = unitemp_singlewire_deinit, + .updater = unitemp_singlewire_update}; +const SensorType DHT12_SW = { + .typename = "DHT12", + .interface = &SINGLE_WIRE, + .datatype = UT_DATA_TYPE_TEMP_HUM, + .pollingInterval = 2000, + .allocator = unitemp_singlewire_alloc, + .mem_releaser = unitemp_singlewire_free, + .initializer = unitemp_singlewire_init, + .deinitializer = unitemp_singlewire_deinit, + .updater = unitemp_singlewire_update}; +const SensorType DHT21 = { + .typename = "DHT21", + .altname = "DHT21 (AM2301)", + .interface = &SINGLE_WIRE, + .datatype = UT_DATA_TYPE_TEMP_HUM, + .pollingInterval = 1000, + .allocator = unitemp_singlewire_alloc, + .mem_releaser = unitemp_singlewire_free, + .initializer = unitemp_singlewire_init, + .deinitializer = unitemp_singlewire_deinit, + .updater = unitemp_singlewire_update}; +const SensorType DHT22 = { + .typename = "DHT22", + .altname = "DHT22 (AM2302)", + .interface = &SINGLE_WIRE, + .datatype = UT_DATA_TYPE_TEMP_HUM, + .pollingInterval = 2000, + .allocator = unitemp_singlewire_alloc, + .mem_releaser = unitemp_singlewire_free, + .initializer = unitemp_singlewire_init, + .deinitializer = unitemp_singlewire_deinit, + .updater = unitemp_singlewire_update}; +const SensorType AM2320_SW = { + .typename = "AM2320", + .altname = "AM2320 (single wire)", + .interface = &SINGLE_WIRE, + .datatype = UT_DATA_TYPE_TEMP_HUM, + .pollingInterval = 2000, + .allocator = unitemp_singlewire_alloc, + .mem_releaser = unitemp_singlewire_free, + .initializer = unitemp_singlewire_init, + .deinitializer = unitemp_singlewire_deinit, + .updater = unitemp_singlewire_update}; + +bool unitemp_singlewire_alloc(Sensor* sensor, char* args) { + if(args == NULL) return false; + SingleWireSensor* instance = malloc(sizeof(SingleWireSensor)); + if(instance == NULL) { + FURI_LOG_E(APP_NAME, "Sensor %s instance allocation error", sensor->name); + return false; + } + sensor->instance = instance; + + int gpio = 255; + sscanf(args, "%d", &gpio); + + if(unitemp_singlewire_sensorSetGPIO(sensor, unitemp_gpio_getFromInt(gpio))) { + return true; + } + FURI_LOG_E(APP_NAME, "Sensor %s GPIO setting error", sensor->name); + free(instance); + return false; +} +bool unitemp_singlewire_free(Sensor* sensor) { + free(sensor->instance); + + return true; +} + +bool unitemp_singlewire_init(Sensor* sensor) { + SingleWireSensor* instance = ((Sensor*)sensor)->instance; + if(instance == NULL || instance->gpio == NULL) { + FURI_LOG_E(APP_NAME, "Sensor pointer is null!"); + return false; + } + unitemp_gpio_lock(instance->gpio, &SINGLE_WIRE); + //Высокий уровень по умолчанию + furi_hal_gpio_write(instance->gpio->pin, true); + //Режим работы - OpenDrain, подтяжка включается на всякий случай + furi_hal_gpio_init( + instance->gpio->pin, //Порт FZ + GpioModeOutputOpenDrain, //Режим работы - открытый сток + GpioPullUp, //Принудительная подтяжка линии данных к питанию + GpioSpeedVeryHigh); //Скорость работы - максимальная + return true; +} + +bool unitemp_singlewire_deinit(Sensor* sensor) { + SingleWireSensor* instance = ((Sensor*)sensor)->instance; + if(instance == NULL || instance->gpio == NULL) return false; + unitemp_gpio_unlock(instance->gpio); + //Низкий уровень по умолчанию + furi_hal_gpio_write(instance->gpio->pin, false); + //Режим работы - аналог, подтяжка выключена + furi_hal_gpio_init( + instance->gpio->pin, //Порт FZ + GpioModeAnalog, //Режим работы - аналог + GpioPullNo, //Подтяжка выключена + GpioSpeedLow); //Скорость работы - минимальная + return true; +} + +bool unitemp_singlewire_sensorSetGPIO(Sensor* sensor, const GPIO* gpio) { + if(sensor == NULL || gpio == NULL) return false; + SingleWireSensor* instance = sensor->instance; + instance->gpio = gpio; + return true; +} +const GPIO* unitemp_singlewire_sensorGetGPIO(Sensor* sensor) { + if(sensor == NULL) return NULL; + SingleWireSensor* instance = sensor->instance; + return instance->gpio; +} + +UnitempStatus unitemp_singlewire_update(Sensor* sensor) { + SingleWireSensor* instance = sensor->instance; + + //Массив для приёма данных + uint8_t data[5] = {0}; + + /* Запрос */ + //Опускание линии + furi_hal_gpio_write(instance->gpio->pin, false); + //Ожидание более 18 мс + furi_delay_ms(19); + //Выключение прерываний, чтобы ничто не мешало обработке данных + __disable_irq(); + //Подъём линии + furi_hal_gpio_write(instance->gpio->pin, true); + + /* Ответ датчика */ + //Переменная-счётчик + uint16_t timeout = 0; + + //Ожидание подъёма линии + while(!furi_hal_gpio_read(instance->gpio->pin)) { + timeout++; + if(timeout > POLLING_TIMEOUT_TICKS) { + //Включение прерываний + __enable_irq(); + //Возврат признака отсутствующего датчика + return UT_SENSORSTATUS_TIMEOUT; + } + } + timeout = 0; + + //Ожидание спада линии + while(furi_hal_gpio_read(instance->gpio->pin)) { + timeout++; + if(timeout > POLLING_TIMEOUT_TICKS) { + //Включение прерываний + __enable_irq(); + //Возврат признака отсутствующего датчика + return UT_SENSORSTATUS_TIMEOUT; + } + } + + //Ожидание подъёма линии + while(!furi_hal_gpio_read(instance->gpio->pin)) { + timeout++; + if(timeout > POLLING_TIMEOUT_TICKS) { + //Включение прерываний + __enable_irq(); + //Возврат признака отсутствующего датчика + return UT_SENSORSTATUS_TIMEOUT; + } + } + timeout = 0; + + //Ожидание спада линии + while(furi_hal_gpio_read(instance->gpio->pin)) { + timeout++; + if(timeout > POLLING_TIMEOUT_TICKS) { + //Включение прерываний + __enable_irq(); + //Возврат признака отсутствующего датчика + return UT_SENSORSTATUS_TIMEOUT; + } + } + + /* Чтение данных с датчика*/ + //Приём 5 байт + for(uint8_t a = 0; a < 5; a++) { + for(uint8_t b = 7; b != 255; b--) { + uint16_t hT = 0, lT = 0; + //Пока линия в низком уровне, инкремент переменной lT + while(!furi_hal_gpio_read(instance->gpio->pin) && lT != 65535) lT++; + //Пока линия в высоком уровне, инкремент переменной hT + while(furi_hal_gpio_read(instance->gpio->pin) && hT != 65535) hT++; + //Если hT больше lT, то пришла единица + if(hT > lT) data[a] |= (1 << b); + } + } + //Включение прерываний + __enable_irq(); + + //Проверка контрольной суммы + if((uint8_t)(data[0] + data[1] + data[2] + data[3]) != data[4]) { + //Если контрольная сумма не совпала, возврат ошибки + return UT_SENSORSTATUS_BADCRC; + } + + /* Преобразование данных в явный вид */ + //DHT11 и DHT12 + if(sensor->type == &DHT11 || sensor->type == &DHT12_SW) { + sensor->hum = (float)data[0]; + sensor->temp = (float)data[2]; + + //Проверка на отрицательность температуры + if(data[3] != 0) { + //Проверка знака + if(!(data[3] & (1 << 7))) { + //Добавление положительной дробной части + sensor->temp += data[3] * 0.1f; + } else { + //А тут делаем отрицательное значение + data[3] &= ~(1 << 7); + sensor->temp += data[3] * 0.1f; + sensor->temp *= -1; + } + } + } + + //DHT21, DHT22, AM2320 + if(sensor->type == &DHT21 || sensor->type == &DHT22 || sensor->type == &AM2320_SW) { + sensor->hum = (float)(((uint16_t)data[0] << 8) | data[1]) / 10; + + uint16_t raw = (((uint16_t)data[2] << 8) | data[3]); + //Проверка на отрицательность температуры + if(READ_BIT(raw, 1 << 15)) { + //Проверка на способ кодирования данных + if(READ_BIT(raw, 0x60)) { + //Не оригинал + sensor->temp = (float)((int16_t)raw) / 10; + } else { + //Оригинальный датчик + CLEAR_BIT(raw, 1 << 15); + sensor->temp = (float)(raw) / -10; + } + } else { + sensor->temp = (float)(raw) / 10; + } + } + //Возврат признака успешного опроса + return UT_SENSORSTATUS_OK; +} diff --git a/applications/plugins/unitemp/interfaces/SingleWireSensor.h b/applications/plugins/unitemp/interfaces/SingleWireSensor.h new file mode 100644 index 000000000..f5bc74734 --- /dev/null +++ b/applications/plugins/unitemp/interfaces/SingleWireSensor.h @@ -0,0 +1,92 @@ +/* + Unitemp - Universal temperature reader + Copyright (C) 2022 Victor Nikitchuk (https://github.com/quen0n) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +#ifndef UNITEMP_SINGLE_WIRE +#define UNITEMP_SINGLE_WIRE + +#include "../unitemp.h" +#include "../Sensors.h" + +//Интерфейс Single Wire +typedef struct { + //Порт подключения датчика + const GPIO* gpio; +} SingleWireSensor; + +/* Датчики */ +extern const SensorType DHT11; +extern const SensorType DHT12_SW; +extern const SensorType DHT21; +extern const SensorType DHT22; +extern const SensorType AM2320_SW; + +/** + * @brief Инициализация датчика + * + * @param sensor Указатель на инициализируемый датчик + * @return Истина если всё прошло успешно + */ +bool unitemp_singlewire_init(Sensor* sensor); + +/** + * @brief Деинициализация датчика + * + * @param sensor Указатель на инициализируемый датчик + * @return Истина если всё прошло успешно + */ +bool unitemp_singlewire_deinit(Sensor* sensor); + +/** + * @brief Получение данных с датчика по однопроводному интерфейсу DHTxx и AM2xxx + * + * @param sensor Указатель на датчик + * @return Статус опроса + */ +UnitempStatus unitemp_singlewire_update(Sensor* sensor); + +/** + * @brief Установить порт датчика + * + * @param sensor Указатель на датчик + * @param gpio Устанавливаемый порт + * @return Истина если всё ок + */ +bool unitemp_singlewire_sensorSetGPIO(Sensor* sensor, const GPIO* gpio); + +/** + * @brief Получить порт датчика + * + * @param sensor Указатель на датчик + * @return Указатель на GPIO + */ +const GPIO* unitemp_singlewire_sensorGetGPIO(Sensor* sensor); + +/** + * @brief Выделение памяти под датчик на линии One Wire + * + * @param sensor Указатель на датчик + * @param args Указатель на массив с аргументами параметров датчка + */ +bool unitemp_singlewire_alloc(Sensor* sensor, char* args); + +/** + * @brief Высвобождение памяти инстанса датчика + * + * @param sensor Указатель на датчик + */ +bool unitemp_singlewire_free(Sensor* sensor); +#endif \ No newline at end of file diff --git a/applications/plugins/unitemp/sensors/AM2320.c b/applications/plugins/unitemp/sensors/AM2320.c new file mode 100644 index 000000000..e77707005 --- /dev/null +++ b/applications/plugins/unitemp/sensors/AM2320.c @@ -0,0 +1,106 @@ +/* + Unitemp - Universal temperature reader + Copyright (C) 2022 Victor Nikitchuk (https://github.com/quen0n) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +#include "AM2320.h" +#include "../interfaces/I2CSensor.h" + +const SensorType AM2320_I2C = { + .typename = "AM2320_I2C", + .altname = "AM2320 (I2C)", + .interface = &I2C, + .datatype = UT_TEMPERATURE | UT_HUMIDITY, + .pollingInterval = 2000, + .allocator = unitemp_AM2320_I2C_alloc, + .mem_releaser = unitemp_AM2320_I2C_free, + .initializer = unitemp_AM2320_init, + .deinitializer = unitemp_AM2320_I2C_deinit, + .updater = unitemp_AM2320_I2C_update}; + +static uint16_t AM2320_calc_CRC(uint8_t* ptr, uint8_t len) { + uint16_t crc = 0xFFFF; + uint8_t i; + while(len--) { + crc ^= *ptr++; + for(i = 0; i < 8; i++) { + if(crc & 0x01) { + crc >>= 1; + crc ^= 0xA001; + } else { + crc >>= 1; + } + } + } + return crc; +} + +bool unitemp_AM2320_I2C_alloc(Sensor* sensor, char* args) { + UNUSED(args); + I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance; + + //Адреса на шине I2C (7 бит) + i2c_sensor->minI2CAdr = 0x5C << 1; + i2c_sensor->maxI2CAdr = 0x5C << 1; + return true; +} + +bool unitemp_AM2320_I2C_free(Sensor* sensor) { + //Нечего высвобождать, так как ничего не было выделено + UNUSED(sensor); + return true; +} + +bool unitemp_AM2320_init(Sensor* sensor) { + //Нечего инициализировать + UNUSED(sensor); + return true; +} + +bool unitemp_AM2320_I2C_deinit(Sensor* sensor) { + //Нечего деинициализировать + UNUSED(sensor); + return true; +} + +UnitempStatus unitemp_AM2320_I2C_update(Sensor* sensor) { + I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance; + + uint8_t data[8] = {0x03, 0x00, 0x04}; + + //Wake up + unitemp_i2c_isDeviceReady(i2c_sensor); + furi_delay_ms(1); + + //Запрос + if(!unitemp_i2c_writeArray(i2c_sensor, 3, data)) return UT_SENSORSTATUS_TIMEOUT; + furi_delay_ms(2); + //Ответ + if(!unitemp_i2c_readArray(i2c_sensor, 8, data)) return UT_SENSORSTATUS_TIMEOUT; + + if(AM2320_calc_CRC(data, 6) != ((data[7] << 8) | data[6])) { + return UT_SENSORSTATUS_BADCRC; + } + + sensor->hum = (float)(((uint16_t)data[2] << 8) | data[3]) / 10; + //Проверка на отрицательность температуры + if(!(data[4] & (1 << 7))) { + sensor->temp = (float)(((uint16_t)data[4] << 8) | data[5]) / 10; + } else { + data[4] &= ~(1 << 7); + sensor->temp = (float)(((uint16_t)data[4] << 8) | data[5]) / -10; + } + return UT_SENSORSTATUS_OK; +} diff --git a/applications/plugins/unitemp/sensors/AM2320.h b/applications/plugins/unitemp/sensors/AM2320.h new file mode 100644 index 000000000..f13105470 --- /dev/null +++ b/applications/plugins/unitemp/sensors/AM2320.h @@ -0,0 +1,62 @@ +/* + Unitemp - Universal temperature reader + Copyright (C) 2022 Victor Nikitchuk (https://github.com/quen0n) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +#ifndef UNITEMP_AM2320 +#define UNITEMP_AM2320 + +#include "../unitemp.h" +#include "../Sensors.h" +extern const SensorType AM2320_I2C; +/** + * @brief Выделение памяти и установка начальных значений датчика AM2320 + * + * @param sensor Указатель на создаваемый датчик + * @return Истина при успехе + */ +bool unitemp_AM2320_I2C_alloc(Sensor* sensor, char* args); + +/** + * @brief Инициализации датчика AM2320 + * + * @param sensor Указатель на датчик + * @return Истина если инициализация упспешная + */ +bool unitemp_AM2320_init(Sensor* sensor); + +/** + * @brief Деинициализация датчика + * + * @param sensor Указатель на датчик + */ +bool unitemp_AM2320_I2C_deinit(Sensor* sensor); + +/** + * @brief Обновление значений из датчика + * + * @param sensor Указатель на датчик + * @return Статус обновления + */ +UnitempStatus unitemp_AM2320_I2C_update(Sensor* sensor); + +/** + * @brief Высвободить память датчика + * + * @param sensor Указатель на датчик + */ +bool unitemp_AM2320_I2C_free(Sensor* sensor); + +#endif \ No newline at end of file diff --git a/applications/plugins/unitemp/sensors/BMx280.c b/applications/plugins/unitemp/sensors/BMx280.c new file mode 100644 index 000000000..a64daaa1d --- /dev/null +++ b/applications/plugins/unitemp/sensors/BMx280.c @@ -0,0 +1,352 @@ +/* + Unitemp - Universal temperature reader + Copyright (C) 2022 Victor Nikitchuk (https://github.com/quen0n) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +#include "BMx280.h" + +const SensorType BMP280 = { + .typename = "BMP280", + .interface = &I2C, + .datatype = UT_TEMPERATURE | UT_PRESSURE, + .pollingInterval = 500, + .allocator = unitemp_BMx280_alloc, + .mem_releaser = unitemp_BMx280_free, + .initializer = unitemp_BMx280_init, + .deinitializer = unitemp_BMx280_deinit, + .updater = unitemp_BMx280_update}; +const SensorType BME280 = { + .typename = "BME280", + .interface = &I2C, + .datatype = UT_TEMPERATURE | UT_HUMIDITY | UT_PRESSURE, + + .pollingInterval = 500, + .allocator = unitemp_BMx280_alloc, + .mem_releaser = unitemp_BMx280_free, + .initializer = unitemp_BMx280_init, + .deinitializer = unitemp_BMx280_deinit, + .updater = unitemp_BMx280_update}; + +//Интервал обновления калибровочных значений +#define BOSCH_CAL_UPDATE_INTERVAL 60000 + +#define TEMP_CAL_START_ADDR 0x88 +#define PRESS_CAL_START_ADDR 0x8E +#define HUM_CAL_H1_ADDR 0xA1 +#define HUM_CAL_H2_ADDR 0xE1 + +#define BMP280_ID 0x58 +#define BME280_ID 0x60 + +#define BMx280_I2C_ADDR_MIN (0x76 << 1) +#define BMx280_I2C_ADDR_MAX (0x77 << 1) + +#define BMx280_REG_STATUS 0xF3 +#define BMx280_REG_CTRL_MEAS 0xF4 +#define BMx280_REG_CONFIG 0xF5 +#define BME280_REG_CTRL_HUM 0xF2 +//Преддескретизация температуры +#define BMx280_TEMP_OVERSAMPLING_SKIP 0b00000000 +#define BMx280_TEMP_OVERSAMPLING_1 0b00100000 +#define BMx280_TEMP_OVERSAMPLING_2 0b01000000 +#define BMx280_TEMP_OVERSAMPLING_4 0b01100000 +#define BMx280_TEMP_OVERSAMPLING_8 0b10000000 +#define BMx280_TEMP_OVERSAMPLING_16 0b10100000 +//Преддескретизация давления +#define BMx280_PRESS_OVERSAMPLING_SKIP 0b00000000 +#define BMx280_PRESS_OVERSAMPLING_1 0b00000100 +#define BMx280_PRESS_OVERSAMPLING_2 0b00001000 +#define BMx280_PRESS_OVERSAMPLING_4 0b00001100 +#define BMx280_PRESS_OVERSAMPLING_8 0b00010000 +#define BMx280_PRESS_OVERSAMPLING_16 0b00010100 +//Преддескретизация влажности +#define BME280_HUM_OVERSAMPLING_SKIP 0b00000000 +#define BME280_HUM_OVERSAMPLING_1 0b00000001 +#define BME280_HUM_OVERSAMPLING_2 0b00000010 +#define BME280_HUM_OVERSAMPLING_4 0b00000011 +#define BME280_HUM_OVERSAMPLING_8 0b00000100 +#define BME280_HUM_OVERSAMPLING_16 0b00000101u +//Режимы работы датчика +#define BMx280_MODE_SLEEP 0b00000000 //Наелся и спит +#define BMx280_MODE_FORCED 0b00000001 //Обновляет значения 1 раз, после чего уходит в сон +#define BMx280_MODE_NORMAL 0b00000011 //Регулярно обновляет значения +//Период обновления в нормальном режиме +#define BMx280_STANDBY_TIME_0_5 0b00000000 +#define BMx280_STANDBY_TIME_62_5 0b00100000 +#define BMx280_STANDBY_TIME_125 0b01000000 +#define BMx280_STANDBY_TIME_250 0b01100000 +#define BMx280_STANDBY_TIME_500 0b10000000 +#define BMx280_STANDBY_TIME_1000 0b10100000 +#define BMx280_STANDBY_TIME_2000 0b11000000 +#define BMx280_STANDBY_TIME_4000 0b11100000 +//Коэффициент фильтрации значений +#define BMx280_FILTER_COEFF_1 0b00000000 +#define BMx280_FILTER_COEFF_2 0b00000100 +#define BMx280_FILTER_COEFF_4 0b00001000 +#define BMx280_FILTER_COEFF_8 0b00001100 +#define BMx280_FILTER_COEFF_16 0b00010000 +//Разрешить работу по SPI +#define BMx280_SPI_3W_ENABLE 0b00000001 +#define BMx280_SPI_3W_DISABLE 0b00000000 + +static float BMx280_compensate_temperature(I2CSensor* i2c_sensor, int32_t adc_T) { + BMx280_instance* bmx280_instance = (BMx280_instance*)i2c_sensor->sensorInstance; + int32_t var1, var2; + var1 = ((((adc_T >> 3) - ((int32_t)bmx280_instance->temp_cal.dig_T1 << 1))) * + ((int32_t)bmx280_instance->temp_cal.dig_T2)) >> + 11; + var2 = (((((adc_T >> 4) - ((int32_t)bmx280_instance->temp_cal.dig_T1)) * + ((adc_T >> 4) - ((int32_t)bmx280_instance->temp_cal.dig_T1))) >> + 12) * + ((int32_t)bmx280_instance->temp_cal.dig_T3)) >> + 14; + bmx280_instance->t_fine = var1 + var2; + return ((bmx280_instance->t_fine * 5 + 128) >> 8) / 100.0f; +} + +static float BMx280_compensate_pressure(I2CSensor* i2c_sensor, int32_t adc_P) { + BMx280_instance* bmx280_instance = (BMx280_instance*)i2c_sensor->sensorInstance; + + int32_t var1, var2; + uint32_t p; + var1 = (((int32_t)bmx280_instance->t_fine) >> 1) - (int32_t)64000; + var2 = (((var1 >> 2) * (var1 >> 2)) >> 11) * ((int32_t)bmx280_instance->press_cal.dig_P6); + var2 = var2 + ((var1 * ((int32_t)bmx280_instance->press_cal.dig_P5)) << 1); + var2 = (var2 >> 2) + (((int32_t)bmx280_instance->press_cal.dig_P4) << 16); + var1 = (((bmx280_instance->press_cal.dig_P3 * (((var1 >> 2) * (var1 >> 2)) >> 13)) >> 3) + + ((((int32_t)bmx280_instance->press_cal.dig_P2) * var1) >> 1)) >> + 18; + var1 = ((((32768 + var1)) * ((int32_t)bmx280_instance->press_cal.dig_P1)) >> 15); + if(var1 == 0) { + return 0; // avoid exception caused by division by zero + } + p = (((uint32_t)(((int32_t)1048576) - adc_P) - (var2 >> 12))) * 3125; + if(p < 0x80000000) { + p = (p << 1) / ((uint32_t)var1); + } else { + p = (p / (uint32_t)var1) * 2; + } + var1 = (((int32_t)bmx280_instance->press_cal.dig_P9) * + ((int32_t)(((p >> 3) * (p >> 3)) >> 13))) >> + 12; + var2 = (((int32_t)(p >> 2)) * ((int32_t)bmx280_instance->press_cal.dig_P8)) >> 13; + p = (uint32_t)((int32_t)p + ((var1 + var2 + bmx280_instance->press_cal.dig_P7) >> 4)); + return p; +} + +static float BMx280_compensate_humidity(I2CSensor* i2c_sensor, int32_t adc_H) { + BMx280_instance* bmx280_instance = (BMx280_instance*)i2c_sensor->sensorInstance; + int32_t v_x1_u32r; + v_x1_u32r = (bmx280_instance->t_fine - ((int32_t)76800)); + + v_x1_u32r = + (((((adc_H << 14) - (((int32_t)bmx280_instance->hum_cal.dig_H4) << 20) - + (((int32_t)bmx280_instance->hum_cal.dig_H5) * v_x1_u32r)) + + ((int32_t)16384)) >> + 15) * + (((((((v_x1_u32r * ((int32_t)bmx280_instance->hum_cal.dig_H6)) >> 10) * + (((v_x1_u32r * ((int32_t)bmx280_instance->hum_cal.dig_H3)) >> 11) + + ((int32_t)32768))) >> + 10) + + ((int32_t)2097152)) * + ((int32_t)bmx280_instance->hum_cal.dig_H2) + + 8192) >> + 14)); + + v_x1_u32r = + (v_x1_u32r - (((((v_x1_u32r >> 15) * (v_x1_u32r >> 15)) >> 7) * + ((int32_t)bmx280_instance->hum_cal.dig_H1)) >> + 4)); + + v_x1_u32r = (v_x1_u32r < 0 ? 0 : v_x1_u32r); + v_x1_u32r = (v_x1_u32r > 419430400 ? 419430400 : v_x1_u32r); + return ((uint32_t)(v_x1_u32r >> 12)) / 1024.0f; +} + +static bool bmx280_readCalValues(I2CSensor* i2c_sensor) { + BMx280_instance* bmx280_instance = (BMx280_instance*)i2c_sensor->sensorInstance; + if(!unitemp_i2c_readRegArray( + i2c_sensor, TEMP_CAL_START_ADDR, 6, (uint8_t*)&bmx280_instance->temp_cal)) + return false; +#ifdef UNITEMP_DEBUG + FURI_LOG_D( + APP_NAME, + "Sensor BMx280 (0x%02X) T1-T3: %d, %d, %d", + i2c_sensor->currentI2CAdr, + bmx280_instance->temp_cal.dig_T1, + bmx280_instance->temp_cal.dig_T2, + bmx280_instance->temp_cal.dig_T3); +#endif + + if(!unitemp_i2c_readRegArray( + i2c_sensor, PRESS_CAL_START_ADDR, 18, (uint8_t*)&bmx280_instance->press_cal)) + return false; +#ifdef UNITEMP_DEBUG + FURI_LOG_D( + APP_NAME, + "Sensor BMx280 (0x%02X): P1-P9: %d, %d, %d, %d, %d, %d, %d, %d, %d", + i2c_sensor->currentI2CAdr, + bmx280_instance->press_cal.dig_P1, + bmx280_instance->press_cal.dig_P2, + bmx280_instance->press_cal.dig_P3, + bmx280_instance->press_cal.dig_P4, + bmx280_instance->press_cal.dig_P5, + bmx280_instance->press_cal.dig_P6, + bmx280_instance->press_cal.dig_P7, + bmx280_instance->press_cal.dig_P8, + bmx280_instance->press_cal.dig_P9); +#endif + + if(bmx280_instance->chip_id == BME280_ID) { + uint8_t buff[7] = {0}; + if(!unitemp_i2c_readRegArray(i2c_sensor, HUM_CAL_H1_ADDR, 1, buff)) return false; + bmx280_instance->hum_cal.dig_H1 = buff[0]; + + if(!unitemp_i2c_readRegArray(i2c_sensor, HUM_CAL_H2_ADDR, 7, buff)) return false; + bmx280_instance->hum_cal.dig_H2 = (uint16_t)(buff[0] | ((uint16_t)buff[1] << 8)); + bmx280_instance->hum_cal.dig_H3 = buff[2]; + bmx280_instance->hum_cal.dig_H4 = ((int16_t)buff[3] << 4) | (buff[4] & 0x0F); + bmx280_instance->hum_cal.dig_H5 = (buff[4] & 0x0F) | ((int16_t)buff[5] << 4); + bmx280_instance->hum_cal.dig_H6 = buff[6]; + +#ifdef UNITEMP_DEBUG + FURI_LOG_D( + APP_NAME, + "Sensor BMx280 (0x%02X): H1-H6: %d, %d, %d, %d, %d, %d", + i2c_sensor->currentI2CAdr, + bmx280_instance->hum_cal.dig_H1, + bmx280_instance->hum_cal.dig_H2, + bmx280_instance->hum_cal.dig_H3, + bmx280_instance->hum_cal.dig_H4, + bmx280_instance->hum_cal.dig_H5, + bmx280_instance->hum_cal.dig_H6); +#endif + } + + bmx280_instance->last_cal_update_time = furi_get_tick(); + return true; +} +static bool bmp280_isMeasuring(Sensor* sensor) { + I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance; + return (bool)((unitemp_i2c_readReg(i2c_sensor, BMx280_REG_STATUS) & 0x08) >> 3); +} + +bool unitemp_BMx280_alloc(Sensor* sensor, char* args) { + UNUSED(args); + I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance; + BMx280_instance* bmx280_instance = malloc(sizeof(BMx280_instance)); + if(bmx280_instance == NULL) { + FURI_LOG_E(APP_NAME, "Failed to allocation sensor %s instance", sensor->name); + return false; + } + + if(sensor->type == &BMP280) bmx280_instance->chip_id = BMP280_ID; + if(sensor->type == &BME280) bmx280_instance->chip_id = BME280_ID; + + i2c_sensor->sensorInstance = bmx280_instance; + + i2c_sensor->minI2CAdr = BMx280_I2C_ADDR_MIN; + i2c_sensor->maxI2CAdr = BMx280_I2C_ADDR_MAX; + return true; +} + +bool unitemp_BMx280_init(Sensor* sensor) { + I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance; + //Перезагрузка + unitemp_i2c_writeReg(i2c_sensor, 0xE0, 0xB6); + //Чтение ID датчика + uint8_t id = unitemp_i2c_readReg(i2c_sensor, 0xD0); + if(id != BMP280_ID && id != BME280_ID) { + FURI_LOG_E( + APP_NAME, + "Sensor %s returned wrong ID 0x%02X, expected 0x%02X or 0x%02X", + sensor->name, + id, + BMP280_ID, + BME280_ID); + return false; + } + + //Настройка режимов работы + if(id == BME280_ID) { + unitemp_i2c_writeReg(i2c_sensor, BME280_REG_CTRL_HUM, BME280_HUM_OVERSAMPLING_1); + unitemp_i2c_writeReg( + i2c_sensor, BME280_REG_CTRL_HUM, unitemp_i2c_readReg(i2c_sensor, BME280_REG_CTRL_HUM)); + } + unitemp_i2c_writeReg( + i2c_sensor, + BMx280_REG_CTRL_MEAS, + BMx280_TEMP_OVERSAMPLING_2 | BMx280_PRESS_OVERSAMPLING_4 | BMx280_MODE_NORMAL); + //Настройка периода опроса и фильтрации значений + unitemp_i2c_writeReg( + i2c_sensor, + BMx280_REG_CONFIG, + BMx280_STANDBY_TIME_500 | BMx280_FILTER_COEFF_16 | BMx280_SPI_3W_DISABLE); + //Чтение калибровочных значений + if(!bmx280_readCalValues(i2c_sensor)) { + FURI_LOG_E(APP_NAME, "Failed to read calibration values sensor %s", sensor->name); + return false; + } + return true; +} + +bool unitemp_BMx280_deinit(Sensor* sensor) { + I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance; + //Перевод в сон + unitemp_i2c_writeReg(i2c_sensor, BMx280_REG_CTRL_MEAS, BMx280_MODE_SLEEP); + return true; +} + +UnitempStatus unitemp_BMx280_update(Sensor* sensor) { + I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance; + BMx280_instance* instance = i2c_sensor->sensorInstance; + + uint32_t t = furi_get_tick(); + + uint8_t buff[3]; + //Проверка инициализированности датчика + unitemp_i2c_readRegArray(i2c_sensor, 0xF4, 2, buff); + if(buff[0] == 0) { + FURI_LOG_W(APP_NAME, "Sensor %s is not initialized!", sensor->name); + return UT_SENSORSTATUS_ERROR; + } + + while(bmp280_isMeasuring(sensor)) { + if(furi_get_tick() - t > 100) { + return UT_SENSORSTATUS_TIMEOUT; + } + } + + if(furi_get_tick() - instance->last_cal_update_time > BOSCH_CAL_UPDATE_INTERVAL) { + bmx280_readCalValues(i2c_sensor); + } + + if(!unitemp_i2c_readRegArray(i2c_sensor, 0xFA, 3, buff)) return UT_SENSORSTATUS_TIMEOUT; + int32_t adc_T = ((int32_t)buff[0] << 12) | ((int32_t)buff[1] << 4) | ((int32_t)buff[2] >> 4); + if(!unitemp_i2c_readRegArray(i2c_sensor, 0xF7, 3, buff)) return UT_SENSORSTATUS_TIMEOUT; + int32_t adc_P = ((int32_t)buff[0] << 12) | ((int32_t)buff[1] << 4) | ((int32_t)buff[2] >> 4); + if(!unitemp_i2c_readRegArray(i2c_sensor, 0xFD, 2, buff)) return UT_SENSORSTATUS_TIMEOUT; + int32_t adc_H = ((uint16_t)buff[0] << 8) | buff[1]; + sensor->temp = BMx280_compensate_temperature(i2c_sensor, adc_T); + sensor->pressure = BMx280_compensate_pressure(i2c_sensor, adc_P); + sensor->hum = BMx280_compensate_humidity(i2c_sensor, adc_H); + return UT_SENSORSTATUS_OK; +} + +bool unitemp_BMx280_free(Sensor* sensor) { + I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance; + free(i2c_sensor->sensorInstance); + return true; +} \ No newline at end of file diff --git a/applications/plugins/unitemp/sensors/BMx280.h b/applications/plugins/unitemp/sensors/BMx280.h new file mode 100644 index 000000000..fe52a364e --- /dev/null +++ b/applications/plugins/unitemp/sensors/BMx280.h @@ -0,0 +1,102 @@ +/* + Unitemp - Universal temperature reader + Copyright (C) 2022 Victor Nikitchuk (https://github.com/quen0n) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +#ifndef UNITEMP_BMx280 +#define UNITEMP_BMx280 + +#include "../unitemp.h" +#include "../Sensors.h" +#include "../interfaces/I2CSensor.h" + +typedef struct { + uint16_t dig_T1; + int16_t dig_T2; + int16_t dig_T3; +} BMx280_temp_cal; + +typedef struct { + uint16_t dig_P1; + int16_t dig_P2; + int16_t dig_P3; + int16_t dig_P4; + int16_t dig_P5; + int16_t dig_P6; + int16_t dig_P7; + int16_t dig_P8; + int16_t dig_P9; +} BMx280_press_cal; + +typedef struct { + uint8_t dig_H1; + int16_t dig_H2; + uint8_t dig_H3; + int16_t dig_H4; + int16_t dig_H5; + int8_t dig_H6; +} BMx280_hum_cal; + +typedef struct { + //Калибровочные значения температуры + BMx280_temp_cal temp_cal; + //Калибровочные значения давления + BMx280_press_cal press_cal; + //Калибровочные значения влажности воздуха + BMx280_hum_cal hum_cal; + //Время последнего обновления калибровочных значений + uint32_t last_cal_update_time; + //Индификатор датчика + uint8_t chip_id; + //Корректировочное значение температуры + int32_t t_fine; +} BMx280_instance; + +extern const SensorType BMP280; +extern const SensorType BME280; +/** + * @brief Выделение памяти и установка начальных значений датчика BMP280 + * @param sensor Указатель на создаваемый датчик + * @return Истина при успехе + */ +bool unitemp_BMx280_alloc(Sensor* sensor, char* args); + +/** + * @brief Инициализации датчика BMP280 + * @param sensor Указатель на датчик + * @return Истина если инициализация упспешная + */ +bool unitemp_BMx280_init(Sensor* sensor); + +/** + * @brief Деинициализация датчика + * @param sensor Указатель на датчик + */ +bool unitemp_BMx280_deinit(Sensor* sensor); + +/** + * @brief Обновление значений из датчика + * @param sensor Указатель на датчик + * @return Статус опроса датчика + */ +UnitempStatus unitemp_BMx280_update(Sensor* sensor); + +/** + * @brief Высвободить память датчика + * @param sensor Указатель на датчик + */ +bool unitemp_BMx280_free(Sensor* sensor); + +#endif \ No newline at end of file diff --git a/applications/plugins/unitemp/sensors/DHT20.c b/applications/plugins/unitemp/sensors/DHT20.c new file mode 100644 index 000000000..ee1b042c9 --- /dev/null +++ b/applications/plugins/unitemp/sensors/DHT20.c @@ -0,0 +1,65 @@ +/* + Unitemp - Universal temperature reader + Copyright (C) 2022 Victor Nikitchuk (https://github.com/quen0n) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +#include "DHT20.h" +#include "../interfaces/I2CSensor.h" + +const SensorType DHT20 = { + .typename = "DHT20", + .altname = "DHT20 (AM2108)", + .interface = &I2C, + .datatype = UT_TEMPERATURE | UT_HUMIDITY, + .pollingInterval = 2000, + .allocator = unitemp_DHT20_I2C_alloc, + .mem_releaser = unitemp_DHT20_I2C_free, + .initializer = unitemp_DHT20_init, + .deinitializer = unitemp_DHT20_I2C_deinit, + .updater = unitemp_DHT20_I2C_update}; + +bool unitemp_DHT20_I2C_alloc(Sensor* sensor, char* args) { + UNUSED(args); + I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance; + + //Адреса на шине I2C (7 бит) + i2c_sensor->minI2CAdr = 0x38 << 1; + i2c_sensor->maxI2CAdr = 0x38 << 1; + return true; +} + +bool unitemp_DHT20_I2C_free(Sensor* sensor) { + //Нечего высвобождать, так как ничего не было выделено + UNUSED(sensor); + return true; +} + +bool unitemp_DHT20_init(Sensor* sensor) { + //Нечего инициализировать + UNUSED(sensor); + return true; +} + +bool unitemp_DHT20_I2C_deinit(Sensor* sensor) { + //Нечего деинициализировать + UNUSED(sensor); + return true; +} + +UnitempStatus unitemp_DHT20_I2C_update(Sensor* sensor) { + //I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance; + UNUSED(sensor); + return UT_SENSORSTATUS_OK; +} diff --git a/applications/plugins/unitemp/sensors/DHT20.h b/applications/plugins/unitemp/sensors/DHT20.h new file mode 100644 index 000000000..e2e89bf18 --- /dev/null +++ b/applications/plugins/unitemp/sensors/DHT20.h @@ -0,0 +1,62 @@ +/* + Unitemp - Universal temperature reader + Copyright (C) 2022 Victor Nikitchuk (https://github.com/quen0n) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +#ifndef UNITEMP_DHT20 +#define UNITEMP_DHT20 + +#include "../unitemp.h" +#include "../Sensors.h" +extern const SensorType DHT20; +/** + * @brief Выделение памяти и установка начальных значений датчика DHT20 + * + * @param sensor Указатель на создаваемый датчик + * @return Истина при успехе + */ +bool unitemp_DHT20_I2C_alloc(Sensor* sensor, char* args); + +/** + * @brief Инициализации датчика DHT20 + * + * @param sensor Указатель на датчик + * @return Истина если инициализация упспешная + */ +bool unitemp_DHT20_init(Sensor* sensor); + +/** + * @brief Деинициализация датчика + * + * @param sensor Указатель на датчик + */ +bool unitemp_DHT20_I2C_deinit(Sensor* sensor); + +/** + * @brief Обновление значений из датчика + * + * @param sensor Указатель на датчик + * @return Статус обновления + */ +UnitempStatus unitemp_DHT20_I2C_update(Sensor* sensor); + +/** + * @brief Высвободить память датчика + * + * @param sensor Указатель на датчик + */ +bool unitemp_DHT20_I2C_free(Sensor* sensor); + +#endif \ No newline at end of file diff --git a/applications/plugins/unitemp/sensors/LM75.c b/applications/plugins/unitemp/sensors/LM75.c new file mode 100644 index 000000000..e71376404 --- /dev/null +++ b/applications/plugins/unitemp/sensors/LM75.c @@ -0,0 +1,87 @@ +/* + Unitemp - Universal temperature reader + Copyright (C) 2022 Victor Nikitchuk (https://github.com/quen0n) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +#include "LM75.h" +#include "../interfaces/I2CSensor.h" + +#define LM75_REG_TEMP 0x00 +#define LM75_REG_CONFIG 0x01 +#define LM75_REG_THYST 0x02 +#define LM75_REG_TOS 0x03 + +#define LM75_CONFIG_SHUTDOWN 0b00000001 +#define LM75_CONFIG_INTERRUPT 0b00000010 +#define LM75_CONFIG_OSPOLARITY_HIGH 0b00000100 +#define LM75_CONFIG_FAULTQUEUE_1 0b00000000 +#define LM75_CONFIG_FAULTQUEUE_2 0b00001000 +#define LM75_CONFIG_FAULTQUEUE_4 0b00010000 +#define LM75_CONFIG_FAULTQUEUE_6 0b00011000 + +const SensorType LM75 = { + .typename = "LM75", + .interface = &I2C, + .datatype = UT_DATA_TYPE_TEMP, + .pollingInterval = 500, + .allocator = unitemp_LM75_alloc, + .mem_releaser = unitemp_LM75_free, + .initializer = unitemp_LM75_init, + .deinitializer = unitemp_LM75_deinit, + .updater = unitemp_LM75_update}; + +bool unitemp_LM75_alloc(Sensor* sensor, char* args) { + UNUSED(args); + I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance; + + //Адреса на шине I2C (7 бит) + i2c_sensor->minI2CAdr = 0b1001000 << 1; + i2c_sensor->maxI2CAdr = 0b1001111 << 1; + return true; +} + +bool unitemp_LM75_free(Sensor* sensor) { + //Нечего высвобождать, так как ничего не было выделено + UNUSED(sensor); + return true; +} + +bool unitemp_LM75_init(Sensor* sensor) { + I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance; + + //Выход если не удалось записать значение в датчик + if(!unitemp_i2c_writeReg(i2c_sensor, LM75_REG_CONFIG, LM75_CONFIG_FAULTQUEUE_1)) return false; + + return true; +} + +bool unitemp_LM75_deinit(Sensor* sensor) { + I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance; + if(!unitemp_i2c_writeReg( + i2c_sensor, LM75_REG_CONFIG, LM75_CONFIG_FAULTQUEUE_1 | LM75_CONFIG_SHUTDOWN)) + return false; + return true; +} + +UnitempStatus unitemp_LM75_update(Sensor* sensor) { + I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance; + + uint8_t buff[2]; + if(!unitemp_i2c_readRegArray(i2c_sensor, LM75_REG_TEMP, 2, buff)) + return UT_SENSORSTATUS_TIMEOUT; + int16_t raw = (((uint16_t)buff[0] << 8) | buff[1]); + sensor->temp = raw / 32 * 0.125; + return UT_SENSORSTATUS_OK; +} diff --git a/applications/plugins/unitemp/sensors/LM75.h b/applications/plugins/unitemp/sensors/LM75.h new file mode 100644 index 000000000..dc1fb791c --- /dev/null +++ b/applications/plugins/unitemp/sensors/LM75.h @@ -0,0 +1,62 @@ +/* + Unitemp - Universal temperature reader + Copyright (C) 2022 Victor Nikitchuk (https://github.com/quen0n) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +#ifndef UNITEMP_LM75 +#define UNITEMP_LM75 + +#include "../unitemp.h" +#include "../Sensors.h" +extern const SensorType LM75; +/** + * @brief Выделение памяти и установка начальных значений датчика LM75 + * + * @param sensor Указатель на создаваемый датчик + * @return Истина при успехе + */ +bool unitemp_LM75_alloc(Sensor* sensor, char* args); + +/** + * @brief Инициализации датчика LM75 + * + * @param sensor Указатель на датчик + * @return Истина если инициализация упспешная + */ +bool unitemp_LM75_init(Sensor* sensor); + +/** + * @brief Деинициализация датчика + * + * @param sensor Указатель на датчик + */ +bool unitemp_LM75_deinit(Sensor* sensor); + +/** + * @brief Обновление значений из датчика + * + * @param sensor Указатель на датчик + * @return Статус обновления + */ +UnitempStatus unitemp_LM75_update(Sensor* sensor); + +/** + * @brief Высвободить память датчика + * + * @param sensor Указатель на датчик + */ +bool unitemp_LM75_free(Sensor* sensor); + +#endif \ No newline at end of file diff --git a/applications/plugins/unitemp/sensors/Sensors.xlsx b/applications/plugins/unitemp/sensors/Sensors.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..2b6578e0ecc66d8bd41e911ea600061bc3c70de0 GIT binary patch literal 11864 zcmeHt1y>!}()Iy@I|p|U?(XjH5`w!s1a}Ya1b4R(+=B;5u%H2gySscRxpVK_Vdnb< z@9lF|_v+PM)xCGuQ&qd3Qj&#ug${rMzyJUM5`Zvg;Dj9*0PqSD0007Dz;#9K?OaUl zT=dmE9Za3|7(8rki1S~8Q{@7{LHGY}`#(Gbm1@Iwy-X+_ntOs`of;8&Vddp8_^tT5 zR3ct<3?H}VZV zSFg?jhewo1$g&cw)Ru(^fi)BsrH&c>jM&^Ciji!&b)=aEu5ZD^+hAW3yY<7*>LyqH z2j0N1anb6ziZk`@Emj+`@aO5y=Lc*BRZxB3fHht9@wVdm-cjp=U4#~JH*+{ zai^Xg!7$FDD3;5BmDWSl3w-A)@EzFG3k^=J6Ri6rr1#TYT(MaTfK+!wtoUl4ON3Ve z2nV807T>R;@oy^@k(NK?3lpuToFd^pfpGTx3;|I33rs(%Gm%|@Y)=k!6&?greJ4{J zXGVr!`~Si7|1c*1Yv`2;@``;-2%*PP4`D;sGs|%(BC_tnQf(w^zW&ln$RA@0$nloD zUgM*v;RZoS`gQm|j4Uqm#q14{T&=TJMxmkek~O(kg{Iy)x2X zXBGqi-~tM5$e@|=uwit!cd|CJx3~Tk$12tJ?0#~f_%crN!o1TI8%dypcf?FF%>%2C zV*5hL2n8;YZ_|rbmY{avQipuV{w|)-_*#NM=1F>1e9g>ysBnlx(3kZb(e=qrRdW`Y z90@DXbc$0?KjN&+)QP=FWY7A#p1uf{QHp|#U~xii{+Pk{Va=OsKYD6qTV23+$1Q3S4RFyp*K9Rww>okx!J-i~J&ViajW%!%i8I?N!us z{NaTdUxoDPwF}%VTz%o*$ZwR}X1$q46ymFo+}%c+ZrbDPeB(eeYz%2a+sI=Do24Zo;w5UB4XCb*0ITgvJ^nEuX*P z#j*u<;fy6k=xPTl{dbVVi#ktGzo<^F7SGs%D3dRySu79#)(~i^6Maa6@HwM1BhEIl zu@bxjOANxM4aPM07a#3&>6p*|Rjha_jt54cdx2{+MkdjAp^-mZCQ&o-#8sQD$#7DlJPN zVr15m)LA)Jyk!#56rZ0q7<~I(~cwqa(y0Osg1^Zpu0MX4fBt|D`!hW)65DF?({|cXrkVM@ z4L-;98bN`z>trI1PNvrYd>!Fp5HXz1{w))mc->L-b>*Zi3OSOH*P%`cyqKF>Fq*u0NV71zW&vNKmG$A{~8S?)!ZSQ+9{;JlOBt%kN`F$niz z!^w|7&YdTAc(B;uju$-1D$9A-V8A77E^VM=@!kfp**}6S|oofd$2G=Z)kcsz^2(%Nuqqy9>d z{;6!=4nLD)as24@sIO6Gx#lL;k3!0$#pUNFrZ!&3wV8vfH344N(+!^lg{J*wKbPLa zK$`^x+#?2dEI$xj{|@8;fuz)P5P(}j(GC{?0|o-}zXINW!};IA4-Ax>g51IX?X6Np zUbY{Uo54SYF}Y{DVW7^tFp?gq?<2zuH&QQBlCb(-{J^7W)zg}lWrVN~^*sJE=yuJH zvn&1#& zB>H-e11rsk=n43`GfhH!(<6QbbJqS~Chs|J-BWOfu!&zGOIXg%?;Yww95y6L{ z)Wwjw_ZLhqe^j*8#7)qsqob7h4&gC*M zWu{4ylzJoDD%gbgbF_Jy0KU5Dl|kjSt#E;`6l|kNYW*_$#dfSZQ*l4fN=iEj2p6z) zK4~3(octCIAKCrMq%3TOt+!jVm}#w)mXKVa>5tSFsE$r%Y|;)9PU?|W5Tp8-j3Y-= zvSC_U!tyF);IypjR3}@3{X%^NTlUuT^Q`g_UJj9&ht=@G8U&=X_3=k#VJi7zPtBGe z*Y=_I1tegxHY`pePYJV4?~XZTWZ(*_s+R!62TA9)w?GcVH*cgqUH%A(Kr=I7^ns`; zy|JYh-wnagH|*o=t%n~YN*#8ur`tJq!j?$<%7U2x*#m=oo)l>-N*OnJm-IfZXx^Jk z|DmYClDe+1lg22QW`}R2ON`pI!BSADiyZ%eHj}IK2?-GfnhH&MecQR}a+X(My#yt` z$^Ar%A#14*+9D&+_x(fk^R9Y(4BA2d>4Lz1)m{3Nl@u(-5B8%>0dMe@G6J{~tumtI zc5j_P46Xty{_y+$5Id9r#r(qOd53|U)1*o|y%-G+sdX2&_mer5J*~+Xax(qnJXOA3 z9=8aL=m)BbJ(nl-M931#TsZGh&T@nfq=~}!xtUs^R?4!oteZ8nFT+ki$-wjTZ?j2C z+qcUULJ86TybiWU2jvuRTjtN4>H|Caaa!LJL(=m8Duga{-dTd{elSv@^Mm%*fsfat zXG?o($@P@){nh@&o-J0oh3liwyN)Zrjx;WvNA}^}hqIsho-NsoHOFl`Yr0ty_lKVT zUC0*iia}1+Nw_J;|LYS#qfGPpOOw1(aD%SAk)8b%awIRQTMXr^eIY z3x)GGvFnHm`Y1JE_B(r3)9**1l00+6SbhjFO6EEA{uE16*ztR9j#i2T#LFp7^SFxm zxlDJtO!sA(?r@oId6~|QI%<22awt9EDT_@t9WnY#R=i{y*!vy75@I|mm*MaLDrm@n zHEn7b4__1VP~>uqS6eCt$T*GT&AZcGsn-8~yWw07$b(?@ zn2jb$DNx1AaHPR3lc+4>MT5nuv`9QKFD3K+S$l1N_`nrsQ z@m0|BL}f^^onz1-37o)Fp=UhO=~BTqHQKgB`iJ|2h)Ls z#AQf#>qC+&TTZpIQRDd-b>qb7q~NA9NB#HvrH2v63OM=^QmhJ9GvQ2s!p5<$6U{`Y1l!7Gf{Jy;4dVSaG=@i;lt+zKD&rGFMf|+GB+1-&m@mp0+I)a2q(Z)!w;9z`w zvU|LF5lOK+X-Qni6gGC4hVhoq{59>tyn#no0Kw}XaOLRe3(_8bNn?xR<|mKr z^?A%<)5I=+z+!kb_A4Ca+d4hdi>)%*jJJ&V zV@Pf>oS$UxE@Zf=&wWv+-a52xuIMBi)7IeDRuiIA4$R5MfThObLeQF2F?$HyiDbDM z+37FD8x^g$YfUI#JpWGWf5X%!PFSIj2}QhyB?AHlfgXl7V!9X{G9Y*wm}ZusRY+*f z0@<_cNCpmHf9@Cc-1%BC!A0#WQ?$dX>Lj>*?U)}m%tuH8Y+byur&CGeoRpjK@Anj) z%x=yfCmYz^+WKjYDx@bm6^ zV;kmj_#B_DV+woEP1tX8eP!>ZtvGX3;BN`t$f{;e*V;arYJ1Hu9?p<(@Ti&agQV62 zZjWsjKAMhZoK{39398Ky%V$s4cYn&NEOS?89(r`~JJ9kL@t;B%+I|3!H#7inOZ=-8 z_dA5SSeV+HGXCEGj$Q}aU+nNX(K?}rc-ig*vb&CMQTdRz<|66mL)qO{$=LhSi$B{T zNxk0)FeG+RXd#DL)jKc^Fk$q9353yNM5E*QuBnvW;~x`qjzV4URVDHcSF$8)L9pM&C64t_aUW z3nJ+)kIVI5ht+&9-d%+?$0yNC>os<&8KTNH=pY7+sFrs*GX(UlYMEAwOxs}}UC-5}GMS`ULc`wWz7Oh%m1P5NjSr_q5aLk(MRqmNhx z0ALhS@#GP&u~_+p$)qkA@=bDp_F-~lQvEnGY5r>}MQl{+Yc+R>7s>CL7J7%`G~5DB zjcldnw^>@@OtQLm=N%~y1BQps~SmIlo*~)^18jQ@qOGc zSz&y>x!X(>EK)00qdI zF;@`1HH}?uz2rY9e)2B{mY$B#q#x-HZ^dq}w^) zvG0oQTGbkcY)jsb8O{?;WMtKHN|Par-lm46EGHj!OpMbKy`*~*AF&S)8=q}7(*A@E zzn0K4Y}g?&Sp(76+}A0KRTs07fZyzLL_8p85=-DhhCY$Skx0%`M-&AirM$6h^bPWd z=@nl75eX92JoZuBE8R6;e$*u}CwBP}pcsF;vm{-Oky%$PJdg1A&n&itx(5qe{pkcB zZl`4q38JZaM2w?5c!_xFc52f%&zE+rQENvy+$d(*O}5Kh6>Gb*tDR@5ox)&fO|MUq zY-`Qk9V0z3sUlE+6y8FKEz9VA-^@~bm-rq@ZWR$6gLETmikcI0hbf`0OM1TwM%yd3 z73;`b&wKF1*`OS?|>1?fCD!zg((R|0$249~F8@Dw$tY`+o3O|oGap$7-d=OP#I}*!ocIw&AD|M!?B;Z)G5Bj!TGG+;Q4d?D6odg%qn8Ujp5 z_3S~Sb9&8!>UamoE3W!o>a-I$h3UuXm=&dZQ}V^fc8Q#oBb>^koVoJ~V5)h!!5 zQfk`_rx|+J2zY^pIs)mY8TB3?LNUzE3z{sqwNS9LOKndUtzsSCt=qc$#_@jmXceS2 zo6d83N!{iNH-2>t;d!yj=crILnHS7z`#yI^R*_0|!C!MDV9ZLa`D`nz@gc0JmcWJ9 z@RO2N@S}(lqD+Xy_iD@qLoNF2KBuy|GQPkVaS2CmImH`jheBbjdG0Pkect`p)Dn(V z2|5NApJ4sz(EWxdr_Vh5im^8xc|60{{NRP>wHi2V6<-vpCM%nuLvrUV(KD8HN+v5@ z_dA4Z@HjGpBs<+pdxYB-5Un01@-&Xz;DxIS$L!hQFLPBap)cJEh#HAZO%W>jieksG zR^QsrCI9n!Q@C^9*z^?ufCE}vfTH=Y^`^6nr;VxeuSMpx=9gcU#!jpqUYJ+!TrP}< z>kvkxJ_5nuZK5C(z&pO7nF3|wp~OqhFd-wsm-!DqZeL+6h$7p5^?};PxFaV}NuVE8 zz;8xeAViBI8$Zds-IBx?pmjD|M$uO4A=}929wOyKrQU3S@$n^azZ;@K-=a1POoXYD z3doKTc(Twc#URsk7Dq0yt|sdKAO^m$j%~+L!l|9`^++BsoSlc;D@YdKE;oejZmw*NsBbt zYB*X{*=kaBfD%Dqk2OQ*Mx8Gt!d#m~-+GK|5RTLvli1J*0}&m#(x4$6*g@_xH#w#P zRq4wngzcm%nmY4~EiKGAh=HW-8yz#2+}SVGCwU=F0~#a`#~X6g3W|nH7+}0eCtj#0`egTt|-?!CY1U! zYY+3(=Bz~{dtRdG)cd#$(J5OiLdN7$*_GKX>wU>o6-0h@Pqkp8;belDE3kg6Gqi8>9p8!G?%#kZN7t9mlgb|(a&-||3F}@a-4UP%ZzK3h(^)kc z`Z{EmrlEj&7N39=iqF2WB8}aUUHv1FQIK!^JiA5jX!R1w{Y&fT4}@7v(6lsFEpZo! z8!3djunw}T_u!rR=<+&fdZDInVau+UTv2O9cMs{LB(cTk2~qXY89WE41WSzJ3jaq zzrsEY->j1&5lp0eFSiap#OOx1xCxs(Jqq`%R+CwqTc2p^Y6t5?!F5Iub=w&ucVwUQ zIebu9a$S1=qwi6nYYvgearGS*zn?cwzc9R)!@BKYo!+NbPRo-Xp|4p?BY4%gp$jU+ zuqnt~hMaQW7kLkN7=N#jUJ`@`W4ancpmHfA5&(eyhd;6~bTTzjb#bz^GymCD#cmrKE_z_PeDvWqy-eF0PYKQs4KtPJCxw*w4_^z?>b~DUyT0OKmag$Y9t~ zHhq44>@6@T{oXGLvUOW#TSX26Z5`kkKdjDhRNS+jdEP@Vw7^tn6oT%J%jZOZUs9t_ zp@V1Q6Os*OZOkF5T-d9*&Cur%4n>7zd{K1Bo=S@(qUTH>HKKK}xMhyT8Jzp6 zp<;8U1o?PjVYy~%n>3ly%FypZ(PaV-+5)90g8nUyiH)w!wp9dAYA+jQN##&i+0y)S zJqsft9&;7JoSbDRXW>ova6XUl@&#FkVLGB^TX{?Cws7mB%Cy=B&F%H)={G;+KbtZX zzEd&0!y?cf0@kCvGGN^$%1Uh*wFa;X+b;|uQ#BC~8e)%9?gIur`)|6|8|*c((77%G zQ5Qv!jXiem#=@{^+;lAti^0jI?nT=94DZM1knl;ucoD!{-F3eUN%}2paUl-ssa6^W zhLOHkCMH9))XbG#$S|NJz;BLXIekN)$b;B}ZBl!w%1OEH5)Rwkr4VIJ-vg{kIr5>N zY>h+zl&8JWq2+K9*QdwU2;(yL6npN704053hnH7pPG#68#J4`a?9@b_2-G@AUfroE zQ88B#p8MdkyIMYj4RB)7pM(XN{9yCzz(x)~#MNOmJN%jGaIlISO4dx{u6vJEr+`q| zaX85tR2sG?O58^>VDhT3^MYKYt!m%B>yx>l(B3V4fwj@x^|TA`MUS8WpM&q^U@KXOvATPc$1aoR<>vQ(2a}=ACKuad^Cf2_*Jx@4&M6^ z@!1u0jZ2+P1WR+r83^P{aSw?fp7|RI@4H`RTl*lrDz+UPJ->qer^3OPu`jM=zbFtY z0O0k%X21aidKXhCRZ|z2U+8Cian8(OWNCMh)}`1hgg)3M8I{Dex_)Fp8bp~pQ;K&g ziOt5$Oj%9Il!gs$>QKoW#M^E4Z+?OiER9~KH(Kk7W>1>fd?(Vk!22LbeDw|ZVa8;EUGD2M_c?|*y{+e&lN|n%{H;8 z!liO}T`8};WQef|wmf3%bKjq6!q_ZKxt!0kUdYxnqx9Y~3W)34zviBAdA|{M6NAJ3 zsJPv4aW;b)s#zAjbuFQc_E_(X|2$Q{zeB6hg|@5jIDAzRb9}RK(|YZi#X1w z(v#Y|TFe=@AY6%-g2+5@r~__MN!yl+N$rll`En#4{{T1eqO+k3zQ{k#_a<(PQT=u_PW1 zm%KBWPa_}(>U3ASpWLt|P&Zjti4yEkD`7HtSRez340a&n3vt0h1Ds)0;v3WH?^PK_ z((|kzz>$2O5zGy!N=cm41(MzUFC z$3zz7@Y?Qe#uwHV_~(Wdw9rE|U2ySc{rrg}Io;*eI8iV4n1vksaSY)~ht=Qsrc^lT z(T^VQeWC8!lW!?cnUTDFTTRwpQfbd#v=Ds&A&0h_8fE+?4AIRA*hql9R5vS)=c4Pm z^F}W=j0)+I$t9?}+7Ua>F4m>DWMLxbiATmO&h`xfh0f0H_iJfRazp#rX2^b2fg6ES z(9-80?6atGoi7PwpXeZJ7v;b0)6l`;f946AvOl)0gdRH}Gl+?lYVRAl9>@%&LHE?l zL!X8Smy-4(YHx@LnQJ=OrCEd0Bn-e>W6t0-a?_OO?2cF*d+74@Bh=M9*41=Lt4Hia zJJ+U$yP%w&wWoFb-=)XyPF^9Qvd ze8%$)--4&X9W$F)*kaf{fMcRj*&C+XZ>5)jcewje9&qT!>-h!Gh*d$mw`Y%AC^%Xb zpO|^9{clq43fQSPCY4U@>?f(YF)*g8zVeaFLhI3R+s-Wb_fbIaL%orp$bsI&%+p_h zvl-EGZQPkuk`iyy_q0vTzd^w9%93%3qw_GTT;bplJv1sVB=Qq@;_>D66?{@kK>X=I zRkC7ftG|D1u)4nV9XgtrE{n{Lj(aR81B3hy`qW#7k14u+JNjpxYX91-_jjVx(MgFD zXiS9}=>4vZwq{sPp_|69=XpBE#D-sD9TsjZbL~9Shc8J+32x|T6|=iSo*s_}6|1*3 z8_v{+6?xOCZy!Ef+JnByzdS7%I6WwT{^#!4e}B1uAOE3OR!R1+3jW%+_iw>p#}W`c z{;P}crQpk6rawi;KrHu*&Zd{bf9;?6QxpJ5hWjo2|LLZ9sploB{->rPg#Wz6-zfH% zT3+%Re`<*W(Hx+mykt3EDtK9I`BMQG-fsneR$g9;zFZdlDLO>-FVUBaq?a0Ark8(e z=p}tQ2mhFCzEt=!Tl-UCKPY_#O~;E|?dAFJ-0h{(za|BLsssS)DFA@Kr3f#@|LU{< hTU-T{Q~pQ%4-c*+3kkA80005>qXd~TKJBlw{|E77&n5r> literal 0 HcmV?d00001 diff --git a/applications/plugins/unitemp/unitemp.c b/applications/plugins/unitemp/unitemp.c new file mode 100644 index 000000000..ce1cd1dc9 --- /dev/null +++ b/applications/plugins/unitemp/unitemp.c @@ -0,0 +1,310 @@ +/* + Unitemp - Universal temperature reader + Copyright (C) 2022 Victor Nikitchuk (https://github.com/quen0n) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +#include "unitemp.h" +#include "interfaces/SingleWireSensor.h" +#include "Sensors.h" +#include "./views/UnitempViews.h" + +#include +#include + +/* Переменные */ +//Данные приложения +Unitemp* app; + +void uintemp_celsiumToFarengate(Sensor* sensor) { + sensor->temp = sensor->temp * (9.0 / 5.0) + 32; +} + +void unitemp_pascalToMmHg(Sensor* sensor) { + sensor->pressure = sensor->pressure * 0.007500638; +} +void unitemp_pascalToKPa(Sensor* sensor) { + sensor->pressure = sensor->pressure / 1000.0f; +} +void unitemp_pascalToInHg(Sensor* sensor) { + sensor->pressure = sensor->pressure * 0.0002953007; +} + +bool unitemp_saveSettings(void) { + //Выделение памяти для потока + app->file_stream = file_stream_alloc(app->storage); + + //Переменная пути к файлу + FuriString* filepath = furi_string_alloc(); + //Составление пути к файлу + furi_string_printf(filepath, "%s/%s", APP_PATH_FOLDER, APP_FILENAME_SETTINGS); + //Создание папки плагина + storage_common_mkdir(app->storage, APP_PATH_FOLDER); + //Открытие потока + if(!file_stream_open( + app->file_stream, furi_string_get_cstr(filepath), FSAM_READ_WRITE, FSOM_CREATE_ALWAYS)) { + FURI_LOG_E( + APP_NAME, + "An error occurred while saving the settings file: %d", + file_stream_get_error(app->file_stream)); + //Закрытие потока и освобождение памяти + file_stream_close(app->file_stream); + stream_free(app->file_stream); + return false; + } + + //Сохранение настроек + stream_write_format( + app->file_stream, "INFINITY_BACKLIGHT %d\n", app->settings.infinityBacklight); + stream_write_format(app->file_stream, "TEMP_UNIT %d\n", app->settings.temp_unit); + stream_write_format(app->file_stream, "PRESSURE_UNIT %d\n", app->settings.pressure_unit); + + //Закрытие потока и освобождение памяти + file_stream_close(app->file_stream); + stream_free(app->file_stream); + + FURI_LOG_I(APP_NAME, "Settings have been successfully saved"); + return true; +} + +bool unitemp_loadSettings(void) { +#ifdef UNITEMP_DEBUG + FURI_LOG_D(APP_NAME, "Loading settings..."); +#endif + + //Выделение памяти на поток + app->file_stream = file_stream_alloc(app->storage); + + //Переменная пути к файлу + FuriString* filepath = furi_string_alloc(); + //Составление пути к файлу + furi_string_printf(filepath, "%s/%s", APP_PATH_FOLDER, APP_FILENAME_SETTINGS); + + //Открытие потока к файлу настроек + if(!file_stream_open( + app->file_stream, furi_string_get_cstr(filepath), FSAM_READ_WRITE, FSOM_OPEN_EXISTING)) { + //Сохранение настроек по умолчанию в случае отсутствия файла + if(file_stream_get_error(app->file_stream) == FSE_NOT_EXIST) { + FURI_LOG_W(APP_NAME, "Missing settings file. Setting defaults and saving..."); + //Закрытие потока и освобождение памяти + file_stream_close(app->file_stream); + stream_free(app->file_stream); + //Сохранение стандартного конфига + unitemp_saveSettings(); + return false; + } else { + FURI_LOG_E( + APP_NAME, + "An error occurred while loading the settings file: %d. Standard values have been applied", + file_stream_get_error(app->file_stream)); + //Закрытие потока и освобождение памяти + file_stream_close(app->file_stream); + stream_free(app->file_stream); + return false; + } + } + + //Вычисление размера файла + uint8_t file_size = stream_size(app->file_stream); + //Если файл пустой, то: + if(file_size == (uint8_t)0) { + FURI_LOG_W(APP_NAME, "Settings file is empty"); + //Закрытие потока и освобождение памяти + file_stream_close(app->file_stream); + stream_free(app->file_stream); + //Сохранение стандартного конфига + unitemp_saveSettings(); + return false; + } + //Выделение памяти под загрузку файла + uint8_t* file_buf = malloc(file_size); + //Опустошение буфера файла + memset(file_buf, 0, file_size); + //Загрузка файла + if(stream_read(app->file_stream, file_buf, file_size) != file_size) { + //Выход при ошибке чтения + FURI_LOG_E(APP_NAME, "Error reading settings file"); + //Закрытие потока и освобождение памяти + file_stream_close(app->file_stream); + stream_free(app->file_stream); + free(file_buf); + return false; + } + //Построчное чтение файла + //Указатель на начало строки + FuriString* file = furi_string_alloc_set_str((char*)file_buf); + //Сколько байт до конца строки + size_t line_end = 0; + + while(line_end != STRING_FAILURE && line_end != (size_t)(file_size - 1)) { + char buff[20] = {0}; + sscanf(((char*)(file_buf + line_end)), "%s", buff); + + if(!strcmp(buff, "INFINITY_BACKLIGHT")) { + //Чтение значения параметра + int p = 0; + sscanf(((char*)(file_buf + line_end)), "INFINITY_BACKLIGHT %d", &p); + app->settings.infinityBacklight = p; + } else if(!strcmp(buff, "TEMP_UNIT")) { + //Чтение значения параметра + int p = 0; + sscanf(((char*)(file_buf + line_end)), "\nTEMP_UNIT %d", &p); + app->settings.temp_unit = p; + } else if(!strcmp(buff, "PRESSURE_UNIT")) { + //Чтение значения параметра + int p = 0; + sscanf(((char*)(file_buf + line_end)), "\nPRESSURE_UNIT %d", &p); + app->settings.pressure_unit = p; + } else { + FURI_LOG_W(APP_NAME, "Unknown settings parameter: %s", buff); + } + + //Вычисление конца строки + line_end = furi_string_search_char(file, '\n', line_end + 1); + } + free(file_buf); + file_stream_close(app->file_stream); + stream_free(app->file_stream); + + FURI_LOG_I(APP_NAME, "Settings have been successfully loaded"); + return true; +} + +/** + * @brief Выделение места под переменные плагина + * + * @return true Если всё прошло успешно + * @return false Если в процессе загрузки произошла ошибка + */ +static bool unitemp_alloc(void) { + //Выделение памяти под данные приложения + app = malloc(sizeof(Unitemp)); + //Разрешение работы приложения + app->processing = true; + + //Открытие хранилища (?) + app->storage = furi_record_open(RECORD_STORAGE); + + //Уведомления + app->notifications = furi_record_open(RECORD_NOTIFICATION); + + //Установка значений по умолчанию + app->settings.infinityBacklight = true; //Подсветка горит всегда + app->settings.temp_unit = UT_TEMP_CELSIUS; //Единица измерения температуры - градусы Цельсия + app->settings.pressure_unit = UT_PRESSURE_MM_HG; //Единица измерения давления - мм рт. ст. + + app->gui = furi_record_open(RECORD_GUI); + //Диспетчер окон + app->view_dispatcher = view_dispatcher_alloc(); + + app->sensors = NULL; + + app->buff = malloc(BUFF_SIZE); + + unitemp_General_alloc(); + + unitemp_MainMenu_alloc(); + unitemp_Settings_alloc(); + unitemp_SensorsList_alloc(); + unitemp_SensorEdit_alloc(); + unitemp_SensorNameEdit_alloc(); + unitemp_SensorActions_alloc(); + unitemp_widgets_alloc(); + + //Всплывающее окно + app->popup = popup_alloc(); + view_dispatcher_add_view(app->view_dispatcher, UnitempViewPopup, popup_get_view(app->popup)); + + view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen); + + return true; +} + +/** + * @brief Освыбождение памяти после работы приложения + */ +static void unitemp_free(void) { + popup_free(app->popup); + unitemp_widgets_free(); + + unitemp_SensorActions_free(); + unitemp_SensorNameEdit_free(); + unitemp_SensorEdit_free(); + unitemp_SensorsList_free(); + unitemp_Settings_free(); + unitemp_MainMenu_free(); + unitemp_General_free(); + + free(app->buff); + + view_dispatcher_free(app->view_dispatcher); + furi_record_close(RECORD_GUI); + //Очистка датчиков + //Высвыбождение данных датчиков + unitemp_sensors_free(); + free(app->sensors); + + //Закрытие уведомлений + furi_record_close(RECORD_NOTIFICATION); + //Закрытие хранилища + furi_record_close(RECORD_STORAGE); + //Удаление в самую последнюю очередь + free(app); +} + +/** + * @brief Точка входа в приложение + * + * @return Код ошибки + */ +int32_t unitemp_app() { + //Выделение памяти под переменные + //Выход если произошла ошибка + if(unitemp_alloc() == false) { + //Освобождение памяти + unitemp_free(); + //Выход + return 0; + } + + //Загрузка настроек из SD-карты + unitemp_loadSettings(); + //Применение настроек + if(app->settings.infinityBacklight == true) { + //Постоянное свечение подсветки + notification_message(app->notifications, &sequence_display_backlight_enforce_on); + } + app->settings.lastOTGState = furi_hal_power_is_otg_enabled(); + //Загрузка датчиков из SD-карты + unitemp_sensors_load(); + //Инициализация датчиков + unitemp_sensors_init(); + + unitemp_General_switch(); + + while(app->processing) { + if(app->sensors_ready) unitemp_sensors_updateValues(); + furi_delay_ms(100); + } + + //Деинициализация датчиков + unitemp_sensors_deInit(); + //Автоматическое управление подсветкой + if(app->settings.infinityBacklight == true) + notification_message(app->notifications, &sequence_display_backlight_enforce_auto); + //Освобождение памяти + unitemp_free(); + //Выход + return 0; +} \ No newline at end of file diff --git a/applications/plugins/unitemp/unitemp.h b/applications/plugins/unitemp/unitemp.h new file mode 100644 index 000000000..144780968 --- /dev/null +++ b/applications/plugins/unitemp/unitemp.h @@ -0,0 +1,148 @@ +/* + Unitemp - Universal temperature reader + Copyright (C) 2022 Victor Nikitchuk (https://github.com/quen0n) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +#ifndef UNITEMP +#define UNITEMP + +/* Подключение стандартных библиотек */ + +/* Подключение API Flipper Zero */ +//Файловый поток +#include +//Экран +#include +#include +#include +#include +//Уведомления +#include +#include + +/* Внутренние библиотеки */ +//Интерфейсы подключения датчиков +#include "Sensors.h" + +/* Объявление макроподстановок */ +//Имя приложения +#define APP_NAME "Unitemp" +//Путь хранения файлов плагина +#define APP_PATH_FOLDER "/ext/unitemp" +//Имя файла с настройками +#define APP_FILENAME_SETTINGS "settings.cfg" +//Имя файла с датчиками +#define APP_FILENAME_SENSORS "sensors.cfg" +//Версия приложения +#define UNITEMP_APP_VER "1.0" + +//Размер буффера текста +#define BUFF_SIZE 32 + +#define UNITEMP_DEBUG + +/* Объявление перечислений */ +//Единицы измерения температуры +typedef enum { UT_TEMP_CELSIUS, UT_TEMP_FAHRENHEIT, UT_TEMP_COUNT } tempMeasureUnit; +//Единицы измерения давления +typedef enum { + UT_PRESSURE_MM_HG, + UT_PRESSURE_IN_HG, + UT_PRESSURE_KPA, + + UT_PRESSURE_COUNT +} pressureMeasureUnit; +/* Объявление структур */ +//Настройки плагина +typedef struct { + //Бесконечная работа подсветки + bool infinityBacklight; + //Единица измерения температуры + tempMeasureUnit temp_unit; + //Единица измерения давления + pressureMeasureUnit pressure_unit; + //Последнее состояние OTG + bool lastOTGState; +} UnitempSettings; + +//Основная структура плагина +typedef struct { + //Система + bool processing; //Флаг работы приложения. При ложном значении приложение закрывается + bool sensors_ready; //Флаг готовности датчиков к опросу + //Основные настройки + UnitempSettings settings; + //Массив указателей на датчики + Sensor** sensors; + //Количество загруженных датчиков + uint8_t sensors_count; + //SD-карта + Storage* storage; //Хранилище + Stream* file_stream; //Файловый поток + + //Экран + Gui* gui; + ViewDispatcher* view_dispatcher; + NotificationApp* notifications; + Widget* widget; + Popup* popup; + //Буффер для различного текста + char* buff; +} Unitemp; + +/* Объявление прототипов функций */ + +/** + * @brief Перевод значения температуры датчика из Цельсия в Фаренгейты + * + * @param sensor Указатель на датчик + */ +void uintemp_celsiumToFarengate(Sensor* sensor); + +/** + * @brief Конвертация давления из паскалей в мм рт.ст. + * + * @param sensor Указатель на датчик + */ +void unitemp_pascalToMmHg(Sensor* sensor); + +/** + * @brief Конвертация давления из паскалей в килопаскали + * + * @param sensor Указатель на датчик + */ +void unitemp_pascalToKPa(Sensor* sensor); +/** + * @brief Конвертация давления из паскалей в дюйм рт.ст. + * + * @param sensor Указатель на датчик + */ +void unitemp_pascalToInHg(Sensor* sensor); + +/** + * @brief Сохранение настроек на SD-карту + * + * @return Истина если сохранение успешное + */ +bool unitemp_saveSettings(void); +/** + * @brief Загрузка настроек с SD-карты + * + * @return Истина если загрузка успешная + */ +bool unitemp_loadSettings(void); + +extern Unitemp* app; +#endif \ No newline at end of file diff --git a/applications/plugins/unitemp/views/General_view.c b/applications/plugins/unitemp/views/General_view.c new file mode 100644 index 000000000..e21b04de2 --- /dev/null +++ b/applications/plugins/unitemp/views/General_view.c @@ -0,0 +1,562 @@ +/* + Unitemp - Universal temperature reader + Copyright (C) 2022 Victor Nikitchuk (https://github.com/quen0n) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +#include "UnitempViews.h" +#include "unitemp_icons.h" + +#include + +static View* view; + +typedef enum general_views { + G_NO_SENSORS_VIEW, //Нет датчиков + G_LIST_VIEW, //Вид в ввиде списка + G_CAROUSEL_VIEW, //Карусель +} general_view; + +typedef enum carousel_info { + CAROUSEL_VALUES, //Отображение значений датчиков + CAROUSEL_INFO, //Отображение информации о датчике +} carousel_info; + +static general_view current_view; + +carousel_info carousel_info_selector = CAROUSEL_VALUES; +uint8_t generalview_sensor_index = 0; + +static void _draw_temperature(Canvas* canvas, Sensor* sensor, uint8_t x, uint8_t y, Color color) { + //Рисование рамки + canvas_draw_rframe(canvas, x, y, 54, 20, 3); + + if(color == ColorBlack) { + canvas_draw_rbox(canvas, x, y, 54, 19, 3); + canvas_invert_color(canvas); + } else { + canvas_draw_rframe(canvas, x, y, 54, 19, 3); + } + + int8_t temp_dec = abs((int16_t)(sensor->temp * 10) % 10); + + //Рисование иконки + canvas_draw_icon( + canvas, + x + 3, + y + 3, + (app->settings.temp_unit == UT_TEMP_CELSIUS ? &I_temp_C_11x14 : &I_temp_F_11x14)); + + if((int16_t)sensor->temp == -128 || sensor->status == UT_SENSORSTATUS_TIMEOUT) { + canvas_set_font(canvas, FontBigNumbers); + canvas_draw_str_aligned(canvas, x + 27, y + 10, AlignCenter, AlignCenter, "--"); + canvas_set_font(canvas, FontPrimary); + canvas_draw_str_aligned(canvas, x + 50, y + 10 + 3, AlignRight, AlignCenter, ". -"); + if(color == ColorBlack) canvas_invert_color(canvas); + return; + } + + //Целая часть температуры + //Костыль для отображения знака числа меньше 0 + uint8_t offset = 0; + if(sensor->temp < 0 && sensor->temp > -1) { + app->buff[0] = '-'; + offset = 1; + } + snprintf((char*)(app->buff + offset), BUFF_SIZE, "%d", (int8_t)sensor->temp); + canvas_set_font(canvas, FontBigNumbers); + canvas_draw_str_aligned( + canvas, + x + 27 + ((sensor->temp <= -10 || sensor->temp > 99) ? 5 : 0), + y + 10, + AlignCenter, + AlignCenter, + app->buff); + //Печать дробной части температуры в диапазоне от -9 до 99 (когда два знака в числе) + if(sensor->temp > -10 && sensor->temp <= 99) { + uint8_t int_len = canvas_string_width(canvas, app->buff); + snprintf(app->buff, BUFF_SIZE, ".%d", temp_dec); + canvas_set_font(canvas, FontPrimary); + canvas_draw_str(canvas, x + 27 + int_len / 2 + 2, y + 10 + 7, app->buff); + } + if(color == ColorBlack) canvas_invert_color(canvas); +} + +static void _draw_humidity(Canvas* canvas, Sensor* sensor, const uint8_t pos[2]) { + //Рисование рамки + canvas_draw_rframe(canvas, pos[0], pos[1], 54, 20, 3); + canvas_draw_rframe(canvas, pos[0], pos[1], 54, 19, 3); + + //Рисование иконки + canvas_draw_icon(canvas, pos[0] + 3, pos[1] + 2, &I_hum_9x15); + + //Целая часть влажности + snprintf(app->buff, BUFF_SIZE, "%d", (uint8_t)sensor->hum); + canvas_set_font(canvas, FontBigNumbers); + canvas_draw_str_aligned(canvas, pos[0] + 27, pos[1] + 10, AlignCenter, AlignCenter, app->buff); + uint8_t int_len = canvas_string_width(canvas, app->buff); + //Единица измерения + canvas_set_font(canvas, FontPrimary); + canvas_draw_str(canvas, pos[0] + 27 + int_len / 2 + 4, pos[1] + 10 + 7, "%"); +} + +static void _draw_pressure(Canvas* canvas, Sensor* sensor) { + const uint8_t x = 29, y = 39; + //Рисование рамки + canvas_draw_rframe(canvas, x, y, 69, 20, 3); + canvas_draw_rframe(canvas, x, y, 69, 19, 3); + + //Рисование иконки + canvas_draw_icon(canvas, x + 3, y + 4, &I_pressure_7x13); + + int16_t press_int = sensor->pressure; + int8_t press_dec = (int16_t)(sensor->temp * 10) % 10; + + //Целая часть давления + snprintf(app->buff, BUFF_SIZE, "%d", press_int); + canvas_set_font(canvas, FontBigNumbers); + canvas_draw_str_aligned( + canvas, x + 27 + ((press_int > 99) ? 5 : 0), y + 10, AlignCenter, AlignCenter, app->buff); + //Печать дробной части давления в диапазоне от 0 до 99 (когда два знака в числе) + if(press_int <= 99) { + uint8_t int_len = canvas_string_width(canvas, app->buff); + snprintf(app->buff, BUFF_SIZE, ".%d", press_dec); + canvas_set_font(canvas, FontPrimary); + canvas_draw_str(canvas, x + 27 + int_len / 2 + 2, y + 10 + 7, app->buff); + } + canvas_set_font(canvas, FontSecondary); + //Единица измерения + if(app->settings.pressure_unit == UT_PRESSURE_MM_HG) { + canvas_draw_icon(canvas, x + 50, y + 2, &I_mm_hg_15x15); + } else if(app->settings.pressure_unit == UT_PRESSURE_IN_HG) { + canvas_draw_icon(canvas, x + 50, y + 2, &I_in_hg_15x15); + } else if(app->settings.pressure_unit == UT_PRESSURE_KPA) { + canvas_draw_str(canvas, x + 52, y + 13, "kPa"); + } +} + +static void _draw_singleSensor(Canvas* canvas, Sensor* sensor, const uint8_t pos[2], Color color) { + canvas_set_font(canvas, FontPrimary); + + const uint8_t max_width = 56; + + char sensor_name[12] = {0}; + memcpy(sensor_name, sensor->name, 10); + + if(canvas_string_width(canvas, sensor_name) > max_width) { + uint8_t i = 10; + while((canvas_string_width(canvas, sensor_name) > max_width - 6) && (i != 0)) { + sensor_name[i--] = '\0'; + } + sensor_name[++i] = '.'; + sensor_name[++i] = '.'; + } + + canvas_draw_str_aligned( + canvas, pos[0] + 27, pos[1] + 3, AlignCenter, AlignCenter, sensor_name); + _draw_temperature(canvas, sensor, pos[0], pos[1] + 8, color); +} + +static void _draw_view_noSensors(Canvas* canvas) { + canvas_draw_icon(canvas, 7, 17, &I_sherlok_53x45); + //Рисование рамки + canvas_draw_rframe(canvas, 0, 0, 128, 63, 7); + canvas_draw_rframe(canvas, 0, 0, 128, 64, 7); + canvas_set_font(canvas, FontPrimary); + canvas_draw_str_aligned(canvas, 63, 10, AlignCenter, AlignCenter, "No sensors found"); + canvas_set_font(canvas, FontSecondary); + const uint8_t x = 65, y = 32; + canvas_draw_rframe(canvas, x - 4, y - 11, 54, 33, 3); + canvas_draw_rframe(canvas, x - 4, y - 11, 54, 34, 3); + canvas_draw_str(canvas, x, y, "To add the"); + canvas_draw_str(canvas, x, y + 9, "new sensor"); + canvas_draw_str(canvas, x, y + 18, "press OK"); + + canvas_draw_icon(canvas, x + 37, y + 10, &I_Ok_btn_9x9); +} + +static void _draw_view_sensorsList(Canvas* canvas) { + //Текущая страница + uint8_t page = generalview_sensor_index / 4; + //Количество датчиков, которые будут отображаться на странице + uint8_t page_sensors_count; + if((unitemp_sensors_getActiveCount() - page * 4) / 4) { + page_sensors_count = 4; + } else { + page_sensors_count = (unitemp_sensors_getActiveCount() - page * 4) % 4; + } + + //Количество страниц + uint8_t pages = + unitemp_sensors_getActiveCount() / 4 + (unitemp_sensors_getActiveCount() % 4 ? 1 : 0); + + //Стрелка влево + if(page > 0) { + canvas_draw_icon(canvas, 2, 32, &I_ButtonLeft_4x7); + } + //Стрелка вправо + if(pages > 0 && page < pages - 1) { + canvas_draw_icon(canvas, 122, 32, &I_ButtonRight_4x7); + } + + const uint8_t value_positions[][4][2] = { + {{36, 18}}, //1 датчик + {{7, 18}, {67, 18}}, //2 датчика + {{7, 3}, {67, 3}, {37, 33}}, //3 датчика + {{7, 3}, {67, 3}, {7, 33}, {67, 33}}}; //4 датчика + //Рисование рамки + canvas_draw_rframe(canvas, 0, 0, 128, 63, 7); + canvas_draw_rframe(canvas, 0, 0, 128, 64, 7); + for(uint8_t i = 0; i < page_sensors_count; i++) { + _draw_singleSensor( + canvas, + unitemp_sensor_getActive(page * 4 + i), + value_positions[page_sensors_count - 1][i], + ColorWhite); + } +} + +static void _draw_carousel_values(Canvas* canvas) { + UnitempStatus sensor_status = unitemp_sensor_getActive(generalview_sensor_index)->status; + if(sensor_status == UT_SENSORSTATUS_ERROR || sensor_status == UT_SENSORSTATUS_TIMEOUT) { + const Icon* frames[] = { + &I_flipper_happy_60x38, &I_flipper_happy_2_60x38, &I_flipper_sad_60x38}; + canvas_draw_icon(canvas, 34, 23, frames[furi_get_tick() % 2250 / 750]); + + canvas_set_font(canvas, FontSecondary); + if(unitemp_sensor_getActive(generalview_sensor_index)->type->interface == &SINGLE_WIRE) { + snprintf( + app->buff, + BUFF_SIZE, + "Waiting for module on pin %d", + ((SingleWireSensor*)unitemp_sensor_getActive(generalview_sensor_index)->instance) + ->gpio->num); + } + if(unitemp_sensor_getActive(generalview_sensor_index)->type->interface == &ONE_WIRE) { + snprintf( + app->buff, + BUFF_SIZE, + "Waiting for module on pin %d", + ((OneWireSensor*)unitemp_sensor_getActive(generalview_sensor_index)->instance) + ->bus->gpio->num); + } + if(unitemp_sensor_getActive(generalview_sensor_index)->type->interface == &I2C) { + snprintf(app->buff, BUFF_SIZE, "Waiting for module on I2C pins"); + } + canvas_draw_str_aligned(canvas, 64, 19, AlignCenter, AlignCenter, app->buff); + return; + } + + static const uint8_t temp_positions[3][2] = {{37, 23}, {37, 16}, {9, 16}}; + static const uint8_t hum_positions[2][2] = {{37, 38}, {65, 16}}; + //Селектор значений для отображения + switch(unitemp_sensor_getActive(generalview_sensor_index)->type->datatype) { + case UT_DATA_TYPE_TEMP: + _draw_temperature( + canvas, + unitemp_sensor_getActive(generalview_sensor_index), + temp_positions[0][0], + temp_positions[0][1], + ColorWhite); + break; + case UT_DATA_TYPE_TEMP_HUM: + _draw_temperature( + canvas, + unitemp_sensor_getActive(generalview_sensor_index), + temp_positions[1][0], + temp_positions[1][1], + ColorWhite); + _draw_humidity( + canvas, unitemp_sensor_getActive(generalview_sensor_index), hum_positions[0]); + break; + case UT_DATA_TYPE_TEMP_PRESS: + _draw_temperature( + canvas, + unitemp_sensor_getActive(generalview_sensor_index), + temp_positions[1][0], + temp_positions[1][1], + ColorWhite); + _draw_pressure(canvas, unitemp_sensor_getActive(generalview_sensor_index)); + break; + case UT_DATA_TYPE_TEMP_HUM_PRESS: + _draw_temperature( + canvas, + unitemp_sensor_getActive(generalview_sensor_index), + temp_positions[2][0], + temp_positions[2][1], + ColorWhite); + _draw_humidity( + canvas, unitemp_sensor_getActive(generalview_sensor_index), hum_positions[1]); + _draw_pressure(canvas, unitemp_sensor_getActive(generalview_sensor_index)); + break; + } +} +static void _draw_carousel_info(Canvas* canvas) { + canvas_set_font(canvas, FontPrimary); + canvas_draw_str(canvas, 10, 23, "Type:"); + + if(unitemp_sensor_getActive(generalview_sensor_index)->type->interface == &ONE_WIRE) { + OneWireSensor* s = unitemp_sensor_getActive(generalview_sensor_index)->instance; + canvas_set_font(canvas, FontPrimary); + canvas_draw_str(canvas, 10, 35, "GPIO:"); + canvas_draw_str(canvas, 10, 47, "ID:"); + + canvas_set_font(canvas, FontSecondary); + canvas_draw_str( + canvas, + 41, + 23, + unitemp_onewire_sensor_getModel(unitemp_sensor_getActive(generalview_sensor_index))); + canvas_draw_str(canvas, 41, 35, s->bus->gpio->name); + snprintf( + app->buff, + BUFF_SIZE, + "%02X%02X%02X%02X%02X%02X%02X%02X", + s->deviceID[0], + s->deviceID[1], + s->deviceID[2], + s->deviceID[3], + s->deviceID[4], + s->deviceID[5], + s->deviceID[6], + s->deviceID[7]); + canvas_draw_str(canvas, 24, 47, app->buff); + } + + if(unitemp_sensor_getActive(generalview_sensor_index)->type->interface == &SINGLE_WIRE) { + canvas_set_font(canvas, FontPrimary); + canvas_draw_str(canvas, 10, 35, "GPIO:"); + + canvas_set_font(canvas, FontSecondary); + canvas_draw_str( + canvas, 41, 23, unitemp_sensor_getActive(generalview_sensor_index)->type->typename); + canvas_draw_str( + canvas, + 41, + 35, + ((SingleWireSensor*)unitemp_sensor_getActive(generalview_sensor_index)->instance) + ->gpio->name); + } + + if(unitemp_sensor_getActive(generalview_sensor_index)->type->interface == &I2C) { + canvas_set_font(canvas, FontPrimary); + canvas_draw_str(canvas, 10, 35, "I2C addr:"); + canvas_draw_str(canvas, 10, 46, "SDA pin:"); + canvas_draw_str(canvas, 10, 58, "SCL pin:"); + canvas_set_font(canvas, FontSecondary); + canvas_draw_str( + canvas, 41, 23, unitemp_sensor_getActive(generalview_sensor_index)->type->typename); + snprintf( + app->buff, + BUFF_SIZE, + "0x%02X", + ((I2CSensor*)unitemp_sensor_getActive(generalview_sensor_index)->instance) + ->currentI2CAdr); + canvas_draw_str(canvas, 57, 35, app->buff); + canvas_draw_str(canvas, 54, 46, "15 (C0)"); + canvas_draw_str(canvas, 54, 58, "16 (C1)"); + } +} +static void _draw_view_sensorsCarousel(Canvas* canvas) { + //Рисование рамки + canvas_draw_rframe(canvas, 0, 0, 128, 63, 7); + canvas_draw_rframe(canvas, 0, 0, 128, 64, 7); + + //Печать имени + canvas_set_font(canvas, FontPrimary); + canvas_draw_str_aligned( + canvas, + 64, + 7, + AlignCenter, + AlignCenter, + unitemp_sensor_getActive(generalview_sensor_index)->name); + //Подчёркивание + uint8_t line_len = + canvas_string_width(canvas, unitemp_sensor_getActive(generalview_sensor_index)->name) + 2; + canvas_draw_line(canvas, 64 - line_len / 2, 12, 64 + line_len / 2, 12); + + //Стрелка вправо + if(unitemp_sensors_getTypesCount() > 0 && + generalview_sensor_index < unitemp_sensors_getActiveCount() - 1) { + canvas_draw_icon(canvas, 122, 29, &I_ButtonRight_4x7); + } + //Стрелка влево + if(generalview_sensor_index > 0) { + canvas_draw_icon(canvas, 2, 29, &I_ButtonLeft_4x7); + } + + switch(carousel_info_selector) { + case CAROUSEL_VALUES: + _draw_carousel_values(canvas); + break; + case CAROUSEL_INFO: + _draw_carousel_info(canvas); + break; + } +} + +static void _draw_callback(Canvas* canvas, void* _model) { + UNUSED(_model); + + app->sensors_ready = true; + + uint8_t sensors_count = unitemp_sensors_getActiveCount(); + + if(generalview_sensor_index + 1 > sensors_count) generalview_sensor_index = 0; + + if(sensors_count == 0) { + current_view = G_NO_SENSORS_VIEW; + _draw_view_noSensors(canvas); + } else { + if(sensors_count == 1) current_view = G_CAROUSEL_VIEW; + if(current_view == G_NO_SENSORS_VIEW) current_view = G_CAROUSEL_VIEW; + if(current_view == G_LIST_VIEW) _draw_view_sensorsList(canvas); + if(current_view == G_CAROUSEL_VIEW) _draw_view_sensorsCarousel(canvas); + } +} + +static bool _input_callback(InputEvent* event, void* context) { + UNUSED(context); + + //Обработка короткого нажатия "ок" + if(event->key == InputKeyOk && event->type == InputTypeShort) { + //Меню добавления датчика при их отсутствии + if(current_view == G_NO_SENSORS_VIEW) { + app->sensors_ready = false; + unitemp_SensorsList_switch(); + } else if(current_view == G_LIST_VIEW) { + //Переход в главное меню при выключенном селекторе + app->sensors_ready = false; + unitemp_MainMenu_switch(); + } else if(current_view == G_CAROUSEL_VIEW) { + app->sensors_ready = false; + unitemp_SensorActions_switch(unitemp_sensor_getActive(generalview_sensor_index)); + } + } + + //Обработка короткого нажатия "вниз" + if(event->key == InputKeyDown && event->type == InputTypeShort) { + //Переход из значений в информацию в карусели + if(current_view == G_CAROUSEL_VIEW && carousel_info_selector == CAROUSEL_VALUES) { + carousel_info_selector = CAROUSEL_INFO; + return true; + } + //Переход в карусель из списка + if(current_view == G_LIST_VIEW) { + current_view = G_CAROUSEL_VIEW; + return true; + } + } + + //Обработка короткого нажатия "вверх" + if(event->key == InputKeyUp && event->type == InputTypeShort) { + //Переход из информации в значения в карусели + if(current_view == G_CAROUSEL_VIEW && carousel_info_selector == CAROUSEL_INFO) { + carousel_info_selector = CAROUSEL_VALUES; + return true; + } + //Переход в список из карусели + if(current_view == G_CAROUSEL_VIEW && carousel_info_selector == CAROUSEL_VALUES && + unitemp_sensors_getActiveCount() > 1) { + current_view = G_LIST_VIEW; + return true; + } + } + + //Обработка короткого нажатия "вправо" + if(event->key == InputKeyRight && event->type == InputTypeShort) { + //Пролистывание карусели вперёд + if(current_view == G_CAROUSEL_VIEW) { + if(++generalview_sensor_index >= unitemp_sensors_getActiveCount()) { + generalview_sensor_index = 0; + if(carousel_info_selector == CAROUSEL_VALUES) current_view = G_LIST_VIEW; + } + + return true; + } + //Пролистывание списка вперёд + if(current_view == G_LIST_VIEW) { + generalview_sensor_index += 4; + if(generalview_sensor_index >= unitemp_sensors_getActiveCount()) { + generalview_sensor_index = 0; + current_view = G_CAROUSEL_VIEW; + } + return true; + } + } + + //Обработка короткого нажатия "влево" + if(event->key == InputKeyLeft && event->type == InputTypeShort) { + //Пролистывание карусели назад + if(current_view == G_CAROUSEL_VIEW) { + if(--generalview_sensor_index >= unitemp_sensors_getActiveCount()) { + generalview_sensor_index = unitemp_sensors_getActiveCount() - 1; + if(carousel_info_selector == CAROUSEL_VALUES) current_view = G_LIST_VIEW; + } + + return true; + } + //Пролистывание списка назад + if(current_view == G_LIST_VIEW) { + generalview_sensor_index -= 4; + if(generalview_sensor_index >= unitemp_sensors_getActiveCount()) { + generalview_sensor_index = unitemp_sensors_getActiveCount() - 1; + current_view = G_CAROUSEL_VIEW; + } + + return true; + } + } + + //Обработка короткого нажатия "назад" + if(event->key == InputKeyBack && event->type == InputTypeShort) { + //Выход из приложения при карусели или отсутствии датчиков + if(current_view == G_NO_SENSORS_VIEW || + ((current_view == G_CAROUSEL_VIEW) && (carousel_info_selector == CAROUSEL_VALUES))) { + app->processing = false; + return true; + } + //Переключение селектора вида карусели + if((current_view == G_CAROUSEL_VIEW) && (carousel_info_selector != CAROUSEL_VALUES)) { + carousel_info_selector = CAROUSEL_VALUES; + return true; + } + //Переход в карусель из списка + if(current_view == G_LIST_VIEW) { + current_view = G_CAROUSEL_VIEW; + return true; + } + } + + return true; +} + +void unitemp_General_alloc(void) { + view = view_alloc(); + view_set_context(view, app); + view_set_draw_callback(view, _draw_callback); + view_set_input_callback(view, _input_callback); + + view_dispatcher_add_view(app->view_dispatcher, UnitempViewGeneral, view); +} + +void unitemp_General_switch(void) { + app->sensors_ready = true; + view_dispatcher_switch_to_view(app->view_dispatcher, UnitempViewGeneral); +} + +void unitemp_General_free(void) { + view_free(view); +} diff --git a/applications/plugins/unitemp/views/MainMenu_view.c b/applications/plugins/unitemp/views/MainMenu_view.c new file mode 100644 index 000000000..4b2820eee --- /dev/null +++ b/applications/plugins/unitemp/views/MainMenu_view.c @@ -0,0 +1,99 @@ +/* + Unitemp - Universal temperature reader + Copyright (C) 2022 Victor Nikitchuk (https://github.com/quen0n) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +#include "UnitempViews.h" +#include + +//Текущий вид +static View* view; +//Список +static VariableItemList* variable_item_list; + +#define VIEW_ID UnitempViewMainMenu + +/** + * @brief Функция обработки нажатия кнопки "Назад" + * + * @param context Указатель на данные приложения + * @return ID вида в который нужно переключиться + */ +static uint32_t _exit_callback(void* context) { + UNUSED(context); + //Возврат в общий вид + return UnitempViewGeneral; +} +/** + * @brief Функция обработки нажатия средней кнопки + * + * @param context Указатель на данные приложения + * @param index На каком элементе списка была нажата кнопка + */ +static void _enter_callback(void* context, uint32_t index) { + UNUSED(context); + if(index == 0) { //Add new sensor + unitemp_SensorsList_switch(); + } + if(index == 1) { //Settings + unitemp_Settings_switch(); + } + if(index == 2) { + unitemp_widget_help_switch(); + } + if(index == 3) { + unitemp_widget_about_switch(); + } +} + +/** + * @brief Создание списка действий с указанным датчиком + */ +void unitemp_MainMenu_alloc(void) { + variable_item_list = variable_item_list_alloc(); + //Сброс всех элементов меню + variable_item_list_reset(variable_item_list); + + variable_item_list_add(variable_item_list, "Add new sensor", 1, NULL, NULL); + variable_item_list_add(variable_item_list, "Settings", 1, NULL, NULL); + variable_item_list_add(variable_item_list, "Help", 1, NULL, NULL); + variable_item_list_add(variable_item_list, "About", 1, NULL, NULL); + + //Добавление колбека на нажатие средней кнопки + variable_item_list_set_enter_callback(variable_item_list, _enter_callback, app); + + //Создание вида из списка + view = variable_item_list_get_view(variable_item_list); + //Добавление колбека на нажатие кнопки "Назад" + view_set_previous_callback(view, _exit_callback); + //Добавление вида в диспетчер + view_dispatcher_add_view(app->view_dispatcher, VIEW_ID, view); +} + +void unitemp_MainMenu_switch(void) { + //Обнуление последнего выбранного пункта + variable_item_list_set_selected_item(variable_item_list, 0); + //Переключение в вид + view_dispatcher_switch_to_view(app->view_dispatcher, VIEW_ID); +} + +void unitemp_MainMenu_free(void) { + //Очистка списка элементов + variable_item_list_free(variable_item_list); + //Очистка вида + view_free(view); + //Удаление вида после обработки + view_dispatcher_remove_view(app->view_dispatcher, VIEW_ID); +} \ No newline at end of file diff --git a/applications/plugins/unitemp/views/Popup_view.c b/applications/plugins/unitemp/views/Popup_view.c new file mode 100644 index 000000000..d495462b2 --- /dev/null +++ b/applications/plugins/unitemp/views/Popup_view.c @@ -0,0 +1,50 @@ +/* + Unitemp - Universal temperature reader + Copyright (C) 2022 Victor Nikitchuk (https://github.com/quen0n) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +#include "UnitempViews.h" +#include +#include +#include + +uint32_t _prev_view_id; + +#define VIEW_ID UnitempViewPopup + +static void _popup_callback(void* context) { + UNUSED(context); + view_dispatcher_switch_to_view(app->view_dispatcher, _prev_view_id); +} + +void unitemp_popup(const Icon* icon, char* header, char* message, uint32_t prev_view_id) { + _prev_view_id = prev_view_id; + popup_reset(app->popup); + popup_set_icon(app->popup, 0, 64 - icon_get_height(icon), icon); + popup_set_header(app->popup, header, 64, 6, AlignCenter, AlignCenter); + popup_set_text( + app->popup, + message, + (128 - icon_get_width(icon)) / 2 + icon_get_width(icon), + 32, + AlignCenter, + AlignCenter); + + popup_set_timeout(app->popup, 5000); + popup_set_callback(app->popup, _popup_callback); + popup_enable_timeout(app->popup); + + view_dispatcher_switch_to_view(app->view_dispatcher, VIEW_ID); +} \ No newline at end of file diff --git a/applications/plugins/unitemp/views/SensorActions_view.c b/applications/plugins/unitemp/views/SensorActions_view.c new file mode 100644 index 000000000..6f375a50a --- /dev/null +++ b/applications/plugins/unitemp/views/SensorActions_view.c @@ -0,0 +1,125 @@ +/* + Unitemp - Universal temperature reader + Copyright (C) 2022 Victor Nikitchuk (https://github.com/quen0n) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +#include "UnitempViews.h" +#include +#include + +//Текущий вид +static View* view; +//Список +static VariableItemList* variable_item_list; +//Текущий датчик +static Sensor* current_sensor; + +typedef enum carousel_info { + CAROUSEL_VALUES, //Отображение значений датчиков + CAROUSEL_INFO, //Отображение информации о датчике +} carousel_info; +extern carousel_info carousel_info_selector; + +#define VIEW_ID UnitempViewSensorActions + +/** + * @brief Функция обработки нажатия кнопки "Назад" + * + * @param context Указатель на данные приложения + * @return ID вида в который нужно переключиться + */ +static uint32_t _exit_callback(void* context) { + UNUSED(context); + + //Возврат предыдущий вид + return UnitempViewGeneral; +} +/** + * @brief Функция обработки нажатия средней кнопки + * + * @param context Указатель на данные приложения + * @param index На каком элементе списка была нажата кнопка + */ +static void _enter_callback(void* context, uint32_t index) { + UNUSED(context); + switch(index) { + case 0: + carousel_info_selector = CAROUSEL_INFO; + unitemp_General_switch(); + return; + case 1: + unitemp_SensorEdit_switch(current_sensor); + break; + case 2: + unitemp_widget_delete_switch(current_sensor); + break; + case 3: + unitemp_SensorsList_switch(); + break; + case 4: + unitemp_Settings_switch(); + break; + case 5: + unitemp_widget_help_switch(); + break; + case 6: + unitemp_widget_about_switch(); + break; + } +} + +/** + * @brief Создание меню действий с датчиком + */ +void unitemp_SensorActions_alloc(void) { + variable_item_list = variable_item_list_alloc(); + //Сброс всех элементов меню + variable_item_list_reset(variable_item_list); + + variable_item_list_add(variable_item_list, "Info", 1, NULL, NULL); + variable_item_list_add(variable_item_list, "Edit", 1, NULL, NULL); + variable_item_list_add(variable_item_list, "Delete", 1, NULL, NULL); + + variable_item_list_add(variable_item_list, "Add new sensor", 1, NULL, NULL); + variable_item_list_add(variable_item_list, "Settings", 1, NULL, NULL); + variable_item_list_add(variable_item_list, "Help", 1, NULL, NULL); + variable_item_list_add(variable_item_list, "About", 1, NULL, NULL); + + //Добавление колбека на нажатие средней кнопки + variable_item_list_set_enter_callback(variable_item_list, _enter_callback, app); + //Создание вида из списка + view = variable_item_list_get_view(variable_item_list); + //Добавление колбека на нажатие кнопки "Назад" + view_set_previous_callback(view, _exit_callback); + //Добавление вида в диспетчер + view_dispatcher_add_view(app->view_dispatcher, VIEW_ID, view); +} + +void unitemp_SensorActions_switch(Sensor* sensor) { + current_sensor = sensor; + //Обнуление последнего выбранного пункта + variable_item_list_set_selected_item(variable_item_list, 0); + + view_dispatcher_switch_to_view(app->view_dispatcher, VIEW_ID); +} + +void unitemp_SensorActions_free(void) { + //Очистка списка элементов + variable_item_list_free(variable_item_list); + //Очистка вида + view_free(view); + //Удаление вида после обработки + view_dispatcher_remove_view(app->view_dispatcher, VIEW_ID); +} \ No newline at end of file diff --git a/applications/plugins/unitemp/views/SensorEdit_view.c b/applications/plugins/unitemp/views/SensorEdit_view.c new file mode 100644 index 000000000..4d5bc17db --- /dev/null +++ b/applications/plugins/unitemp/views/SensorEdit_view.c @@ -0,0 +1,376 @@ +/* + Unitemp - Universal temperature reader + Copyright (C) 2022 Victor Nikitchuk (https://github.com/quen0n) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +#include "UnitempViews.h" +#include + +#include "../interfaces/SingleWireSensor.h" +#include "../interfaces/OneWireSensor.h" +#include "../interfaces/I2CSensor.h" + +//Текущий вид +static View* view; +//Список +static VariableItemList* variable_item_list; +//Текущий редактируемый датчик +static Sensor* editable_sensor; +//Изначальный GPIO датчика +static const GPIO* initial_gpio = NULL; + +//Элемент списка - имя датчика +static VariableItem* sensor_name_item; +//Элемент списка - адрес датчика one wire +static VariableItem* onewire_addr_item; +//Элемент списка - адрес датчика one wire +static VariableItem* onewire_type_item; +//Элемент списка - смещение температуры +VariableItem* temp_offset_item; + +#define OFFSET_BUFF_SIZE 5 +//Буффер для текста смещения +static char* offset_buff; + +extern uint8_t generalview_sensor_index; + +#define VIEW_ID UnitempViewSensorEdit + +bool _onewire_id_exist(uint8_t* id) { + if(id == NULL) return false; + for(uint8_t i = 0; i < unitemp_sensors_getActiveCount(); i++) { + if(unitemp_sensor_getActive(i)->type == &Dallas) { + if(unitemp_onewire_id_compare( + id, ((OneWireSensor*)(unitemp_sensor_getActive(i)->instance))->deviceID)) { + return true; + } + } + } + return false; +} + +static void _onewire_scan(void) { + OneWireSensor* ow_sensor = editable_sensor->instance; +#ifdef UNITEMP_DEBUG + FURI_LOG_D( + APP_NAME, + "devices on wire %d: %d", + ow_sensor->bus->gpio->num, + ow_sensor->bus->device_count); +#endif + + //Сканирование шины one wire + unitemp_onewire_bus_init(ow_sensor->bus); + uint8_t* id = NULL; + do { + id = unitemp_onewire_bus_enum_next(ow_sensor->bus); + } while(_onewire_id_exist(id)); + + if(id == NULL) { + unitemp_onewire_bus_enum_init(); + id = unitemp_onewire_bus_enum_next(ow_sensor->bus); + if(_onewire_id_exist(id)) { + do { + id = unitemp_onewire_bus_enum_next(ow_sensor->bus); + } while(_onewire_id_exist(id) && id != NULL); + } + if(id == NULL) { + memset(ow_sensor->deviceID, 0, 8); + ow_sensor->familyCode = 0; + unitemp_onewire_bus_deinit(ow_sensor->bus); + variable_item_set_current_value_text(onewire_addr_item, "empty"); + variable_item_set_current_value_text( + onewire_type_item, unitemp_onewire_sensor_getModel(editable_sensor)); + return; + } + } + + unitemp_onewire_bus_deinit(ow_sensor->bus); + + memcpy(ow_sensor->deviceID, id, 8); + ow_sensor->familyCode = id[0]; +#ifdef UNITEMP_DEBUG + FURI_LOG_D( + APP_NAME, + "Found sensor's ID: %02X%02X%02X%02X%02X%02X%02X%02X", + id[0], + id[1], + id[2], + id[3], + id[4], + id[5], + id[6], + id[7]); +#endif + + if(ow_sensor->familyCode != 0) { + char id_buff[10]; + snprintf( + id_buff, + 10, + "%02X%02X%02X", + ow_sensor->deviceID[1], + ow_sensor->deviceID[2], + ow_sensor->deviceID[3]); + //А больше не лезет( + variable_item_set_current_value_text(onewire_addr_item, id_buff); + } else { + variable_item_set_current_value_text(onewire_addr_item, "empty"); + } + variable_item_set_current_value_text( + onewire_type_item, unitemp_onewire_sensor_getModel(editable_sensor)); +} + +/** + * @brief Функция обработки нажатия кнопки "Назад" + * + * @param context Указатель на данные приложения + * @return ID вида в который нужно переключиться + */ +static uint32_t _exit_callback(void* context) { + UNUSED(context); + editable_sensor->status = UT_SENSORSTATUS_TIMEOUT; + if(!unitemp_sensor_isContains(editable_sensor)) unitemp_sensor_free(editable_sensor); + unitemp_sensors_reload(); + //Возврат предыдущий вид + return UnitempViewGeneral; +} +/** + * @brief Функция обработки нажатия средней кнопки + * + * @param context Указатель на данные приложения + * @param index На каком элементе списка была нажата кнопка + */ +static void _enter_callback(void* context, uint32_t index) { + UNUSED(context); + //Смена имени + if(index == 0) { + unitemp_SensorNameEdit_switch(editable_sensor); + } + //Сохранение + if((index == 4 && editable_sensor->type->interface != &ONE_WIRE) || + (index == 5 && editable_sensor->type->interface == &ONE_WIRE)) { + //Выход если датчик one wire не имеет ID + if(editable_sensor->type->interface == &ONE_WIRE && + ((OneWireSensor*)(editable_sensor->instance))->familyCode == 0) { + return; + } + if(initial_gpio != NULL) { + unitemp_gpio_unlock(initial_gpio); + initial_gpio = NULL; + } + editable_sensor->status = UT_SENSORSTATUS_TIMEOUT; + if(!unitemp_sensor_isContains(editable_sensor)) unitemp_sensors_add(editable_sensor); + unitemp_sensors_save(); + unitemp_sensors_reload(); + + generalview_sensor_index = unitemp_sensors_getActiveCount() - 1; + unitemp_General_switch(); + } + + //Адрес устройства на шине one wire + if(index == 4 && editable_sensor->type->interface == &ONE_WIRE) { + _onewire_scan(); + } +} + +/** + * @brief Функция обработки изменения значения GPIO + * + * @param item Указатель на элемент списка + */ +static void _gpio_change_callback(VariableItem* item) { + uint8_t index = variable_item_get_current_value_index(item); + if(editable_sensor->type->interface == &SINGLE_WIRE) { + SingleWireSensor* instance = editable_sensor->instance; + instance->gpio = + unitemp_gpio_getAviablePort(editable_sensor->type->interface, index, initial_gpio); + variable_item_set_current_value_text(item, instance->gpio->name); + } + if(editable_sensor->type->interface == &ONE_WIRE) { + OneWireSensor* instance = editable_sensor->instance; + instance->bus->gpio = + unitemp_gpio_getAviablePort(editable_sensor->type->interface, index, NULL); + variable_item_set_current_value_text(item, instance->bus->gpio->name); + } +} +/** + * @brief Функция обработки изменения значения GPIO + * + * @param item Указатель на элемент списка + */ +static void _i2caddr_change_callback(VariableItem* item) { + uint8_t index = variable_item_get_current_value_index(item); + ((I2CSensor*)editable_sensor->instance)->currentI2CAdr = + ((I2CSensor*)editable_sensor->instance)->minI2CAdr + index; + char buff[5]; + snprintf(buff, 5, "0x%2X", ((I2CSensor*)editable_sensor->instance)->currentI2CAdr); + variable_item_set_current_value_text(item, buff); +} +/** + * @brief Функция обработки изменения значения имени датчика + * + * @param item Указатель на элемент списка + */ +static void _name_change_callback(VariableItem* item) { + variable_item_set_current_value_index(item, 0); + unitemp_SensorNameEdit_switch(editable_sensor); +} +/** + * @brief Функция обработки изменения значения адреса датчика one wire + * + * @param item Указатель на элемент списка + */ +static void _onwire_addr_change_callback(VariableItem* item) { + variable_item_set_current_value_index(item, 0); + _onewire_scan(); +} + +/** + * @brief Функция обработки изменения значения смещения температуры + * + * @param item Указатель на элемент списка + */ +static void _offset_change_callback(VariableItem* item) { + editable_sensor->temp_offset = variable_item_get_current_value_index(item) - 20; + snprintf( + offset_buff, OFFSET_BUFF_SIZE, "%+1.1f", (double)(editable_sensor->temp_offset / 10.0)); + variable_item_set_current_value_text(item, offset_buff); +} + +/** + * @brief Создание меню редактирования датчка + */ +void unitemp_SensorEdit_alloc(void) { + variable_item_list = variable_item_list_alloc(); + //Сброс всех элементов меню + variable_item_list_reset(variable_item_list); + + //Добавление колбека на нажатие средней кнопки + variable_item_list_set_enter_callback(variable_item_list, _enter_callback, app); + + //Создание вида из списка + view = variable_item_list_get_view(variable_item_list); + //Добавление колбека на нажатие кнопки "Назад" + view_set_previous_callback(view, _exit_callback); + //Добавление вида в диспетчер + view_dispatcher_add_view(app->view_dispatcher, VIEW_ID, view); + + offset_buff = malloc(OFFSET_BUFF_SIZE); +} + +void unitemp_SensorEdit_switch(Sensor* sensor) { + editable_sensor = sensor; + + editable_sensor->status = UT_SENSORSTATUS_INACTIVE; + + //Сброс всех элементов меню + variable_item_list_reset(variable_item_list); + //Обнуление последнего выбранного пункта + variable_item_list_set_selected_item(variable_item_list, 0); + + //Имя датчика + sensor_name_item = variable_item_list_add( + variable_item_list, "Name", strlen(sensor->name) > 7 ? 1 : 2, _name_change_callback, NULL); + variable_item_set_current_value_index(sensor_name_item, 0); + variable_item_set_current_value_text(sensor_name_item, sensor->name); + + //Тип датчика (не редактируется) + onewire_type_item = variable_item_list_add(variable_item_list, "Type", 1, NULL, NULL); + variable_item_set_current_value_index(onewire_type_item, 0); + variable_item_set_current_value_text( + onewire_type_item, + (sensor->type->interface == &ONE_WIRE ? unitemp_onewire_sensor_getModel(editable_sensor) : + sensor->type->typename)); + //Смещение температуры + temp_offset_item = variable_item_list_add( + variable_item_list, "Temp. offset", 41, _offset_change_callback, NULL); + variable_item_set_current_value_index(temp_offset_item, sensor->temp_offset + 20); + snprintf( + offset_buff, OFFSET_BUFF_SIZE, "%+1.1f", (double)(editable_sensor->temp_offset / 10.0)); + variable_item_set_current_value_text(temp_offset_item, offset_buff); + + //Порт подключения датчка (для one wire и single wire) + if(sensor->type->interface == &ONE_WIRE || sensor->type->interface == &SINGLE_WIRE) { + if(sensor->type->interface == &ONE_WIRE) { + initial_gpio = ((OneWireSensor*)editable_sensor->instance)->bus->gpio; + } else { + initial_gpio = ((SingleWireSensor*)editable_sensor->instance)->gpio; + } + + uint8_t aviable_gpio_count = + unitemp_gpio_getAviablePortsCount(sensor->type->interface, initial_gpio); + VariableItem* item = variable_item_list_add( + variable_item_list, "GPIO", aviable_gpio_count, _gpio_change_callback, app); + + uint8_t gpio_index = 0; + if(unitemp_sensor_isContains(editable_sensor)) { + for(uint8_t i = 0; i < aviable_gpio_count; i++) { + if(unitemp_gpio_getAviablePort(sensor->type->interface, i, initial_gpio) == + initial_gpio) { + gpio_index = i; + break; + } + } + } + variable_item_set_current_value_index(item, gpio_index); + variable_item_set_current_value_text( + item, + unitemp_gpio_getAviablePort(sensor->type->interface, gpio_index, initial_gpio)->name); + } + //Адрес устройства на шине I2C (для датчиков I2C) + if(sensor->type->interface == &I2C) { + VariableItem* item = variable_item_list_add( + variable_item_list, + "I2C address", + ((I2CSensor*)sensor->instance)->maxI2CAdr - ((I2CSensor*)sensor->instance)->minI2CAdr + + 1, + _i2caddr_change_callback, + app); + snprintf(app->buff, 5, "0x%2X", ((I2CSensor*)sensor->instance)->currentI2CAdr); + variable_item_set_current_value_text(item, app->buff); + } + + //Адрес устройства на шине one wire (для датчиков one wire) + if(sensor->type->interface == &ONE_WIRE) { + onewire_addr_item = variable_item_list_add( + variable_item_list, "Address", 2, _onwire_addr_change_callback, NULL); + OneWireSensor* ow_sensor = sensor->instance; + if(ow_sensor->familyCode == 0) { + variable_item_set_current_value_text(onewire_addr_item, "Scan"); + } else { + snprintf( + app->buff, + 10, + "%02X%02X%02X", + ow_sensor->deviceID[1], + ow_sensor->deviceID[2], + ow_sensor->deviceID[3]); + variable_item_set_current_value_text(onewire_addr_item, app->buff); + } + } + variable_item_list_add(variable_item_list, "Save", 1, NULL, NULL); + view_dispatcher_switch_to_view(app->view_dispatcher, VIEW_ID); +} + +void unitemp_SensorEdit_free(void) { + //Очистка списка элементов + variable_item_list_free(variable_item_list); + //Очистка вида + view_free(view); + //Удаление вида после обработки + view_dispatcher_remove_view(app->view_dispatcher, VIEW_ID); + free(offset_buff); +} \ No newline at end of file diff --git a/applications/plugins/unitemp/views/SensorNameEdit_view.c b/applications/plugins/unitemp/views/SensorNameEdit_view.c new file mode 100644 index 000000000..1d36ac142 --- /dev/null +++ b/applications/plugins/unitemp/views/SensorNameEdit_view.c @@ -0,0 +1,46 @@ +/* + Unitemp - Universal temperature reader + Copyright (C) 2022 Victor Nikitchuk (https://github.com/quen0n) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +#include "UnitempViews.h" +#include + +//Окно ввода текста +static TextInput* text_input; +//Текущий редактируемый датчик +static Sensor* editable_sensor; + +#define VIEW_ID UnitempViewSensorNameEdit + +static void _sensor_name_changed_callback(void* context) { + UNUSED(context); + unitemp_SensorEdit_switch(editable_sensor); +} + +void unitemp_SensorNameEdit_alloc(void) { + text_input = text_input_alloc(); + view_dispatcher_add_view(app->view_dispatcher, VIEW_ID, text_input_get_view(text_input)); + text_input_set_header_text(text_input, "Sensor name"); +} +void unitemp_SensorNameEdit_switch(Sensor* sensor) { + editable_sensor = sensor; + text_input_set_result_callback( + text_input, _sensor_name_changed_callback, app, sensor->name, 11, true); + view_dispatcher_switch_to_view(app->view_dispatcher, VIEW_ID); +} +void unitemp_SensorNameEdit_free(void) { + text_input_free(text_input); +} \ No newline at end of file diff --git a/applications/plugins/unitemp/views/SensorsList_view.c b/applications/plugins/unitemp/views/SensorsList_view.c new file mode 100644 index 000000000..a7d3d5556 --- /dev/null +++ b/applications/plugins/unitemp/views/SensorsList_view.c @@ -0,0 +1,162 @@ +/* + Unitemp - Universal temperature reader + Copyright (C) 2022 Victor Nikitchuk (https://github.com/quen0n) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +#include "UnitempViews.h" +#include +#include +#include + +//Текущий вид +static View* view; +//Список +static VariableItemList* variable_item_list; + +#define VIEW_ID UnitempViewSensorsList + +/** + * @brief Функция обработки нажатия кнопки "Назад" + * + * @param context Указатель на данные приложения + * @return ID вида в который нужно переключиться + */ +static uint32_t _exit_callback(void* context) { + UNUSED(context); + + //Возврат предыдущий вид + return UnitempViewGeneral; +} +/** + * @brief Функция обработки нажатия средней кнопки + * + * @param context Указатель на данные приложения + * @param index На каком элементе списка была нажата кнопка + */ +static void _enter_callback(void* context, uint32_t index) { + UNUSED(context); + if(index == unitemp_sensors_getTypesCount()) { + unitemp_widget_help_switch(); + return; + } + + const SensorType* type = unitemp_sensors_getTypes()[index]; + uint8_t sensor_type_count = 0; + + //Подсчёт имеющихся датчиков данного типа + for(uint8_t i = 0; i < unitemp_sensors_getActiveCount(); i++) { + if(unitemp_sensor_getActive(i)->type == type) { + sensor_type_count++; + } + } + + //Имя датчка + char sensor_name[11]; + //Добавление счётчика к имени если такой датчик имеется + if(sensor_type_count == 0) + snprintf(sensor_name, 11, "%s", type->typename); + else + snprintf(sensor_name, 11, "%s_%d", type->typename, sensor_type_count); + + char args[22] = {0}; + + //Проверка доступности датчика + if(unitemp_gpio_getAviablePort(type->interface, 0, NULL) == NULL) { + if(type->interface == &SINGLE_WIRE || type->interface == &ONE_WIRE) { + unitemp_popup( + &I_Cry_dolph_55x52, "Sensor is unavailable", "All GPIOs\nare busy", VIEW_ID); + } + if(type->interface == &I2C) { + unitemp_popup( + &I_Cry_dolph_55x52, "Sensor is unavailable", "GPIOs 15 or 16\nare busy", VIEW_ID); + } + return; + } + + //Выбор первого доступного порта для датчика single wire + if(type->interface == &SINGLE_WIRE) { + snprintf( + args, + 4, + "%d", + unitemp_gpio_toInt(unitemp_gpio_getAviablePort(type->interface, 0, NULL))); + } + //Выбор первого доступного порта для датчика one wire и запись нулевого ID + if(type->interface == &ONE_WIRE) { + snprintf( + args, + 21, + "%d %02X%02X%02X%02X%02X%02X%02X%02X", + unitemp_gpio_toInt(unitemp_gpio_getAviablePort(type->interface, 0, NULL)), + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0); + } + //Для I2C адрес выберется автоматически + + unitemp_SensorEdit_switch(unitemp_sensor_alloc(sensor_name, type, args)); +} + +/** + * @brief Создание меню редактирования настроек + */ +void unitemp_SensorsList_alloc(void) { + variable_item_list = variable_item_list_alloc(); + //Сброс всех элементов меню + variable_item_list_reset(variable_item_list); + + //Добавление в список доступных датчиков + for(uint8_t i = 0; i < unitemp_sensors_getTypesCount(); i++) { + if(unitemp_sensors_getTypes()[i]->altname == NULL) { + variable_item_list_add( + variable_item_list, unitemp_sensors_getTypes()[i]->typename, 1, NULL, app); + } else { + variable_item_list_add( + variable_item_list, unitemp_sensors_getTypes()[i]->altname, 1, NULL, app); + } + } + variable_item_list_add(variable_item_list, "I don't know what to choose", 1, NULL, app); + + //Добавление колбека на нажатие средней кнопки + variable_item_list_set_enter_callback(variable_item_list, _enter_callback, app); + + //Создание вида из списка + view = variable_item_list_get_view(variable_item_list); + //Добавление колбека на нажатие кнопки "Назад" + view_set_previous_callback(view, _exit_callback); + //Добавление вида в диспетчер + view_dispatcher_add_view(app->view_dispatcher, VIEW_ID, view); +} + +void unitemp_SensorsList_switch(void) { + //Обнуление последнего выбранного пункта + variable_item_list_set_selected_item(variable_item_list, 0); + + view_dispatcher_switch_to_view(app->view_dispatcher, VIEW_ID); +} + +void unitemp_SensorsList_free(void) { + //Очистка списка элементов + variable_item_list_free(variable_item_list); + //Очистка вида + view_free(view); + //Удаление вида после обработки + view_dispatcher_remove_view(app->view_dispatcher, VIEW_ID); +} \ No newline at end of file diff --git a/applications/plugins/unitemp/views/Settings_view.c b/applications/plugins/unitemp/views/Settings_view.c new file mode 100644 index 000000000..c3f82c14e --- /dev/null +++ b/applications/plugins/unitemp/views/Settings_view.c @@ -0,0 +1,152 @@ +/* + Unitemp - Universal temperature reader + Copyright (C) 2022 Victor Nikitchuk (https://github.com/quen0n) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +#include "UnitempViews.h" +#include + +//Текущий вид +static View* view; +//Список +static VariableItemList* variable_item_list; + +static const char states[2][9] = {"Auto", "Infinity"}; +static const char temp_units[UT_TEMP_COUNT][3] = {"*C", "*F"}; +static const char pressure_units[UT_PRESSURE_COUNT][6] = {"mm Hg", "in Hg", "kPa"}; + +//Элемент списка - бесконечная подсветка +VariableItem* infinity_backlight_item; +//Единица измерения температуры +VariableItem* temperature_unit_item; +//Единица измерения давления +VariableItem* pressure_unit_item; +#define VIEW_ID UnitempViewSettings + +/** + * @brief Функция обработки нажатия кнопки "Назад" + * + * @param context Указатель на данные приложения + * @return ID вида в который нужно переключиться + */ +static uint32_t _exit_callback(void* context) { + UNUSED(context); + //Костыль с зависающей подсветкой + if((bool)variable_item_get_current_value_index(infinity_backlight_item) != + app->settings.infinityBacklight) { + if((bool)variable_item_get_current_value_index(infinity_backlight_item)) { + notification_message(app->notifications, &sequence_display_backlight_enforce_on); + } else { + notification_message(app->notifications, &sequence_display_backlight_enforce_auto); + } + } + + app->settings.infinityBacklight = + (bool)variable_item_get_current_value_index(infinity_backlight_item); + app->settings.temp_unit = variable_item_get_current_value_index(temperature_unit_item); + app->settings.pressure_unit = variable_item_get_current_value_index(pressure_unit_item); + unitemp_saveSettings(); + unitemp_loadSettings(); + + //Возврат предыдущий вид + return UnitempViewMainMenu; +} +/** + * @brief Функция обработки нажатия средней кнопки + * + * @param context Указатель на данные приложения + * @param index На каком элементе списка была нажата кнопка + */ +static void _enter_callback(void* context, uint32_t index) { + UNUSED(context); + UNUSED(index); +} + +static void _setting_change_callback(VariableItem* item) { + if(item == infinity_backlight_item) { + variable_item_set_current_value_text( + infinity_backlight_item, + states[variable_item_get_current_value_index(infinity_backlight_item)]); + } + if(item == temperature_unit_item) { + variable_item_set_current_value_text( + temperature_unit_item, + temp_units[variable_item_get_current_value_index(temperature_unit_item)]); + } + if(item == pressure_unit_item) { + variable_item_set_current_value_text( + pressure_unit_item, + pressure_units[variable_item_get_current_value_index(pressure_unit_item)]); + } +} + +/** + * @brief Создание меню редактирования настроек + */ +void unitemp_Settings_alloc(void) { + variable_item_list = variable_item_list_alloc(); + //Сброс всех элементов меню + variable_item_list_reset(variable_item_list); + + infinity_backlight_item = variable_item_list_add( + variable_item_list, "Backlight time", UT_TEMP_COUNT, _setting_change_callback, app); + temperature_unit_item = + variable_item_list_add(variable_item_list, "Temp. unit", 2, _setting_change_callback, app); + pressure_unit_item = variable_item_list_add( + variable_item_list, "Press. unit", UT_PRESSURE_COUNT, _setting_change_callback, app); + + //Добавление колбека на нажатие средней кнопки + variable_item_list_set_enter_callback(variable_item_list, _enter_callback, app); + + //Создание вида из списка + view = variable_item_list_get_view(variable_item_list); + //Добавление колбека на нажатие кнопки "Назад" + view_set_previous_callback(view, _exit_callback); + //Добавление вида в диспетчер + view_dispatcher_add_view(app->view_dispatcher, VIEW_ID, view); +} + +void unitemp_Settings_switch(void) { + //Обнуление последнего выбранного пункта + variable_item_list_set_selected_item(variable_item_list, 0); + + variable_item_set_current_value_index( + infinity_backlight_item, (uint8_t)app->settings.infinityBacklight); + variable_item_set_current_value_text( + infinity_backlight_item, + states[variable_item_get_current_value_index(infinity_backlight_item)]); + + variable_item_set_current_value_index(temperature_unit_item, (uint8_t)app->settings.temp_unit); + variable_item_set_current_value_text( + temperature_unit_item, + temp_units[variable_item_get_current_value_index(temperature_unit_item)]); + + variable_item_set_current_value_index( + pressure_unit_item, (uint8_t)app->settings.pressure_unit); + variable_item_set_current_value_text( + pressure_unit_item, + pressure_units[variable_item_get_current_value_index(pressure_unit_item)]); + + view_dispatcher_switch_to_view(app->view_dispatcher, VIEW_ID); +} + +void unitemp_Settings_free(void) { + //Очистка списка элементов + variable_item_list_free(variable_item_list); + //Очистка вида + view_free(view); + //Удаление вида после обработки + view_dispatcher_remove_view(app->view_dispatcher, VIEW_ID); +} \ No newline at end of file diff --git a/applications/plugins/unitemp/views/UnitempViews.h b/applications/plugins/unitemp/views/UnitempViews.h new file mode 100644 index 000000000..b7c2467e0 --- /dev/null +++ b/applications/plugins/unitemp/views/UnitempViews.h @@ -0,0 +1,94 @@ +/* + Unitemp - Universal temperature reader + Copyright (C) 2022 Victor Nikitchuk (https://github.com/quen0n) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +#ifndef UNITEMP_SCENES +#define UNITEMP_SCENES + +#include "../unitemp.h" + +//Виды менюшек +typedef enum UnitempViews { + UnitempViewGeneral, + UnitempViewMainMenu, + UnitempViewSettings, + UnitempViewSensorsList, + UnitempViewSensorEdit, + UnitempViewSensorNameEdit, + UnitempViewSensorActions, + UnitempViewWidget, + UnitempViewPopup, + + UnitempViewsCount +} UnitempViews; + +/** + * @brief Вывести всплывающее окно + * + * @param icon Указатель на иконку + * @param header Заголовок + * @param message Сообщение + * @param prev_view_id ID вида куда в который нужно вернуться + */ +void unitemp_popup(const Icon* icon, char* header, char* message, uint32_t prev_view_id); + +/* Общий вид на датчики */ +void unitemp_General_alloc(void); +void unitemp_General_switch(void); +void unitemp_General_free(void); + +/* Главное меню */ +void unitemp_MainMenu_alloc(void); +void unitemp_MainMenu_switch(void); +void unitemp_MainMenu_free(void); + +/* Настройки */ +void unitemp_Settings_alloc(void); +void unitemp_Settings_switch(void); +void unitemp_Settings_free(void); + +/* Список датчиков */ +void unitemp_SensorsList_alloc(void); +void unitemp_SensorsList_switch(void); +void unitemp_SensorsList_free(void); + +/* Редактор датчка */ +void unitemp_SensorEdit_alloc(void); +//sensor - указатель на редактируемый датчик +void unitemp_SensorEdit_switch(Sensor* sensor); +void unitemp_SensorEdit_free(void); + +/* Редактор имени датчика */ +void unitemp_SensorNameEdit_alloc(void); +void unitemp_SensorNameEdit_switch(Sensor* sensor); +void unitemp_SensorNameEdit_free(void); + +/* Список действий с датчиком */ +void unitemp_SensorActions_alloc(void); +void unitemp_SensorActions_switch(Sensor* sensor); +void unitemp_SensorActions_free(void); + +/* Виджеты */ +void unitemp_widgets_alloc(void); +void unitemp_widgets_free(void); + +/* Подтверждение удаления */ +void unitemp_widget_delete_switch(Sensor* sensor); +/* Помощь */ +void unitemp_widget_help_switch(void); +/* О приложении */ +void unitemp_widget_about_switch(void); +#endif \ No newline at end of file diff --git a/applications/plugins/unitemp/views/Widgets_view.c b/applications/plugins/unitemp/views/Widgets_view.c new file mode 100644 index 000000000..6d8702ca1 --- /dev/null +++ b/applications/plugins/unitemp/views/Widgets_view.c @@ -0,0 +1,204 @@ +/* + Unitemp - Universal temperature reader + Copyright (C) 2022 Victor Nikitchuk (https://github.com/quen0n) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +#include "UnitempViews.h" +#include "unitemp_icons.h" + +#include + +void unitemp_widgets_alloc(void) { + app->widget = widget_alloc(); + view_dispatcher_add_view( + app->view_dispatcher, UnitempViewWidget, widget_get_view(app->widget)); +} + +void unitemp_widgets_free(void) { + widget_free(app->widget); +} + +/* ================== Подтверждение удаления ================== */ +Sensor* current_sensor; +/** + * @brief Функция обработки нажатия кнопки "Назад" + * + * @param context Указатель на данные приложения + * @return ID вида в который нужно переключиться + */ +static uint32_t _delete_exit_callback(void* context) { + UNUSED(context); + //Возвращаем ID вида, в который нужно вернуться + return UnitempViewSensorActions; +} +/** + * @brief Обработчик нажатий на кнопку в виджете + * + * @param result Какая из кнопок была нажата + * @param type Тип нажатия + * @param context Указатель на данные плагина + */ +static void _delete_click_callback(GuiButtonType result, InputType type, void* context) { + UNUSED(context); + //Коротко нажата левая кнопка (Cancel) + if(result == GuiButtonTypeLeft && type == InputTypeShort) { + unitemp_SensorActions_switch(current_sensor); + } + //Коротко нажата правая кнопка (Delete) + if(result == GuiButtonTypeRight && type == InputTypeShort) { + //Удаление датчика + unitemp_sensor_delete(current_sensor); + //Выход из меню + unitemp_General_switch(); + } +} +/** + * @brief Переключение в виджет удаления датчика + */ +void unitemp_widget_delete_switch(Sensor* sensor) { + current_sensor = sensor; + //Очистка виджета + widget_reset(app->widget); + //Добавление кнопок + widget_add_button_element( + app->widget, GuiButtonTypeLeft, "Cancel", _delete_click_callback, app); + widget_add_button_element( + app->widget, GuiButtonTypeRight, "Delete", _delete_click_callback, app); + + snprintf(app->buff, BUFF_SIZE, "\e#Delete %s?\e#", current_sensor->name); + widget_add_text_box_element( + app->widget, 0, 0, 128, 23, AlignCenter, AlignCenter, app->buff, false); + + if(current_sensor->type->interface == &ONE_WIRE) { + OneWireSensor* s = current_sensor->instance; + + snprintf( + app->buff, + BUFF_SIZE, + "\e#Type:\e# %s", + unitemp_onewire_sensor_getModel(current_sensor)); + widget_add_text_box_element( + app->widget, 0, 16, 128, 23, AlignLeft, AlignTop, app->buff, false); + snprintf(app->buff, BUFF_SIZE, "\e#GPIO:\e# %s", s->bus->gpio->name); + widget_add_text_box_element( + app->widget, 0, 28, 128, 23, AlignLeft, AlignTop, app->buff, false); + + snprintf( + app->buff, + BUFF_SIZE, + "\e#ID:\e# %02X%02X%02X%02X%02X%02X%02X%02X", + s->deviceID[0], + s->deviceID[1], + s->deviceID[2], + s->deviceID[3], + s->deviceID[4], + s->deviceID[5], + s->deviceID[6], + s->deviceID[7]); + widget_add_text_box_element( + app->widget, 0, 40, 128, 23, AlignLeft, AlignTop, app->buff, false); + } + + if(current_sensor->type->interface == &SINGLE_WIRE) { + snprintf(app->buff, BUFF_SIZE, "\e#Type:\e# %s", current_sensor->type->typename); + widget_add_text_box_element( + app->widget, 0, 16, 128, 23, AlignLeft, AlignTop, app->buff, false); + snprintf( + app->buff, + BUFF_SIZE, + "\e#GPIO:\e# %s", + ((SingleWireSensor*)current_sensor->instance)->gpio->name); + widget_add_text_box_element( + app->widget, 0, 28, 128, 23, AlignLeft, AlignTop, app->buff, false); + } + + if(current_sensor->type->interface == &I2C) { + snprintf(app->buff, BUFF_SIZE, "\e#Type:\e# %s", current_sensor->type->typename); + widget_add_text_box_element( + app->widget, 0, 16, 128, 23, AlignLeft, AlignTop, app->buff, false); + snprintf( + app->buff, + BUFF_SIZE, + "\e#I2C addr:\e# 0x%02X", + ((I2CSensor*)current_sensor->instance)->currentI2CAdr); + widget_add_text_box_element( + app->widget, 0, 28, 128, 23, AlignLeft, AlignTop, app->buff, false); + } + + view_set_previous_callback(widget_get_view(app->widget), _delete_exit_callback); + view_dispatcher_switch_to_view(app->view_dispatcher, UnitempViewWidget); +} + +/* ========================== Помощь ========================== */ + +/** + * @brief Функция обработки нажатия кнопки "Назад" + * + * @param context Указатель на данные приложения + * @return ID вида в который нужно переключиться + */ +static uint32_t _help_exit_callback(void* context) { + UNUSED(context); + //Возвращаем ID вида, в который нужно вернуться + return UnitempViewGeneral; +} + +/** + * @brief Переключение в виджет помощи + */ +void unitemp_widget_help_switch(void) { + //Очистка виджета + widget_reset(app->widget); + + widget_add_icon_element(app->widget, 3, 7, &I_repo_qr_50x50); + widget_add_icon_element(app->widget, 71, 15, &I_DolphinCommon_56x48); + + widget_add_string_multiline_element( + app->widget, 55, 5, AlignLeft, AlignTop, FontSecondary, "You can find help\nthere"); + + widget_add_frame_element(app->widget, 0, 0, 128, 63, 7); + widget_add_frame_element(app->widget, 0, 0, 128, 64, 7); + + view_set_previous_callback(widget_get_view(app->widget), _help_exit_callback); + view_dispatcher_switch_to_view(app->view_dispatcher, UnitempViewWidget); +} + +/* ========================== О приложении ========================== */ + +/** + * @brief Переключение в виджет о приложении + */ +void unitemp_widget_about_switch(void) { + //Очистка виджета + widget_reset(app->widget); + + widget_add_frame_element(app->widget, 0, 0, 128, 63, 7); + widget_add_frame_element(app->widget, 0, 0, 128, 64, 7); + + snprintf(app->buff, BUFF_SIZE, "#Unitemp %s#", UNITEMP_APP_VER); + widget_add_text_box_element( + app->widget, 0, 4, 128, 12, AlignCenter, AlignCenter, app->buff, false); + + widget_add_text_scroll_element( + app->widget, + 4, + 16, + 121, + 44, + "Universal plugin for viewing the values of temperature\nsensors\n\e#Author: Quenon\ngithub.com/quen0n\n\e#Designer: Svaarich\ngithub.com/Svaarich\n\e#Issues & suggestions\ntiny.one/unitemp"); + + view_set_previous_callback(widget_get_view(app->widget), _help_exit_callback); + view_dispatcher_switch_to_view(app->view_dispatcher, UnitempViewWidget); +} \ No newline at end of file From 453666192c524fc5540e65a2e16d5f47cb189290 Mon Sep 17 00:00:00 2001 From: Victor Date: Mon, 19 Dec 2022 20:16:33 +0300 Subject: [PATCH 2/2] DHT monitor remove --- applications/plugins/dht_temp_sensor/DHT.c | 169 ------- applications/plugins/dht_temp_sensor/DHT.h | 40 -- .../plugins/dht_temp_sensor/application.fam | 13 - applications/plugins/dht_temp_sensor/icon.png | Bin 136 -> 0 bytes .../plugins/dht_temp_sensor/quenon_dht_mon.c | 464 ------------------ .../plugins/dht_temp_sensor/quenon_dht_mon.h | 176 ------- .../scenes/DHTMon_mainMenu_scene.c | 157 ------ .../scenes/DHTMon_main_scene.c | 40 -- .../scenes/DHTMon_sensorActions_scene.c | 194 -------- .../scenes/DHTMon_sensorEdit_scene.c | 103 ---- 10 files changed, 1356 deletions(-) delete mode 100644 applications/plugins/dht_temp_sensor/DHT.c delete mode 100644 applications/plugins/dht_temp_sensor/DHT.h delete mode 100644 applications/plugins/dht_temp_sensor/application.fam delete mode 100644 applications/plugins/dht_temp_sensor/icon.png delete mode 100644 applications/plugins/dht_temp_sensor/quenon_dht_mon.c delete mode 100644 applications/plugins/dht_temp_sensor/quenon_dht_mon.h delete mode 100644 applications/plugins/dht_temp_sensor/scenes/DHTMon_mainMenu_scene.c delete mode 100644 applications/plugins/dht_temp_sensor/scenes/DHTMon_main_scene.c delete mode 100644 applications/plugins/dht_temp_sensor/scenes/DHTMon_sensorActions_scene.c delete mode 100644 applications/plugins/dht_temp_sensor/scenes/DHTMon_sensorEdit_scene.c diff --git a/applications/plugins/dht_temp_sensor/DHT.c b/applications/plugins/dht_temp_sensor/DHT.c deleted file mode 100644 index 63a189ce1..000000000 --- a/applications/plugins/dht_temp_sensor/DHT.c +++ /dev/null @@ -1,169 +0,0 @@ -#include "DHT.h" - -#define lineDown() furi_hal_gpio_write(sensor->GPIO, false) -#define lineUp() furi_hal_gpio_write(sensor->GPIO, true) -#define getLine() furi_hal_gpio_read(sensor->GPIO) -#define Delay(d) furi_delay_ms(d) - -DHT_data DHT_getData(DHT_sensor* sensor) { - DHT_data data = {-128.0f, -128.0f}; - -#if DHT_POLLING_CONTROL == 1 - /* Ограничение по частоте опроса датчика */ - //Определение интервала опроса в зависимости от датчика - uint16_t pollingInterval; - if(sensor->type == DHT11) { - pollingInterval = DHT_POLLING_INTERVAL_DHT11; - } else { - pollingInterval = DHT_POLLING_INTERVAL_DHT22; - } - - //Если интервал маленький, то возврат последнего удачного значения - if((furi_get_tick() - sensor->lastPollingTime < pollingInterval) && - sensor->lastPollingTime != 0) { - data.hum = sensor->lastHum; - data.temp = sensor->lastTemp; - return data; - } - sensor->lastPollingTime = furi_get_tick() + 1; -#endif - - //Опускание линии данных на 18 мс - lineDown(); -#ifdef DHT_IRQ_CONTROL - //Выключение прерываний, чтобы ничто не мешало обработке данных - __disable_irq(); -#endif - Delay(18); - - //Подъём линии - lineUp(); - - /* Ожидание ответа от датчика */ - uint16_t timeout = 0; - while(!getLine()) { - timeout++; - if(timeout > DHT_TIMEOUT) { -#ifdef DHT_IRQ_CONTROL - __enable_irq(); -#endif - //Если датчик не отозвался, значит его точно нет - //Обнуление последнего удачного значения, чтобы - //не получать фантомные значения - sensor->lastHum = -128.0f; - sensor->lastTemp = -128.0f; - - return data; - } - } - //Ожидание спада - while(getLine()) { - timeout++; - if(timeout > DHT_TIMEOUT) { -#ifdef DHT_IRQ_CONTROL - __enable_irq(); -#endif - //Если датчик не отозвался, значит его точно нет - //Обнуление последнего удачного значения, чтобы - //не получать фантомные значения - sensor->lastHum = -128.0f; - sensor->lastTemp = -128.0f; - - return data; - } - } - timeout = 0; - //Ожидание подъёма - while(!getLine()) { - timeout++; - if(timeout > DHT_TIMEOUT) { - if(timeout > DHT_TIMEOUT) { -#ifdef DHT_IRQ_CONTROL - __enable_irq(); -#endif - //Если датчик не отозвался, значит его точно нет - //Обнуление последнего удачного значения, чтобы - //не получать фантомные значения - sensor->lastHum = -128.0f; - sensor->lastTemp = -128.0f; - - return data; - } - } - } - timeout = 0; - //Ожидание спада - while(getLine()) { - timeout++; - if(timeout > DHT_TIMEOUT) { -#ifdef DHT_IRQ_CONTROL - __enable_irq(); -#endif - //Если датчик не отозвался, значит его точно нет - //Обнуление последнего удачного значения, чтобы - //не получать фантомные значения - sensor->lastHum = -128.0f; - sensor->lastTemp = -128.0f; - return data; - } - } - - /* Чтение ответа от датчика */ - uint8_t rawData[5] = {0, 0, 0, 0, 0}; - for(uint8_t a = 0; a < 5; a++) { - for(uint8_t b = 7; b != 255; b--) { - uint16_t hT = 0, lT = 0; - //Пока линия в низком уровне, инкремент переменной lT - while(!getLine() && lT != 65535) lT++; - //Пока линия в высоком уровне, инкремент переменной hT - timeout = 0; - while(getLine() && hT != 65535) hT++; - //Если hT больше lT, то пришла единица - if(hT > lT) rawData[a] |= (1 << b); - } - } -#ifdef DHT_IRQ_CONTROL - //Включение прерываний после приёма данных - __enable_irq(); -#endif - /* Проверка целостности данных */ - if((uint8_t)(rawData[0] + rawData[1] + rawData[2] + rawData[3]) == rawData[4]) { - //Если контрольная сумма совпадает, то конвертация и возврат полученных значений - if(sensor->type == DHT22) { - data.hum = (float)(((uint16_t)rawData[0] << 8) | rawData[1]) * 0.1f; - //Проверка на отрицательность температуры - if(!(rawData[2] & (1 << 7))) { - data.temp = (float)(((uint16_t)rawData[2] << 8) | rawData[3]) * 0.1f; - } else { - rawData[2] &= ~(1 << 7); - data.temp = (float)(((uint16_t)rawData[2] << 8) | rawData[3]) * -0.1f; - } - } - if(sensor->type == DHT11) { - data.hum = (float)rawData[0]; - data.temp = (float)rawData[2]; - //DHT11 производства ASAIR имеют дробную часть в температуре - //А ещё температуру измеряет от -20 до +60 *С - //Вот прикол, да? - if(rawData[3] != 0) { - //Проверка знака - if(!(rawData[3] & (1 << 7))) { - //Добавление положительной дробной части - data.temp += rawData[3] * 0.1f; - } else { - //А тут делаем отрицательное значение - rawData[3] &= ~(1 << 7); - data.temp += rawData[3] * 0.1f; - data.temp *= -1; - } - } - } - } - -#if DHT_POLLING_CONTROL == 1 - sensor->lastHum = data.hum; - sensor->lastTemp = data.temp; -#endif - - return data; -} \ No newline at end of file diff --git a/applications/plugins/dht_temp_sensor/DHT.h b/applications/plugins/dht_temp_sensor/DHT.h deleted file mode 100644 index 409847d8b..000000000 --- a/applications/plugins/dht_temp_sensor/DHT.h +++ /dev/null @@ -1,40 +0,0 @@ -#ifndef DHT_H_ -#define DHT_H_ - -#include - -/* Настройки */ -#define DHT_TIMEOUT 65534 //Количество итераций, после которых функция вернёт пустые значения -#define DHT_POLLING_CONTROL 1 //Включение проверки частоты опроса датчика -#define DHT_POLLING_INTERVAL_DHT11 \ - 2000 //Интервал опроса DHT11 (0.5 Гц по даташиту). Можно поставить 1500, будет работать -//Костыль, временно 2 секунды для датчика AM2302 -#define DHT_POLLING_INTERVAL_DHT22 2000 //Интервал опроса DHT22 (1 Гц по даташиту) -#define DHT_IRQ_CONTROL //Выключать прерывания во время обмена данных с датчиком -/* Структура возвращаемых датчиком данных */ -typedef struct { - float hum; - float temp; -} DHT_data; - -/* Тип используемого датчика */ -typedef enum { DHT11, DHT22 } DHT_type; - -/* Структура объекта датчика */ -typedef struct { - char name[11]; - const GpioPin* GPIO; //Пин датчика - DHT_type type; //Тип датчика (DHT11 или DHT22) - -//Контроль частоты опроса датчика. Значения не заполнять! -#if DHT_POLLING_CONTROL == 1 - uint32_t lastPollingTime; //Время последнего опроса датчика - float lastTemp; //Последнее значение температуры - float lastHum; //Последнее значение влажности -#endif -} DHT_sensor; - -/* Прототипы функций */ -DHT_data DHT_getData(DHT_sensor* sensor); //Получить данные с датчика - -#endif diff --git a/applications/plugins/dht_temp_sensor/application.fam b/applications/plugins/dht_temp_sensor/application.fam deleted file mode 100644 index ad74919a4..000000000 --- a/applications/plugins/dht_temp_sensor/application.fam +++ /dev/null @@ -1,13 +0,0 @@ -App( - appid="quenon_dht_mon", - name="[DHT] Temp. Monitor", - apptype=FlipperAppType.EXTERNAL, - entry_point="quenon_dht_mon_app", - cdefines=["QUENON_DHT_MON"], - requires=[ - "gui", - ], - fap_category="GPIO", - fap_icon="icon.png", - stack_size=2 * 1024, -) \ No newline at end of file diff --git a/applications/plugins/dht_temp_sensor/icon.png b/applications/plugins/dht_temp_sensor/icon.png deleted file mode 100644 index 0e87c26c256489c6221cdaf061792ee277ce6e56..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 136 zcmeAS@N?(olHy`uVBq!ia0vp^AT}2V8<6ZZI=>f4F%}28J29*~C-V}>aq@I=43U_+ zx8ISEL4o6N+sFU$>(qHA3rgKDl`1k!5fYrFVx4Wt!coyGcE-)0vG!B*N4rVfH{*IP i*Kn78uAcSj5F^V%CH{xExxNBTWAJqKb6Mw<&;$T>2`Y2| diff --git a/applications/plugins/dht_temp_sensor/quenon_dht_mon.c b/applications/plugins/dht_temp_sensor/quenon_dht_mon.c deleted file mode 100644 index bbb653988..000000000 --- a/applications/plugins/dht_temp_sensor/quenon_dht_mon.c +++ /dev/null @@ -1,464 +0,0 @@ -#include "quenon_dht_mon.h" -#include - -//Порты ввода/вывода, которые не были обозначены в общем списке -const GpioPin SWC_10 = {.pin = LL_GPIO_PIN_14, .port = GPIOA}; -const GpioPin SIO_12 = {.pin = LL_GPIO_PIN_13, .port = GPIOA}; -const GpioPin TX_13 = {.pin = LL_GPIO_PIN_6, .port = GPIOB}; -const GpioPin RX_14 = {.pin = LL_GPIO_PIN_7, .port = GPIOB}; - -//Количество доступных портов ввода/вывода -#define GPIO_ITEMS (sizeof(gpio_item) / sizeof(GpioItem)) - -//Перечень достуных портов ввода/вывода -static const GpioItem gpio_item[] = { - {2, "2 (A7)", &gpio_ext_pa7}, - {3, "3 (A6)", &gpio_ext_pa6}, - {4, "4 (A4)", &gpio_ext_pa4}, - {5, "5 (B3)", &gpio_ext_pb3}, - {6, "6 (B2)", &gpio_ext_pb2}, - {7, "7 (C3)", &gpio_ext_pc3}, - {10, " 10(SWC) ", &SWC_10}, - {12, "12 (SIO)", &SIO_12}, - {13, "13 (TX)", &TX_13}, - {14, "14 (RX)", &RX_14}, - {15, "15 (C1)", &gpio_ext_pc1}, - {16, "16 (C0)", &gpio_ext_pc0}, - {17, "17 (1W)", &ibutton_gpio}}; - -//Данные плагина -static PluginData* app; - -uint8_t DHTMon_GPIO_to_int(const GpioPin* gpio) { - if(gpio == NULL) return 255; - for(uint8_t i = 0; i < GPIO_ITEMS; i++) { - if(gpio_item[i].pin->pin == gpio->pin && gpio_item[i].pin->port == gpio->port) { - return gpio_item[i].num; - } - } - return 255; -} - -const GpioPin* DHTMon_GPIO_form_int(uint8_t name) { - for(uint8_t i = 0; i < GPIO_ITEMS; i++) { - if(gpio_item[i].num == name) { - return gpio_item[i].pin; - } - } - return NULL; -} - -const GpioPin* DHTMon_GPIO_from_index(uint8_t index) { - if(index > GPIO_ITEMS) return NULL; - return gpio_item[index].pin; -} - -uint8_t DHTMon_GPIO_to_index(const GpioPin* gpio) { - if(gpio == NULL) return 255; - for(uint8_t i = 0; i < GPIO_ITEMS; i++) { - if(gpio_item[i].pin->pin == gpio->pin && gpio_item[i].pin->port == gpio->port) { - return i; - } - } - return 255; -} - -const char* DHTMon_GPIO_getName(const GpioPin* gpio) { - if(gpio == NULL) return NULL; - for(uint8_t i = 0; i < GPIO_ITEMS; i++) { - if(gpio_item[i].pin->pin == gpio->pin && gpio_item[i].pin->port == gpio->port) { - return gpio_item[i].name; - } - } - return NULL; -} - -void DHTMon_sensors_init(void) { - //Включение 5V если на порту 1 FZ его нет - if(furi_hal_power_is_otg_enabled() != true) { - furi_hal_power_enable_otg(); - } - - //Настройка GPIO загруженных датчиков - for(uint8_t i = 0; i < app->sensors_count; i++) { - //Высокий уровень по умолчанию - furi_hal_gpio_write(app->sensors[i].GPIO, true); - //Режим работы - OpenDrain, подтяжка включается на всякий случай - furi_hal_gpio_init( - app->sensors[i].GPIO, //Порт FZ - GpioModeOutputOpenDrain, //Режим работы - открытый сток - GpioPullUp, //Принудительная подтяжка линии данных к питанию - GpioSpeedVeryHigh); //Скорость работы - максимальная - } -} - -void DHTMon_sensors_deinit(void) { - //Возврат исходного состояния 5V - if(app->last_OTG_State != true) { - furi_hal_power_disable_otg(); - } - - //Перевод портов GPIO в состояние по умолчанию - for(uint8_t i = 0; i < app->sensors_count; i++) { - furi_hal_gpio_init( - app->sensors[i].GPIO, //Порт FZ - GpioModeAnalog, //Режим работы - аналог - GpioPullNo, //Отключение подтяжки - GpioSpeedLow); //Скорость работы - низкая - //Установка низкого уровня - furi_hal_gpio_write(app->sensors[i].GPIO, false); - } -} - -bool DHTMon_sensor_check(DHT_sensor* sensor) { - /* Проверка имени */ - //1) Строка должна быть длиной от 1 до 10 символов - //2) Первый символ строки должен быть только 0-9, A-Z, a-z и _ - if(strlen(sensor->name) == 0 || strlen(sensor->name) > 10 || - (!(sensor->name[0] >= '0' && sensor->name[0] <= '9') && - !(sensor->name[0] >= 'A' && sensor->name[0] <= 'Z') && - !(sensor->name[0] >= 'a' && sensor->name[0] <= 'z') && !(sensor->name[0] == '_'))) { - FURI_LOG_D(APP_NAME, "Sensor [%s] name check failed\r\n", sensor->name); - return false; - } - //Проверка GPIO - if(DHTMon_GPIO_to_int(sensor->GPIO) == 255) { - FURI_LOG_D( - APP_NAME, - "Sensor [%s] GPIO check failed: %d\r\n", - sensor->name, - DHTMon_GPIO_to_int(sensor->GPIO)); - return false; - } - //Проверка типа датчика - if(sensor->type != DHT11 && sensor->type != DHT22) { - FURI_LOG_D(APP_NAME, "Sensor [%s] type check failed: %d\r\n", sensor->name, sensor->type); - return false; - } - - //Возврат истины если всё ок - FURI_LOG_D(APP_NAME, "Sensor [%s] all checks passed\r\n", sensor->name); - return true; -} - -void DHTMon_sensor_delete(DHT_sensor* sensor) { - if(sensor == NULL) return; - //Делаем параметры датчика неверными - sensor->name[0] = '\0'; - sensor->type = 255; - //Теперь сохраняем текущие датчики. Сохранятор не сохранит неисправный датчик - DHTMon_sensors_save(); - //Перезагружаемся с SD-карты - DHTMon_sensors_reload(); -} - -uint8_t DHTMon_sensors_save(void) { - //Выделение памяти для потока - app->file_stream = file_stream_alloc(app->storage); - uint8_t savedSensorsCount = 0; - //Переменная пути к файлу - FuriString* filepath = furi_string_alloc(); - //Составление пути к файлу - furi_string_printf(filepath, "%s/%s", APP_PATH_FOLDER, APP_FILENAME); - - //Открытие потока. Если поток открылся, то выполнение сохранения датчиков - if(file_stream_open( - app->file_stream, furi_string_get_cstr(filepath), FSAM_READ_WRITE, FSOM_CREATE_ALWAYS)) { - const char template[] = - "#DHT monitor sensors file\n#Name - name of sensor. Up to 10 sumbols\n#Type - type of sensor. DHT11 - 0, DHT22 - 1\n#GPIO - connection port. May being 2-7, 10, 12-17\n#Name Type GPIO\n"; - stream_write(app->file_stream, (uint8_t*)template, strlen(template)); - //Сохранение датчиков - for(uint8_t i = 0; i < app->sensors_count; i++) { - //Если параметры датчика верны, то сохраняемся - if(DHTMon_sensor_check(&app->sensors[i])) { - stream_write_format( - app->file_stream, - "%s %d %d\n", - app->sensors[i].name, - app->sensors[i].type, - DHTMon_GPIO_to_int(app->sensors[i].GPIO)); - savedSensorsCount++; - } - } - } else { - //TODO: печать ошибки на экран - FURI_LOG_E(APP_NAME, "cannot create sensors file\r\n"); - } - stream_free(app->file_stream); - - return savedSensorsCount; -} - -bool DHTMon_sensors_load(void) { - //Обнуление количества датчиков - app->sensors_count = -1; - //Очистка предыдущих датчиков - memset(app->sensors, 0, sizeof(app->sensors)); - - //Открытие файла на SD-карте - //Выделение памяти для потока - app->file_stream = file_stream_alloc(app->storage); - //Переменная пути к файлу - FuriString* filepath = furi_string_alloc(); - //Составление пути к файлу - furi_string_printf(filepath, "%s/%s", APP_PATH_FOLDER, APP_FILENAME); - //Открытие потока к файлу - if(!file_stream_open( - app->file_stream, furi_string_get_cstr(filepath), FSAM_READ_WRITE, FSOM_OPEN_EXISTING)) { - //Если файл отсутствует, то создание болванки - FURI_LOG_W(APP_NAME, "Missing sensors file. Creating new file\r\n"); - app->sensors_count = 0; - stream_free(app->file_stream); - DHTMon_sensors_save(); - return false; - } - //Вычисление размера файла - size_t file_size = stream_size(app->file_stream); - if(file_size == (size_t)0) { - //Выход если файл пустой - FURI_LOG_W(APP_NAME, "Sensors file is empty\r\n"); - app->sensors_count = 0; - stream_free(app->file_stream); - return false; - } - - //Выделение памяти под загрузку файла - uint8_t* file_buf = malloc(file_size); - //Опустошение буфера файла - memset(file_buf, 0, file_size); - //Загрузка файла - if(stream_read(app->file_stream, file_buf, file_size) != file_size) { - //Выход при ошибке чтения - FURI_LOG_E(APP_NAME, "Error reading sensor file\r\n"); - app->sensors_count = 0; - stream_free(app->file_stream); - return false; - } - //Построчное чтение файла - //Указатель на начало строки - FuriString* file = furi_string_alloc_set_str((char*)file_buf); - //Сколько байт до конца строки - size_t line_end = 0; - while(line_end != STRING_FAILURE && app->sensors_count < MAX_SENSORS) { - if(((char*)(file_buf + line_end))[1] != '#') { - DHT_sensor s = {0}; - int type, port; - char name[11] = {0}; - sscanf(((char*)(file_buf + line_end)), "%s %d %d", name, &type, &port); - s.type = type; - s.GPIO = DHTMon_GPIO_form_int(port); - - name[10] = '\0'; - strcpy(s.name, name); - //Если данные корректны, то - if(DHTMon_sensor_check(&s) == true) { - //Установка нуля при первом датчике - if(app->sensors_count == -1) app->sensors_count = 0; - //Добавление датчика в общий список - app->sensors[app->sensors_count] = s; - //Увеличение количества загруженных датчиков - app->sensors_count++; - } - } - line_end = furi_string_search_char(file, '\n', line_end + 1); - } - stream_free(app->file_stream); - free(file_buf); - - //Обнуление количества датчиков если ни один из них не был загружен - if(app->sensors_count == -1) app->sensors_count = 0; - - //Инициализация портов датчиков если таковые есть - if(app->sensors_count > 0) { - DHTMon_sensors_init(); - return true; - } else { - return false; - } - return false; -} - -bool DHTMon_sensors_reload(void) { - DHTMon_sensors_deinit(); - return DHTMon_sensors_load(); -} - -/** - * @brief Обработчик отрисовки экрана - * - * @param canvas Указатель на холст - * @param ctx Данные плагина - */ -static void render_callback(Canvas* const canvas, void* ctx) { - PluginData* app = acquire_mutex((ValueMutex*)ctx, 25); - if(app == NULL) { - return; - } - //Вызов отрисовки главного экрана - scene_main(canvas, app); - - release_mutex((ValueMutex*)ctx, app); -} - -/** - * @brief Обработчик нажатия кнопок главного экрана - * - * @param input_event Указатель на событие - * @param event_queue Указатель на очередь событий - */ -static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) { - furi_assert(event_queue); - - PluginEvent event = {.type = EventTypeKey, .input = *input_event}; - furi_message_queue_put(event_queue, &event, FuriWaitForever); -} - -/** - * @brief Выделение места под переменные плагина - * - * @return true Если всё прошло успешно - * @return false Если в процессе загрузки произошла ошибка - */ -static bool DHTMon_alloc(void) { - //Выделение места под данные плагина - app = malloc(sizeof(PluginData)); - //Выделение места под очередь событий - app->event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent)); - - //Обнуление количества датчиков - app->sensors_count = -1; - - //Инициализация мутекса - if(!init_mutex(&app->state_mutex, app, sizeof(PluginData))) { - FURI_LOG_E(APP_NAME, "cannot create mutex\r\n"); - return false; - } - - // Set system callbacks - app->view_port = view_port_alloc(); - view_port_draw_callback_set(app->view_port, render_callback, &app->state_mutex); - view_port_input_callback_set(app->view_port, input_callback, app->event_queue); - - // Open GUI and register view_port - app->gui = furi_record_open(RECORD_GUI); - gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen); - - app->view_dispatcher = view_dispatcher_alloc(); - - sensorActions_sceneCreate(app); - sensorEdit_sceneCreate(app); - - app->widget = widget_alloc(); - view_dispatcher_add_view(app->view_dispatcher, WIDGET_VIEW, widget_get_view(app->widget)); - - app->text_input = text_input_alloc(); - view_dispatcher_add_view( - app->view_dispatcher, TEXTINPUT_VIEW, text_input_get_view(app->text_input)); - - view_dispatcher_enable_queue(app->view_dispatcher); - view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen); - - //Уведомления - app->notifications = furi_record_open(RECORD_NOTIFICATION); - - //Подготовка хранилища - app->storage = furi_record_open(RECORD_STORAGE); - storage_common_mkdir(app->storage, APP_PATH_FOLDER); - app->file_stream = file_stream_alloc(app->storage); - - return true; -} - -/** - * @brief Освыбождение памяти после работы приложения - */ -static void DHTMon_free(void) { - //Автоматическое управление подсветкой - notification_message(app->notifications, &sequence_display_backlight_enforce_auto); - - furi_record_close(RECORD_STORAGE); - furi_record_close(RECORD_NOTIFICATION); - - text_input_free(app->text_input); - widget_free(app->widget); - sensorEdit_sceneRemove(); - sensorActions_screneRemove(); - view_dispatcher_free(app->view_dispatcher); - - furi_record_close(RECORD_GUI); - - view_port_enabled_set(app->view_port, false); - gui_remove_view_port(app->gui, app->view_port); - - view_port_free(app->view_port); - furi_message_queue_free(app->event_queue); - delete_mutex(&app->state_mutex); - - free(app); -} - -/** - * @brief Точка входа в приложение - * - * @return Код ошибки - */ -int32_t quenon_dht_mon_app() { - if(!DHTMon_alloc()) { - DHTMon_free(); - return 255; - } - //Постоянное свечение подсветки - notification_message(app->notifications, &sequence_display_backlight_enforce_on); - //Сохранение состояния наличия 5V на порту 1 FZ - app->last_OTG_State = furi_hal_power_is_otg_enabled(); - - //Загрузка датчиков с SD-карты - DHTMon_sensors_load(); - - app->currentSensorEdit = &app->sensors[0]; - - PluginEvent event; - for(bool processing = true; processing;) { - FuriStatus event_status = furi_message_queue_get(app->event_queue, &event, 100); - - acquire_mutex_block(&app->state_mutex); - - if(event_status == FuriStatusOk) { - // press events - if(event.type == EventTypeKey) { - if(event.input.type == InputTypePress) { - switch(event.input.key) { - case InputKeyUp: - break; - case InputKeyDown: - break; - case InputKeyRight: - break; - case InputKeyLeft: - break; - case InputKeyOk: - view_port_update(app->view_port); - release_mutex(&app->state_mutex, app); - mainMenu_scene(app); - break; - case InputKeyBack: - processing = false; - break; - default: - break; - } - } - } - } - - view_port_update(app->view_port); - release_mutex(&app->state_mutex, app); - } - //Освобождение памяти и деинициализация - DHTMon_sensors_deinit(); - DHTMon_free(); - - return 0; -} -//TODO: Обработка ошибок -//TODO: Пропуск использованных портов в меню добавления датчиков \ No newline at end of file diff --git a/applications/plugins/dht_temp_sensor/quenon_dht_mon.h b/applications/plugins/dht_temp_sensor/quenon_dht_mon.h deleted file mode 100644 index 4e888f6c1..000000000 --- a/applications/plugins/dht_temp_sensor/quenon_dht_mon.h +++ /dev/null @@ -1,176 +0,0 @@ -#ifndef QUENON_DHT_MON -#define QUENON_DHT_MON - -#include -#include - -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include -#include - -#include "DHT.h" - -#define APP_NAME "DHT_monitor" -#define APP_PATH_FOLDER "/ext/dht_monitor" -#define APP_FILENAME "sensors.txt" -#define MAX_SENSORS 5 - -// //Виды менюшек -typedef enum { - MAIN_MENU_VIEW, - ADDSENSOR_MENU_VIEW, - TEXTINPUT_VIEW, - SENSOR_ACTIONS_VIEW, - WIDGET_VIEW, -} MENU_VIEWS; - -typedef enum { - EventTypeTick, - EventTypeKey, -} EventType; - -typedef struct { - EventType type; - InputEvent input; -} PluginEvent; - -typedef struct { - const uint8_t num; - const char* name; - const GpioPin* pin; -} GpioItem; - -//Структура с данными плагина -typedef struct { - //Очередь сообщений - FuriMessageQueue* event_queue; - //Мутекс - ValueMutex state_mutex; - //Вьюпорт - ViewPort* view_port; - //GUI - Gui* gui; - NotificationApp* notifications; - ViewDispatcher* view_dispatcher; - View* view; - TextInput* text_input; - VariableItem* item; - Widget* widget; - - char txtbuff[30]; //Буффер для печати строк на экране - bool last_OTG_State; //Состояние OTG до запуска приложения - Storage* storage; //Хранилище датчиков - Stream* file_stream; //Поток файла с датчиками - int8_t sensors_count; // Количество загруженных датчиков - DHT_sensor sensors[MAX_SENSORS]; //Сохранённые датчики - DHT_data data; //Инфа из датчика - DHT_sensor* currentSensorEdit; //Указатель на редактируемый датчик - -} PluginData; - -/* ================== Работа с GPIO ================== */ -/** - * @brief Конвертация GPIO в его номер на корпусе FZ - * - * @param gpio Указатель на преобразовываемый GPIO - * @return Номер порта на корпусе FZ - */ -uint8_t DHTMon_GPIO_to_int(const GpioPin* gpio); -/** - * @brief Конвертация номера порта на корпусе FZ в GPIO - * - * @param name Номер порта на корпусе FZ - * @return Указатель на GPIO при успехе, NULL при ошибке - */ -const GpioPin* DHTMon_GPIO_form_int(uint8_t name); -/** - * @brief Преобразование порядкового номера порта в GPIO - * - * @param index Индекс порта от 0 до GPIO_ITEMS-1 - * @return Указатель на GPIO при успехе, NULL при ошибке - */ -const GpioPin* DHTMon_GPIO_from_index(uint8_t index); -/** - * @brief Преобразование GPIO в порядковый номер порта - * - * @param gpio Указатель на GPIO - * @return index при успехе, 255 при ошибке - */ -uint8_t DHTMon_GPIO_to_index(const GpioPin* gpio); - -/** - * @brief Получить имя GPIO в виде строки - * - * @param gpio Искомый порт - * @return char* Указатель на строку с именем порта - */ -const char* DHTMon_GPIO_getName(const GpioPin* gpio); - -/* ================== Работа с датчиками ================== */ -/** - * @brief Инициализация портов ввода/вывода датчиков - */ -void DHTMon_sensors_init(void); -/** - * @brief Функция деинициализации портов ввода/вывода датчиков - */ -void DHTMon_sensors_deinit(void); -/** - * @brief Проверка корректности параметров датчика - * - * @param sensor Указатель на проверяемый датчик - * @return true Параметры датчика корректные - * @return false Параметры датчика некорректные - */ -bool DHTMon_sensor_check(DHT_sensor* sensor); -/** - * @brief Удаление датчика из списка и перезагрузка - * - * @param sensor Указатель на удаляемый датчик - */ -void DHTMon_sensor_delete(DHT_sensor* sensor); -/** - * @brief Сохранение датчиков на SD-карту - * - * @return Количество сохранённых датчиков - */ -uint8_t DHTMon_sensors_save(void); -/** - * @brief Загрузка датчиков с SD-карты - * - * @return true Был загружен хотя бы 1 датчик - * @return false Датчики отсутствуют - */ -bool DHTMon_sensors_load(void); -/** - * @brief Перезагрузка датчиков с SD-карты - * - * @return true Когда был загружен хотя бы 1 датчик - * @return false Ни один из датчиков не был загружен - */ -bool DHTMon_sensors_reload(void); - -void scene_main(Canvas* const canvas, PluginData* app); -void mainMenu_scene(PluginData* app); - -void sensorEdit_sceneCreate(PluginData* app); -void sensorEdit_scene(PluginData* app); -void sensorEdit_sceneRemove(void); - -void sensorActions_sceneCreate(PluginData* app); -void sensorActions_scene(PluginData* app); -void sensorActions_screneRemove(void); -#endif \ No newline at end of file diff --git a/applications/plugins/dht_temp_sensor/scenes/DHTMon_mainMenu_scene.c b/applications/plugins/dht_temp_sensor/scenes/DHTMon_mainMenu_scene.c deleted file mode 100644 index 26ac9ca89..000000000 --- a/applications/plugins/dht_temp_sensor/scenes/DHTMon_mainMenu_scene.c +++ /dev/null @@ -1,157 +0,0 @@ -#include "../quenon_dht_mon.h" -//Текущий вид -static View* view; -//Список -static VariableItemList* variable_item_list; - -/** - * @brief Функция обработки нажатия кнопки "Назад" - * - * @param context Указатель на данные приложения - * @return ID вида в который нужно переключиться - */ -static uint32_t actions_exitCallback(void* context) { - PluginData* app = context; - UNUSED(app); - //Возвращаем ID вида, в который нужно вернуться - return VIEW_NONE; -} -/** - * @brief Функция обработки нажатия средней кнопки - * - * @param context Указатель на данные приложения - * @param index На каком элементе списка была нажата кнопка - */ -static void enterCallback(void* context, uint32_t index) { - PluginData* app = context; - if((uint8_t)index < (uint8_t)app->sensors_count) { - app->currentSensorEdit = &app->sensors[index]; - sensorActions_scene(app); - } - if((uint8_t)index == (uint8_t)app->sensors_count) { - app->currentSensorEdit = &app->sensors[app->sensors_count++]; - strcpy(app->currentSensorEdit->name, "NewSensor"); - app->currentSensorEdit->GPIO = DHTMon_GPIO_from_index(0); - app->currentSensorEdit->type = DHT11; - sensorEdit_scene(app); - } -} - -/** - * @brief Создание списка действий с указанным датчиком - * - * @param app Указатель на данные плагина - */ -void mainMenu_scene(PluginData* app) { - variable_item_list = variable_item_list_alloc(); - //Сброс всех элементов меню - variable_item_list_reset(variable_item_list); - //Добавление названий датчиков в качестве элементов списка - for(uint8_t i = 0; i < app->sensors_count; i++) { - variable_item_list_add(variable_item_list, app->sensors[i].name, 1, NULL, NULL); - } - if(app->sensors_count < (uint8_t)MAX_SENSORS) { - variable_item_list_add(variable_item_list, " + Add new sensor +", 1, NULL, NULL); - } - - //Добавление колбека на нажатие средней кнопки - variable_item_list_set_enter_callback(variable_item_list, enterCallback, app); - - //Создание вида из списка - view = variable_item_list_get_view(variable_item_list); - //Добавление колбека на нажатие кнопки "Назад" - view_set_previous_callback(view, actions_exitCallback); - //Добавление вида в диспетчер - view_dispatcher_add_view(app->view_dispatcher, MAIN_MENU_VIEW, view); - - view_dispatcher_enable_queue(app->view_dispatcher); - - //Переключение на наш вид - view_dispatcher_switch_to_view(app->view_dispatcher, MAIN_MENU_VIEW); - - //Запуск диспетчера - view_dispatcher_run(app->view_dispatcher); - - //Очистка списка элементов - variable_item_list_free(variable_item_list); - //Удаление вида после обработки - view_dispatcher_remove_view(app->view_dispatcher, MAIN_MENU_VIEW); -} - -/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/ - -// static VariableItemList* variable_item_list; -// /* ============== Главное меню ============== */ -// static uint32_t mainMenu_exitCallback(void* context) { -// UNUSED(context); -// variable_item_list_free(variable_item_list); -// DHT_sensors_reload(); -// return VIEW_NONE; -// } -// static void mainMenu_enterCallback(void* context, uint32_t index) { -// PluginData* app = context; -// if((uint8_t)index == (uint8_t)app->sensors_count) { -// addSensor_scene(app); -// view_dispatcher_run(app->view_dispatcher); -// } -// } -// void mainMenu_scene(PluginData* app) { -// variable_item_list = variable_item_list_alloc(); -// variable_item_list_reset(variable_item_list); -// for(uint8_t i = 0; i < app->sensors_count; i++) { -// variable_item_list_add(variable_item_list, app->sensors[i].name, 1, NULL, NULL); -// } -// variable_item_list_add(variable_item_list, "+ Add new sensor +", 1, NULL, NULL); - -// app->view = variable_item_list_get_view(variable_item_list); -// app->view_dispatcher = view_dispatcher_alloc(); - -// view_dispatcher_enable_queue(app->view_dispatcher); -// view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen); -// view_dispatcher_add_view(app->view_dispatcher, MAIN_MENU_VIEW, app->view); -// view_dispatcher_switch_to_view(app->view_dispatcher, MAIN_MENU_VIEW); - -// variable_item_list_set_enter_callback(variable_item_list, mainMenu_enterCallback, app); -// view_set_previous_callback(app->view, mainMenu_exitCallback); -// } \ No newline at end of file diff --git a/applications/plugins/dht_temp_sensor/scenes/DHTMon_main_scene.c b/applications/plugins/dht_temp_sensor/scenes/DHTMon_main_scene.c deleted file mode 100644 index aab343752..000000000 --- a/applications/plugins/dht_temp_sensor/scenes/DHTMon_main_scene.c +++ /dev/null @@ -1,40 +0,0 @@ -#include "../quenon_dht_mon.h" - -/* ============== Главный экран ============== */ -void scene_main(Canvas* const canvas, PluginData* app) { - //Рисование бара - canvas_draw_box(canvas, 0, 0, 128, 14); - canvas_set_color(canvas, ColorWhite); - canvas_set_font(canvas, FontPrimary); - canvas_draw_str(canvas, 32, 11, "DHT Monitor"); - - canvas_set_color(canvas, ColorBlack); - if(app->sensors_count > 0) { - if(!furi_hal_power_is_otg_enabled()) { - furi_hal_power_enable_otg(); - } - for(uint8_t i = 0; i < app->sensors_count; i++) { - app->data = DHT_getData(&app->sensors[i]); - - canvas_set_font(canvas, FontPrimary); - canvas_draw_str(canvas, 0, 24 + 10 * i, app->sensors[i].name); - - canvas_set_font(canvas, FontSecondary); - if(app->data.hum == -128.0f && app->data.temp == -128.0f) { - canvas_draw_str(canvas, 96, 24 + 10 * i, "timeout"); - } else { - snprintf( - app->txtbuff, - sizeof(app->txtbuff), - "%2.1f*C/%d%%", - (double)app->data.temp, - (int8_t)app->data.hum); - canvas_draw_str(canvas, 64, 24 + 10 * i, app->txtbuff); - } - } - } else { - canvas_set_font(canvas, FontSecondary); - if(app->sensors_count == 0) canvas_draw_str(canvas, 0, 24, "Sensors not found"); - if(app->sensors_count == -1) canvas_draw_str(canvas, 0, 24, "Loading..."); - } -} diff --git a/applications/plugins/dht_temp_sensor/scenes/DHTMon_sensorActions_scene.c b/applications/plugins/dht_temp_sensor/scenes/DHTMon_sensorActions_scene.c deleted file mode 100644 index ae7674f70..000000000 --- a/applications/plugins/dht_temp_sensor/scenes/DHTMon_sensorActions_scene.c +++ /dev/null @@ -1,194 +0,0 @@ -#include "../quenon_dht_mon.h" - -//Текущий вид -static View* view; -//Список -static VariableItemList* variable_item_list; - -/* ================== Информация о датчике ================== */ -/** - * @brief Функция обработки нажатия кнопки "Назад" - * - * @param context Указатель на данные приложения - * @return ID вида в который нужно переключиться - */ -static uint32_t infoWidget_exitCallback(void* context) { - PluginData* app = context; - UNUSED(app); - //Возвращаем ID вида, в который нужно вернуться - return SENSOR_ACTIONS_VIEW; -} -/** - * @brief Обработчик нажатий на кнопку в виджете - * - * @param result Какая из кнопок была нажата - * @param type Тип нажатия - * @param context Указатель на данные плагина - */ -static void infoWidget_callback(GuiButtonType result, InputType type, void* context) { - PluginData* app = context; - //Коротко нажата левая кнопка (Back) - if(result == GuiButtonTypeLeft && type == InputTypeShort) { - view_dispatcher_switch_to_view(app->view_dispatcher, SENSOR_ACTIONS_VIEW); - } -} -/** - * @brief Создание виджета информации о датчике - * - * @param app Указатель на данные плагина - */ -static void sensorInfo_widget(PluginData* app) { - //Очистка виджета - widget_reset(app->widget); - //Добавление кнопок - widget_add_button_element(app->widget, GuiButtonTypeLeft, "Back", infoWidget_callback, app); - - char str[32]; - snprintf(str, sizeof(str), "\e#%s\e#", app->currentSensorEdit->name); - widget_add_text_box_element(app->widget, 0, 0, 128, 23, AlignCenter, AlignCenter, str, false); - snprintf(str, sizeof(str), "\e#Type:\e# %s", app->currentSensorEdit->type ? "DHT22" : "DHT11"); - widget_add_text_box_element(app->widget, 0, 0, 128, 47, AlignLeft, AlignCenter, str, false); - snprintf( - str, sizeof(str), "\e#GPIO:\e# %s", DHTMon_GPIO_getName(app->currentSensorEdit->GPIO)); - widget_add_text_box_element(app->widget, 0, 0, 128, 72, AlignLeft, AlignCenter, str, false); - view_set_previous_callback(widget_get_view(app->widget), infoWidget_exitCallback); - view_dispatcher_switch_to_view(app->view_dispatcher, WIDGET_VIEW); -} - -/* ================== Подтверждение удаления ================== */ -/** - * @brief Функция обработки нажатия кнопки "Назад" - * - * @param context Указатель на данные приложения - * @return ID вида в который нужно переключиться - */ -static uint32_t deleteWidget_exitCallback(void* context) { - PluginData* app = context; - UNUSED(app); - //Возвращаем ID вида, в который нужно вернуться - return SENSOR_ACTIONS_VIEW; -} -/** - * @brief Обработчик нажатий на кнопку в виджете - * - * @param result Какая из кнопок была нажата - * @param type Тип нажатия - * @param context Указатель на данные плагина - */ -static void deleteWidget_callback(GuiButtonType result, InputType type, void* context) { - PluginData* app = context; - //Коротко нажата левая кнопка (Cancel) - if(result == GuiButtonTypeLeft && type == InputTypeShort) { - view_dispatcher_switch_to_view(app->view_dispatcher, SENSOR_ACTIONS_VIEW); - } - //Коротко нажата правая кнопка (Delete) - if(result == GuiButtonTypeRight && type == InputTypeShort) { - //Удаление датчика - DHTMon_sensor_delete(app->currentSensorEdit); - //Выход из меню - view_dispatcher_switch_to_view(app->view_dispatcher, VIEW_NONE); - } -} -/** - * @brief Создание виджета удаления датчика - * - * @param app Указатель на данные плагина - */ -static void sensorDelete_widget(PluginData* app) { - //Очистка виджета - widget_reset(app->widget); - //Добавление кнопок - widget_add_button_element( - app->widget, GuiButtonTypeLeft, "Cancel", deleteWidget_callback, app); - widget_add_button_element( - app->widget, GuiButtonTypeRight, "Delete", deleteWidget_callback, app); - - char delete_str[32]; - snprintf(delete_str, sizeof(delete_str), "\e#Delete %s?\e#", app->currentSensorEdit->name); - widget_add_text_box_element( - app->widget, 0, 0, 128, 23, AlignCenter, AlignCenter, delete_str, false); - snprintf( - delete_str, - sizeof(delete_str), - "\e#Type:\e# %s", - app->currentSensorEdit->type ? "DHT22" : "DHT11"); - widget_add_text_box_element( - app->widget, 0, 0, 128, 47, AlignLeft, AlignCenter, delete_str, false); - snprintf( - delete_str, - sizeof(delete_str), - "\e#GPIO:\e# %s", - DHTMon_GPIO_getName(app->currentSensorEdit->GPIO)); - widget_add_text_box_element( - app->widget, 0, 0, 128, 72, AlignLeft, AlignCenter, delete_str, false); - view_set_previous_callback(widget_get_view(app->widget), deleteWidget_exitCallback); - view_dispatcher_switch_to_view(app->view_dispatcher, WIDGET_VIEW); -} - -/* ================== Меню действий ================== */ -/** - * @brief Функция обработки нажатия средней кнопки - * - * @param context Указатель на данные приложения - * @param index На каком элементе списка была нажата кнопка - */ -static void enterCallback(void* context, uint32_t index) { - PluginData* app = context; - if(index == 0) { - sensorInfo_widget(app); - } - if(index == 1) { - sensorEdit_scene(app); - } - if(index == 2) { - sensorDelete_widget(app); - } -} - -/** - * @brief Функция обработки нажатия кнопки "Назад" - * - * @param context Указатель на данные приложения - * @return ID вида в который нужно переключиться - */ -static uint32_t actions_exitCallback(void* context) { - PluginData* app = context; - UNUSED(app); - //Возвращаем ID вида, в который нужно вернуться - return MAIN_MENU_VIEW; -} - -/** - * @brief Создание списка действий с указанным датчиком - * - * @param app Указатель на данные плагина - */ -void sensorActions_sceneCreate(PluginData* app) { - variable_item_list = variable_item_list_alloc(); - //Сброс всех элементов меню - variable_item_list_reset(variable_item_list); - //Добавление элементов в список - variable_item_list_add(variable_item_list, "Info", 0, NULL, NULL); - variable_item_list_add(variable_item_list, "Edit", 0, NULL, NULL); - variable_item_list_add(variable_item_list, "Delete", 0, NULL, NULL); - - //Добавление колбека на нажатие средней кнопки - variable_item_list_set_enter_callback(variable_item_list, enterCallback, app); - - //Создание вида из списка - view = variable_item_list_get_view(variable_item_list); - //Добавление колбека на нажатие кнопки "Назад" - view_set_previous_callback(view, actions_exitCallback); - //Добавление вида в диспетчер - view_dispatcher_add_view(app->view_dispatcher, SENSOR_ACTIONS_VIEW, view); -} -void sensorActions_scene(PluginData* app) { - //Сброс выбранного пункта в ноль - variable_item_list_set_selected_item(variable_item_list, 0); - //Переключение на наш вид - view_dispatcher_switch_to_view(app->view_dispatcher, SENSOR_ACTIONS_VIEW); -} - -void sensorActions_screneRemove(void) { - variable_item_list_free(variable_item_list); -} diff --git a/applications/plugins/dht_temp_sensor/scenes/DHTMon_sensorEdit_scene.c b/applications/plugins/dht_temp_sensor/scenes/DHTMon_sensorEdit_scene.c deleted file mode 100644 index 5decac3d1..000000000 --- a/applications/plugins/dht_temp_sensor/scenes/DHTMon_sensorEdit_scene.c +++ /dev/null @@ -1,103 +0,0 @@ -#include "../quenon_dht_mon.h" - -static VariableItem* nameItem; -static VariableItemList* variable_item_list; - -static const char* const sensorsTypes[2] = { - "DHT11", - "DHT22", -}; - -// /* ============== Добавление датчика ============== */ -static uint32_t addSensor_exitCallback(void* context) { - UNUSED(context); - DHTMon_sensors_reload(); - return VIEW_NONE; -} - -static void addSensor_sensorTypeChanged(VariableItem* item) { - uint8_t index = variable_item_get_current_value_index(item); - PluginData* app = variable_item_get_context(item); - variable_item_set_current_value_text(item, sensorsTypes[index]); - app->currentSensorEdit->type = index; -} - -static void addSensor_GPIOChanged(VariableItem* item) { - uint8_t index = variable_item_get_current_value_index(item); - variable_item_set_current_value_text(item, DHTMon_GPIO_getName(DHTMon_GPIO_from_index(index))); - PluginData* app = variable_item_get_context(item); - app->currentSensorEdit->GPIO = DHTMon_GPIO_from_index(index); -} - -static void addSensor_sensorNameChanged(void* context) { - PluginData* app = context; - variable_item_set_current_value_text(nameItem, app->currentSensorEdit->name); - view_dispatcher_switch_to_view(app->view_dispatcher, ADDSENSOR_MENU_VIEW); -} -static void addSensor_sensorNameChange(PluginData* app) { - text_input_set_header_text(app->text_input, "Sensor name"); - //По неясной мне причине в длину строки входит терминатор. Поэтому при длине 10 приходится указывать 11 - text_input_set_result_callback( - app->text_input, addSensor_sensorNameChanged, app, app->currentSensorEdit->name, 11, true); - view_dispatcher_switch_to_view(app->view_dispatcher, TEXTINPUT_VIEW); -} - -static void addSensor_enterCallback(void* context, uint32_t index) { - PluginData* app = context; - if(index == 0) { - addSensor_sensorNameChange(app); - } - if(index == 3) { - //Сохранение датчика - DHTMon_sensors_save(); - DHTMon_sensors_reload(); - view_dispatcher_switch_to_view(app->view_dispatcher, VIEW_NONE); - } -} - -void sensorEdit_sceneCreate(PluginData* app) { - variable_item_list = variable_item_list_alloc(); - - variable_item_list_reset(variable_item_list); - - variable_item_list_set_enter_callback(variable_item_list, addSensor_enterCallback, app); - - app->view = variable_item_list_get_view(variable_item_list); - - view_set_previous_callback(app->view, addSensor_exitCallback); - - view_dispatcher_add_view(app->view_dispatcher, ADDSENSOR_MENU_VIEW, app->view); -} -void sensorEdit_scene(PluginData* app) { - //Очистка списка - variable_item_list_reset(variable_item_list); - - //Имя редактируемого датчика - nameItem = variable_item_list_add(variable_item_list, "Name: ", 1, NULL, NULL); - variable_item_set_current_value_index(nameItem, 0); - variable_item_set_current_value_text(nameItem, app->currentSensorEdit->name); - - //Тип датчика - app->item = - variable_item_list_add(variable_item_list, "Type:", 2, addSensor_sensorTypeChanged, app); - - variable_item_set_current_value_index(app->item, app->currentSensorEdit->type); - variable_item_set_current_value_text(app->item, sensorsTypes[app->currentSensorEdit->type]); - - //GPIO - app->item = - variable_item_list_add(variable_item_list, "GPIO:", 13, addSensor_GPIOChanged, app); - variable_item_set_current_value_index( - app->item, DHTMon_GPIO_to_index(app->currentSensorEdit->GPIO)); - variable_item_set_current_value_text( - app->item, DHTMon_GPIO_getName(app->currentSensorEdit->GPIO)); - variable_item_list_add(variable_item_list, "Save", 1, NULL, app); - - //Сброс выбранного пункта в ноль - variable_item_list_set_selected_item(variable_item_list, 0); - - view_dispatcher_switch_to_view(app->view_dispatcher, ADDSENSOR_MENU_VIEW); -} -void sensorEdit_sceneRemove(void) { - variable_item_list_free(variable_item_list); -} \ No newline at end of file