├── LICENSE ├── README.md ├── images ├── menu.jpg └── project.png ├── libraries_used.zip ├── misc ├── FRITZING-esp32-oled.fzz └── pcb.jpg ├── oldVersion.ino └── oled.ino /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Basic OLED with rotary encoder Menu for ESP8266/ESP32/Probably others 2 | 3 | For either Arduino IDE or PlatformIO. 4 | 5 | Note: This is a completely different sketch as of Nov21 as I decided to start again and create a version which is none blocking. 6 | The old version oldVersion.ino is easier to use but will stop everything else whilst waiting for user input etc. 7 | 8 | 9 | 10 | 11 |
12 | 13 |
A simple to use, cheap to build and simple to wire menu system using a small oled display and a rotary encoder. 14 | I created it so I can quickly and easily add an oLed menu to any new projects, as you can see it only requires 5 wires (plus power). 15 |
Uses libraries: Adafruit_SSD1306 and Adafruit_GFX_Library 16 | 17 |
It gives basic menus, the ability to enter a numerical value or choose from a list. 18 | 19 |
The sketch uses the cheap 128x64 oled displays but the settings can be changed to work with different size displays. 20 |
I am very impressed with these displays, they are cheap, easy to use and despite being small they are still easy to read. 21 |
Note: I have seen reports of them being easily damaged by static and I have had one fail which could have been for this reason, I think it best to be careful of this when using one on a breadboard etc, they seem ok once installed in a project. 22 |
If you have any poor connections (especially to the rotary encoder) this can result in garbage being displayed which can make you think there is a fault with the display. 23 | 24 |
If you are new to the esp8266 using one of these OLED displays would be a very good project for you to try. 25 |
earch ebay for "SSD1306 i2c" and you should be able to pick up an oled display like the one in the picture above for around £3. Search for "ky-040" for a rotary encoder for around £1. 26 | 27 |
You can try it out on this online emulation: https://wokwi.com/arduino/projects/323967017900048980 28 | 29 |
BTW - I have incorporated all this in to my basicWebserver starting point sketch which may be of interest: https://github.com/alanesq/BasicWebserver 30 |
Even if you do not use BasicWebserver, this version may be easier to include in your own projects as you just have to include oled.h and add a single line to your setup and loop. 31 | 32 |
for more oled info see: https://randomnerdtutorials.com/guide-for-oled-display-with-arduino/ 33 | 34 |
Note: I had a lot of trouble finding some code which would work reliably with a rotary encoder (i.e. the interupt section) but find this to be very good although this may not be the case with all encoders? 35 |
See these videos for more info: https://www.youtube.com/watch?v=b2uUYiGrS5Y 36 | 37 |
If you have a cnc router there is a Fritzing file in the MISC folder I use for making PCBs which may be of interest. It is in a format which is very basic but it allows me to modify it and create a PCB as quickly as possible. 38 | 39 |
40 | 41 | 42 | https://github.com/alanesq/BasicOLEDMenu 43 | -------------------------------------------------------------------------------- /images/menu.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alanesq/BasicOLEDMenu/802e74a943fd95be9d288c28eda8b77f1fdaf86d/images/menu.jpg -------------------------------------------------------------------------------- /images/project.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alanesq/BasicOLEDMenu/802e74a943fd95be9d288c28eda8b77f1fdaf86d/images/project.png -------------------------------------------------------------------------------- /libraries_used.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alanesq/BasicOLEDMenu/802e74a943fd95be9d288c28eda8b77f1fdaf86d/libraries_used.zip -------------------------------------------------------------------------------- /misc/FRITZING-esp32-oled.fzz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alanesq/BasicOLEDMenu/802e74a943fd95be9d288c28eda8b77f1fdaf86d/misc/FRITZING-esp32-oled.fzz -------------------------------------------------------------------------------- /misc/pcb.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alanesq/BasicOLEDMenu/802e74a943fd95be9d288c28eda8b77f1fdaf86d/misc/pcb.jpg -------------------------------------------------------------------------------- /oldVersion.ino: -------------------------------------------------------------------------------- 1 | /************************************************************************************************** 2 | * 3 | * OLED display Menu System - i2c version SSD1306 - 31Aug21 4 | * 5 | * https://github.com/alanesq/BasicOLEDMenu 6 | * 7 | * 8 | ************************************************************************************************** 9 | 10 | oled pins: esp8266: sda=d2, scl=d1 11 | esp32: sda=21, scl=22 12 | Uno: sda=A4 , scl=A5 13 | oled address = 3C 14 | rotary encoder pins: 15 | esp8266: d5, d6, d7 (button) 16 | esp32: 13, 14, 15 17 | Uno: 2, 3, 4 18 | Note: on esp8266 you can change the button from d7 to d3 instead leaving d7 and d8 free to use 19 | 20 | 21 | The sketch displays a menu on the oled and when an item is selected it sets a 22 | flag and waits until the event is acted upon. Max menu items on a 128x64 oled 23 | is four. 24 | 25 | See the section "customise the menus below" for how to create custom menus inclusing selecting a value, 26 | choose from a list or display a message. 27 | 28 | 29 | for more oled info see: https://randomnerdtutorials.com/guide-for-oled-display-with-arduino/ 30 | or: https://lastminuteengineers.com/oled-display-esp32-tutorial/ 31 | 32 | 33 | 34 | **************************************************************************************************/ 35 | 36 | // settings 37 | 38 | const String stitle = "simple_menu"; // script title 39 | const String sversion = "07Dec20"; // script version 40 | 41 | bool serialDebug = 1; // enable debug info on serial port 42 | 43 | int OLEDDisplayTimeout = 10; // oled menu display timeout (seconds) 44 | 45 | int itemTrigger = 1; // rotary encoder - counts per click 46 | 47 | #define OLED_ADDR 0x3C // OLED i2c address 48 | 49 | #if defined(ESP8266) 50 | // esp8266 51 | const String boardType="ESP8266"; 52 | #define I2C_SDA D2 // i2c pins 53 | #define I2C_SCL D1 54 | #define encoder0PinA D5 55 | #define encoder0PinB D6 56 | #define encoder0Press D7 // button - Note: on esp8266 you can change this from d7 to d3 leaving d7 and d8 free to use 57 | 58 | #elif defined(ESP32) 59 | // esp32 60 | const String boardType="ESP32"; 61 | #define I2C_SDA 21 // i2c pins 62 | #define I2C_SCL 22 63 | #define encoder0PinA 13 64 | #define encoder0PinB 14 65 | #define encoder0Press 15 // button 66 | 67 | #elif defined (__AVR_ATmega328P__) 68 | // Arduino Uno 69 | const String boardType="Uno"; 70 | #define I2C_SDA A4 // i2c pins 71 | #define I2C_SCL A5 72 | #define encoder0PinA 2 73 | #define encoder0PinB 3 74 | #define encoder0Press 4 // button 75 | #else 76 | #error Unsupported board - must be esp32, esp8266 or Arduino Uno 77 | #endif 78 | 79 | 80 | // ------------------------------------------------------------------------------------------------- 81 | 82 | 83 | //#include // used to display free memory on Arduino (useful as it can be very limited) 84 | #include 85 | #include 86 | 87 | // rotary encoder 88 | volatile int16_t encoder0Pos = 0; // current value selected with rotary encoder (updated in interrupt routine) 89 | volatile bool encoderPrevA = 0; // used to debounced rotary encoder 90 | volatile bool encoderPrevB = 0; // used to debounced rotary encoder 91 | bool reButtonState = 0; // current debounced state of the button 92 | uint32_t reButtonTimer = millis(); // time button state last changed 93 | int reButtonMinTime = 500; // minimum milliseconds between allowed button status changes 94 | 95 | // oled menu 96 | const byte menuMax = 5; // max number of menu items 97 | const byte lineSpace1 = 9; // line spacing (6 lines) 98 | const byte lineSpace2 = 16; // line spacing (4 lines) 99 | String menuOption[menuMax]; // options displayed in menu 100 | byte menuCount = 0; // which menu item is curently highlighted 101 | String menuTitle = ""; // current menu ID number (blank = none) 102 | byte menuItemClicked = 100; // menu item has been clicked flag (100=none) 103 | uint32_t lastREActivity = 0; // time last activity was seen on rotary encoder 104 | 105 | // oled SSD1306 display connected to I2C (SDA, SCL pins) 106 | #define SCREEN_WIDTH 128 // OLED display width, in pixels 107 | #define SCREEN_HEIGHT 64 // OLED display height, in pixels 108 | #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) 109 | Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); 110 | 111 | 112 | 113 | // ------------------------------------------------------------------------------------------------- 114 | // customise the menus below 115 | // ------------------------------------------------------------------------------------------------- 116 | 117 | // Useful commands: 118 | // void reWaitKeypress(20000); = wait for the button to be pressed on the rotary encoder (timeout in 20 seconds if not) 119 | // chooseFromList(8, "TestList", q); = choose from the list of 8 items in a string array 'q' 120 | // enterValue("Testval", 15, 0, 30); = enter a value between 0 and 30 (with a starting value of 15) 121 | 122 | 123 | // Available Menus 124 | 125 | // main menu 126 | void Main_Menu() { 127 | menuTitle = "Main Menu"; // set the menu title 128 | setMenu(0,""); // clear all menu items 129 | setMenu(0,"list"); // choose from a list 130 | setMenu(1,"enter a value"); // enter a value 131 | setMenu(2,"message"); // display a message 132 | setMenu(3,"MENU 2"); // change menu 133 | } 134 | 135 | 136 | // menu 2 137 | void menu2() { 138 | menuTitle = "Menu 2"; 139 | setMenu(0,""); 140 | setMenu(0,"menu off"); 141 | setMenu(1,"RETURN"); 142 | } 143 | 144 | 145 | // ------------------------------------------------------------------------------------------------- 146 | 147 | 148 | 149 | // menu action procedures 150 | // check if menu item has been selected with: if (menuTitle == "" && menuItemClicked==) 151 | 152 | void menuItemActions() { 153 | 154 | if (menuItemClicked == 100) return; // if no menu item has been clicked exit function 155 | 156 | 157 | 158 | // --------------------- Main Menu Actions ------------------ 159 | 160 | if (menuTitle == "Main Menu" && menuItemClicked==0) { 161 | menuItemClicked=100; // flag that the button press has been actioned (the menu stops and waits until this) 162 | String q[] = {"item0","item1","item2","item3","item4","item5","item6","item7"}; 163 | int tres=chooseFromList(8, "TestList", q); 164 | Serial.println("Menu: item " + String(tres) + " chosen from list"); 165 | } 166 | 167 | if (menuTitle == "Main Menu" && menuItemClicked==1) { 168 | menuItemClicked=100; 169 | int tres=enterValue("Testval", 50, 1, 0, 100); // enter a value (title, start value, step size, low limit, high limit) 170 | Serial.println("Menu: Value set = " + String(tres)); 171 | } 172 | 173 | if (menuTitle == "Main Menu" && menuItemClicked==2) { 174 | menuItemClicked=100; 175 | Serial.println("Menu: display message selected"); 176 | // display a message 177 | display.clearDisplay(); 178 | display.setTextSize(2); 179 | display.setTextColor(WHITE); 180 | display.setCursor(20, 20); 181 | display.print("Hello"); 182 | display.display(); 183 | reWaitKeypress(20000); // wait for key press on rotary encoder 184 | } 185 | 186 | if (menuTitle == "Main Menu" && menuItemClicked==3) { 187 | menuItemClicked=100; 188 | Serial.println("Menu: Menu 2 selected"); 189 | menu2(); // show a different menu 190 | } 191 | 192 | 193 | // --------------------- Menu 2 Actions --------------------- 194 | 195 | if (menuTitle == "Menu 2" && menuItemClicked==0) { 196 | menuItemClicked=100; 197 | Serial.println("Menu: menu off"); 198 | menuTitle = ""; // turn menu off 199 | display.clearDisplay(); 200 | display.display(); 201 | } 202 | 203 | if (menuTitle == "Menu 2" && menuItemClicked==1) { 204 | menuItemClicked=100; 205 | Serial.println("Menu: Main Menu selected"); 206 | Main_Menu(); // show main menu 207 | } 208 | 209 | } 210 | 211 | 212 | // ------------------------------------------------------------------------------------------------- 213 | // customise the menus above 214 | // ------------------------------------------------------------------------------------------------- 215 | 216 | 217 | 218 | void setup() { 219 | 220 | Serial.begin(115200); 221 | delay(200); 222 | Serial.println("\n\n\nOled display sketch"); 223 | 224 | // configure gpio pins 225 | pinMode(encoder0Press, INPUT_PULLUP); 226 | pinMode(encoder0PinA, INPUT); 227 | pinMode(encoder0PinB, INPUT); 228 | 229 | // initialise the oled display 230 | Wire.begin(I2C_SDA,I2C_SCL); // if you get an error it may be the board you are using does not allow defining the pins in which case try: Wire.begin(); 231 | if(!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) { 232 | Serial.println(("\nError initialising the oled display")); 233 | } 234 | 235 | // Display splash screen on OLED 236 | display.clearDisplay(); 237 | display.setTextColor(WHITE); 238 | display.setCursor(0, 0); 239 | display.setTextSize(1); 240 | display.print(stitle); 241 | display.setCursor(0, lineSpace1 * 2); 242 | display.print(sversion); 243 | display.setCursor(0, lineSpace1 * 4); 244 | display.print(boardType); 245 | display.setCursor(0, lineSpace1 * 5); 246 | //display.print(freeMemory()); 247 | display.display(); 248 | delay(2500); 249 | 250 | // Interrupt for reading the rotary encoder position 251 | attachInterrupt(digitalPinToInterrupt(encoder0PinA), doEncoder, CHANGE); 252 | 253 | Main_Menu(); // start the menu displaying - see menuItemActions() to alter the menus 254 | } 255 | 256 | // ------------------------------------------------------------------------------------------- 257 | 258 | 259 | void loop() { 260 | 261 | // if a oled menu is active service it 262 | if (menuTitle != "") { // if a menu is active 263 | menuCheck(); // check if encoder selection button is pressed 264 | menuItemSelection(); // check for change in menu item highlighted 265 | staticMenu(); // display the menu 266 | menuItemActions(); // act if a menu item has been clicked 267 | } 268 | 269 | 270 | 271 | // <<< your code here >>>> 272 | 273 | 274 | 275 | 276 | yield(); 277 | } 278 | 279 | 280 | // ------------------------------------------------------------------------------------------- 281 | // ------------------------------------- menu procedures ------------------------------------- 282 | // ------------------------------------------------------------------------------------------- 283 | 284 | 285 | // set menu item 286 | // pass: new menu items number, name (blank iname clears all entries) 287 | 288 | void setMenu(byte inum, String iname) { 289 | if (inum >= menuMax) return; // invalid number 290 | if (iname == "") { // clear all menu items 291 | for (int i=0; i < menuMax; i++) menuOption[i] = ""; 292 | menuCount = 0; // move highlight to top menu item 293 | } else { 294 | menuOption[inum] = iname; 295 | menuItemClicked = 100; // set item selected flag as none 296 | } 297 | } 298 | 299 | // -------------------------------------- 300 | 301 | // confirm that a requested action is not a mistake 302 | // returns 1 if confirmed 303 | 304 | bool confirmActionRequired() { 305 | display.clearDisplay(); 306 | display.setTextSize(2); 307 | display.setTextColor(WHITE,BLACK); 308 | display.setCursor(0, lineSpace2 * 0); 309 | display.print("HOLD"); 310 | display.setCursor(0, lineSpace2 * 1); 311 | display.print("BUTTON TO"); 312 | display.setCursor(0, lineSpace2 * 2); 313 | display.print("CONFIRM!"); 314 | display.display(); 315 | 316 | delay(2000); 317 | display.clearDisplay(); 318 | display.display(); 319 | 320 | exitMenu(); // close menu 321 | 322 | if (digitalRead(encoder0Press) == LOW) return 1; // if button still pressed 323 | return 0; 324 | } 325 | 326 | // -------------------------------------- 327 | 328 | // display menu on oled 329 | void staticMenu() { 330 | display.clearDisplay(); 331 | // title 332 | display.setTextSize(1); 333 | display.setTextColor(WHITE); 334 | display.setCursor(lineSpace1, 0); 335 | display.print(menuTitle); 336 | display.drawLine(0, lineSpace1, display.width(), lineSpace1, WHITE); 337 | 338 | // menu options 339 | int i=0; 340 | while (i < menuMax && menuOption[i] != "") { // if menu item is not blank display it 341 | if (i == menuItemClicked) display.setTextColor(BLACK,WHITE); // if this item has been clicked 342 | else display.setTextColor(WHITE,BLACK); 343 | display.setCursor(10, 18 + (i*lineSpace1)); 344 | display.print(menuOption[i]); 345 | i++; 346 | } 347 | 348 | // highlighted item if none yet clicked 349 | if (menuItemClicked == 100) { 350 | display.setCursor(2, 18 + (menuCount * lineSpace1)); 351 | display.print(">"); 352 | } 353 | 354 | display.display(); // update display 355 | } 356 | 357 | 358 | // -------------------------------------- 359 | 360 | 361 | // rotary encoder button 362 | // returns 1 if the button status has changed since last time 363 | 364 | bool menuCheck() { 365 | 366 | if (digitalRead(encoder0Press) == reButtonState) return 0; // no change 367 | delay(40); 368 | if (digitalRead(encoder0Press) == reButtonState) return 0; // debounce 369 | if (millis() - reButtonTimer < reButtonMinTime) return 0; // if too soon since last change 370 | 371 | // button status has changed 372 | reButtonState = !reButtonState; 373 | reButtonTimer = millis(); // update timer 374 | if (serialDebug) Serial.println("button state has changed"); 375 | 376 | // oled menu action on button press 377 | if (reButtonState==LOW) { // if button is now pressed 378 | lastREActivity = millis(); // log time last activity seen (don't count button release as activity) 379 | if (menuItemClicked != 100 || menuTitle == "") return 1; // menu item already selected or there is no live menu 380 | if (serialDebug) Serial.println("menu '" + menuTitle + "' item " + String(menuCount) + " selected"); 381 | menuItemClicked = menuCount; // set item selected flag 382 | } 383 | 384 | return 1; 385 | } 386 | 387 | 388 | 389 | // wait for key press or turn on rotary encoder 390 | // pass timeout in ms 391 | void reWaitKeypress(int timeout) { 392 | uint32_t tTimer = millis(); // log time 393 | // wait for button to be released 394 | while ( (digitalRead(encoder0Press) == LOW) && (millis() - tTimer < timeout) ) { // wait for button release 395 | yield(); // service any web page requests 396 | delay(20); 397 | } 398 | // clear rotary encoder position counter 399 | noInterrupts(); 400 | encoder0Pos = 0; 401 | interrupts(); 402 | // wait for button to be pressed or encoder to be turned 403 | while ( (digitalRead(encoder0Press) == HIGH) && (encoder0Pos == 0) && (millis() - tTimer < timeout) ) { 404 | yield(); // service any web page requests 405 | delay(20); 406 | } 407 | exitMenu(); // close menu 408 | } 409 | 410 | 411 | // -------------------------------------- 412 | 413 | 414 | // handle menu item selection 415 | 416 | void menuItemSelection() { 417 | if (encoder0Pos >= itemTrigger) { 418 | noInterrupts(); 419 | encoder0Pos = 0; 420 | interrupts(); 421 | lastREActivity = millis(); // log time last activity seen 422 | if (menuCount+1 < menuMax) menuCount++; // if not past max menu items move 423 | if (menuOption[menuCount] == "") menuCount--; // if menu item is blank move back 424 | } 425 | if (encoder0Pos <= -itemTrigger) { 426 | noInterrupts(); 427 | encoder0Pos = 0; 428 | interrupts(); 429 | lastREActivity = millis(); // log time last activity seen 430 | if (menuCount > 0) menuCount--; 431 | } 432 | } 433 | 434 | 435 | // --------------------------------------- 436 | 437 | 438 | // enter a value using the rotary encoder 439 | // pass Value title, starting value, step size, low limit , high limit 440 | // returns the chosen value 441 | int enterValue(String title, int start, int stepSize, int low, int high) { 442 | uint32_t tTimer = millis(); // log time of start of function 443 | // display title 444 | display.clearDisplay(); 445 | if (title.length() > 8) display.setTextSize(1); // if title is longer than 8 chars make text smaller 446 | else display.setTextSize(2); 447 | display.setTextColor(WHITE); 448 | display.setCursor(0, 0); 449 | display.print(title); 450 | display.display(); // update display 451 | int tvalue = start; 452 | while ( (digitalRead(encoder0Press) == LOW) && (millis() - tTimer < (OLEDDisplayTimeout * 1000)) ) delay(5); // wait for button release 453 | tTimer = millis(); 454 | while ( (digitalRead(encoder0Press) == HIGH) && (millis() - tTimer < (OLEDDisplayTimeout * 1000)) ) { // while button is not pressed and still within time limit 455 | if (encoder0Pos >= itemTrigger) { // encoder0Pos is updated via the interrupt procedure 456 | tvalue -= stepSize; 457 | noInterrupts(); // stop interrupt changing the value whilst it is changed here 458 | encoder0Pos -= itemTrigger; 459 | interrupts(); 460 | tTimer = millis(); 461 | } 462 | else if (encoder0Pos <= -itemTrigger) { 463 | tvalue += stepSize; 464 | noInterrupts(); 465 | encoder0Pos += itemTrigger; 466 | interrupts(); 467 | tTimer = millis(); 468 | } 469 | // value limits 470 | if (tvalue > high) tvalue=high; 471 | if (tvalue < low) tvalue=low; 472 | display.setTextSize(3); 473 | const int textPos = 27; // height of number on display 474 | display.fillRect(0, textPos, SCREEN_WIDTH, SCREEN_HEIGHT - textPos, BLACK); // clear bottom half of display (128x64) 475 | display.setCursor(0, textPos); 476 | display.print(tvalue); 477 | // bar graph at bottom of display 478 | int tmag=map(tvalue, low, high, 0 ,SCREEN_WIDTH); 479 | display.fillRect(0, SCREEN_HEIGHT - 10, tmag, 10, WHITE); 480 | display.display(); // update display 481 | yield(); // service any web page requests 482 | } 483 | exitMenu(); // close menu 484 | return tvalue; 485 | } 486 | 487 | 488 | // -------------------------------------- 489 | 490 | 491 | // choose from list using rotary encoder 492 | // pass the number of items in list (max 8), list title, list of options in a string array 493 | 494 | int chooseFromList(byte noOfElements, String listTitle, String list[]) { 495 | 496 | const byte noList = 10; // max number of items to list 497 | uint32_t tTimer = millis(); // log time of start of function 498 | int highlightedItem = 0; // which item in list is highlighted 499 | int xpos, ypos; 500 | 501 | // display title 502 | display.clearDisplay(); 503 | display.setTextSize(1); 504 | display.setTextColor(WHITE,BLACK); 505 | display.setCursor(10, 0); 506 | display.print(listTitle); 507 | display.drawLine(0, lineSpace1, display.width(), lineSpace1, WHITE); 508 | 509 | // scroll through list 510 | while ( (digitalRead(encoder0Press) == LOW) && (millis() - tTimer < (OLEDDisplayTimeout * 1000)) ) delay(5); // wait for button release 511 | tTimer = millis(); 512 | while ( (digitalRead(encoder0Press) == HIGH) && (millis() - tTimer < (OLEDDisplayTimeout * 1000)) ) { // while button is not pressed and still within time limit 513 | if (encoder0Pos >= itemTrigger) { // encoder0Pos is updated via the interrupt procedure 514 | noInterrupts(); 515 | encoder0Pos = 0; 516 | interrupts(); 517 | highlightedItem++; 518 | tTimer = millis(); 519 | } 520 | if (encoder0Pos <= -itemTrigger) { 521 | noInterrupts(); 522 | encoder0Pos = 0; 523 | interrupts(); 524 | highlightedItem--; 525 | tTimer = millis(); 526 | } 527 | // value limits 528 | if (highlightedItem > noOfElements - 1) highlightedItem = noOfElements - 1; 529 | if (highlightedItem < 0) highlightedItem = 0; 530 | // display the list 531 | for (int i=0; i < noOfElements; i++) { 532 | if (i < (noList/2)) { 533 | xpos = 0; 534 | ypos = lineSpace1 * (i+1) + 7; 535 | } else { 536 | xpos = display.width() / 2; 537 | ypos = lineSpace1 * (i-((noList/2)-1)) + 7; 538 | } 539 | display.setCursor(xpos, ypos); 540 | if (i == highlightedItem) display.setTextColor(BLACK,WHITE); 541 | else display.setTextColor(WHITE,BLACK); 542 | display.print(list[i]); 543 | } 544 | display.display(); // update display 545 | yield(); // service any web page requests 546 | } 547 | 548 | // if it timed out set selection to cancel (i.e. item 0) 549 | if (millis() - tTimer >= (OLEDDisplayTimeout * 1000)) highlightedItem=0; 550 | 551 | // // wait for button to be released (up to 1 second) 552 | // tTimer = millis(); // log time 553 | // while ( (digitalRead(encoder0Press) == LOW) && (millis() - tTimer < 1000) ) { 554 | // yield(); // service any web page requests 555 | // delay(20); 556 | // } 557 | 558 | exitMenu(); // close menu 559 | 560 | return highlightedItem; 561 | } 562 | 563 | 564 | // -------------------------------------- 565 | 566 | 567 | // close the menus and return to sleep mode 568 | 569 | void exitMenu() { 570 | reButtonState = digitalRead(encoder0Press); // update current button status 571 | lastREActivity = 0; // clear time of last rotary encoder usage 572 | noInterrupts(); 573 | encoder0Pos = 0; // clear rotary encoder position change counter 574 | interrupts(); 575 | } 576 | 577 | 578 | // -------------------------------------- 579 | 580 | // rotary encoder interrupt routine to update position counter when turned 581 | // interrupt info: https://www.gammon.com.au/forum/bbshowpost.php?id=11488 582 | 583 | #if defined (__AVR_ATmega328P__) 584 | void doEncoder() { 585 | #elif defined ESP32 586 | IRAM_ATTR void doEncoder() { 587 | #else // esp8266 588 | ICACHE_RAM_ATTR void doEncoder() { 589 | #endif 590 | 591 | bool pinA = digitalRead(encoder0PinA); 592 | bool pinB = digitalRead(encoder0PinB); 593 | 594 | if ( (encoderPrevA == pinA && encoderPrevB == pinB) ) return; // no change since last time (i.e. reject bounce) 595 | 596 | // same direction (alternating between 0,1 and 1,0 in one direction or 1,1 and 0,0 in the other direction) 597 | if (encoderPrevA == 1 && encoderPrevB == 0 && pinA == 0 && pinB == 1) encoder0Pos -= 1; 598 | else if (encoderPrevA == 0 && encoderPrevB == 1 && pinA == 1 && pinB == 0) encoder0Pos -= 1; 599 | else if (encoderPrevA == 0 && encoderPrevB == 0 && pinA == 1 && pinB == 1) encoder0Pos += 1; 600 | else if (encoderPrevA == 1 && encoderPrevB == 1 && pinA == 0 && pinB == 0) encoder0Pos += 1; 601 | 602 | // change of direction 603 | else if (encoderPrevA == 1 && encoderPrevB == 0 && pinA == 0 && pinB == 0) encoder0Pos += 1; 604 | else if (encoderPrevA == 0 && encoderPrevB == 1 && pinA == 1 && pinB == 1) encoder0Pos += 1; 605 | else if (encoderPrevA == 0 && encoderPrevB == 0 && pinA == 1 && pinB == 0) encoder0Pos -= 1; 606 | else if (encoderPrevA == 1 && encoderPrevB == 1 && pinA == 0 && pinB == 1) encoder0Pos -= 1; 607 | 608 | else if (serialDebug) Serial.println("Error: invalid rotary encoder pin state - prev=" + String(encoderPrevA) + "," 609 | + String(encoderPrevB) + " new=" + String(pinA) + "," + String(pinB)); 610 | 611 | // update previous readings 612 | encoderPrevA = pinA; 613 | encoderPrevB = pinB; 614 | 615 | } 616 | 617 | 618 | // ---------------------------------------------- end ---------------------------------------------- 619 | -------------------------------------------------------------------------------- /oled.ino: -------------------------------------------------------------------------------- 1 | /************************************************************************************************** 2 | * 3 | * OLED display simple none blocking menu System on esp8266/esp32 - i2c version SSD1306 - 05Apr24 4 | * 5 | * from: https://github.com/alanesq/BasicOLEDMenu 6 | * 7 | * 8 | ************************************************************************************************** 9 | 10 | The sketch displays a menu on the oled and when an item is selected it sets a 11 | flag and waits until the event is acted upon. Max menu items on a 128x64 oled 12 | is four. 13 | 14 | Notes: text size 1 = 21 x 8 characters on the larger oLED display 15 | text size 2 = 10 x 4 16 | 17 | For more oled info see: https://randomnerdtutorials.com/guide-for-oled-display-with-arduino/ 18 | 19 | See the "menus below here" section for examples of how to use the menus 20 | 21 | Note: If you get garbage on the display and the device locking up etc. it may just be a poor connection 22 | to the rotary encoder 23 | 24 | 25 | **************************************************************************************************/ 26 | 27 | 28 | #include // required by platformIO 29 | #include 30 | #include 31 | #include 32 | 33 | 34 | // ---------------------------------------------------------------- 35 | // S E T T I N G S 36 | // ---------------------------------------------------------------- 37 | 38 | // esp32 lolin lite gpio pins 39 | #define encoder0PinA 32 // Rotary encoder gpio pin - 16 40 | #define encoder0PinB 33 // Rotary encoder gpio pin - 17 41 | #define encoder0Press 23 // Rotary encoder button gpio pin - 23 42 | #define OLEDC 26 // oled clock pin (set to -1 for default) - 26 43 | #define OLEDD 27 // oled data pin - 27 44 | #define OLEDE -1 // oled enable pin (set to -1 if not used) 45 | 46 | // // esp32 HiLetGo gpio pins - https://robotzero.one/heltec-wifi-kit-32/ 47 | // #define encoder0PinA 25 // Rotary encoder gpio pin 48 | // #define encoder0PinB 33 // Rotary encoder gpio pin 49 | // #define encoder0Press 32 // Rotary encoder button gpio pin 50 | // #define OLEDC 15 // oled clock pin (set to 0 for default) 51 | // #define OLEDD 4 // oled data pin 52 | // #define OLEDE 16 // oled enable pin 53 | 54 | // // esp8266 gpio pins 55 | // #define encoder0PinA 14 // Rotary encoder gpio pin, 14 = D5 on esp8266 56 | // #define encoder0PinB 12 // Rotary encoder gpio pin, 12 = D6 on esp8266 57 | // #define encoder0Press 13 // Rotary encoder button gpio pin, 13 = D7 on esp8266 58 | // #define OLEDC 0 // oled clock pin (set to 0 for default) 59 | // #define OLEDD 0 // oled data pin 60 | // #define OLEDE 0 // oled enable pin 61 | 62 | // oLED 63 | #define OLED_ADDR 0x3C // OLED i2c address 64 | #define SCREEN_WIDTH 128 // OLED display width, in pixels (usually 128) 65 | #define SCREEN_HEIGHT 64 // OLED display height, in pixels (64 for larger oLEDs) 66 | #define OLED_RESET -1 // Reset pin gpio (-1 if sharing Arduino reset pin) 67 | 68 | // Misc 69 | const int serialDebug = 1; 70 | const int iLED = 22; // onboard indicator led gpio pin 71 | #define BUTTONPRESSEDSTATE 0 // rotary encoder gpio pin logic level when the button is pressed (usually 0) 72 | #define DEBOUNCEDELAY 100 // debounce delay for button inputs 73 | const int menuTimeout = 10; // menu inactivity timeout (seconds) 74 | const bool menuLargeText = 0; // show larger text when possible (if struggling to read the small text) 75 | const int maxmenuItems = 12; // max number of items used in any of the menus (keep as low as possible to save memory) 76 | const int itemTrigger = 1; // rotary encoder - counts per tick (varies between encoders usually 1 or 2) 77 | const int topLine = 18; // y position of lower area of the display (18 with two colour displays) 78 | const byte lineSpace1 = 9; // line spacing for textsize 1 (small text) 79 | const byte lineSpace2 = 17; // line spacing for textsize 2 (large text) 80 | const int displayMaxLines = 5; // max lines that can be displayed in lower section of display in textsize1 (5 on larger oLeds) 81 | const int MaxmenuTitleLength = 10; // max characters per line when using text size 2 (usually 10) 82 | 83 | 84 | // ------------------------------------------------------------------------------------------------- 85 | 86 | 87 | // forward declarations - required by PlatformIO 88 | void ICACHE_RAM_ATTR doEncoder(); 89 | void demoMenu(); 90 | void menuActions(); 91 | void value1(); 92 | void menuValues(); 93 | void reUpdateButton(); 94 | void serviceMenu(); 95 | int serviceValue(bool _blocking); 96 | void createList(String _title, int _noOfElements, String *_list); 97 | void displayMessage(String _title, String _message); 98 | void resetMenu(); 99 | 100 | 101 | // menus 102 | // modes that the menu system can be in 103 | enum menuModes { 104 | off, // display is off 105 | menu, // a menu is active 106 | value, // 'enter a value' none blocking is active 107 | message, // displaying a message 108 | blocking // a blocking procedure is in progress (see enter value) 109 | }; 110 | menuModes menuMode = off; // default mode at startup is off 111 | 112 | struct oledMenus { 113 | // menu 114 | String menuTitle = ""; // the title of active mode 115 | int noOfmenuItems = 0; // number if menu items in the active menu 116 | int selectedMenuItem = 0; // when a menu item is selected it is flagged here until actioned and cleared 117 | int highlightedMenuItem = 0; // which item is curently highlighted in the menu 118 | String menuItems[maxmenuItems+1]; // store for the menu item titles 119 | uint32_t lastMenuActivity = 0; // time the menu last saw any activity (used for timeout) 120 | // 'enter a value' 121 | int mValueEntered = 0; // store for number entered by value entry menu 122 | int mValueLow = 0; // lowest allowed value 123 | int mValueHigh = 0; // highest allowed value 124 | int mValueStep = 0; // step size when encoder is turned 125 | }; 126 | oledMenus oledMenu; 127 | 128 | struct rotaryEncoders { 129 | volatile int encoder0Pos = 0; // current value selected with rotary encoder (updated by interrupt routine) 130 | volatile bool encoderPrevA; // used to debounced rotary encoder 131 | volatile bool encoderPrevB; // used to debounced rotary encoder 132 | uint32_t reLastButtonChange = 0; // last time state of button changed (for debouncing) 133 | bool encoderPrevButton = 0; // used to debounce button 134 | int reButtonDebounced = 0; // debounced current button state (1 when pressed) 135 | const bool reButtonPressedState = BUTTONPRESSEDSTATE; // the logic level when the button is pressed 136 | const uint32_t reDebounceDelay = DEBOUNCEDELAY; // button debounce delay setting 137 | bool reButtonPressed = 0; // flag set when the button is pressed (it has to be manually reset) 138 | }; 139 | rotaryEncoders rotaryEncoder; 140 | 141 | // oled SSD1306 display connected to I2C 142 | Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); 143 | 144 | 145 | // ------------------------------------------------------------------------------------------------- 146 | // menus below here 147 | // ------------------------------------------------------------------------------------------------- 148 | 149 | 150 | // Start the default menu 151 | void defaultMenu() { 152 | demoMenu(); 153 | } 154 | 155 | 156 | // ----------------------------------------------- 157 | 158 | 159 | // demonstration menu 160 | void demoMenu() { 161 | resetMenu(); // clear any previous menu 162 | menuMode = menu; // enable menu mode 163 | oledMenu.noOfmenuItems = 8; // set the number of items in this menu 164 | oledMenu.menuTitle = "demo_menu"; // menus title (used to identify it) 165 | oledMenu.menuItems[1] = "item1"; // set the menu items 166 | oledMenu.menuItems[2] = "item2"; 167 | oledMenu.menuItems[3] = "Quick menu"; 168 | oledMenu.menuItems[4] = "On or Off"; 169 | oledMenu.menuItems[5] = "Enter value"; 170 | oledMenu.menuItems[6] = "Enter value-blocking"; 171 | oledMenu.menuItems[7] = "Message"; 172 | oledMenu.menuItems[8] = "Menus Off"; 173 | } // demoMenu 174 | 175 | 176 | // actions for menu selections are put in here 177 | void menuActions() { 178 | 179 | // actions when an item is selected in "demo_menu" 180 | if (oledMenu.menuTitle == "demo_menu") { 181 | 182 | // demonstrate quickly create a menu from a list 183 | if (oledMenu.selectedMenuItem == 3) { 184 | if (serialDebug) Serial.println("demo_menu: create menu from a list"); 185 | String tList[]={"main menu", "2", "3", "4", "5", "6"}; 186 | createList("demo_list", 6, &tList[0]); 187 | } 188 | 189 | // demonstrate selecting between 2 options only 190 | if (oledMenu.selectedMenuItem == 4) { 191 | resetMenu(); 192 | menuMode = value; oledMenu.menuTitle = "on or off"; oledMenu.mValueLow = 0; oledMenu.mValueHigh = 1; oledMenu.mValueStep = 1; oledMenu.mValueEntered = 0; // set parameters 193 | } 194 | 195 | // demonstrate usage of 'enter a value' (none blocking) 196 | if (oledMenu.selectedMenuItem == 5) { 197 | if (serialDebug) Serial.println("demo_menu: none blocking enter value"); 198 | resetMenu(); 199 | value1(); // enter a value 200 | } 201 | 202 | // demonstrate usage of 'enter a value' (blocking) which is quick and easy but stops all other tasks until the value is entered 203 | if (oledMenu.selectedMenuItem == 6) { 204 | if (serialDebug) Serial.println("demo_menu: blocking enter a value"); 205 | // set perameters 206 | resetMenu(); 207 | menuMode = value; 208 | oledMenu.menuTitle = "blocking"; 209 | oledMenu.mValueLow = 0; 210 | oledMenu.mValueHigh = 50; 211 | oledMenu.mValueStep = 1; 212 | oledMenu.mValueEntered = 5; 213 | int tEntered = serviceValue(1); // request value 214 | Serial.println("The value entered was " + String(tEntered)); 215 | defaultMenu(); 216 | } 217 | 218 | // demonstrate usage of message 219 | if (oledMenu.selectedMenuItem == 7) { 220 | if (serialDebug) Serial.println("demo_menu: message"); 221 | displayMessage("Message", "Hello\nThis is a demo\nmessage."); // 21 chars per line, "\n" = next line 222 | } 223 | 224 | // turn menu/oLED off 225 | else if (oledMenu.selectedMenuItem == 8) { 226 | if (serialDebug) Serial.println("demo_menu: menu off"); 227 | resetMenu(); // turn menus off 228 | } 229 | 230 | oledMenu.selectedMenuItem = 0; // clear menu item selected flag 231 | } 232 | 233 | 234 | // actions when an item is selected in the demo_list menu 235 | if (oledMenu.menuTitle == "demo_list") { 236 | 237 | // back to main menu 238 | if (oledMenu.selectedMenuItem == 1) { 239 | if (serialDebug) Serial.println("demo_list: back to main menu"); 240 | defaultMenu(); 241 | } 242 | 243 | oledMenu.selectedMenuItem = 0; // clear menu item selected flag 244 | } 245 | 246 | } // menuActions 247 | 248 | 249 | // ----------------------------------------------- 250 | 251 | 252 | // demonstration enter a value 253 | void value1() { 254 | resetMenu(); // clear any previous menu 255 | menuMode = value; // enable value entry 256 | oledMenu.menuTitle = "demo_value"; // title (used to identify which number was entered) 257 | oledMenu.mValueLow = 0; // minimum value allowed 258 | oledMenu.mValueHigh = 100; // maximum value allowed 259 | oledMenu.mValueStep = 1; // step size 260 | oledMenu.mValueEntered = 50; // starting value 261 | } 262 | 263 | 264 | // actions for value entered put in here 265 | void menuValues() { 266 | 267 | // action for "demo_value" 268 | if (oledMenu.menuTitle == "demo_value") { 269 | String tString = String(oledMenu.mValueEntered); 270 | if (serialDebug) Serial.println("demo_value: The value entered was " + tString); 271 | displayMessage("ENTERED", "\nYou entered\nthe value\n " + tString); 272 | // alternatively use 'resetMenu()' here to turn menus off after value entered - or use 'defaultMenu()' to re-start the default menu 273 | } 274 | 275 | // action for "on or off" 276 | if (oledMenu.menuTitle == "on or off") { 277 | if (serialDebug) Serial.println("demo_menu: on off selection was " + String(oledMenu.mValueEntered)); 278 | defaultMenu(); 279 | } 280 | 281 | } 282 | 283 | 284 | // ------------------------------------------------------------------------------------------------- 285 | // menus above here 286 | // ------------------------------------------------------------------------------------------------- 287 | 288 | 289 | // ---------------------------------------------------------------- 290 | // -setup 291 | // ---------------------------------------------------------------- 292 | // called from main setup 293 | 294 | void setup() { 295 | 296 | Serial.begin(115200); while (!Serial); delay(50); // start serial comms 297 | Serial.println("\n\n\nStarting menu demo\n"); 298 | 299 | pinMode(iLED, OUTPUT); // onboard indicator led 300 | 301 | // configure gpio pins for rotary encoder 302 | pinMode(encoder0Press, INPUT_PULLUP); 303 | pinMode(encoder0PinA, INPUT); 304 | pinMode(encoder0PinB, INPUT); 305 | 306 | // initialise the oled display 307 | // enable pin 308 | if (OLEDE != 0) { 309 | pinMode(OLEDE , OUTPUT); 310 | digitalWrite(OLEDE, HIGH); 311 | } 312 | if (0 == OLEDC) Wire.begin(); 313 | else Wire.begin(OLEDD, OLEDC); 314 | if(!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) { 315 | if (serialDebug) Serial.println(("\nError initialising the oled display")); 316 | } 317 | 318 | // Interrupt for reading the rotary encoder position 319 | rotaryEncoder.encoder0Pos = 0; 320 | attachInterrupt(digitalPinToInterrupt(encoder0PinA), doEncoder, CHANGE); 321 | 322 | //defaultMenu(); // start the default menu 323 | 324 | // display greeting message - pressing button will start menu 325 | displayMessage("STARTED", "BasicWebserver\nsketch"); 326 | 327 | } 328 | 329 | 330 | // ---------------------------------------------------------------- 331 | // -loop 332 | // ---------------------------------------------------------------- 333 | // called from main loop 334 | 335 | void loop() { 336 | 337 | reUpdateButton(); // update rotary encoder button status (if pressed activate default menu) 338 | menuUpdate(); // update or action the oled menu 339 | 340 | 341 | 342 | // flash onboard led 343 | static uint32_t ledTimer = millis(); 344 | if ( (unsigned long)(millis() - ledTimer) > 1000 ) { 345 | digitalWrite(iLED, !digitalRead(iLED)); 346 | ledTimer = millis(); 347 | } 348 | 349 | } // oledLoop 350 | 351 | 352 | // ---------------------------------------------------------------- 353 | // -button debounce (rotary encoder) 354 | // ---------------------------------------------------------------- 355 | // update rotary encoder current button status 356 | 357 | void reUpdateButton() { 358 | bool tReading = digitalRead(encoder0Press); // read current button state 359 | if (tReading != rotaryEncoder.encoderPrevButton) rotaryEncoder.reLastButtonChange = millis(); // if it has changed reset timer 360 | if ( (unsigned long)(millis() - rotaryEncoder.reLastButtonChange) > rotaryEncoder.reDebounceDelay ) { // if button state is stable 361 | if (rotaryEncoder.encoderPrevButton == rotaryEncoder.reButtonPressedState) { 362 | if (rotaryEncoder.reButtonDebounced == 0) { // if the button has been pressed 363 | rotaryEncoder.reButtonPressed = 1; // flag set when the button has been pressed 364 | if (menuMode == off) defaultMenu(); // if the display is off start the default menu 365 | } 366 | rotaryEncoder.reButtonDebounced = 1; // debounced button status (1 when pressed) 367 | } else { 368 | rotaryEncoder.reButtonDebounced = 0; 369 | } 370 | } 371 | rotaryEncoder.encoderPrevButton = tReading; // update last state read 372 | } 373 | 374 | 375 | // ---------------------------------------------------------------- 376 | // -update the active menu 377 | // ---------------------------------------------------------------- 378 | 379 | void menuUpdate() { 380 | 381 | if (menuMode == off) return; // if menu system is turned off do nothing more 382 | 383 | // if no recent activity then turn oled off 384 | if ( (unsigned long)(millis() - oledMenu.lastMenuActivity) > (menuTimeout * 1000) ) { 385 | resetMenu(); 386 | return; 387 | } 388 | 389 | switch (menuMode) { 390 | 391 | // if there is an active menu 392 | case menu: 393 | serviceMenu(); 394 | menuActions(); 395 | break; 396 | 397 | // if there is an active none blocking 'enter value' 398 | case value: 399 | serviceValue(0); 400 | if (rotaryEncoder.reButtonPressed) { // if the button has been pressed 401 | menuValues(); // a value has been entered so action it 402 | break; 403 | } 404 | 405 | // if a message is being displayed 406 | case message: 407 | if (rotaryEncoder.reButtonPressed == 1) defaultMenu(); // if button has been pressed return to default menu 408 | break; 409 | 410 | default: 411 | break; 412 | } 413 | } 414 | 415 | 416 | // ---------------------------------------------------------------- 417 | // -service active menu 418 | // ---------------------------------------------------------------- 419 | 420 | void serviceMenu() { 421 | 422 | // rotary encoder 423 | if (rotaryEncoder.encoder0Pos >= itemTrigger) { 424 | rotaryEncoder.encoder0Pos -= itemTrigger; 425 | oledMenu.highlightedMenuItem++; 426 | oledMenu.lastMenuActivity = millis(); // log time 427 | } 428 | if (rotaryEncoder.encoder0Pos <= -itemTrigger) { 429 | rotaryEncoder.encoder0Pos += itemTrigger; 430 | oledMenu.highlightedMenuItem--; 431 | oledMenu.lastMenuActivity = millis(); // log time 432 | } 433 | if (rotaryEncoder.reButtonPressed == 1) { 434 | oledMenu.selectedMenuItem = oledMenu.highlightedMenuItem; // flag that the item has been selected 435 | oledMenu.lastMenuActivity = millis(); // log time 436 | if (serialDebug) Serial.println("menu '" + oledMenu.menuTitle + "' item '" + oledMenu.menuItems[oledMenu.highlightedMenuItem] + "' selected"); 437 | } 438 | 439 | const int _centreLine = displayMaxLines / 2 + 1; // mid list point 440 | display.clearDisplay(); 441 | display.setTextColor(WHITE); 442 | 443 | // verify valid highlighted item 444 | if (oledMenu.highlightedMenuItem > oledMenu.noOfmenuItems) oledMenu.highlightedMenuItem = oledMenu.noOfmenuItems; 445 | if (oledMenu.highlightedMenuItem < 1) oledMenu.highlightedMenuItem = 1; 446 | 447 | // title 448 | display.setCursor(0, 0); 449 | if (menuLargeText) { 450 | display.setTextSize(2); 451 | display.println(oledMenu.menuItems[oledMenu.highlightedMenuItem].substring(0, MaxmenuTitleLength)); 452 | } else { 453 | if (oledMenu.menuTitle.length() > MaxmenuTitleLength) display.setTextSize(1); 454 | else display.setTextSize(2); 455 | display.println(oledMenu.menuTitle); 456 | } 457 | display.drawLine(0, topLine-1, display.width(), topLine-1, WHITE); // draw horizontal line under title 458 | 459 | // menu 460 | display.setTextSize(1); 461 | display.setCursor(0, topLine); 462 | for (int i=1; i <= displayMaxLines; i++) { 463 | int item = oledMenu.highlightedMenuItem - _centreLine + i; 464 | if (item == oledMenu.highlightedMenuItem) display.setTextColor(BLACK, WHITE); 465 | else display.setTextColor(WHITE); 466 | if (item > 0 && item <= oledMenu.noOfmenuItems) display.println(oledMenu.menuItems[item]); 467 | else display.println(" "); 468 | } 469 | 470 | //// how to display some updating info. on the menu screen 471 | // display.setCursor(80, 25); 472 | // display.println(millis()); 473 | 474 | display.display(); 475 | } 476 | 477 | 478 | // ---------------------------------------------------------------- 479 | // -service value entry 480 | // ---------------------------------------------------------------- 481 | // if _blocking set to 1 then all other tasks are stopped until a value is entered 482 | 483 | int serviceValue(bool _blocking) { 484 | 485 | const int _valueSpacingX = 30; // spacing for the displayed value y position 486 | const int _valueSpacingY = 5; // spacing for the displayed value y position 487 | 488 | if (_blocking) { 489 | menuMode = blocking; 490 | oledMenu.lastMenuActivity = millis(); // log time of last activity (for timeout) 491 | } 492 | uint32_t tTime; 493 | 494 | do { 495 | 496 | // rotary encoder 497 | if (rotaryEncoder.encoder0Pos >= itemTrigger) { 498 | rotaryEncoder.encoder0Pos -= itemTrigger; 499 | oledMenu.mValueEntered-= oledMenu.mValueStep; 500 | oledMenu.lastMenuActivity = millis(); // log time 501 | } 502 | if (rotaryEncoder.encoder0Pos <= -itemTrigger) { 503 | rotaryEncoder.encoder0Pos += itemTrigger; 504 | oledMenu.mValueEntered+= oledMenu.mValueStep; 505 | oledMenu.lastMenuActivity = millis(); // log time 506 | } 507 | if (oledMenu.mValueEntered < oledMenu.mValueLow) { 508 | oledMenu.mValueEntered = oledMenu.mValueLow; 509 | oledMenu.lastMenuActivity = millis(); // log time 510 | } 511 | if (oledMenu.mValueEntered > oledMenu.mValueHigh) { 512 | oledMenu.mValueEntered = oledMenu.mValueHigh; 513 | oledMenu.lastMenuActivity = millis(); // log time 514 | } 515 | 516 | display.clearDisplay(); 517 | display.setTextColor(WHITE); 518 | 519 | // title 520 | display.setCursor(0, 0); 521 | if (oledMenu.menuTitle.length() > MaxmenuTitleLength) display.setTextSize(1); 522 | else display.setTextSize(2); 523 | display.println(oledMenu.menuTitle); 524 | display.drawLine(0, topLine-1, display.width(), topLine-1, WHITE); // draw horizontal line under title 525 | 526 | // value selected 527 | display.setCursor(_valueSpacingX, topLine + _valueSpacingY); 528 | display.setTextSize(3); 529 | display.println(oledMenu.mValueEntered); 530 | 531 | // range 532 | display.setCursor(0, display.height() - lineSpace1 - 1 ); // bottom of display 533 | display.setTextSize(1); 534 | display.println(String(oledMenu.mValueLow) + " to " + String(oledMenu.mValueHigh)); 535 | 536 | // bar 537 | int Tlinelength = map(oledMenu.mValueEntered, oledMenu.mValueLow, oledMenu.mValueHigh, 0 , display.width()); 538 | display.drawLine(0, display.height()-1, Tlinelength, display.height()-1, WHITE); 539 | 540 | display.display(); 541 | 542 | reUpdateButton(); // check status of button 543 | tTime = (unsigned long)(millis() - oledMenu.lastMenuActivity); // time since last activity 544 | 545 | } while (_blocking && rotaryEncoder.reButtonPressed == 0 && tTime < (menuTimeout * 1000)); // if in blocking mode repeat until button is pressed or timeout 546 | 547 | if (_blocking) menuMode = off; 548 | 549 | return oledMenu.mValueEntered; // used when in blocking mode 550 | 551 | } 552 | 553 | 554 | // ---------------------------------------------------------------- 555 | // -list create 556 | // ---------------------------------------------------------------- 557 | // create a menu from a list 558 | // e.g. String tList[]={"main menu", "2", "3", "4", "5", "6"}; 559 | // createList("demo_list", 6, &tList[0]); 560 | 561 | void createList(String _title, int _noOfElements, String *_list) { 562 | resetMenu(); // clear any previous menu 563 | menuMode = menu; // enable menu mode 564 | oledMenu.noOfmenuItems = _noOfElements; // set the number of items in this menu 565 | oledMenu.menuTitle = _title; // menus title (used to identify it) 566 | 567 | for (int i=1; i <= _noOfElements; i++) { 568 | oledMenu.menuItems[i] = _list[i-1]; // set the menu items 569 | } 570 | } 571 | 572 | 573 | // ---------------------------------------------------------------- 574 | // -message display 575 | // ---------------------------------------------------------------- 576 | // 21 characters per line, use "\n" for next line 577 | // assistant: < line 1 >< line 2 >< line 3 >< line 4 > 578 | 579 | void displayMessage(String _title, String _message) { 580 | resetMenu(); 581 | menuMode = message; 582 | 583 | display.clearDisplay(); 584 | display.setTextColor(WHITE); 585 | 586 | // title 587 | display.setCursor(0, 0); 588 | if (menuLargeText) { 589 | display.setTextSize(2); 590 | display.println(_title.substring(0, MaxmenuTitleLength)); 591 | } else { 592 | if (_title.length() > MaxmenuTitleLength) display.setTextSize(1); 593 | else display.setTextSize(2); 594 | display.println(_title); 595 | } 596 | 597 | // message 598 | display.setCursor(0, topLine); 599 | display.setTextSize(1); 600 | display.println(_message); 601 | 602 | display.display(); 603 | 604 | } 605 | 606 | 607 | // ---------------------------------------------------------------- 608 | // -reset menu system 609 | // ---------------------------------------------------------------- 610 | 611 | void resetMenu() { 612 | // reset all menu variables / flags 613 | menuMode = off; 614 | oledMenu.selectedMenuItem = 0; 615 | rotaryEncoder.encoder0Pos = 0; 616 | oledMenu.noOfmenuItems = 0; 617 | oledMenu.menuTitle = ""; 618 | oledMenu.highlightedMenuItem = 0; 619 | oledMenu.mValueEntered = 0; 620 | rotaryEncoder.reButtonPressed = 0; 621 | 622 | oledMenu.lastMenuActivity = millis(); // log time 623 | 624 | // clear oled display 625 | display.clearDisplay(); 626 | display.display(); 627 | } 628 | 629 | 630 | // ---------------------------------------------------------------- 631 | // -interrupt for rotary encoder 632 | // ---------------------------------------------------------------- 633 | // rotary encoder interrupt routine to update position counter when turned 634 | // interrupt info: https://www.gammon.com.au/forum/bbshowpost.php?id=11488 635 | 636 | void ICACHE_RAM_ATTR doEncoder() { 637 | 638 | bool pinA = digitalRead(encoder0PinA); 639 | bool pinB = digitalRead(encoder0PinB); 640 | 641 | if ( (rotaryEncoder.encoderPrevA == pinA && rotaryEncoder.encoderPrevB == pinB) ) return; // no change since last time (i.e. reject bounce) 642 | 643 | // same direction (alternating between 0,1 and 1,0 in one direction or 1,1 and 0,0 in the other direction) 644 | if (rotaryEncoder.encoderPrevA == 1 && rotaryEncoder.encoderPrevB == 0 && pinA == 0 && pinB == 1) rotaryEncoder.encoder0Pos -= 1; 645 | else if (rotaryEncoder.encoderPrevA == 0 && rotaryEncoder.encoderPrevB == 1 && pinA == 1 && pinB == 0) rotaryEncoder.encoder0Pos -= 1; 646 | else if (rotaryEncoder.encoderPrevA == 0 && rotaryEncoder.encoderPrevB == 0 && pinA == 1 && pinB == 1) rotaryEncoder.encoder0Pos += 1; 647 | else if (rotaryEncoder.encoderPrevA == 1 && rotaryEncoder.encoderPrevB == 1 && pinA == 0 && pinB == 0) rotaryEncoder.encoder0Pos += 1; 648 | 649 | // change of direction 650 | else if (rotaryEncoder.encoderPrevA == 1 && rotaryEncoder.encoderPrevB == 0 && pinA == 0 && pinB == 0) rotaryEncoder.encoder0Pos += 1; 651 | else if (rotaryEncoder.encoderPrevA == 0 && rotaryEncoder.encoderPrevB == 1 && pinA == 1 && pinB == 1) rotaryEncoder.encoder0Pos += 1; 652 | else if (rotaryEncoder.encoderPrevA == 0 && rotaryEncoder.encoderPrevB == 0 && pinA == 1 && pinB == 0) rotaryEncoder.encoder0Pos -= 1; 653 | else if (rotaryEncoder.encoderPrevA == 1 && rotaryEncoder.encoderPrevB == 1 && pinA == 0 && pinB == 1) rotaryEncoder.encoder0Pos -= 1; 654 | 655 | //else if (serialDebug) Serial.println("Error: invalid rotary encoder pin state - prev=" + String(rotaryEncoder.encoderPrevA) + "," 656 | // + String(rotaryEncoder.encoderPrevB) + " new=" + String(pinA) + "," + String(pinB)); 657 | 658 | // update previous readings 659 | rotaryEncoder.encoderPrevA = pinA; 660 | rotaryEncoder.encoderPrevB = pinB; 661 | } 662 | 663 | 664 | // ---------------------------------------------- end ---------------------------------------------- 665 | --------------------------------------------------------------------------------