diff --git a/libraries/DS18B20-ESP8266-Arduino/Basic Code.ino b/libraries/DS18B20-ESP8266-Arduino/Basic Code.ino new file mode 100644 index 0000000..1ddf75d --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/Basic Code.ino @@ -0,0 +1,42 @@ +// Include the libraries we need +#include +#include + +// Data wire is plugged into port 2 on the Arduino +#define ONE_WIRE_BUS 12 + +// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs) +OneWire oneWire(ONE_WIRE_BUS); + +// Pass our oneWire reference to Dallas Temperature. +DallasTemperature sensors(&oneWire); + +/* + * The setup function. We only start the sensors here + */ +void setup(void) +{ + // start serial port + Serial.begin(115200); + Serial.println("Dallas Temperature IC Control Library Demo"); + + // Start up the library + sensors.begin(); +} + +/* + * Main function, get and show the temperature + */ +void loop(void) +{ + // call sensors.requestTemperatures() to issue a global temperature + // request to all devices on the bus + Serial.print("Requesting temperatures..."); + sensors.requestTemperatures(); // Send the command to get temperatures + Serial.println("DONE"); + // After we got the temperatures, we can print them here. + // We use the function ByIndex, and as an example get the temperature from the first sensor only. + Serial.print("Temperature for the device 1 (index 0) is: "); + Serial.println(sensors.getTempCByIndex(0)); + delay(1000); +} diff --git a/libraries/DS18B20-ESP8266-Arduino/ESP8266 Temperature Web Server.ino b/libraries/DS18B20-ESP8266-Arduino/ESP8266 Temperature Web Server.ino new file mode 100644 index 0000000..b4d609b --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/ESP8266 Temperature Web Server.ino @@ -0,0 +1,164 @@ +#include +#include +#include +#include +#include +#include +#include + +// Data wire is connected to GPIO 4 +#define ONE_WIRE_BUS 12 + +// Setup a oneWire instance to communicate with any OneWire devices +OneWire oneWire(ONE_WIRE_BUS); + +// Pass our oneWire reference to Dallas Temperature sensor +DallasTemperature sensors(&oneWire); + +// Replace with your network credentials +const char* ssid = "YOUR_SSID"; +const char* password = "YOUR_PASSWORD"; + +// Create AsyncWebServer object on port 80 +AsyncWebServer server(80); + +String readDSTemperatureC() { + // Call sensors.requestTemperatures() to issue a global temperature and Requests to all devices on the bus + sensors.requestTemperatures(); + float tempC = sensors.getTempCByIndex(0); + + if(tempC == -127.00) { + Serial.println("Failed to read from DS18B20 sensor"); + return "--"; + } else { + Serial.print("Temperature Celsius: "); + Serial.println(tempC); + } + return String(tempC); +} + +String readDSTemperatureF() { + // Call sensors.requestTemperatures() to issue a global temperature and Requests to all devices on the bus + sensors.requestTemperatures(); + float tempF = sensors.getTempFByIndex(0); + + if(int(tempF) == -196){ + Serial.println("Failed to read from DS18B20 sensor"); + return "--"; + } else { + Serial.print("Temperature Fahrenheit: "); + Serial.println(tempF); + } + return String(tempF); +} + +const char index_html[] PROGMEM = R"rawliteral( + + + + + + + +

ESP DS18B20 Server

+

+ + Temperature Celsius + %TEMPERATUREC% + °C +

+

+ + Temperature Fahrenheit + %TEMPERATUREF% + °F +

+ + +)rawliteral"; + +// Replaces placeholder with DHT values +String processor(const String& var){ + //Serial.println(var); + if(var == "TEMPERATUREC"){ + return readDSTemperatureC(); + } + else if(var == "TEMPERATUREF"){ + return readDSTemperatureF(); + } + return String(); +} + +void setup(){ + // Serial port for debugging purposes + Serial.begin(115200); + Serial.println(); + + // Start up the DS18B20 library + sensors.begin(); + + // Connect to Wi-Fi + WiFi.begin(ssid, password); + Serial.println("Connecting to WiFi"); + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + Serial.println(); + + // Print ESP Local IP Address + Serial.println(WiFi.localIP()); + + // Route for root / web page + server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ + request->send_P(200, "text/html", index_html, processor); + }); + server.on("/temperaturec", HTTP_GET, [](AsyncWebServerRequest *request){ + request->send_P(200, "text/plain", readDSTemperatureC().c_str()); + }); + server.on("/temperaturef", HTTP_GET, [](AsyncWebServerRequest *request){ + request->send_P(200, "text/plain", readDSTemperatureF().c_str()); + }); + // Start server + server.begin(); +} + +void loop(){ + +} diff --git a/libraries/DS18B20-ESP8266-Arduino/LICENSE b/libraries/DS18B20-ESP8266-Arduino/LICENSE new file mode 100644 index 0000000..f288702 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/LICENSE @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. diff --git a/libraries/DS18B20-ESP8266-Arduino/README.md b/libraries/DS18B20-ESP8266-Arduino/README.md new file mode 100644 index 0000000..04ea9a2 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/README.md @@ -0,0 +1,30 @@ +# Interfacing DS18B20 with Arduino and ESP8266 + +![alt text](https://github.com/akarsh98/DS18B20-ESP8266-Arduino/blob/master/images/2.PNG) + +Video tutorial : + +In this project we are going to Interface the DS18B20 Temperature Sensor with Arduino UNO and ESP8266. + +The DS18B20 is a temperature sensor like DHT11 but it is more robust and accurate in comparison to DHT11.It also covers a larger temperature range in comparison to the DHT11 sensor. + +So.in this project we will first interface this Sensor with Arduino UNO and display its temperature reading on the Serial Monitor of Arduino IDE and after that we will connect this sensor with ESP8266 and after that we will dislay the temperature sensed on a beautiful web server. + +You must check out [PCBWAY](https://www.pcbway.com/) for ordering PCBs online for cheap! + +You get 10 good quality PCBs manufactured and shipped to your doorstep for cheap. You will also get a discount on shipping on your first order. Upload your Gerber files onto [PCBWAY](https://www.pcbway.com/) to get them manufactured with good quality and quick turnaround time. +Check out their online [Gerber viewer](https://www.pcbway.com/project/OnlineGerberViewer.html) function. With reward points you can get free stuff from their [gift shop](https://www.pcbway.com/project/gifts.html). +![alt text](https://github.com/akarsh98/Controlling-ESP8266-with-Alexa/blob/master/images/pcbway.JPG?raw=true) + +![alt text](https://github.com/akarsh98/DS18B20-ESP8266-Arduino/blob/master/images/8.PNG) + +![alt text](https://github.com/akarsh98/DS18B20-ESP8266-Arduino/blob/master/images/14.PNG) + + + +### These are the schematics for this project: +Schematic for Arduino UNO +![alt text](https://github.com/akarsh98/DS18B20-ESP8266-Arduino/blob/master/images/5.png) + +Schematic for ESP8266 +![alt text](https://github.com/akarsh98/DS18B20-ESP8266-Arduino/blob/master/images/10.png) diff --git a/libraries/DS18B20-ESP8266-Arduino/images/10.png b/libraries/DS18B20-ESP8266-Arduino/images/10.png new file mode 100644 index 0000000..4f45961 Binary files /dev/null and b/libraries/DS18B20-ESP8266-Arduino/images/10.png differ diff --git a/libraries/DS18B20-ESP8266-Arduino/images/11.PNG b/libraries/DS18B20-ESP8266-Arduino/images/11.PNG new file mode 100644 index 0000000..3488985 Binary files /dev/null and b/libraries/DS18B20-ESP8266-Arduino/images/11.PNG differ diff --git a/libraries/DS18B20-ESP8266-Arduino/images/12.PNG b/libraries/DS18B20-ESP8266-Arduino/images/12.PNG new file mode 100644 index 0000000..daeafcb Binary files /dev/null and b/libraries/DS18B20-ESP8266-Arduino/images/12.PNG differ diff --git a/libraries/DS18B20-ESP8266-Arduino/images/13.PNG b/libraries/DS18B20-ESP8266-Arduino/images/13.PNG new file mode 100644 index 0000000..7965cad Binary files /dev/null and b/libraries/DS18B20-ESP8266-Arduino/images/13.PNG differ diff --git a/libraries/DS18B20-ESP8266-Arduino/images/14.PNG b/libraries/DS18B20-ESP8266-Arduino/images/14.PNG new file mode 100644 index 0000000..eb762ef Binary files /dev/null and b/libraries/DS18B20-ESP8266-Arduino/images/14.PNG differ diff --git a/libraries/DS18B20-ESP8266-Arduino/images/2.PNG b/libraries/DS18B20-ESP8266-Arduino/images/2.PNG new file mode 100644 index 0000000..1e612fb Binary files /dev/null and b/libraries/DS18B20-ESP8266-Arduino/images/2.PNG differ diff --git a/libraries/DS18B20-ESP8266-Arduino/images/3.PNG b/libraries/DS18B20-ESP8266-Arduino/images/3.PNG new file mode 100644 index 0000000..ec83332 Binary files /dev/null and b/libraries/DS18B20-ESP8266-Arduino/images/3.PNG differ diff --git a/libraries/DS18B20-ESP8266-Arduino/images/4.PNG b/libraries/DS18B20-ESP8266-Arduino/images/4.PNG new file mode 100644 index 0000000..1748d86 Binary files /dev/null and b/libraries/DS18B20-ESP8266-Arduino/images/4.PNG differ diff --git a/libraries/DS18B20-ESP8266-Arduino/images/5.png b/libraries/DS18B20-ESP8266-Arduino/images/5.png new file mode 100644 index 0000000..ee3a1e4 Binary files /dev/null and b/libraries/DS18B20-ESP8266-Arduino/images/5.png differ diff --git a/libraries/DS18B20-ESP8266-Arduino/images/6.PNG b/libraries/DS18B20-ESP8266-Arduino/images/6.PNG new file mode 100644 index 0000000..fe550a1 Binary files /dev/null and b/libraries/DS18B20-ESP8266-Arduino/images/6.PNG differ diff --git a/libraries/DS18B20-ESP8266-Arduino/images/7.PNG b/libraries/DS18B20-ESP8266-Arduino/images/7.PNG new file mode 100644 index 0000000..673e46a Binary files /dev/null and b/libraries/DS18B20-ESP8266-Arduino/images/7.PNG differ diff --git a/libraries/DS18B20-ESP8266-Arduino/images/8.PNG b/libraries/DS18B20-ESP8266-Arduino/images/8.PNG new file mode 100644 index 0000000..e6659e8 Binary files /dev/null and b/libraries/DS18B20-ESP8266-Arduino/images/8.PNG differ diff --git a/libraries/DS18B20-ESP8266-Arduino/images/9.PNG b/libraries/DS18B20-ESP8266-Arduino/images/9.PNG new file mode 100644 index 0000000..eac1e24 Binary files /dev/null and b/libraries/DS18B20-ESP8266-Arduino/images/9.PNG differ diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/DallasTemperature.cpp b/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/DallasTemperature.cpp new file mode 100644 index 0000000..829c99d --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/DallasTemperature.cpp @@ -0,0 +1,885 @@ +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +#include "DallasTemperature.h" + +#if ARDUINO >= 100 +#include "Arduino.h" +#else +extern "C" { +#include "WConstants.h" +} +#endif + +// OneWire commands +#define STARTCONVO 0x44 // Tells device to take a temperature reading and put it on the scratchpad +#define COPYSCRATCH 0x48 // Copy EEPROM +#define READSCRATCH 0xBE // Read EEPROM +#define WRITESCRATCH 0x4E // Write to EEPROM +#define RECALLSCRATCH 0xB8 // Reload from last known +#define READPOWERSUPPLY 0xB4 // Determine if device needs parasite power +#define ALARMSEARCH 0xEC // Query bus for devices with an alarm condition + +// Scratchpad locations +#define TEMP_LSB 0 +#define TEMP_MSB 1 +#define HIGH_ALARM_TEMP 2 +#define LOW_ALARM_TEMP 3 +#define CONFIGURATION 4 +#define INTERNAL_BYTE 5 +#define COUNT_REMAIN 6 +#define COUNT_PER_C 7 +#define SCRATCHPAD_CRC 8 + +// Device resolution +#define TEMP_9_BIT 0x1F // 9 bit +#define TEMP_10_BIT 0x3F // 10 bit +#define TEMP_11_BIT 0x5F // 11 bit +#define TEMP_12_BIT 0x7F // 12 bit + +#define NO_ALARM_HANDLER ((AlarmHandler *)0) + +DallasTemperature::DallasTemperature() +{ +#if REQUIRESALARMS + setAlarmHandler(NO_ALARM_HANDLER); +#endif +} +DallasTemperature::DallasTemperature(OneWire* _oneWire) +{ + setOneWire(_oneWire); +#if REQUIRESALARMS + setAlarmHandler(NO_ALARM_HANDLER); +#endif +} + +bool DallasTemperature::validFamily(const uint8_t* deviceAddress) { + switch (deviceAddress[0]) { + case DS18S20MODEL: + case DS18B20MODEL: + case DS1822MODEL: + case DS1825MODEL: + case DS28EA00MODEL: + return true; + default: + return false; + } +} + +void DallasTemperature::setOneWire(OneWire* _oneWire) { + + _wire = _oneWire; + devices = 0; + ds18Count = 0; + parasite = false; + bitResolution = 9; + waitForConversion = true; + checkForConversion = true; + +} + +// initialise the bus +void DallasTemperature::begin(void) { + + DeviceAddress deviceAddress; + + _wire->reset_search(); + devices = 0; // Reset the number of devices when we enumerate wire devices + ds18Count = 0; // Reset number of DS18xxx Family devices + + while (_wire->search(deviceAddress)) { + + if (validAddress(deviceAddress)) { + + if (!parasite && readPowerSupply(deviceAddress)) + parasite = true; + + bitResolution = max(bitResolution, getResolution(deviceAddress)); + + devices++; + if (validFamily(deviceAddress)) { + ds18Count++; + } + } + } + +} + +// returns the number of devices found on the bus +uint8_t DallasTemperature::getDeviceCount(void) { + return devices; +} + +uint8_t DallasTemperature::getDS18Count(void) { + return ds18Count; +} + +// returns true if address is valid +bool DallasTemperature::validAddress(const uint8_t* deviceAddress) { + return (_wire->crc8(deviceAddress, 7) == deviceAddress[7]); +} + +// finds an address at a given index on the bus +// returns true if the device was found +bool DallasTemperature::getAddress(uint8_t* deviceAddress, uint8_t index) { + + uint8_t depth = 0; + + _wire->reset_search(); + + while (depth <= index && _wire->search(deviceAddress)) { + if (depth == index && validAddress(deviceAddress)) + return true; + depth++; + } + + return false; + +} + +// attempt to determine if the device at the given address is connected to the bus +bool DallasTemperature::isConnected(const uint8_t* deviceAddress) { + + ScratchPad scratchPad; + return isConnected(deviceAddress, scratchPad); + +} + +// attempt to determine if the device at the given address is connected to the bus +// also allows for updating the read scratchpad +bool DallasTemperature::isConnected(const uint8_t* deviceAddress, + uint8_t* scratchPad) { + bool b = readScratchPad(deviceAddress, scratchPad); + return b && (_wire->crc8(scratchPad, 8) == scratchPad[SCRATCHPAD_CRC]); +} + +bool DallasTemperature::readScratchPad(const uint8_t* deviceAddress, + uint8_t* scratchPad) { + + // send the reset command and fail fast + int b = _wire->reset(); + if (b == 0) + return false; + + _wire->select(deviceAddress); + _wire->write(READSCRATCH); + + // Read all registers in a simple loop + // byte 0: temperature LSB + // byte 1: temperature MSB + // byte 2: high alarm temp + // byte 3: low alarm temp + // byte 4: DS18S20: store for crc + // DS18B20 & DS1822: configuration register + // byte 5: internal use & crc + // byte 6: DS18S20: COUNT_REMAIN + // DS18B20 & DS1822: store for crc + // byte 7: DS18S20: COUNT_PER_C + // DS18B20 & DS1822: store for crc + // byte 8: SCRATCHPAD_CRC + for (uint8_t i = 0; i < 9; i++) { + scratchPad[i] = _wire->read(); + } + + b = _wire->reset(); + return (b == 1); +} + +void DallasTemperature::writeScratchPad(const uint8_t* deviceAddress, + const uint8_t* scratchPad) { + + _wire->reset(); + _wire->select(deviceAddress); + _wire->write(WRITESCRATCH); + _wire->write(scratchPad[HIGH_ALARM_TEMP]); // high alarm temp + _wire->write(scratchPad[LOW_ALARM_TEMP]); // low alarm temp + + // DS1820 and DS18S20 have no configuration register + if (deviceAddress[0] != DS18S20MODEL) + _wire->write(scratchPad[CONFIGURATION]); + + _wire->reset(); + + // save the newly written values to eeprom + _wire->select(deviceAddress); + _wire->write(COPYSCRATCH, parasite); + delay(20); // <--- added 20ms delay to allow 10ms long EEPROM write operation (as specified by datasheet) + + if (parasite) + delay(10); // 10ms delay + _wire->reset(); + +} + +bool DallasTemperature::readPowerSupply(const uint8_t* deviceAddress) { + + bool ret = false; + _wire->reset(); + _wire->select(deviceAddress); + _wire->write(READPOWERSUPPLY); + if (_wire->read_bit() == 0) + ret = true; + _wire->reset(); + return ret; + +} + +// set resolution of all devices to 9, 10, 11, or 12 bits +// if new resolution is out of range, it is constrained. +void DallasTemperature::setResolution(uint8_t newResolution) { + + bitResolution = constrain(newResolution, 9, 12); + DeviceAddress deviceAddress; + for (int i = 0; i < devices; i++) { + getAddress(deviceAddress, i); + setResolution(deviceAddress, bitResolution, true); + } + +} + +// set resolution of a device to 9, 10, 11, or 12 bits +// if new resolution is out of range, 9 bits is used. +bool DallasTemperature::setResolution(const uint8_t* deviceAddress, + uint8_t newResolution, bool skipGlobalBitResolutionCalculation) { + + // ensure same behavior as setResolution(uint8_t newResolution) + newResolution = constrain(newResolution, 9, 12); + + // return when stored value == new value + if (getResolution(deviceAddress) == newResolution) + return true; + + ScratchPad scratchPad; + if (isConnected(deviceAddress, scratchPad)) { + + // DS1820 and DS18S20 have no resolution configuration register + if (deviceAddress[0] != DS18S20MODEL) { + + switch (newResolution) { + case 12: + scratchPad[CONFIGURATION] = TEMP_12_BIT; + break; + case 11: + scratchPad[CONFIGURATION] = TEMP_11_BIT; + break; + case 10: + scratchPad[CONFIGURATION] = TEMP_10_BIT; + break; + case 9: + default: + scratchPad[CONFIGURATION] = TEMP_9_BIT; + break; + } + writeScratchPad(deviceAddress, scratchPad); + + // without calculation we can always set it to max + bitResolution = max(bitResolution, newResolution); + + if (!skipGlobalBitResolutionCalculation + && (bitResolution > newResolution)) { + bitResolution = newResolution; + DeviceAddress deviceAddr; + for (int i = 0; i < devices; i++) { + getAddress(deviceAddr, i); + bitResolution = max(bitResolution, + getResolution(deviceAddr)); + } + } + } + return true; // new value set + } + + return false; + +} + +// returns the global resolution +uint8_t DallasTemperature::getResolution() { + return bitResolution; +} + +// returns the current resolution of the device, 9-12 +// returns 0 if device not found +uint8_t DallasTemperature::getResolution(const uint8_t* deviceAddress) { + + // DS1820 and DS18S20 have no resolution configuration register + if (deviceAddress[0] == DS18S20MODEL) + return 12; + + ScratchPad scratchPad; + if (isConnected(deviceAddress, scratchPad)) { + switch (scratchPad[CONFIGURATION]) { + case TEMP_12_BIT: + return 12; + + case TEMP_11_BIT: + return 11; + + case TEMP_10_BIT: + return 10; + + case TEMP_9_BIT: + return 9; + } + } + return 0; + +} + +// sets the value of the waitForConversion flag +// TRUE : function requestTemperature() etc returns when conversion is ready +// FALSE: function requestTemperature() etc returns immediately (USE WITH CARE!!) +// (1) programmer has to check if the needed delay has passed +// (2) but the application can do meaningful things in that time +void DallasTemperature::setWaitForConversion(bool flag) { + waitForConversion = flag; +} + +// gets the value of the waitForConversion flag +bool DallasTemperature::getWaitForConversion() { + return waitForConversion; +} + +// sets the value of the checkForConversion flag +// TRUE : function requestTemperature() etc will 'listen' to an IC to determine whether a conversion is complete +// FALSE: function requestTemperature() etc will wait a set time (worst case scenario) for a conversion to complete +void DallasTemperature::setCheckForConversion(bool flag) { + checkForConversion = flag; +} + +// gets the value of the waitForConversion flag +bool DallasTemperature::getCheckForConversion() { + return checkForConversion; +} + +bool DallasTemperature::isConversionComplete() { + uint8_t b = _wire->read_bit(); + return (b == 1); +} + +// sends command for all devices on the bus to perform a temperature conversion +void DallasTemperature::requestTemperatures() { + + _wire->reset(); + _wire->skip(); + _wire->write(STARTCONVO, parasite); + + // ASYNC mode? + if (!waitForConversion) + return; + blockTillConversionComplete(bitResolution); + +} + +// sends command for one device to perform a temperature by address +// returns FALSE if device is disconnected +// returns TRUE otherwise +bool DallasTemperature::requestTemperaturesByAddress( + const uint8_t* deviceAddress) { + + uint8_t bitResolution = getResolution(deviceAddress); + if (bitResolution == 0) { + return false; //Device disconnected + } + + _wire->reset(); + _wire->select(deviceAddress); + _wire->write(STARTCONVO, parasite); + + // ASYNC mode? + if (!waitForConversion) + return true; + + blockTillConversionComplete(bitResolution); + + return true; + +} + +// Continue to check if the IC has responded with a temperature +void DallasTemperature::blockTillConversionComplete(uint8_t bitResolution) { + + int delms = millisToWaitForConversion(bitResolution); + if (checkForConversion && !parasite) { + unsigned long now = millis(); + while (!isConversionComplete() && (millis() - delms < now)) + ; + } else { + delay(delms); + } + +} + +// returns number of milliseconds to wait till conversion is complete (based on IC datasheet) +int16_t DallasTemperature::millisToWaitForConversion(uint8_t bitResolution) { + + switch (bitResolution) { + case 9: + return 94; + case 10: + return 188; + case 11: + return 375; + default: + return 750; + } + +} + +// sends command for one device to perform a temp conversion by index +bool DallasTemperature::requestTemperaturesByIndex(uint8_t deviceIndex) { + + DeviceAddress deviceAddress; + getAddress(deviceAddress, deviceIndex); + + return requestTemperaturesByAddress(deviceAddress); + +} + +// Fetch temperature for device index +float DallasTemperature::getTempCByIndex(uint8_t deviceIndex) { + + DeviceAddress deviceAddress; + if (!getAddress(deviceAddress, deviceIndex)) { + return DEVICE_DISCONNECTED_C; + } + + return getTempC((uint8_t*) deviceAddress); + +} + +// Fetch temperature for device index +float DallasTemperature::getTempFByIndex(uint8_t deviceIndex) { + + DeviceAddress deviceAddress; + + if (!getAddress(deviceAddress, deviceIndex)) { + return DEVICE_DISCONNECTED_F; + } + + return getTempF((uint8_t*) deviceAddress); + +} + +// reads scratchpad and returns fixed-point temperature, scaling factor 2^-7 +int16_t DallasTemperature::calculateTemperature(const uint8_t* deviceAddress, + uint8_t* scratchPad) { + + int16_t fpTemperature = (((int16_t) scratchPad[TEMP_MSB]) << 11) + | (((int16_t) scratchPad[TEMP_LSB]) << 3); + + /* + DS1820 and DS18S20 have a 9-bit temperature register. + + Resolutions greater than 9-bit can be calculated using the data from + the temperature, and COUNT REMAIN and COUNT PER °C registers in the + scratchpad. The resolution of the calculation depends on the model. + + While the COUNT PER °C register is hard-wired to 16 (10h) in a + DS18S20, it changes with temperature in DS1820. + + After reading the scratchpad, the TEMP_READ value is obtained by + truncating the 0.5°C bit (bit 0) from the temperature data. The + extended resolution temperature can then be calculated using the + following equation: + + COUNT_PER_C - COUNT_REMAIN + TEMPERATURE = TEMP_READ - 0.25 + -------------------------- + COUNT_PER_C + + Hagai Shatz simplified this to integer arithmetic for a 12 bits + value for a DS18S20, and James Cameron added legacy DS1820 support. + + See - http://myarduinotoy.blogspot.co.uk/2013/02/12bit-result-from-ds18s20.html + */ + + if (deviceAddress[0] == DS18S20MODEL) { + fpTemperature = ((fpTemperature & 0xfff0) << 3) - 16 + + (((scratchPad[COUNT_PER_C] - scratchPad[COUNT_REMAIN]) << 7) + / scratchPad[COUNT_PER_C]); + } + + return fpTemperature; +} + +// returns temperature in 1/128 degrees C or DEVICE_DISCONNECTED_RAW if the +// device's scratch pad cannot be read successfully. +// the numeric value of DEVICE_DISCONNECTED_RAW is defined in +// DallasTemperature.h. It is a large negative number outside the +// operating range of the device +int16_t DallasTemperature::getTemp(const uint8_t* deviceAddress) { + + ScratchPad scratchPad; + if (isConnected(deviceAddress, scratchPad)) + return calculateTemperature(deviceAddress, scratchPad); + return DEVICE_DISCONNECTED_RAW; + +} + +// returns temperature in degrees C or DEVICE_DISCONNECTED_C if the +// device's scratch pad cannot be read successfully. +// the numeric value of DEVICE_DISCONNECTED_C is defined in +// DallasTemperature.h. It is a large negative number outside the +// operating range of the device +float DallasTemperature::getTempC(const uint8_t* deviceAddress) { + return rawToCelsius(getTemp(deviceAddress)); +} + +// returns temperature in degrees F or DEVICE_DISCONNECTED_F if the +// device's scratch pad cannot be read successfully. +// the numeric value of DEVICE_DISCONNECTED_F is defined in +// DallasTemperature.h. It is a large negative number outside the +// operating range of the device +float DallasTemperature::getTempF(const uint8_t* deviceAddress) { + return rawToFahrenheit(getTemp(deviceAddress)); +} + +// returns true if the bus requires parasite power +bool DallasTemperature::isParasitePowerMode(void) { + return parasite; +} + +// IF alarm is not used one can store a 16 bit int of userdata in the alarm +// registers. E.g. an ID of the sensor. +// See github issue #29 + +// note if device is not connected it will fail writing the data. +void DallasTemperature::setUserData(const uint8_t* deviceAddress, + int16_t data) { + // return when stored value == new value + if (getUserData(deviceAddress) == data) + return; + + ScratchPad scratchPad; + if (isConnected(deviceAddress, scratchPad)) { + scratchPad[HIGH_ALARM_TEMP] = data >> 8; + scratchPad[LOW_ALARM_TEMP] = data & 255; + writeScratchPad(deviceAddress, scratchPad); + } +} + +int16_t DallasTemperature::getUserData(const uint8_t* deviceAddress) { + int16_t data = 0; + ScratchPad scratchPad; + if (isConnected(deviceAddress, scratchPad)) { + data = scratchPad[HIGH_ALARM_TEMP] << 8; + data += scratchPad[LOW_ALARM_TEMP]; + } + return data; +} + +// note If address cannot be found no error will be reported. +int16_t DallasTemperature::getUserDataByIndex(uint8_t deviceIndex) { + DeviceAddress deviceAddress; + getAddress(deviceAddress, deviceIndex); + return getUserData((uint8_t*) deviceAddress); +} + +void DallasTemperature::setUserDataByIndex(uint8_t deviceIndex, int16_t data) { + DeviceAddress deviceAddress; + getAddress(deviceAddress, deviceIndex); + setUserData((uint8_t*) deviceAddress, data); +} + +// Convert float Celsius to Fahrenheit +float DallasTemperature::toFahrenheit(float celsius) { + return (celsius * 1.8) + 32; +} + +// Convert float Fahrenheit to Celsius +float DallasTemperature::toCelsius(float fahrenheit) { + return (fahrenheit - 32) * 0.555555556; +} + +// convert from raw to Celsius +float DallasTemperature::rawToCelsius(int16_t raw) { + + if (raw <= DEVICE_DISCONNECTED_RAW) + return DEVICE_DISCONNECTED_C; + // C = RAW/128 + return (float) raw * 0.0078125; + +} + +// convert from raw to Fahrenheit +float DallasTemperature::rawToFahrenheit(int16_t raw) { + + if (raw <= DEVICE_DISCONNECTED_RAW) + return DEVICE_DISCONNECTED_F; + // C = RAW/128 + // F = (C*1.8)+32 = (RAW/128*1.8)+32 = (RAW*0.0140625)+32 + return ((float) raw * 0.0140625) + 32; + +} + +#if REQUIRESALARMS + +/* + + ALARMS: + + TH and TL Register Format + + BIT 7 BIT 6 BIT 5 BIT 4 BIT 3 BIT 2 BIT 1 BIT 0 + S 2^6 2^5 2^4 2^3 2^2 2^1 2^0 + + Only bits 11 through 4 of the temperature register are used + in the TH and TL comparison since TH and TL are 8-bit + registers. If the measured temperature is lower than or equal + to TL or higher than or equal to TH, an alarm condition exists + and an alarm flag is set inside the DS18B20. This flag is + updated after every temperature measurement; therefore, if the + alarm condition goes away, the flag will be turned off after + the next temperature conversion. + + */ + +// sets the high alarm temperature for a device in degrees Celsius +// accepts a float, but the alarm resolution will ignore anything +// after a decimal point. valid range is -55C - 125C +void DallasTemperature::setHighAlarmTemp(const uint8_t* deviceAddress, + int8_t celsius) { + + // return when stored value == new value + if (getHighAlarmTemp(deviceAddress) == celsius) + return; + + // make sure the alarm temperature is within the device's range + if (celsius > 125) + celsius = 125; + else if (celsius < -55) + celsius = -55; + + ScratchPad scratchPad; + if (isConnected(deviceAddress, scratchPad)) { + scratchPad[HIGH_ALARM_TEMP] = (uint8_t) celsius; + writeScratchPad(deviceAddress, scratchPad); + } + +} + +// sets the low alarm temperature for a device in degrees Celsius +// accepts a float, but the alarm resolution will ignore anything +// after a decimal point. valid range is -55C - 125C +void DallasTemperature::setLowAlarmTemp(const uint8_t* deviceAddress, + int8_t celsius) { + + // return when stored value == new value + if (getLowAlarmTemp(deviceAddress) == celsius) + return; + + // make sure the alarm temperature is within the device's range + if (celsius > 125) + celsius = 125; + else if (celsius < -55) + celsius = -55; + + ScratchPad scratchPad; + if (isConnected(deviceAddress, scratchPad)) { + scratchPad[LOW_ALARM_TEMP] = (uint8_t) celsius; + writeScratchPad(deviceAddress, scratchPad); + } + +} + +// returns a int8_t with the current high alarm temperature or +// DEVICE_DISCONNECTED for an address +int8_t DallasTemperature::getHighAlarmTemp(const uint8_t* deviceAddress) { + + ScratchPad scratchPad; + if (isConnected(deviceAddress, scratchPad)) + return (int8_t) scratchPad[HIGH_ALARM_TEMP]; + return DEVICE_DISCONNECTED_C; + +} + +// returns a int8_t with the current low alarm temperature or +// DEVICE_DISCONNECTED for an address +int8_t DallasTemperature::getLowAlarmTemp(const uint8_t* deviceAddress) { + + ScratchPad scratchPad; + if (isConnected(deviceAddress, scratchPad)) + return (int8_t) scratchPad[LOW_ALARM_TEMP]; + return DEVICE_DISCONNECTED_C; + +} + +// resets internal variables used for the alarm search +void DallasTemperature::resetAlarmSearch() { + + alarmSearchJunction = -1; + alarmSearchExhausted = 0; + for (uint8_t i = 0; i < 7; i++) { + alarmSearchAddress[i] = 0; + } + +} + +// This is a modified version of the OneWire::search method. +// +// Also added the OneWire search fix documented here: +// http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1238032295 +// +// Perform an alarm search. If this function returns a '1' then it has +// enumerated the next device and you may retrieve the ROM from the +// OneWire::address variable. If there are no devices, no further +// devices, or something horrible happens in the middle of the +// enumeration then a 0 is returned. If a new device is found then +// its address is copied to newAddr. Use +// DallasTemperature::resetAlarmSearch() to start over. +bool DallasTemperature::alarmSearch(uint8_t* newAddr) { + + uint8_t i; + int8_t lastJunction = -1; + uint8_t done = 1; + + if (alarmSearchExhausted) + return false; + if (!_wire->reset()) + return false; + + // send the alarm search command + _wire->write(0xEC, 0); + + for (i = 0; i < 64; i++) { + + uint8_t a = _wire->read_bit(); + uint8_t nota = _wire->read_bit(); + uint8_t ibyte = i / 8; + uint8_t ibit = 1 << (i & 7); + + // I don't think this should happen, this means nothing responded, but maybe if + // something vanishes during the search it will come up. + if (a && nota) + return false; + + if (!a && !nota) { + if (i == alarmSearchJunction) { + // this is our time to decide differently, we went zero last time, go one. + a = 1; + alarmSearchJunction = lastJunction; + } else if (i < alarmSearchJunction) { + + // take whatever we took last time, look in address + if (alarmSearchAddress[ibyte] & ibit) { + a = 1; + } else { + // Only 0s count as pending junctions, we've already exhausted the 0 side of 1s + a = 0; + done = 0; + lastJunction = i; + } + } else { + // we are blazing new tree, take the 0 + a = 0; + alarmSearchJunction = i; + done = 0; + } + // OneWire search fix + // See: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1238032295 + } + + if (a) + alarmSearchAddress[ibyte] |= ibit; + else + alarmSearchAddress[ibyte] &= ~ibit; + + _wire->write_bit(a); + } + + if (done) + alarmSearchExhausted = 1; + for (i = 0; i < 8; i++) + newAddr[i] = alarmSearchAddress[i]; + return true; + +} + +// returns true if device address might have an alarm condition +// (only an alarm search can verify this) +bool DallasTemperature::hasAlarm(const uint8_t* deviceAddress) { + + ScratchPad scratchPad; + if (isConnected(deviceAddress, scratchPad)) { + + int8_t temp = calculateTemperature(deviceAddress, scratchPad) >> 7; + + // check low alarm + if (temp <= (int8_t) scratchPad[LOW_ALARM_TEMP]) + return true; + + // check high alarm + if (temp >= (int8_t) scratchPad[HIGH_ALARM_TEMP]) + return true; + } + + // no alarm + return false; + +} + +// returns true if any device is reporting an alarm condition on the bus +bool DallasTemperature::hasAlarm(void) { + + DeviceAddress deviceAddress; + resetAlarmSearch(); + return alarmSearch(deviceAddress); +} + +// runs the alarm handler for all devices returned by alarmSearch() +// unless there no _AlarmHandler exist. +void DallasTemperature::processAlarms(void) { + +if (!hasAlarmHandler()) +{ + return; +} + + resetAlarmSearch(); + DeviceAddress alarmAddr; + + while (alarmSearch(alarmAddr)) { + if (validAddress(alarmAddr)) { + _AlarmHandler(alarmAddr); + } + } +} + +// sets the alarm handler +void DallasTemperature::setAlarmHandler(const AlarmHandler *handler) { + _AlarmHandler = handler; +} + +// checks if AlarmHandler has been set. +bool DallasTemperature::hasAlarmHandler() +{ + return _AlarmHandler != NO_ALARM_HANDLER; +} + +#endif + +#if REQUIRESNEW + +// MnetCS - Allocates memory for DallasTemperature. Allows us to instance a new object +void* DallasTemperature::operator new(unsigned int size) { // Implicit NSS obj size + + void * p;// void pointer + p = malloc(size);// Allocate memory + memset((DallasTemperature*)p,0,size);// Initialise memory + + //!!! CANT EXPLICITLY CALL CONSTRUCTOR - workaround by using an init() methodR - workaround by using an init() method + return (DallasTemperature*) p;// Cast blank region to NSS pointer +} + +// MnetCS 2009 - Free the memory used by this instance +void DallasTemperature::operator delete(void* p) { + + DallasTemperature* pNss = (DallasTemperature*) p; // Cast to NSS pointer + pNss->~DallasTemperature();// Destruct the object + + free(p);// Free the memory +} + +#endif diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/DallasTemperature.h b/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/DallasTemperature.h new file mode 100644 index 0000000..446bd58 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/DallasTemperature.h @@ -0,0 +1,251 @@ +#ifndef DallasTemperature_h +#define DallasTemperature_h + +#define DALLASTEMPLIBVERSION "3.7.9" // To be deprecated + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// set to true to include code for new and delete operators +#ifndef REQUIRESNEW +#define REQUIRESNEW false +#endif + +// set to true to include code implementing alarm search functions +#ifndef REQUIRESALARMS +#define REQUIRESALARMS true +#endif + +#include +#include + +// Model IDs +#define DS18S20MODEL 0x10 // also DS1820 +#define DS18B20MODEL 0x28 +#define DS1822MODEL 0x22 +#define DS1825MODEL 0x3B +#define DS28EA00MODEL 0x42 + +// Error Codes +#define DEVICE_DISCONNECTED_C -127 +#define DEVICE_DISCONNECTED_F -196.6 +#define DEVICE_DISCONNECTED_RAW -7040 + +typedef uint8_t DeviceAddress[8]; + +class DallasTemperature { +public: + + DallasTemperature(); + DallasTemperature(OneWire*); + + void setOneWire(OneWire*); + + // initialise bus + void begin(void); + + // returns the number of devices found on the bus + uint8_t getDeviceCount(void); + + // returns the number of DS18xxx Family devices on bus + uint8_t getDS18Count(void); + + // returns true if address is valid + bool validAddress(const uint8_t*); + + // returns true if address is of the family of sensors the lib supports. + bool validFamily(const uint8_t* deviceAddress); + + // finds an address at a given index on the bus + bool getAddress(uint8_t*, uint8_t); + + // attempt to determine if the device at the given address is connected to the bus + bool isConnected(const uint8_t*); + + // attempt to determine if the device at the given address is connected to the bus + // also allows for updating the read scratchpad + bool isConnected(const uint8_t*, uint8_t*); + + // read device's scratchpad + bool readScratchPad(const uint8_t*, uint8_t*); + + // write device's scratchpad + void writeScratchPad(const uint8_t*, const uint8_t*); + + // read device's power requirements + bool readPowerSupply(const uint8_t*); + + // get global resolution + uint8_t getResolution(); + + // set global resolution to 9, 10, 11, or 12 bits + void setResolution(uint8_t); + + // returns the device resolution: 9, 10, 11, or 12 bits + uint8_t getResolution(const uint8_t*); + + // set resolution of a device to 9, 10, 11, or 12 bits + bool setResolution(const uint8_t*, uint8_t, + bool skipGlobalBitResolutionCalculation = false); + + // sets/gets the waitForConversion flag + void setWaitForConversion(bool); + bool getWaitForConversion(void); + + // sets/gets the checkForConversion flag + void setCheckForConversion(bool); + bool getCheckForConversion(void); + + // sends command for all devices on the bus to perform a temperature conversion + void requestTemperatures(void); + + // sends command for one device to perform a temperature conversion by address + bool requestTemperaturesByAddress(const uint8_t*); + + // sends command for one device to perform a temperature conversion by index + bool requestTemperaturesByIndex(uint8_t); + + // returns temperature raw value (12 bit integer of 1/128 degrees C) + int16_t getTemp(const uint8_t*); + + // returns temperature in degrees C + float getTempC(const uint8_t*); + + // returns temperature in degrees F + float getTempF(const uint8_t*); + + // Get temperature for device index (slow) + float getTempCByIndex(uint8_t); + + // Get temperature for device index (slow) + float getTempFByIndex(uint8_t); + + // returns true if the bus requires parasite power + bool isParasitePowerMode(void); + + // Is a conversion complete on the wire? Only applies to the first sensor on the wire. + bool isConversionComplete(void); + + int16_t millisToWaitForConversion(uint8_t); + +#if REQUIRESALARMS + + typedef void AlarmHandler(const uint8_t*); + + // sets the high alarm temperature for a device + // accepts a int8_t. valid range is -55C - 125C + void setHighAlarmTemp(const uint8_t*, int8_t); + + // sets the low alarm temperature for a device + // accepts a int8_t. valid range is -55C - 125C + void setLowAlarmTemp(const uint8_t*, int8_t); + + // returns a int8_t with the current high alarm temperature for a device + // in the range -55C - 125C + int8_t getHighAlarmTemp(const uint8_t*); + + // returns a int8_t with the current low alarm temperature for a device + // in the range -55C - 125C + int8_t getLowAlarmTemp(const uint8_t*); + + // resets internal variables used for the alarm search + void resetAlarmSearch(void); + + // search the wire for devices with active alarms + bool alarmSearch(uint8_t*); + + // returns true if ia specific device has an alarm + bool hasAlarm(const uint8_t*); + + // returns true if any device is reporting an alarm on the bus + bool hasAlarm(void); + + // runs the alarm handler for all devices returned by alarmSearch() + void processAlarms(void); + + // sets the alarm handler + void setAlarmHandler(const AlarmHandler *); + + // returns true if an AlarmHandler has been set + bool hasAlarmHandler(); + +#endif + + // if no alarm handler is used the two bytes can be used as user data + // example of such usage is an ID. + // note if device is not connected it will fail writing the data. + // note if address cannot be found no error will be reported. + // in short use carefully + void setUserData(const uint8_t*, int16_t); + void setUserDataByIndex(uint8_t, int16_t); + int16_t getUserData(const uint8_t*); + int16_t getUserDataByIndex(uint8_t); + + // convert from Celsius to Fahrenheit + static float toFahrenheit(float); + + // convert from Fahrenheit to Celsius + static float toCelsius(float); + + // convert from raw to Celsius + static float rawToCelsius(int16_t); + + // convert from raw to Fahrenheit + static float rawToFahrenheit(int16_t); + +#if REQUIRESNEW + + // initialize memory area + void* operator new (unsigned int); + + // delete memory reference + void operator delete(void*); + +#endif + +private: + typedef uint8_t ScratchPad[9]; + + // parasite power on or off + bool parasite; + + // used to determine the delay amount needed to allow for the + // temperature conversion to take place + uint8_t bitResolution; + + // used to requestTemperature with or without delay + bool waitForConversion; + + // used to requestTemperature to dynamically check if a conversion is complete + bool checkForConversion; + + // count of devices on the bus + uint8_t devices; + + // count of DS18xxx Family devices on bus + uint8_t ds18Count; + + // Take a pointer to one wire instance + OneWire* _wire; + + // reads scratchpad and returns the raw temperature + int16_t calculateTemperature(const uint8_t*, uint8_t*); + + void blockTillConversionComplete(uint8_t); + +#if REQUIRESALARMS + + // required for alarmSearch + uint8_t alarmSearchAddress[8]; + int8_t alarmSearchJunction; + uint8_t alarmSearchExhausted; + + // the alarm handler function pointer + AlarmHandler *_AlarmHandler; + +#endif + +}; +#endif diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/README.md b/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/README.md new file mode 100644 index 0000000..2595b0f --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/README.md @@ -0,0 +1,67 @@ +# Arduino Library for Maxim Temperature Integrated Circuits + +## Usage + +This library supports the following devices : + + +* DS18B20 +* DS18S20 - Please note there appears to be an issue with this series. +* DS1822 +* DS1820 +* MAX31820 + + +You will need a pull-up resistor of about 5 KOhm between the 1-Wire data line +and your 5V power. If you are using the DS18B20, ground pins 1 and 3. The +centre pin is the data line '1-wire'. + +We have included a "REQUIRESNEW" and "REQUIRESALARMS" definition. If you +want to slim down the code feel free to use either of these by including + + + + #define REQUIRESNEW + +or + + #define REQUIRESALARMS + + +at the top of DallasTemperature.h + +Finally, please include OneWire from Paul Stoffregen in the library manager before you begin. + +## Credits + +The OneWire code has been derived from +http://www.arduino.cc/playground/Learning/OneWire. +Miles Burton originally developed this library. +Tim Newsome added support for multiple sensors on +the same bus. +Guil Barros [gfbarros@bappos.com] added getTempByAddress (v3.5) + Note: these are implemented as getTempC(address) and getTempF(address) +Rob Tillaart [rob.tillaart@gmail.com] added async modus (v3.7.0) + + +## Website + + +You can find the latest version of the library at +https://www.milesburton.com/Dallas_Temperature_Control_Library + +# License + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/examples/Alarm/Alarm.pde b/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/examples/Alarm/Alarm.pde new file mode 100644 index 0000000..d9c6e6c --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/examples/Alarm/Alarm.pde @@ -0,0 +1,162 @@ +#include +#include + +// Data wire is plugged into port 2 on the Arduino +#define ONE_WIRE_BUS 2 + +// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs) +OneWire oneWire(ONE_WIRE_BUS); + +// Pass our oneWire reference to Dallas Temperature. +DallasTemperature sensors(&oneWire); + +// arrays to hold device addresses +DeviceAddress insideThermometer, outsideThermometer; + +void setup(void) +{ + // start serial port + Serial.begin(9600); + Serial.println("Dallas Temperature IC Control Library Demo"); + + // Start up the library + sensors.begin(); + + // locate devices on the bus + Serial.print("Found "); + Serial.print(sensors.getDeviceCount(), DEC); + Serial.println(" devices."); + + // search for devices on the bus and assign based on an index. + if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0"); + if (!sensors.getAddress(outsideThermometer, 1)) Serial.println("Unable to find address for Device 1"); + + // show the addresses we found on the bus + Serial.print("Device 0 Address: "); + printAddress(insideThermometer); + Serial.println(); + + Serial.print("Device 0 Alarms: "); + printAlarms(insideThermometer); + Serial.println(); + + Serial.print("Device 1 Address: "); + printAddress(outsideThermometer); + Serial.println(); + + Serial.print("Device 1 Alarms: "); + printAlarms(outsideThermometer); + Serial.println(); + + Serial.println("Setting alarm temps..."); + + // alarm when temp is higher than 30C + sensors.setHighAlarmTemp(insideThermometer, 30); + + // alarm when temp is lower than -10C + sensors.setLowAlarmTemp(insideThermometer, -10); + + // alarm when temp is higher than 31C + sensors.setHighAlarmTemp(outsideThermometer, 31); + + // alarn when temp is lower than 27C + sensors.setLowAlarmTemp(outsideThermometer, 27); + + Serial.print("New Device 0 Alarms: "); + printAlarms(insideThermometer); + Serial.println(); + + Serial.print("New Device 1 Alarms: "); + printAlarms(outsideThermometer); + Serial.println(); +} + +// function to print a device address +void printAddress(DeviceAddress deviceAddress) +{ + for (uint8_t i = 0; i < 8; i++) + { + if (deviceAddress[i] < 16) Serial.print("0"); + Serial.print(deviceAddress[i], HEX); + } +} + +// function to print the temperature for a device +void printTemperature(DeviceAddress deviceAddress) +{ + float tempC = sensors.getTempC(deviceAddress); + Serial.print("Temp C: "); + Serial.print(tempC); + Serial.print(" Temp F: "); + Serial.print(DallasTemperature::toFahrenheit(tempC)); +} + +void printAlarms(uint8_t deviceAddress[]) +{ + char temp; + temp = sensors.getHighAlarmTemp(deviceAddress); + Serial.print("High Alarm: "); + Serial.print(temp, DEC); + Serial.print("C/"); + Serial.print(DallasTemperature::toFahrenheit(temp)); + Serial.print("F | Low Alarm: "); + temp = sensors.getLowAlarmTemp(deviceAddress); + Serial.print(temp, DEC); + Serial.print("C/"); + Serial.print(DallasTemperature::toFahrenheit(temp)); + Serial.print("F"); +} + +// main function to print information about a device +void printData(DeviceAddress deviceAddress) +{ + Serial.print("Device Address: "); + printAddress(deviceAddress); + Serial.print(" "); + printTemperature(deviceAddress); + Serial.println(); +} + +void checkAlarm(DeviceAddress deviceAddress) +{ + if (sensors.hasAlarm(deviceAddress)) + { + Serial.print("ALARM: "); + printData(deviceAddress); + } +} + +void loop(void) +{ + // call sensors.requestTemperatures() to issue a global temperature + // request to all devices on the bus + Serial.print("Requesting temperatures..."); + sensors.requestTemperatures(); + Serial.println("DONE"); + + // Method 1: + // check each address individually for an alarm condition + checkAlarm(insideThermometer); + checkAlarm(outsideThermometer); +/* + // Alternate method: + // Search the bus and iterate through addresses of devices with alarms + + // space for the alarm device's address + DeviceAddress alarmAddr; + + Serial.println("Searching for alarms..."); + + // resetAlarmSearch() must be called before calling alarmSearch() + sensors.resetAlarmSearch(); + + // alarmSearch() returns 0 when there are no devices with alarms + while (sensors.alarmSearch(alarmAddr)) + { + Serial.print("ALARM: "); + printData(alarmAddr); + } +*/ + +} + diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/examples/AlarmHandler/AlarmHandler.pde b/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/examples/AlarmHandler/AlarmHandler.pde new file mode 100644 index 0000000..e0750d5 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/examples/AlarmHandler/AlarmHandler.pde @@ -0,0 +1,144 @@ +#include +#include + +// Data wire is plugged into port 2 on the Arduino +#define ONE_WIRE_BUS 2 + +// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs) +OneWire oneWire(ONE_WIRE_BUS); + +// Pass our oneWire reference to Dallas Temperature. +DallasTemperature sensors(&oneWire); + +// arrays to hold device addresses +DeviceAddress insideThermometer, outsideThermometer; + +// function that will be called when an alarm condition exists during DallasTemperatures::processAlarms(); +void newAlarmHandler(uint8_t* deviceAddress) +{ + Serial.println("Alarm Handler Start"); + printAlarmInfo(deviceAddress); + printTemp(deviceAddress); + Serial.println(); + Serial.println("Alarm Handler Finish"); +} + +void printCurrentTemp(DeviceAddress deviceAddress) +{ + printAddress(deviceAddress); + printTemp(deviceAddress); + Serial.println(); +} + +void printAddress(DeviceAddress deviceAddress) +{ + Serial.print("Address: "); + for (uint8_t i = 0; i < 8; i++) + { + if (deviceAddress[i] < 16) Serial.print("0"); + Serial.print(deviceAddress[i], HEX); + } + Serial.print(" "); +} + +void printTemp(DeviceAddress deviceAddress) +{ + float tempC = sensors.getTempC(deviceAddress); + if (tempC != DEVICE_DISCONNECTED_C) + { + Serial.print("Current Temp C: "); + Serial.print(tempC); + } + else Serial.print("DEVICE DISCONNECTED"); + Serial.print(" "); +} + +void printAlarmInfo(DeviceAddress deviceAddress) +{ + char temp; + printAddress(deviceAddress); + temp = sensors.getHighAlarmTemp(deviceAddress); + Serial.print("High Alarm: "); + Serial.print(temp, DEC); + Serial.print("C"); + Serial.print(" Low Alarm: "); + temp = sensors.getLowAlarmTemp(deviceAddress); + Serial.print(temp, DEC); + Serial.print("C"); + Serial.print(" "); +} + +void setup(void) +{ + // start serial port + Serial.begin(9600); + Serial.println("Dallas Temperature IC Control Library Demo"); + + // Start up the library + sensors.begin(); + + // locate devices on the bus + Serial.print("Found "); + Serial.print(sensors.getDeviceCount(), DEC); + Serial.println(" devices."); + + // search for devices on the bus and assign based on an index + if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0"); + if (!sensors.getAddress(outsideThermometer, 1)) Serial.println("Unable to find address for Device 1"); + + Serial.print("Device insideThermometer "); + printAlarmInfo(insideThermometer); + Serial.println(); + + Serial.print("Device outsideThermometer "); + printAlarmInfo(outsideThermometer); + Serial.println(); + + // set alarm ranges + Serial.println("Setting alarm temps..."); + sensors.setHighAlarmTemp(insideThermometer, 26); + sensors.setLowAlarmTemp(insideThermometer, 22); + sensors.setHighAlarmTemp(outsideThermometer, 25); + sensors.setLowAlarmTemp(outsideThermometer, 21); + + Serial.print("New insideThermometer "); + printAlarmInfo(insideThermometer); + Serial.println(); + + Serial.print("New outsideThermometer "); + printAlarmInfo(outsideThermometer); + Serial.println(); + + // attach alarm handler + sensors.setAlarmHandler(&newAlarmHandler); + +} + +void loop(void) +{ + // ask the devices to measure the temperature + sensors.requestTemperatures(); + + // if an alarm condition exists as a result of the most recent + // requestTemperatures() request, it exists until the next time + // requestTemperatures() is called AND there isn't an alarm condition + // on the device + if (sensors.hasAlarm()) + { + Serial.println("Oh noes! There is at least one alarm on the bus."); + } + + // call alarm handler function defined by sensors.setAlarmHandler + // for each device reporting an alarm + sensors.processAlarms(); + + if (!sensors.hasAlarm()) + { + // just print out the current temperature + printCurrentTemp(insideThermometer); + printCurrentTemp(outsideThermometer); + } + + delay(1000); +} + diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/examples/Multibus_simple/Multibus_simple.ino b/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/examples/Multibus_simple/Multibus_simple.ino new file mode 100644 index 0000000..1055579 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/examples/Multibus_simple/Multibus_simple.ino @@ -0,0 +1,43 @@ +#include +#include + +OneWire ds18x20[] = { 3, 7 }; +const int oneWireCount = sizeof(ds18x20)/sizeof(OneWire); +DallasTemperature sensor[oneWireCount]; + +void setup(void) { + // start serial port + Serial.begin(9600); + Serial.println("Dallas Temperature Multiple Bus Control Library Simple Demo"); + Serial.print("============Ready with "); + Serial.print(oneWireCount); + Serial.println(" Sensors================"); + + // Start up the library on all defined bus-wires + DeviceAddress deviceAddress; + for (int i = 0; i < oneWireCount; i++) {; + sensor[i].setOneWire(&ds18x20[i]); + sensor[i].begin(); + if (sensor[i].getAddress(deviceAddress, 0)) sensor[i].setResolution(deviceAddress, 12); + } +} + +void loop(void) { + // call sensors.requestTemperatures() to issue a global temperature + // request to all devices on the bus + Serial.print("Requesting temperatures..."); + for (int i = 0; i < oneWireCount; i++) { + sensor[i].requestTemperatures(); + } + Serial.println("DONE"); + + delay(1000); + for (int i = 0; i < oneWireCount; i++) { + float temperature = sensor[i].getTempCByIndex(0); + Serial.print("Temperature for the sensor "); + Serial.print(i); + Serial.print(" is "); + Serial.println(temperature); + } + Serial.println(); +} \ No newline at end of file diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/examples/Multiple/Multiple.pde b/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/examples/Multiple/Multiple.pde new file mode 100644 index 0000000..7ccc7b1 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/examples/Multiple/Multiple.pde @@ -0,0 +1,143 @@ +// Include the libraries we need +#include +#include + +// Data wire is plugged into port 2 on the Arduino +#define ONE_WIRE_BUS 2 +#define TEMPERATURE_PRECISION 9 + +// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs) +OneWire oneWire(ONE_WIRE_BUS); + +// Pass our oneWire reference to Dallas Temperature. +DallasTemperature sensors(&oneWire); + +// arrays to hold device addresses +DeviceAddress insideThermometer, outsideThermometer; + +// Assign address manually. The addresses below will beed to be changed +// to valid device addresses on your bus. Device address can be retrieved +// by using either oneWire.search(deviceAddress) or individually via +// sensors.getAddress(deviceAddress, index) +// DeviceAddress insideThermometer = { 0x28, 0x1D, 0x39, 0x31, 0x2, 0x0, 0x0, 0xF0 }; +// DeviceAddress outsideThermometer = { 0x28, 0x3F, 0x1C, 0x31, 0x2, 0x0, 0x0, 0x2 }; + +void setup(void) +{ + // start serial port + Serial.begin(9600); + Serial.println("Dallas Temperature IC Control Library Demo"); + + // Start up the library + sensors.begin(); + + // locate devices on the bus + Serial.print("Locating devices..."); + Serial.print("Found "); + Serial.print(sensors.getDeviceCount(), DEC); + Serial.println(" devices."); + + // report parasite power requirements + Serial.print("Parasite power is: "); + if (sensors.isParasitePowerMode()) Serial.println("ON"); + else Serial.println("OFF"); + + // Search for devices on the bus and assign based on an index. Ideally, + // you would do this to initially discover addresses on the bus and then + // use those addresses and manually assign them (see above) once you know + // the devices on your bus (and assuming they don't change). + // + // method 1: by index + if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0"); + if (!sensors.getAddress(outsideThermometer, 1)) Serial.println("Unable to find address for Device 1"); + + // method 2: search() + // search() looks for the next device. Returns 1 if a new address has been + // returned. A zero might mean that the bus is shorted, there are no devices, + // or you have already retrieved all of them. It might be a good idea to + // check the CRC to make sure you didn't get garbage. The order is + // deterministic. You will always get the same devices in the same order + // + // Must be called before search() + //oneWire.reset_search(); + // assigns the first address found to insideThermometer + //if (!oneWire.search(insideThermometer)) Serial.println("Unable to find address for insideThermometer"); + // assigns the seconds address found to outsideThermometer + //if (!oneWire.search(outsideThermometer)) Serial.println("Unable to find address for outsideThermometer"); + + // show the addresses we found on the bus + Serial.print("Device 0 Address: "); + printAddress(insideThermometer); + Serial.println(); + + Serial.print("Device 1 Address: "); + printAddress(outsideThermometer); + Serial.println(); + + // set the resolution to 9 bit per device + sensors.setResolution(insideThermometer, TEMPERATURE_PRECISION); + sensors.setResolution(outsideThermometer, TEMPERATURE_PRECISION); + + Serial.print("Device 0 Resolution: "); + Serial.print(sensors.getResolution(insideThermometer), DEC); + Serial.println(); + + Serial.print("Device 1 Resolution: "); + Serial.print(sensors.getResolution(outsideThermometer), DEC); + Serial.println(); +} + +// function to print a device address +void printAddress(DeviceAddress deviceAddress) +{ + for (uint8_t i = 0; i < 8; i++) + { + // zero pad the address if necessary + if (deviceAddress[i] < 16) Serial.print("0"); + Serial.print(deviceAddress[i], HEX); + } +} + +// function to print the temperature for a device +void printTemperature(DeviceAddress deviceAddress) +{ + float tempC = sensors.getTempC(deviceAddress); + Serial.print("Temp C: "); + Serial.print(tempC); + Serial.print(" Temp F: "); + Serial.print(DallasTemperature::toFahrenheit(tempC)); +} + +// function to print a device's resolution +void printResolution(DeviceAddress deviceAddress) +{ + Serial.print("Resolution: "); + Serial.print(sensors.getResolution(deviceAddress)); + Serial.println(); +} + +// main function to print information about a device +void printData(DeviceAddress deviceAddress) +{ + Serial.print("Device Address: "); + printAddress(deviceAddress); + Serial.print(" "); + printTemperature(deviceAddress); + Serial.println(); +} + +/* + Main function, calls the temperatures in a loop. +*/ +void loop(void) +{ + // call sensors.requestTemperatures() to issue a global temperature + // request to all devices on the bus + Serial.print("Requesting temperatures..."); + sensors.requestTemperatures(); + Serial.println("DONE"); + + // print the device information + printData(insideThermometer); + printData(outsideThermometer); +} diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/examples/Simple/Simple.pde b/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/examples/Simple/Simple.pde new file mode 100644 index 0000000..68f7276 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/examples/Simple/Simple.pde @@ -0,0 +1,41 @@ +// Include the libraries we need +#include +#include + +// Data wire is plugged into port 2 on the Arduino +#define ONE_WIRE_BUS 2 + +// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs) +OneWire oneWire(ONE_WIRE_BUS); + +// Pass our oneWire reference to Dallas Temperature. +DallasTemperature sensors(&oneWire); + +/* + * The setup function. We only start the sensors here + */ +void setup(void) +{ + // start serial port + Serial.begin(9600); + Serial.println("Dallas Temperature IC Control Library Demo"); + + // Start up the library + sensors.begin(); +} + +/* + * Main function, get and show the temperature + */ +void loop(void) +{ + // call sensors.requestTemperatures() to issue a global temperature + // request to all devices on the bus + Serial.print("Requesting temperatures..."); + sensors.requestTemperatures(); // Send the command to get temperatures + Serial.println("DONE"); + // After we got the temperatures, we can print them here. + // We use the function ByIndex, and as an example get the temperature from the first sensor only. + Serial.print("Temperature for the device 1 (index 0) is: "); + Serial.println(sensors.getTempCByIndex(0)); +} diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/examples/Single/Single.pde b/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/examples/Single/Single.pde new file mode 100644 index 0000000..3c4e4b4 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/examples/Single/Single.pde @@ -0,0 +1,116 @@ +// Include the libraries we need +#include +#include + +// Data wire is plugged into port 2 on the Arduino +#define ONE_WIRE_BUS 2 + +// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs) +OneWire oneWire(ONE_WIRE_BUS); + +// Pass our oneWire reference to Dallas Temperature. +DallasTemperature sensors(&oneWire); + +// arrays to hold device address +DeviceAddress insideThermometer; + +/* + * Setup function. Here we do the basics + */ +void setup(void) +{ + // start serial port + Serial.begin(9600); + Serial.println("Dallas Temperature IC Control Library Demo"); + + // locate devices on the bus + Serial.print("Locating devices..."); + sensors.begin(); + Serial.print("Found "); + Serial.print(sensors.getDeviceCount(), DEC); + Serial.println(" devices."); + + // report parasite power requirements + Serial.print("Parasite power is: "); + if (sensors.isParasitePowerMode()) Serial.println("ON"); + else Serial.println("OFF"); + + // Assign address manually. The addresses below will beed to be changed + // to valid device addresses on your bus. Device address can be retrieved + // by using either oneWire.search(deviceAddress) or individually via + // sensors.getAddress(deviceAddress, index) + // Note that you will need to use your specific address here + //insideThermometer = { 0x28, 0x1D, 0x39, 0x31, 0x2, 0x0, 0x0, 0xF0 }; + + // Method 1: + // Search for devices on the bus and assign based on an index. Ideally, + // you would do this to initially discover addresses on the bus and then + // use those addresses and manually assign them (see above) once you know + // the devices on your bus (and assuming they don't change). + if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0"); + + // method 2: search() + // search() looks for the next device. Returns 1 if a new address has been + // returned. A zero might mean that the bus is shorted, there are no devices, + // or you have already retrieved all of them. It might be a good idea to + // check the CRC to make sure you didn't get garbage. The order is + // deterministic. You will always get the same devices in the same order + // + // Must be called before search() + //oneWire.reset_search(); + // assigns the first address found to insideThermometer + //if (!oneWire.search(insideThermometer)) Serial.println("Unable to find address for insideThermometer"); + + // show the addresses we found on the bus + Serial.print("Device 0 Address: "); + printAddress(insideThermometer); + Serial.println(); + + // set the resolution to 9 bit (Each Dallas/Maxim device is capable of several different resolutions) + sensors.setResolution(insideThermometer, 9); + + Serial.print("Device 0 Resolution: "); + Serial.print(sensors.getResolution(insideThermometer), DEC); + Serial.println(); +} + +// function to print the temperature for a device +void printTemperature(DeviceAddress deviceAddress) +{ + // method 1 - slower + //Serial.print("Temp C: "); + //Serial.print(sensors.getTempC(deviceAddress)); + //Serial.print(" Temp F: "); + //Serial.print(sensors.getTempF(deviceAddress)); // Makes a second call to getTempC and then converts to Fahrenheit + + // method 2 - faster + float tempC = sensors.getTempC(deviceAddress); + Serial.print("Temp C: "); + Serial.print(tempC); + Serial.print(" Temp F: "); + Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit +} +/* + * Main function. It will request the tempC from the sensors and display on Serial. + */ +void loop(void) +{ + // call sensors.requestTemperatures() to issue a global temperature + // request to all devices on the bus + Serial.print("Requesting temperatures..."); + sensors.requestTemperatures(); // Send the command to get temperatures + Serial.println("DONE"); + + // It responds almost immediately. Let's print out the data + printTemperature(insideThermometer); // Use a simple function to print out the data +} + +// function to print a device address +void printAddress(DeviceAddress deviceAddress) +{ + for (uint8_t i = 0; i < 8; i++) + { + if (deviceAddress[i] < 16) Serial.print("0"); + Serial.print(deviceAddress[i], HEX); + } +} diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/examples/Tester/Tester.pde b/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/examples/Tester/Tester.pde new file mode 100644 index 0000000..1b7db36 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/examples/Tester/Tester.pde @@ -0,0 +1,124 @@ +#include +#include + +// Data wire is plugged into port 2 on the Arduino +#define ONE_WIRE_BUS 2 +#define TEMPERATURE_PRECISION 9 // Lower resolution + +// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs) +OneWire oneWire(ONE_WIRE_BUS); + +// Pass our oneWire reference to Dallas Temperature. +DallasTemperature sensors(&oneWire); + +int numberOfDevices; // Number of temperature devices found + +DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address + +void setup(void) +{ + // start serial port + Serial.begin(9600); + Serial.println("Dallas Temperature IC Control Library Demo"); + + // Start up the library + sensors.begin(); + + // Grab a count of devices on the wire + numberOfDevices = sensors.getDeviceCount(); + + // locate devices on the bus + Serial.print("Locating devices..."); + + Serial.print("Found "); + Serial.print(numberOfDevices, DEC); + Serial.println(" devices."); + + // report parasite power requirements + Serial.print("Parasite power is: "); + if (sensors.isParasitePowerMode()) Serial.println("ON"); + else Serial.println("OFF"); + + // Loop through each device, print out address + for(int i=0;i +#include + +#define ONE_WIRE_BUS_1 2 +#define ONE_WIRE_BUS_2 4 + +OneWire oneWire_in(ONE_WIRE_BUS_1); +OneWire oneWire_out(ONE_WIRE_BUS_2); + +DallasTemperature sensor_inhouse(&oneWire_in); +DallasTemperature sensor_outhouse(&oneWire_out); + +void setup(void) +{ + Serial.begin(9600); + Serial.println("Dallas Temperature Control Library Demo - TwoPin_DS18B20"); + + sensor_inhouse.begin(); + sensor_outhouse.begin(); +} + +void loop(void) +{ + Serial.print("Requesting temperatures..."); + sensor_inhouse.requestTemperatures(); + sensor_outhouse.requestTemperatures(); + Serial.println(" done"); + + Serial.print("Inhouse: "); + Serial.println(sensor_inhouse.getTempCByIndex(0)); + + Serial.print("Outhouse: "); + Serial.println(sensor_outhouse.getTempCByIndex(0)); +} \ No newline at end of file diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/examples/UserDataDemo/UserDataDemo.ino b/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/examples/UserDataDemo/UserDataDemo.ino new file mode 100644 index 0000000..4cd6d25 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/examples/UserDataDemo/UserDataDemo.ino @@ -0,0 +1,115 @@ +// +// FILE: UserDataDemo.ino +// AUTHOR: Rob Tillaart +// VERSION: 0.1.0 +// PURPOSE: use of alarm field as user identification demo +// DATE: 2019-12-23 +// URL: +// +// Released to the public domain +// + +#include +#include + +#define ONE_WIRE_BUS 2 + +OneWire oneWire(ONE_WIRE_BUS); +DallasTemperature sensors(&oneWire); + +uint8_t deviceCount = 0; + +// Add 4 prepared sensors to the bus +// use the UserDataWriteBatch demo to prepare 4 different labeled sensors +struct +{ + int id; + DeviceAddress addr; +} T[4]; + +float getTempByID(int id) +{ + for (uint8_t index = 0; index < deviceCount; index++) + { + if (T[index].id == id) + { + return sensors.getTempC(T[index].addr); + } + } + return -999; +} + +void printAddress(DeviceAddress deviceAddress) +{ + for (uint8_t i = 0; i < 8; i++) + { + // zero pad the address if necessary + if (deviceAddress[i] < 16) Serial.print("0"); + Serial.print(deviceAddress[i], HEX); + } +} + +void setup(void) +{ + Serial.begin(115200); + Serial.println(__FILE__); + Serial.println("Dallas Temperature Demo"); + + sensors.begin(); + + // count devices + deviceCount = sensors.getDeviceCount(); + Serial.print("#devices: "); + Serial.println(deviceCount); + + // Read ID's per sensor + // and put them in T array + for (uint8_t index = 0; index < deviceCount; index++) + { + // go through sensors + sensors.getAddress(T[index].addr, index); + T[index].id = sensors.getUserData(T[index].addr); + } + + // Check all 4 sensors are set + for (uint8_t index = 0; index < deviceCount; index++) + { + Serial.println(); + Serial.println(T[index].id); + printAddress(T[index].addr); + Serial.println(); + } + Serial.println(); + +} + + +void loop(void) +{ + Serial.println(); + Serial.print(millis()); + Serial.println("\treq temp"); + sensors.requestTemperatures(); + + Serial.print(millis()); + Serial.println("\tGet temp by address"); + for (int i = 0; i < 4; i++) + { + Serial.print(millis()); + Serial.print("\t temp:\t"); + Serial.println(sensors.getTempC(T[i].addr)); + } + + Serial.print(millis()); + Serial.println("\tGet temp by ID"); // assume ID = 0, 1, 2, 3 + for (int id = 0; id < 4; id++) + { + Serial.print(millis()); + Serial.print("\t temp:\t"); + Serial.println(getTempByID(id)); + } + + delay(1000); +} + +// END OF FILE \ No newline at end of file diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/examples/UserDataWriteBatch/UserDataWriteBatch.ino b/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/examples/UserDataWriteBatch/UserDataWriteBatch.ino new file mode 100644 index 0000000..b59f33b --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/examples/UserDataWriteBatch/UserDataWriteBatch.ino @@ -0,0 +1,107 @@ +// +// FILE: UserDataWriteBatch.ino +// AUTHOR: Rob Tillaart +// VERSION: 0.1.0 +// PURPOSE: use of alarm field as user identification demo +// DATE: 2019-12-23 +// URL: +// +// Released to the public domain +// + +#include +#include + +#define ONE_WIRE_BUS 2 + +OneWire oneWire(ONE_WIRE_BUS); +DallasTemperature sensors(&oneWire); + +uint8_t deviceCount = 0; + +void printAddress(DeviceAddress deviceAddress) +{ + for (uint8_t i = 0; i < 8; i++) + { + // zero pad the address if necessary + if (deviceAddress[i] < 16) Serial.print("0"); + Serial.print(deviceAddress[i], HEX); + } +} + + + +void setup(void) +{ + Serial.begin(115200); + Serial.println(__FILE__); + Serial.println("Write user ID to DS18B20\n"); + + sensors.begin(); + + // count devices + deviceCount = sensors.getDeviceCount(); + Serial.print("#devices: "); + Serial.println(deviceCount); + + Serial.println(); + Serial.println("current ID's"); + for (uint8_t index = 0; index < deviceCount; index++) + { + DeviceAddress t; + sensors.getAddress(t, index); + printAddress(t); + Serial.print("\t\tID: "); + int id = sensors.getUserData(t); + Serial.println(id); + } + + Serial.println(); + Serial.print("Enter ID for batch: "); + int c = 0; + int id = 0; + while (c != '\n' && c != '\r') + { + c = Serial.read(); + switch(c) + { + case '0'...'9': + id *= 10; + id += (c - '0'); + break; + default: + break; + } + } + Serial.println(); + Serial.println(id); + Serial.println(); + + Serial.println("Start labeling ..."); + for (uint8_t index = 0; index < deviceCount; index++) + { + Serial.print("."); + DeviceAddress t; + sensors.getAddress(t, index); + sensors.setUserData(t, id); + } + Serial.println(); + + Serial.println(); + Serial.println("Show results ..."); + for (uint8_t index = 0; index < deviceCount; index++) + { + DeviceAddress t; + sensors.getAddress(t, index); + printAddress(t); + Serial.print("\t\tID: "); + int id = sensors.getUserData(t); + Serial.println(id); + } + Serial.println("Done ..."); + +} + +void loop(void) {} + +// END OF FILE \ No newline at end of file diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/examples/WaitForConversion/WaitForConversion.pde b/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/examples/WaitForConversion/WaitForConversion.pde new file mode 100644 index 0000000..3adda17 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/examples/WaitForConversion/WaitForConversion.pde @@ -0,0 +1,66 @@ +#include +#include + +// Data wire is plugged into port 2 on the Arduino +#define ONE_WIRE_BUS 2 + +// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs) +OneWire oneWire(ONE_WIRE_BUS); + +// Pass our oneWire reference to Dallas Temperature. +DallasTemperature sensors(&oneWire); + +void setup(void) +{ + // start serial port + Serial.begin(115200); + Serial.println("Dallas Temperature Control Library - Async Demo"); + Serial.println("\nDemo shows the difference in length of the call\n\n"); + + // Start up the library + sensors.begin(); +} + +void loop(void) +{ + // Request temperature conversion (traditional) + Serial.println("Before blocking requestForConversion"); + unsigned long start = millis(); + + sensors.requestTemperatures(); + + unsigned long stop = millis(); + Serial.println("After blocking requestForConversion"); + Serial.print("Time used: "); + Serial.println(stop - start); + + // get temperature + Serial.print("Temperature: "); + Serial.println(sensors.getTempCByIndex(0)); + Serial.println("\n"); + + // Request temperature conversion - non-blocking / async + Serial.println("Before NON-blocking/async requestForConversion"); + start = millis(); + sensors.setWaitForConversion(false); // makes it async + sensors.requestTemperatures(); + sensors.setWaitForConversion(true); + stop = millis(); + Serial.println("After NON-blocking/async requestForConversion"); + Serial.print("Time used: "); + Serial.println(stop - start); + + + // 9 bit resolution by default + // Note the programmer is responsible for the right delay + // we could do something usefull here instead of the delay + int resolution = 9; + delay(750/ (1 << (12-resolution))); + + // get temperature + Serial.print("Temperature: "); + Serial.println(sensors.getTempCByIndex(0)); + Serial.println("\n\n\n\n"); + + delay(5000); +} diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/examples/WaitForConversion2/WaitForConversion2.pde b/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/examples/WaitForConversion2/WaitForConversion2.pde new file mode 100644 index 0000000..4322330 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/examples/WaitForConversion2/WaitForConversion2.pde @@ -0,0 +1,80 @@ +// +// Sample of using Async reading of Dallas Temperature Sensors +// +#include +#include + +// Data wire is plugged into port 2 on the Arduino +#define ONE_WIRE_BUS 2 + +// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs) +OneWire oneWire(ONE_WIRE_BUS); + +// Pass our oneWire reference to Dallas Temperature. +DallasTemperature sensors(&oneWire); + +DeviceAddress tempDeviceAddress; + +int resolution = 12; +unsigned long lastTempRequest = 0; +int delayInMillis = 0; +float temperature = 0.0; +int idle = 0; +// +// SETUP +// +void setup(void) +{ + Serial.begin(115200); + Serial.println("Dallas Temperature Control Library - Async Demo"); + Serial.print("Library Version: "); + Serial.println(DALLASTEMPLIBVERSION); + Serial.println("\n"); + + sensors.begin(); + sensors.getAddress(tempDeviceAddress, 0); + sensors.setResolution(tempDeviceAddress, resolution); + + sensors.setWaitForConversion(false); + sensors.requestTemperatures(); + delayInMillis = 750 / (1 << (12 - resolution)); + lastTempRequest = millis(); + + pinMode(13, OUTPUT); +} + +void loop(void) +{ + + if (millis() - lastTempRequest >= delayInMillis) // waited long enough?? + { + digitalWrite(13, LOW); + Serial.print(" Temperature: "); + temperature = sensors.getTempCByIndex(0); + Serial.println(temperature, resolution - 8); + Serial.print(" Resolution: "); + Serial.println(resolution); + Serial.print("Idle counter: "); + Serial.println(idle); + Serial.println(); + + idle = 0; + + // immediately after fetching the temperature we request a new sample + // in the async modus + // for the demo we let the resolution change to show differences + resolution++; + if (resolution > 12) resolution = 9; + + sensors.setResolution(tempDeviceAddress, resolution); + sensors.requestTemperatures(); + delayInMillis = 750 / (1 << (12 - resolution)); + lastTempRequest = millis(); + } + + digitalWrite(13, HIGH); + // we can do usefull things here + // for the demo we just count the idle time in millis + delay(1); + idle++; +} diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/examples/oneWireSearch/oneWireSearch.ino b/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/examples/oneWireSearch/oneWireSearch.ino new file mode 100644 index 0000000..44da619 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/examples/oneWireSearch/oneWireSearch.ino @@ -0,0 +1,67 @@ +// +// FILE: oneWireSearch.ino +// AUTHOR: Rob Tillaart +// VERSION: 0.1.02 +// PURPOSE: scan for 1-Wire devices + code snippet generator +// DATE: 2015-june-30 +// URL: http://forum.arduino.cc/index.php?topic=333923 +// +// inspired by http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html +// +// Released to the public domain +// +// 0.1.00 initial version +// 0.1.01 first published version +// 0.1.02 small output changes + +#include + +void setup() +{ + Serial.begin(115200); + Serial.println("//\n// Start oneWireSearch.ino \n//"); + + for (uint8_t pin = 2; pin < 13; pin++) + { + findDevices(pin); + } + Serial.println("\n//\n// End oneWireSearch.ino \n//"); +} + +void loop() +{ +} + +uint8_t findDevices(int pin) +{ + OneWire ow(pin); + + uint8_t address[8]; + uint8_t count = 0; + + + if (ow.search(address)) + { + Serial.print("\nuint8_t pin"); + Serial.print(pin, DEC); + Serial.println("[][8] = {"); + do { + count++; + Serial.println(" {"); + for (uint8_t i = 0; i < 8; i++) + { + Serial.print("0x"); + if (address[i] < 0x10) Serial.print("0"); + Serial.print(address[i], HEX); + if (i < 7) Serial.print(", "); + } + Serial.println(" },"); + } while (ow.search(address)); + + Serial.println("};"); + Serial.print("// nr devices found: "); + Serial.println(count); + } + + return count; +} diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/examples/setUserData/SetUserData.ino b/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/examples/setUserData/SetUserData.ino new file mode 100644 index 0000000..44bd797 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/examples/setUserData/SetUserData.ino @@ -0,0 +1,47 @@ +// +// This sketch does not use the ALARM registers and uses those 2 bytes as a counter +// these 2 bytes can be used for other purposes as well e.g. last temperature or +// a specific ID. +// + +#include +#include + +// Data wire is plugged into port 2 on the Arduino +#define ONE_WIRE_BUS 2 + +// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs) +OneWire oneWire(ONE_WIRE_BUS); + +// Pass our oneWire reference to Dallas Temperature. +DallasTemperature sensors(&oneWire); + +int count = 0; + +void setup(void) +{ + // start serial port + Serial.begin(9600); + Serial.println("Dallas Temperature IC Control Library Demo"); + + // Start up the library + sensors.begin(); + +} + +void loop(void) +{ + // call sensors.requestTemperatures() to issue a global temperature + // request to all devices on the bus + Serial.print("Requesting temperatures..."); + sensors.requestTemperatures(); // Send the command to get temperatures + Serial.println("DONE"); + + Serial.print("Temperature for the device 1 (index 0) is: "); + Serial.println(sensors.getTempCByIndex(0)); + + count++; + sensors.setUserDataByIndex(0, count); + int x = sensors.getUserDataByIndex(0); + Serial.println(count); +} diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/keywords.txt b/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/keywords.txt new file mode 100644 index 0000000..37df467 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/keywords.txt @@ -0,0 +1,54 @@ +####################################### +# Syntax Coloring Map For DallasTemperature +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### +DallasTemperature KEYWORD1 +OneWire KEYWORD1 +AlarmHandler KEYWORD1 +DeviceAddress KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +setResolution KEYWORD2 +getResolution KEYWORD2 +getTempC KEYWORD2 +toFahrenheit KEYWORD2 +getTempF KEYWORD2 +getTempCByIndex KEYWORD2 +getTempFByIndex KEYWORD2 +setWaitForConversion KEYWORD2 +getWaitForConversion KEYWORD2 +requestTemperatures KEYWORD2 +requestTemperaturesByAddress KEYWORD2 +requestTemperaturesByIndex KEYWORD2 +isParasitePowerMode KEYWORD2 +begin KEYWORD2 +getDeviceCount KEYWORD2 +getAddress KEYWORD2 +validAddress KEYWORD2 +isConnected KEYWORD2 +readScratchPad KEYWORD2 +writeScratchPad KEYWORD2 +readPowerSupply KEYWORD2 +setHighAlarmTemp KEYWORD2 +setLowAlarmTemp KEYWORD2 +getHighAlarmTemp KEYWORD2 +getLowAlarmTemp KEYWORD2 +resetAlarmSearch KEYWORD2 +alarmSearch KEYWORD2 +hasAlarm KEYWORD2 +toCelsius KEYWORD2 +processAlarmss KEYWORD2 +setAlarmHandlers KEYWORD2 +defaultAlarmHandler KEYWORD2 +calculateTemperature KEYWORD2 + +####################################### +# Constants (LITERAL1) +####################################### + diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/library.json b/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/library.json new file mode 100644 index 0000000..a43aefe --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/library.json @@ -0,0 +1,40 @@ +{ + "name": "DallasTemperature", + "keywords": "onewire, 1-wire, bus, sensor, temperature", + "description": "Arduino Library for Dallas Temperature ICs (DS18B20, DS18S20, DS1822, DS1820)", + "repository": + { + "type": "git", + "url": "https://github.com/milesburton/Arduino-Temperature-Control-Library.git" + }, + "authors": + [ + { + "name": "Miles Burton", + "email": "miles@mnetcs.com", + "url": "http://www.milesburton.com", + "maintainer": true + }, + { + "name": "Tim Newsome", + "email": "nuisance@casualhacker.net" + }, + { + "name": "Guil Barros", + "email": "gfbarros@bappos.com" + }, + { + "name": "Rob Tillaart", + "email": "rob.tillaart@gmail.com" + } + ], + "dependencies": + { + "name": "OneWire", + "authors": "Paul Stoffregen", + "frameworks": "arduino" + }, + "version": "3.8.0", + "frameworks": "arduino", + "platforms": "*" +} diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/library.properties b/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/library.properties new file mode 100644 index 0000000..969e560 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/DallasTemperature/library.properties @@ -0,0 +1,9 @@ +name=DallasTemperature +version=3.8.0 +author=Miles Burton , Tim Newsome , Guil Barros , Rob Tillaart +maintainer=Miles Burton +sentence=Arduino Library for Dallas Temperature ICs +paragraph=Supports DS18B20, DS18S20, DS1822, DS1820 +category=Sensors +url=https://github.com/milesburton/Arduino-Temperature-Control-Library +architectures=* diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/LICENSE.txt b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/LICENSE.txt new file mode 100644 index 0000000..65c5ca8 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/LICENSE.txt @@ -0,0 +1,165 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/README.md b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/README.md new file mode 100644 index 0000000..593af58 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/README.md @@ -0,0 +1,32 @@ +# ESPAsyncTCP +[![Build Status](https://travis-ci.org/me-no-dev/ESPAsyncTCP.svg?branch=master)](https://travis-ci.org/me-no-dev/ESPAsyncTCP) ![](https://github.com/me-no-dev/ESPAsyncTCP/workflows/ESP%20Async%20TCP%20CI/badge.svg) + +### Async TCP Library for ESP8266 Arduino + +For ESP32 look [HERE](https://github.com/me-no-dev/AsyncTCP) + +[![Join the chat at https://gitter.im/me-no-dev/ESPAsyncWebServer](https://badges.gitter.im/me-no-dev/ESPAsyncWebServer.svg)](https://gitter.im/me-no-dev/ESPAsyncWebServer?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) + +This is a fully asynchronous TCP library, aimed at enabling trouble-free, multi-connection network environment for Espressif's ESP8266 MCUs. + +This library is the base for [ESPAsyncWebServer](https://github.com/me-no-dev/ESPAsyncWebServer) + +## AsyncClient and AsyncServer +The base classes on which everything else is built. They expose all possible scenarios, but are really raw and require more skills to use. + +## AsyncPrinter +This class can be used to send data like any other ```Print``` interface (```Serial``` for example). +The object then can be used outside of the Async callbacks (the loop) and receive asynchronously data using ```onData```. The object can be checked if the underlying ```AsyncClient```is connected, or hook to the ```onDisconnect``` callback. + +## AsyncTCPbuffer +This class is really similar to the ```AsyncPrinter```, but it differs in the fact that it can buffer some of the incoming data. + +## SyncClient +It is exactly what it sounds like. This is a standard, blocking TCP Client, similar to the one included in ```ESP8266WiFi``` + +## Libraries and projects that use AsyncTCP +- [ESP Async Web Server](https://github.com/me-no-dev/ESPAsyncWebServer) +- [Async MQTT client](https://github.com/marvinroger/async-mqtt-client) +- [arduinoWebSockets](https://github.com/Links2004/arduinoWebSockets) +- [ESP8266 Smart Home](https://github.com/baruch/esp8266_smart_home) +- [KBox Firmware](https://github.com/sarfata/kbox-firmware) diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/examples/ClientServer/Client/Client.ino b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/examples/ClientServer/Client/Client.ino new file mode 100644 index 0000000..b30d791 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/examples/ClientServer/Client/Client.ino @@ -0,0 +1,62 @@ +#include +#include + +extern "C" { +#include +#include +} + +#include "config.h" + +static os_timer_t intervalTimer; + +static void replyToServer(void* arg) { + AsyncClient* client = reinterpret_cast(arg); + + // send reply + if (client->space() > 32 && client->canSend()) { + char message[32]; + sprintf(message, "this is from %s", WiFi.localIP().toString().c_str()); + client->add(message, strlen(message)); + client->send(); + } +} + +/* event callbacks */ +static void handleData(void* arg, AsyncClient* client, void *data, size_t len) { + Serial.printf("\n data received from %s \n", client->remoteIP().toString().c_str()); + Serial.write((uint8_t*)data, len); + + os_timer_arm(&intervalTimer, 2000, true); // schedule for reply to server at next 2s +} + +void onConnect(void* arg, AsyncClient* client) { + Serial.printf("\n client has been connected to %s on port %d \n", SERVER_HOST_NAME, TCP_PORT); + replyToServer(client); +} + + +void setup() { + Serial.begin(115200); + delay(20); + + // connects to access point + WiFi.mode(WIFI_STA); + WiFi.begin(SSID, PASSWORD); + while (WiFi.status() != WL_CONNECTED) { + Serial.print('.'); + delay(500); + } + + AsyncClient* client = new AsyncClient; + client->onData(&handleData, client); + client->onConnect(&onConnect, client); + client->connect(SERVER_HOST_NAME, TCP_PORT); + + os_timer_disarm(&intervalTimer); + os_timer_setfn(&intervalTimer, &replyToServer, client); +} + +void loop() { + +} diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/examples/ClientServer/Client/config.h b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/examples/ClientServer/Client/config.h new file mode 100644 index 0000000..cf51e91 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/examples/ClientServer/Client/config.h @@ -0,0 +1,23 @@ +#ifndef CONFIG_H +#define CONFIG_H + +/* + * This example demonstrate how to use asynchronous client & server APIs + * in order to establish tcp socket connections in client server manner. + * server is running (on port 7050) on one ESP, acts as AP, and other clients running on + * remaining ESPs acts as STAs. after connection establishment between server and clients + * there is a simple message transfer in every 2s. clients connect to server via it's host name + * (in this case 'esp_server') with help of DNS service running on server side. + * + * Note: default MSS for ESPAsyncTCP is 536 byte and defualt ACK timeout is 5s. +*/ + +#define SSID "ESP-TEST" +#define PASSWORD "123456789" + +#define SERVER_HOST_NAME "esp_server" + +#define TCP_PORT 7050 +#define DNS_PORT 53 + +#endif // CONFIG_H diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/examples/ClientServer/Server/Server.ino b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/examples/ClientServer/Server/Server.ino new file mode 100644 index 0000000..c8c9b7f --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/examples/ClientServer/Server/Server.ino @@ -0,0 +1,73 @@ +#include +#include +#include +#include + +#include "config.h" + +static DNSServer DNS; + +static std::vector clients; // a list to hold all clients + + /* clients events */ +static void handleError(void* arg, AsyncClient* client, int8_t error) { + Serial.printf("\n connection error %s from client %s \n", client->errorToString(error), client->remoteIP().toString().c_str()); +} + +static void handleData(void* arg, AsyncClient* client, void *data, size_t len) { + Serial.printf("\n data received from client %s \n", client->remoteIP().toString().c_str()); + Serial.write((uint8_t*)data, len); + + // reply to client + if (client->space() > 32 && client->canSend()) { + char reply[32]; + sprintf(reply, "this is from %s", SERVER_HOST_NAME); + client->add(reply, strlen(reply)); + client->send(); + } +} + +static void handleDisconnect(void* arg, AsyncClient* client) { + Serial.printf("\n client %s disconnected \n", client->remoteIP().toString().c_str()); +} + +static void handleTimeOut(void* arg, AsyncClient* client, uint32_t time) { + Serial.printf("\n client ACK timeout ip: %s \n", client->remoteIP().toString().c_str()); +} + + +/* server events */ +static void handleNewClient(void* arg, AsyncClient* client) { + Serial.printf("\n new client has been connected to server, ip: %s", client->remoteIP().toString().c_str()); + + // add to list + clients.push_back(client); + + // register events + client->onData(&handleData, NULL); + client->onError(&handleError, NULL); + client->onDisconnect(&handleDisconnect, NULL); + client->onTimeout(&handleTimeOut, NULL); +} + +void setup() { + Serial.begin(115200); + delay(20); + + // create access point + while (!WiFi.softAP(SSID, PASSWORD, 6, false, 15)) { + delay(500); + } + + // start dns server + if (!DNS.start(DNS_PORT, SERVER_HOST_NAME, WiFi.softAPIP())) + Serial.printf("\n failed to start dns service \n"); + + AsyncServer* server = new AsyncServer(TCP_PORT); // start listening on tcp port 7050 + server->onClient(&handleNewClient, server); + server->begin(); +} + +void loop() { + DNS.processNextRequest(); +} diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/examples/ClientServer/Server/config.h b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/examples/ClientServer/Server/config.h new file mode 100644 index 0000000..cf51e91 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/examples/ClientServer/Server/config.h @@ -0,0 +1,23 @@ +#ifndef CONFIG_H +#define CONFIG_H + +/* + * This example demonstrate how to use asynchronous client & server APIs + * in order to establish tcp socket connections in client server manner. + * server is running (on port 7050) on one ESP, acts as AP, and other clients running on + * remaining ESPs acts as STAs. after connection establishment between server and clients + * there is a simple message transfer in every 2s. clients connect to server via it's host name + * (in this case 'esp_server') with help of DNS service running on server side. + * + * Note: default MSS for ESPAsyncTCP is 536 byte and defualt ACK timeout is 5s. +*/ + +#define SSID "ESP-TEST" +#define PASSWORD "123456789" + +#define SERVER_HOST_NAME "esp_server" + +#define TCP_PORT 7050 +#define DNS_PORT 53 + +#endif // CONFIG_H diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/examples/SyncClient/SyncClient.ino b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/examples/SyncClient/SyncClient.ino new file mode 100644 index 0000000..6ecc525 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/examples/SyncClient/SyncClient.ino @@ -0,0 +1,54 @@ +#ifdef ESP8266 +#include +#include +#include +#else +#include +#endif +#include "ESPAsyncTCP.h" +#include "SyncClient.h" + +const char* ssid = "**********"; +const char* password = "************"; + +void setup(){ + Serial.begin(115200); + WiFi.begin(ssid, password); + if (WiFi.waitForConnectResult() != WL_CONNECTED) { + Serial.printf("WiFi Failed!\n"); + return; + } + Serial.printf("WiFi Connected!\n"); + Serial.println(WiFi.localIP()); +#ifdef ESP8266 + ArduinoOTA.begin(); +#endif + + SyncClient client; + if(!client.connect("www.google.com", 80)){ + Serial.println("Connect Failed"); + return; + } + client.setTimeout(2); + if(client.printf("GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection: close\r\n\r\n") > 0){ + while(client.connected() && client.available() == 0){ + delay(1); + } + while(client.available()){ + Serial.write(client.read()); + } + if(client.connected()){ + client.stop(); + } + } else { + client.stop(); + Serial.println("Send Failed"); + while(client.connected()) delay(0); + } +} + +void loop(){ +#ifdef ESP8266 + ArduinoOTA.handle(); +#endif +} diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/library.json b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/library.json new file mode 100644 index 0000000..deeb9ff --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/library.json @@ -0,0 +1,22 @@ +{ + "name":"ESPAsyncTCP", + "description":"Asynchronous TCP Library for ESP8266", + "keywords":"async,tcp", + "authors": + { + "name": "Hristo Gochkov", + "maintainer": true + }, + "repository": + { + "type": "git", + "url": "https://github.com/me-no-dev/ESPAsyncTCP.git" + }, + "version": "1.2.2", + "license": "LGPL-3.0", + "frameworks": "arduino", + "platforms": "espressif8266", + "build": { + "libCompatMode": 2 + } +} diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/library.properties b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/library.properties new file mode 100644 index 0000000..42b23fa --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/library.properties @@ -0,0 +1,9 @@ +name=ESP AsyncTCP +version=1.2.2 +author=Me-No-Dev +maintainer=Me-No-Dev +sentence=Async TCP Library for ESP8266 and ESP31B +paragraph=Async TCP Library for ESP8266 and ESP31B +category=Other +url=https://github.com/me-no-dev/ESPAsyncTCP +architectures=* diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/src/AsyncPrinter.cpp b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/src/AsyncPrinter.cpp new file mode 100644 index 0000000..8a63f20 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/src/AsyncPrinter.cpp @@ -0,0 +1,214 @@ +/* + Asynchronous TCP library for Espressif MCUs + + Copyright (c) 2016 Hristo Gochkov. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "AsyncPrinter.h" + +AsyncPrinter::AsyncPrinter() + : _client(NULL) + , _data_cb(NULL) + , _data_arg(NULL) + , _close_cb(NULL) + , _close_arg(NULL) + , _tx_buffer(NULL) + , _tx_buffer_size(TCP_MSS) + , next(NULL) +{} + +AsyncPrinter::AsyncPrinter(AsyncClient *client, size_t txBufLen) + : _client(client) + , _data_cb(NULL) + , _data_arg(NULL) + , _close_cb(NULL) + , _close_arg(NULL) + , _tx_buffer(NULL) + , _tx_buffer_size(txBufLen) + , next(NULL) +{ + _attachCallbacks(); + _tx_buffer = new (std::nothrow) cbuf(_tx_buffer_size); + if(_tx_buffer == NULL) { + panic(); //What should we do? + } +} + +AsyncPrinter::~AsyncPrinter(){ + _on_close(); +} + +void AsyncPrinter::onData(ApDataHandler cb, void *arg){ + _data_cb = cb; + _data_arg = arg; +} + +void AsyncPrinter::onClose(ApCloseHandler cb, void *arg){ + _close_cb = cb; + _close_arg = arg; +} + +int AsyncPrinter::connect(IPAddress ip, uint16_t port){ + if(_client != NULL && connected()) + return 0; + _client = new (std::nothrow) AsyncClient(); + if (_client == NULL) { + panic(); + } + + _client->onConnect([](void *obj, AsyncClient *c){ ((AsyncPrinter*)(obj))->_onConnect(c); }, this); + if(_client->connect(ip, port)){ + while(_client && _client->state() < 4) + delay(1); + return connected(); + } + return 0; +} + +int AsyncPrinter::connect(const char *host, uint16_t port){ + if(_client != NULL && connected()) + return 0; + _client = new (std::nothrow) AsyncClient(); + if (_client == NULL) { + panic(); + } + + _client->onConnect([](void *obj, AsyncClient *c){ ((AsyncPrinter*)(obj))->_onConnect(c); }, this); + if(_client->connect(host, port)){ + while(_client && _client->state() < 4) + delay(1); + return connected(); + } + return 0; +} + +void AsyncPrinter::_onConnect(AsyncClient *c){ + (void)c; + if(_tx_buffer != NULL){ + cbuf *b = _tx_buffer; + _tx_buffer = NULL; + delete b; + } + _tx_buffer = new (std::nothrow) cbuf(_tx_buffer_size); + if(_tx_buffer) { + panic(); + } + + _attachCallbacks(); +} + +AsyncPrinter::operator bool(){ return connected(); } + +AsyncPrinter & AsyncPrinter::operator=(const AsyncPrinter &other){ + if(_client != NULL){ + _client->close(true); + _client = NULL; + } + _tx_buffer_size = other._tx_buffer_size; + if(_tx_buffer != NULL){ + cbuf *b = _tx_buffer; + _tx_buffer = NULL; + delete b; + } + _tx_buffer = new (std::nothrow) cbuf(other._tx_buffer_size); + if(_tx_buffer == NULL) { + panic(); + } + + _client = other._client; + _attachCallbacks(); + return *this; +} + +size_t AsyncPrinter::write(uint8_t data){ + return write(&data, 1); +} + +size_t AsyncPrinter::write(const uint8_t *data, size_t len){ + if(_tx_buffer == NULL || !connected()) + return 0; + size_t toWrite = 0; + size_t toSend = len; + while(_tx_buffer->room() < toSend){ + toWrite = _tx_buffer->room(); + _tx_buffer->write((const char*)data, toWrite); + while(connected() && !_client->canSend()) + delay(0); + if(!connected()) + return 0; // or len - toSend; + _sendBuffer(); + toSend -= toWrite; + } + _tx_buffer->write((const char*)(data+(len - toSend)), toSend); + while(connected() && !_client->canSend()) delay(0); + if(!connected()) return 0; // or len - toSend; + _sendBuffer(); + return len; +} + +bool AsyncPrinter::connected(){ + return (_client != NULL && _client->connected()); +} + +void AsyncPrinter::close(){ + if(_client != NULL) + _client->close(true); +} + +size_t AsyncPrinter::_sendBuffer(){ + size_t available = _tx_buffer->available(); + if(!connected() || !_client->canSend() || available == 0) + return 0; + size_t sendable = _client->space(); + if(sendable < available) + available= sendable; + char *out = new (std::nothrow) char[available]; + if (out == NULL) { + panic(); // Connection should be aborted instead + } + + _tx_buffer->read(out, available); + size_t sent = _client->write(out, available); + delete out; + return sent; +} + +void AsyncPrinter::_onData(void *data, size_t len){ + if(_data_cb) + _data_cb(_data_arg, this, (uint8_t*)data, len); +} + +void AsyncPrinter::_on_close(){ + if(_client != NULL){ + _client = NULL; + } + if(_tx_buffer != NULL){ + cbuf *b = _tx_buffer; + _tx_buffer = NULL; + delete b; + } + if(_close_cb) + _close_cb(_close_arg, this); +} + +void AsyncPrinter::_attachCallbacks(){ + _client->onPoll([](void *obj, AsyncClient* c){ (void)c; ((AsyncPrinter*)(obj))->_sendBuffer(); }, this); + _client->onAck([](void *obj, AsyncClient* c, size_t len, uint32_t time){ (void)c; (void)len; (void)time; ((AsyncPrinter*)(obj))->_sendBuffer(); }, this); + _client->onDisconnect([](void *obj, AsyncClient* c){ ((AsyncPrinter*)(obj))->_on_close(); delete c; }, this); + _client->onData([](void *obj, AsyncClient* c, void *data, size_t len){ (void)c; ((AsyncPrinter*)(obj))->_onData(data, len); }, this); +} diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/src/AsyncPrinter.h b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/src/AsyncPrinter.h new file mode 100644 index 0000000..c3ebe3a --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/src/AsyncPrinter.h @@ -0,0 +1,73 @@ +/* + Asynchronous TCP library for Espressif MCUs + + Copyright (c) 2016 Hristo Gochkov. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef ASYNCPRINTER_H_ +#define ASYNCPRINTER_H_ + +#include "Arduino.h" +#include "ESPAsyncTCP.h" +#include "cbuf.h" + +class AsyncPrinter; + +typedef std::function ApDataHandler; +typedef std::function ApCloseHandler; + +class AsyncPrinter: public Print { + private: + AsyncClient *_client; + ApDataHandler _data_cb; + void *_data_arg; + ApCloseHandler _close_cb; + void *_close_arg; + cbuf *_tx_buffer; + size_t _tx_buffer_size; + + void _onConnect(AsyncClient *c); + public: + AsyncPrinter *next; + + AsyncPrinter(); + AsyncPrinter(AsyncClient *client, size_t txBufLen = TCP_MSS); + virtual ~AsyncPrinter(); + + int connect(IPAddress ip, uint16_t port); + int connect(const char *host, uint16_t port); + + void onData(ApDataHandler cb, void *arg); + void onClose(ApCloseHandler cb, void *arg); + + operator bool(); + AsyncPrinter & operator=(const AsyncPrinter &other); + + size_t write(uint8_t data); + size_t write(const uint8_t *data, size_t len); + + bool connected(); + void close(); + + size_t _sendBuffer(); + void _onData(void *data, size_t len); + void _on_close(); + void _attachCallbacks(); +}; + +#endif /* ASYNCPRINTER_H_ */ diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/src/DebugPrintMacros.h b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/src/DebugPrintMacros.h new file mode 100644 index 0000000..29accaf --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/src/DebugPrintMacros.h @@ -0,0 +1,96 @@ +#ifndef _DEBUG_PRINT_MACROS_H +#define _DEBUG_PRINT_MACROS_H +// Some customizable print macros to suite the debug needs de jour. + +// Debug macros +// #include +// https://stackoverflow.com/questions/8487986/file-macro-shows-full-path +// This value is resolved at compile time. +#define _FILENAME_ strrchr("/" __FILE__, '/') + +// #define DEBUG_ESP_ASYNC_TCP 1 +// #define DEBUG_ESP_TCP_SSL 1 +// #define DEBUG_ESP_PORT Serial + +#if defined(DEBUG_ESP_PORT) && !defined(DEBUG_TIME_STAMP_FMT) +#define DEBUG_TIME_STAMP_FMT "%06u.%03u " +struct _DEBUG_TIME_STAMP { + unsigned dec; + unsigned whole; +}; +inline struct _DEBUG_TIME_STAMP debugTimeStamp(void) { + struct _DEBUG_TIME_STAMP st; + unsigned now = millis() % 1000000000; + st.dec = now % 1000; + st.whole = now / 1000; + return st; +} +#endif + +#if defined(DEBUG_ESP_PORT) && !defined(DEBUG_GENERIC) + #define DEBUG_GENERIC( module, format, ... ) \ + do { \ + struct _DEBUG_TIME_STAMP st = debugTimeStamp(); \ + DEBUG_ESP_PORT.printf( DEBUG_TIME_STAMP_FMT module " " format, st.whole, st.dec, ##__VA_ARGS__ ); \ + } while(false) +#endif +#if defined(DEBUG_ESP_PORT) && !defined(DEBUG_GENERIC_P) + #define DEBUG_GENERIC_P( module, format, ... ) \ + do { \ + struct _DEBUG_TIME_STAMP st = debugTimeStamp(); \ + DEBUG_ESP_PORT.printf_P(PSTR( DEBUG_TIME_STAMP_FMT module " " format ), st.whole, st.dec, ##__VA_ARGS__ ); \ + } while(false) +#endif + +#if defined(DEBUG_GENERIC) && !defined(ASSERT_GENERIC) +#define ASSERT_GENERIC( a, module ) \ + do { \ + if ( !(a) ) { \ + DEBUG_GENERIC( module, "%s:%s:%u: ASSERT("#a") failed!\n", __FILE__, __func__, __LINE__); \ + DEBUG_ESP_PORT.flush(); \ + } \ + } while(false) +#endif +#if defined(DEBUG_GENERIC_P) && !defined(ASSERT_GENERIC_P) +#define ASSERT_GENERIC_P( a, module ) \ + do { \ + if ( !(a) ) { \ + DEBUG_GENERIC_P( module, "%s:%s:%u: ASSERT("#a") failed!\n", __FILE__, __func__, __LINE__); \ + DEBUG_ESP_PORT.flush(); \ + } \ + } while(false) +#endif + +#ifndef DEBUG_GENERIC +#define DEBUG_GENERIC(...) do { (void)0;} while(false) +#endif + +#ifndef DEBUG_GENERIC_P +#define DEBUG_GENERIC_P(...) do { (void)0;} while(false) +#endif + +#ifndef ASSERT_GENERIC +#define ASSERT_GENERIC(...) do { (void)0;} while(false) +#endif + +#ifndef ASSERT_GENERIC_P +#define ASSERT_GENERIC_P(...) do { (void)0;} while(false) +#endif + +#ifndef DEBUG_ESP_PRINTF +#define DEBUG_ESP_PRINTF( format, ...) DEBUG_GENERIC_P("[%s]", format, &_FILENAME_[1], ##__VA_ARGS__) +#endif + +#if defined(DEBUG_ESP_ASYNC_TCP) && !defined(ASYNC_TCP_DEBUG) +#define ASYNC_TCP_DEBUG( format, ...) DEBUG_GENERIC_P("[ASYNC_TCP]", format, ##__VA_ARGS__) +#endif + +#ifndef ASYNC_TCP_ASSERT +#define ASYNC_TCP_ASSERT( a ) ASSERT_GENERIC_P( (a), "[ASYNC_TCP]") +#endif + +#if defined(DEBUG_ESP_TCP_SSL) && !defined(TCP_SSL_DEBUG) +#define TCP_SSL_DEBUG( format, ...) DEBUG_GENERIC_P("[TCP_SSL]", format, ##__VA_ARGS__) +#endif + +#endif //_DEBUG_PRINT_MACROS_H diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/src/ESPAsyncTCP.cpp b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/src/ESPAsyncTCP.cpp new file mode 100644 index 0000000..7a9fdc7 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/src/ESPAsyncTCP.cpp @@ -0,0 +1,1394 @@ +/* + Asynchronous TCP library for Espressif MCUs + + Copyright (c) 2016 Hristo Gochkov. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ +/* +Changes for July 2019 + +The operator "new ..." was changed to "new (std::nothrow) ...", which will +return NULL when the heap is out of memory. Without the change "soft WDT" +was the result, starting with Arduino ESP8266 Core 2.5.0. (Note, RE:"soft +WDT" - the error reporting may improve with core 2.6.) With proir core +versions the library appears to work fine. +ref: https://github.com/esp8266/Arduino/issues/6269#issue-464978944 + +To support newer lwIP versions and buffer models. All references to 1460 +were replaced with TCP_MSS. If TCP_MSS is not defined (exp. 1.4v lwIP) +1460 is assumed. + +The ESPAsyncTCP library should build for Arduino ESP8266 Core releases: +2.3.0, 2.4.1, 2.4.2, 2.5.1, 2.5.2. It may still build with core versions +2.4.0 and 2.5.0. I did not do any regression testing with these, since +they had too many issues and were quickly superseded. + +lwIP tcp_err() callback often resulted in crashes. The problem was a +tcp_err() would come in, while processing a send or receive in the +forground. The tcp_err() callback would be passed down to a client's +registered disconnect CB. A common problem with SyncClient and other +modules as well as some client code was: the freeing of ESPAsyncTCP +AsyncClient objects via disconnect CB handlers while the library was +waiting for an operstion to finished. Attempts to access bad pointers +followed. For SyncClient this commonly occured during a call to delay(). +On return to SyncClient _client was invalid. Also the problem described by +issue #94 also surfaced + +Use of tcp_abort() required some very special handling and was very +challenging to make work without changing client API. ERR_ABRT can only be +used once on a return to lwIP for a given connection and since the +AsyncClient structure was sometimes deleted before returning to lwIP, the +state tracking became tricky. While ugly, a global variable for this +seemed to work; however, I abanded it when I saw a possible +reentrancy/concurrency issue. After several approaches I settled the +problem by creating "class ACErrorTracker" to manage the issue. + + +Additional Async Client considerations: + +The client sketch must always test if the connection is still up at loop() +entry and after the return of any function call, that may have done a +delay() or yield() or any ESPAsyncTCP library family call. For example, +the connection could be lost during a call to _client->write(...). Client +sketches that delete _client as part of their onDisconnect() handler must +be very careful as _client will become invalid after calls to delay(), +yield(), etc. + + + */ +#include "Arduino.h" + +#include "ESPAsyncTCP.h" +extern "C"{ + #include "lwip/opt.h" + #include "lwip/tcp.h" + #include "lwip/inet.h" + #include "lwip/dns.h" + #include "lwip/init.h" +} +#include + +/* + Async Client Error Return Tracker +*/ +// Assumption: callbacks are never called with err == ERR_ABRT; however, +// they may return ERR_ABRT. + +ACErrorTracker::ACErrorTracker(AsyncClient *c): + _client(c) + , _close_error(ERR_OK) + , _errored(EE_OK) +#ifdef DEBUG_MORE + , _error_event_cb(NULL) + , _error_event_cb_arg(NULL) +#endif +{} + +#ifdef DEBUG_MORE +/** + * This is not necessary, but a start at gathering some statistics on + * errored out connections. Used from AsyncServer. + */ +void ACErrorTracker::onErrorEvent(AsNotifyHandler cb, void *arg) { + _error_event_cb = cb; + _error_event_cb_arg = arg; +} +#endif + +void ACErrorTracker::setCloseError(err_t e) { + if (e != ERR_OK) + ASYNC_TCP_DEBUG("setCloseError() to: %s(%ld)\n", _client->errorToString(e), e); + if(_errored == EE_OK) + _close_error = e; +} +/** + * Called mainly by callback routines, called when err is not ERR_OK. + * This prevents the possiblity of aborting an already errored out + * connection. + */ +void ACErrorTracker::setErrored(size_t errorEvent){ + if(EE_OK == _errored) + _errored = errorEvent; +#ifdef DEBUG_MORE + if (_error_event_cb) + _error_event_cb(_error_event_cb_arg, errorEvent); +#endif +} +/** + * Used by callback functions only. Used for proper ERR_ABRT return value + * reporting. ERR_ABRT is only reported/returned once; thereafter ERR_OK + * is always returned. + */ +err_t ACErrorTracker::getCallbackCloseError(void){ + if (EE_OK != _errored) + return ERR_OK; + if (ERR_ABRT == _close_error) + setErrored(EE_ABORTED); + return _close_error; +} + +/* + Async TCP Client +*/ +#if DEBUG_ESP_ASYNC_TCP +static size_t _connectionCount=0; +#endif + +#if ASYNC_TCP_SSL_ENABLED +AsyncClient::AsyncClient(tcp_pcb* pcb, SSL_CTX * ssl_ctx): +#else +AsyncClient::AsyncClient(tcp_pcb* pcb): +#endif + _connect_cb(0) + , _connect_cb_arg(0) + , _discard_cb(0) + , _discard_cb_arg(0) + , _sent_cb(0) + , _sent_cb_arg(0) + , _error_cb(0) + , _error_cb_arg(0) + , _recv_cb(0) + , _recv_cb_arg(0) + , _pb_cb(0) + , _pb_cb_arg(0) + , _timeout_cb(0) + , _timeout_cb_arg(0) + , _poll_cb(0) + , _poll_cb_arg(0) + , _pcb_busy(false) +#if ASYNC_TCP_SSL_ENABLED + , _pcb_secure(false) + , _handshake_done(true) +#endif + , _pcb_sent_at(0) + , _close_pcb(false) + , _ack_pcb(true) + , _tx_unacked_len(0) + , _tx_acked_len(0) + , _tx_unsent_len(0) + , _rx_ack_len(0) + , _rx_last_packet(0) + , _rx_since_timeout(0) + , _ack_timeout(ASYNC_MAX_ACK_TIME) + , _connect_port(0) + , _recv_pbuf_flags(0) + , _errorTracker(NULL) + , prev(NULL) + , next(NULL) +{ + _pcb = pcb; + if(_pcb){ + _rx_last_packet = millis(); + tcp_setprio(_pcb, TCP_PRIO_MIN); + tcp_arg(_pcb, this); + tcp_recv(_pcb, &_s_recv); + tcp_sent(_pcb, &_s_sent); + tcp_err(_pcb, &_s_error); + tcp_poll(_pcb, &_s_poll, 1); +#if ASYNC_TCP_SSL_ENABLED + if(ssl_ctx){ + if(tcp_ssl_new_server(_pcb, ssl_ctx) < 0){ + _close(); + return; + } + tcp_ssl_arg(_pcb, this); + tcp_ssl_data(_pcb, &_s_data); + tcp_ssl_handshake(_pcb, &_s_handshake); + tcp_ssl_err(_pcb, &_s_ssl_error); + + _pcb_secure = true; + _handshake_done = false; + } +#endif + } + + _errorTracker = std::make_shared(this); +#if DEBUG_ESP_ASYNC_TCP + _errorTracker->setConnectionId(++_connectionCount); +#endif +} + +AsyncClient::~AsyncClient(){ + if(_pcb) + _close(); + + _errorTracker->clearClient(); +} + +inline void clearTcpCallbacks(tcp_pcb* pcb){ + tcp_arg(pcb, NULL); + tcp_sent(pcb, NULL); + tcp_recv(pcb, NULL); + tcp_err(pcb, NULL); + tcp_poll(pcb, NULL, 0); +} + +#if ASYNC_TCP_SSL_ENABLED +bool AsyncClient::connect(IPAddress ip, uint16_t port, bool secure){ +#else +bool AsyncClient::connect(IPAddress ip, uint16_t port){ +#endif + if (_pcb) //already connected + return false; + ip_addr_t addr; + addr.addr = ip; +#if LWIP_VERSION_MAJOR == 1 + netif* interface = ip_route(&addr); + if (!interface){ //no route to host + return false; + } +#endif + tcp_pcb* pcb = tcp_new(); + if (!pcb){ //could not allocate pcb + return false; + } + + tcp_setprio(pcb, TCP_PRIO_MIN); +#if ASYNC_TCP_SSL_ENABLED + _pcb_secure = secure; + _handshake_done = !secure; +#endif + tcp_arg(pcb, this); + tcp_err(pcb, &_s_error); + size_t err = tcp_connect(pcb, &addr, port,(tcp_connected_fn)&_s_connected); + return (ERR_OK == err); +} + +#if ASYNC_TCP_SSL_ENABLED +bool AsyncClient::connect(const char* host, uint16_t port, bool secure){ +#else +bool AsyncClient::connect(const char* host, uint16_t port){ +#endif + ip_addr_t addr; + err_t err = dns_gethostbyname(host, &addr, (dns_found_callback)&_s_dns_found, this); + if(err == ERR_OK) { +#if ASYNC_TCP_SSL_ENABLED + return connect(IPAddress(addr.addr), port, secure); +#else + return connect(IPAddress(addr.addr), port); +#endif + } else if(err == ERR_INPROGRESS) { +#if ASYNC_TCP_SSL_ENABLED + _pcb_secure = secure; + _handshake_done = !secure; +#endif + _connect_port = port; + return true; + } + return false; +} + +AsyncClient& AsyncClient::operator=(const AsyncClient& other){ + if (_pcb) { + ASYNC_TCP_DEBUG("operator=[%u]: Abandoned _pcb(0x%" PRIXPTR ") forced close.\n", getConnectionId(), uintptr_t(_pcb)); + _close(); + } + _errorTracker = other._errorTracker; + + // I am confused when "other._pcb" falls out of scope the destructor will + // close it? TODO: Look to see where this is used and how it might work. + _pcb = other._pcb; + if (_pcb) { + _rx_last_packet = millis(); + tcp_setprio(_pcb, TCP_PRIO_MIN); + tcp_arg(_pcb, this); + tcp_recv(_pcb, &_s_recv); + tcp_sent(_pcb, &_s_sent); + tcp_err(_pcb, &_s_error); + tcp_poll(_pcb, &_s_poll, 1); +#if ASYNC_TCP_SSL_ENABLED + if(tcp_ssl_has(_pcb)){ + _pcb_secure = true; + _handshake_done = false; + tcp_ssl_arg(_pcb, this); + tcp_ssl_data(_pcb, &_s_data); + tcp_ssl_handshake(_pcb, &_s_handshake); + tcp_ssl_err(_pcb, &_s_ssl_error); + } else { + _pcb_secure = false; + _handshake_done = true; + } +#endif + } + return *this; +} + +bool AsyncClient::operator==(const AsyncClient &other) { + return (_pcb != NULL && other._pcb != NULL && (_pcb->remote_ip.addr == other._pcb->remote_ip.addr) && (_pcb->remote_port == other._pcb->remote_port)); +} + +void AsyncClient::abort(){ + // Notes: + // 1) _pcb is set to NULL, so we cannot call tcp_abort() more than once. + // 2) setCloseError(ERR_ABRT) is only done here! + // 3) Using this abort() function guarantees only one tcp_abort() call is + // made and only one CB returns with ERR_ABORT. + // 4) After abort() is called from _close(), no callbacks with an err + // parameter will be called. eg. _recv(), _error(), _connected(). + // _close() will reset there CB handlers before calling. + // 5) A callback to _error(), will set _pcb to NULL, thus avoiding the + // of a 2nd call to tcp_abort(). + // 6) Callbacks to _recv() or _connected() with err set, will result in _pcb + // set to NULL. Thus, preventing possible calls later to tcp_abort(). + if(_pcb) { + tcp_abort(_pcb); + _pcb = NULL; + setCloseError(ERR_ABRT); + } + return; +} + +void AsyncClient::close(bool now){ + if(_pcb) + tcp_recved(_pcb, _rx_ack_len); + if(now) + _close(); + else + _close_pcb = true; +} + +void AsyncClient::stop() { + close(false); +} + +bool AsyncClient::free(){ + if(!_pcb) + return true; + if(_pcb->state == 0 || _pcb->state > 4) + return true; + return false; +} + +size_t AsyncClient::write(const char* data) { + if(data == NULL) + return 0; + return write(data, strlen(data)); +} + +size_t AsyncClient::write(const char* data, size_t size, uint8_t apiflags) { + size_t will_send = add(data, size, apiflags); + + if(!will_send || !send()) + return 0; + return will_send; +} + +size_t AsyncClient::add(const char* data, size_t size, uint8_t apiflags) { + if(!_pcb || size == 0 || data == NULL) + return 0; + size_t room = space(); + if(!room) + return 0; +#if ASYNC_TCP_SSL_ENABLED + if(_pcb_secure){ + int sent = tcp_ssl_write(_pcb, (uint8_t*)data, size); + if(sent >= 0){ + _tx_unacked_len += sent; + return sent; + } + _close(); + return 0; + } +#endif + size_t will_send = (room < size) ? room : size; + err_t err = tcp_write(_pcb, data, will_send, apiflags); + if(err != ERR_OK) { + ASYNC_TCP_DEBUG("_add[%u]: tcp_write() returned err: %s(%ld)\n", getConnectionId(), errorToString(err), err); + return 0; + } + _tx_unsent_len += will_send; + return will_send; +} + +bool AsyncClient::send(){ +#if ASYNC_TCP_SSL_ENABLED + if(_pcb_secure) + return true; +#endif + err_t err = tcp_output(_pcb); + if(err == ERR_OK){ + _pcb_busy = true; + _pcb_sent_at = millis(); + _tx_unacked_len += _tx_unsent_len; + _tx_unsent_len = 0; + return true; + } + + ASYNC_TCP_DEBUG("send[%u]: tcp_output() returned err: %s(%ld)", getConnectionId(), errorToString(err), err); + _tx_unsent_len = 0; + return false; +} + +size_t AsyncClient::ack(size_t len){ + if(len > _rx_ack_len) + len = _rx_ack_len; + if(len) + tcp_recved(_pcb, len); + _rx_ack_len -= len; + return len; +} + +// Private Callbacks + +void AsyncClient::_connected(std::shared_ptr& errorTracker, void* pcb, err_t err){ + //(void)err; // LWIP v1.4 appears to always call with ERR_OK + // Documentation for 2.1.0 also says: + // "err - An unused error code, always ERR_OK currently ;-)" + // https://www.nongnu.org/lwip/2_1_x/tcp_8h.html#a939867106bd492caf2d85852fb7f6ae8 + // Based on that wording and emoji lets just handle it now. + // After all, the API does allow for an err != ERR_OK. + if(NULL == pcb || ERR_OK != err) { + ASYNC_TCP_DEBUG("_connected[%u]:%s err: %s(%ld)\n", errorTracker->getConnectionId(), ((NULL == pcb) ? " NULL == pcb!," : ""), errorToString(err), err); + errorTracker->setCloseError(err); + errorTracker->setErrored(EE_CONNECTED_CB); + _pcb = reinterpret_cast(pcb); + if (_pcb) + clearTcpCallbacks(_pcb); + _pcb = NULL; + _error(err); + return; + } + + _pcb = reinterpret_cast(pcb); + if(_pcb){ + _pcb_busy = false; + _rx_last_packet = millis(); + tcp_setprio(_pcb, TCP_PRIO_MIN); + tcp_recv(_pcb, &_s_recv); + tcp_sent(_pcb, &_s_sent); + tcp_poll(_pcb, &_s_poll, 1); +#if ASYNC_TCP_SSL_ENABLED + if(_pcb_secure){ + if(tcp_ssl_new_client(_pcb) < 0){ + _close(); + return; + } + tcp_ssl_arg(_pcb, this); + tcp_ssl_data(_pcb, &_s_data); + tcp_ssl_handshake(_pcb, &_s_handshake); + tcp_ssl_err(_pcb, &_s_ssl_error); + } + } + if(!_pcb_secure && _connect_cb) +#else + } + if(_connect_cb) +#endif + _connect_cb(_connect_cb_arg, this); + return; +} + +void AsyncClient::_close(){ + if(_pcb) { +#if ASYNC_TCP_SSL_ENABLED + if(_pcb_secure){ + tcp_ssl_free(_pcb); + } +#endif + clearTcpCallbacks(_pcb); + err_t err = tcp_close(_pcb); + if(ERR_OK == err) { + setCloseError(err); + } else { + ASYNC_TCP_DEBUG("_close[%u]: abort() called for AsyncClient 0x%" PRIXPTR "\n", getConnectionId(), uintptr_t(this)); + abort(); + } + _pcb = NULL; + if(_discard_cb) + _discard_cb(_discard_cb_arg, this); + } + return; +} + +void AsyncClient::_error(err_t err) { + ASYNC_TCP_DEBUG("_error[%u]:%s err: %s(%ld)\n", getConnectionId(), ((NULL == _pcb) ? " NULL == _pcb!," : ""), errorToString(err), err); + if(_pcb){ +#if ASYNC_TCP_SSL_ENABLED + if(_pcb_secure){ + tcp_ssl_free(_pcb); + } +#endif + // At this callback _pcb is possible already freed. Thus, no calls are + // made to set to NULL other callbacks. + _pcb = NULL; + } + if(_error_cb) + _error_cb(_error_cb_arg, this, err); + if(_discard_cb) + _discard_cb(_discard_cb_arg, this); +} + +#if ASYNC_TCP_SSL_ENABLED +void AsyncClient::_ssl_error(int8_t err){ + if(_error_cb) + _error_cb(_error_cb_arg, this, err+64); +} +#endif + +void AsyncClient::_sent(std::shared_ptr& errorTracker, tcp_pcb* pcb, uint16_t len) { + (void)pcb; +#if ASYNC_TCP_SSL_ENABLED + if (_pcb_secure && !_handshake_done) + return; +#endif + _rx_last_packet = millis(); + _tx_unacked_len -= len; + _tx_acked_len += len; + ASYNC_TCP_DEBUG("_sent[%u]: %4u, unacked=%4u, acked=%4u, space=%4u\n", errorTracker->getConnectionId(), len, _tx_unacked_len, _tx_acked_len, space()); + if(_tx_unacked_len == 0){ + _pcb_busy = false; + errorTracker->setCloseError(ERR_OK); + if(_sent_cb) { + _sent_cb(_sent_cb_arg, this, _tx_acked_len, (millis() - _pcb_sent_at)); + if(!errorTracker->hasClient()) + return; + } + _tx_acked_len = 0; + } + return; +} + +void AsyncClient::_recv(std::shared_ptr& errorTracker, tcp_pcb* pcb, pbuf* pb, err_t err) { + // While lwIP v1.4 appears to always call with ERR_OK, 2.x lwIP may present + // a non-ERR_OK value. + // https://www.nongnu.org/lwip/2_1_x/tcp_8h.html#a780cfac08b02c66948ab94ea974202e8 + if(NULL == pcb || ERR_OK != err){ + ASYNC_TCP_DEBUG("_recv[%u]:%s err: %s(%ld)\n", errorTracker->getConnectionId(), ((NULL == pcb) ? " NULL == pcb!," : ""), errorToString(err), err); + ASYNC_TCP_ASSERT(ERR_ABRT != err); + errorTracker->setCloseError(err); + errorTracker->setErrored(EE_RECV_CB); + _pcb = pcb; + if(_pcb) + clearTcpCallbacks(_pcb); + _pcb = NULL; + // I think we are safe from being called from an interrupt context. + // Best Hint that calling _error() is safe: + // https://www.nongnu.org/lwip/2_1_x/group__lwip__nosys.html + // "Feed incoming packets to netif->input(pbuf, netif) function from + // mainloop, not from interrupt context. You can allocate a Packet buffers + // (PBUF) in interrupt context and put them into a queue which is processed + // from mainloop." + // And the description of "Mainloop Mode" option 2: + // https://www.nongnu.org/lwip/2_1_x/pitfalls.html + // "2) Run lwIP in a mainloop. ... lwIP is ONLY called from mainloop + // callstacks here. The ethernet IRQ has to put received telegrams into a + // queue which is polled in the mainloop. Ensure lwIP is NEVER called from + // an interrupt, ...!" + // Based on these comments I am thinking tcp_recv_fn() is called + // from somebody's mainloop(), which could only have been reached from a + // delay like function or the Arduino sketch loop() function has returned. + // What I don't want is for the client sketch to delete the AsyncClient + // object via _error() while it is in the middle of using it. However, + // the client sketch must always test that the connection is still up + // at loop() entry and after the return of any function call, that may + // have done a delay() or yield(). + _error(err); + return; + } + + if(pb == NULL){ + ASYNC_TCP_DEBUG("_recv[%u]: pb == NULL! Closing... %ld\n", errorTracker->getConnectionId(), err); + _close(); + return; + } + _rx_last_packet = millis(); + errorTracker->setCloseError(ERR_OK); +#if ASYNC_TCP_SSL_ENABLED + if(_pcb_secure){ + ASYNC_TCP_DEBUG("_recv[%u]: %d\n", getConnectionId(), pb->tot_len); + int read_bytes = tcp_ssl_read(pcb, pb); + if(read_bytes < 0){ + if (read_bytes != SSL_CLOSE_NOTIFY) { + ASYNC_TCP_DEBUG("_recv[%u] err: %d\n", getConnectionId(), read_bytes); + _close(); + } + } + return; + } +#endif + while(pb != NULL){ + // IF this callback function returns ERR_OK or ERR_ABRT + // then it is assummed we freed the pbufs. + // https://www.nongnu.org/lwip/2_1_x/group__tcp__raw.html#ga8afd0b316a87a5eeff4726dc95006ed0 + if(!errorTracker->hasClient()){ + while(pb != NULL){ + pbuf *b = pb; + pb = b->next; + b->next = NULL; + pbuf_free(b); + } + return; + } + //we should not ack before we assimilate the data + _ack_pcb = true; + pbuf *b = pb; + pb = b->next; + b->next = NULL; + ASYNC_TCP_DEBUG("_recv[%u]: %d%s\n", errorTracker->getConnectionId(), b->len, (b->flags&PBUF_FLAG_PUSH)?", PBUF_FLAG_PUSH":""); + if(_pb_cb){ + _pb_cb(_pb_cb_arg, this, b); + } else { + if(_recv_cb){ + _recv_pbuf_flags = b->flags; + _recv_cb(_recv_cb_arg, this, b->payload, b->len); + } + if(errorTracker->hasClient()){ + if(!_ack_pcb) + _rx_ack_len += b->len; + else + tcp_recved(pcb, b->len); + } + pbuf_free(b); + } + } + return; +} + +void AsyncClient::_poll(std::shared_ptr& errorTracker, tcp_pcb* pcb){ + (void)pcb; + errorTracker->setCloseError(ERR_OK); + + // Close requested + if(_close_pcb){ + _close_pcb = false; + _close(); + return; + } + uint32_t now = millis(); + + // ACK Timeout + if(_pcb_busy && _ack_timeout && (now - _pcb_sent_at) >= _ack_timeout){ + _pcb_busy = false; + if(_timeout_cb) + _timeout_cb(_timeout_cb_arg, this, (now - _pcb_sent_at)); + return; + } + // RX Timeout + if(_rx_since_timeout && (now - _rx_last_packet) >= (_rx_since_timeout * 1000)){ + _close(); + return; + } +#if ASYNC_TCP_SSL_ENABLED + // SSL Handshake Timeout + if(_pcb_secure && !_handshake_done && (now - _rx_last_packet) >= 2000){ + _close(); + return; + } +#endif + // Everything is fine + if(_poll_cb) + _poll_cb(_poll_cb_arg, this); + return; +} + +#if LWIP_VERSION_MAJOR == 1 +void AsyncClient::_dns_found(struct ip_addr *ipaddr){ +#else +void AsyncClient::_dns_found(const ip_addr *ipaddr){ +#endif + if(ipaddr){ +#if ASYNC_TCP_SSL_ENABLED + connect(IPAddress(ipaddr->addr), _connect_port, _pcb_secure); +#else + connect(IPAddress(ipaddr->addr), _connect_port); +#endif + } else { + if(_error_cb) + _error_cb(_error_cb_arg, this, -55); + if(_discard_cb) + _discard_cb(_discard_cb_arg, this); + } +} + +// lwIP Callbacks +#if LWIP_VERSION_MAJOR == 1 +void AsyncClient::_s_dns_found(const char *name, ip_addr_t *ipaddr, void *arg){ +#else +void AsyncClient::_s_dns_found(const char *name, const ip_addr *ipaddr, void *arg){ +#endif + (void)name; + reinterpret_cast(arg)->_dns_found(ipaddr); +} + +err_t AsyncClient::_s_poll(void *arg, struct tcp_pcb *tpcb) { + AsyncClient *c = reinterpret_cast(arg); + std::shared_ptrerrorTracker = c->getACErrorTracker(); + c->_poll(errorTracker, tpcb); + return errorTracker->getCallbackCloseError(); +} + +err_t AsyncClient::_s_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *pb, err_t err) { + AsyncClient *c = reinterpret_cast(arg); + auto errorTracker = c->getACErrorTracker(); + c->_recv(errorTracker, tpcb, pb, err); + return errorTracker->getCallbackCloseError(); +} + +void AsyncClient::_s_error(void *arg, err_t err) { + AsyncClient *c = reinterpret_cast(arg); + auto errorTracker = c->getACErrorTracker(); + errorTracker->setCloseError(err); + errorTracker->setErrored(EE_ERROR_CB); + c->_error(err); +} + +err_t AsyncClient::_s_sent(void *arg, struct tcp_pcb *tpcb, uint16_t len) { + AsyncClient *c = reinterpret_cast(arg); + auto errorTracker = c->getACErrorTracker(); + c->_sent(errorTracker, tpcb, len); + return errorTracker->getCallbackCloseError(); +} + +err_t AsyncClient::_s_connected(void* arg, void* tpcb, err_t err){ + AsyncClient *c = reinterpret_cast(arg); + auto errorTracker = c->getACErrorTracker(); + c->_connected(errorTracker, tpcb, err); + return errorTracker->getCallbackCloseError(); +} + +#if ASYNC_TCP_SSL_ENABLED +void AsyncClient::_s_data(void *arg, struct tcp_pcb *tcp, uint8_t * data, size_t len){ + AsyncClient *c = reinterpret_cast(arg); + if(c->_recv_cb) + c->_recv_cb(c->_recv_cb_arg, c, data, len); +} + +void AsyncClient::_s_handshake(void *arg, struct tcp_pcb *tcp, SSL *ssl){ + AsyncClient *c = reinterpret_cast(arg); + c->_handshake_done = true; + if(c->_connect_cb) + c->_connect_cb(c->_connect_cb_arg, c); +} + +void AsyncClient::_s_ssl_error(void *arg, struct tcp_pcb *tcp, int8_t err){ + reinterpret_cast(arg)->_ssl_error(err); +} +#endif + +// Operators + +AsyncClient & AsyncClient::operator+=(const AsyncClient &other) { + if(next == NULL){ + next = (AsyncClient*)(&other); + next->prev = this; + } else { + AsyncClient *c = next; + while(c->next != NULL) c = c->next; + c->next =(AsyncClient*)(&other); + c->next->prev = c; + } + return *this; +} + +void AsyncClient::setRxTimeout(uint32_t timeout){ + _rx_since_timeout = timeout; +} + +uint32_t AsyncClient::getRxTimeout(){ + return _rx_since_timeout; +} + +uint32_t AsyncClient::getAckTimeout(){ + return _ack_timeout; +} + +void AsyncClient::setAckTimeout(uint32_t timeout){ + _ack_timeout = timeout; +} + +void AsyncClient::setNoDelay(bool nodelay){ + if(!_pcb) + return; + if(nodelay) + tcp_nagle_disable(_pcb); + else + tcp_nagle_enable(_pcb); +} + +bool AsyncClient::getNoDelay(){ + if(!_pcb) + return false; + return tcp_nagle_disabled(_pcb); +} + +uint16_t AsyncClient::getMss(){ + if(_pcb) + return tcp_mss(_pcb); + return 0; +} + +uint32_t AsyncClient::getRemoteAddress() { + if(!_pcb) + return 0; + return _pcb->remote_ip.addr; +} + +uint16_t AsyncClient::getRemotePort() { + if(!_pcb) + return 0; + return _pcb->remote_port; +} + +uint32_t AsyncClient::getLocalAddress() { + if(!_pcb) + return 0; + return _pcb->local_ip.addr; +} + +uint16_t AsyncClient::getLocalPort() { + if(!_pcb) + return 0; + return _pcb->local_port; +} + +IPAddress AsyncClient::remoteIP() { + return IPAddress(getRemoteAddress()); +} + +uint16_t AsyncClient::remotePort() { + return getRemotePort(); +} + +IPAddress AsyncClient::localIP() { + return IPAddress(getLocalAddress()); +} + +uint16_t AsyncClient::localPort() { + return getLocalPort(); +} + +#if ASYNC_TCP_SSL_ENABLED +SSL * AsyncClient::getSSL(){ + if(_pcb && _pcb_secure){ + return tcp_ssl_get_ssl(_pcb); + } + return NULL; +} +#endif + +uint8_t AsyncClient::state() { + if(!_pcb) + return 0; + return _pcb->state; +} + +bool AsyncClient::connected(){ + if (!_pcb) + return false; +#if ASYNC_TCP_SSL_ENABLED + return _pcb->state == 4 && _handshake_done; +#else + return _pcb->state == 4; +#endif +} + +bool AsyncClient::connecting(){ + if (!_pcb) + return false; + return _pcb->state > 0 && _pcb->state < 4; +} + +bool AsyncClient::disconnecting(){ + if (!_pcb) + return false; + return _pcb->state > 4 && _pcb->state < 10; +} + +bool AsyncClient::disconnected(){ + if (!_pcb) + return true; + return _pcb->state == 0 || _pcb->state == 10; +} + +bool AsyncClient::freeable(){ + if (!_pcb) + return true; + return _pcb->state == 0 || _pcb->state > 4; +} + +bool AsyncClient::canSend(){ + return !_pcb_busy && (space() > 0); +} + + +// Callback Setters + +void AsyncClient::onConnect(AcConnectHandler cb, void* arg){ + _connect_cb = cb; + _connect_cb_arg = arg; +} + +void AsyncClient::onDisconnect(AcConnectHandler cb, void* arg){ + _discard_cb = cb; + _discard_cb_arg = arg; +} + +void AsyncClient::onAck(AcAckHandler cb, void* arg){ + _sent_cb = cb; + _sent_cb_arg = arg; +} + +void AsyncClient::onError(AcErrorHandler cb, void* arg){ + _error_cb = cb; + _error_cb_arg = arg; +} + +void AsyncClient::onData(AcDataHandler cb, void* arg){ + _recv_cb = cb; + _recv_cb_arg = arg; +} + +void AsyncClient::onPacket(AcPacketHandler cb, void* arg){ + _pb_cb = cb; + _pb_cb_arg = arg; +} + +void AsyncClient::onTimeout(AcTimeoutHandler cb, void* arg){ + _timeout_cb = cb; + _timeout_cb_arg = arg; +} + +void AsyncClient::onPoll(AcConnectHandler cb, void* arg){ + _poll_cb = cb; + _poll_cb_arg = arg; +} + + +size_t AsyncClient::space(){ +#if ASYNC_TCP_SSL_ENABLED + if((_pcb != NULL) && (_pcb->state == 4) && _handshake_done){ + uint16_t s = tcp_sndbuf(_pcb); + if(_pcb_secure){ +#ifdef AXTLS_2_0_0_SNDBUF + return tcp_ssl_sndbuf(_pcb); +#else + if(s >= 128) //safe approach + return s - 128; + return 0; +#endif + } + return s; + } +#else // ASYNC_TCP_SSL_ENABLED + if((_pcb != NULL) && (_pcb->state == 4)){ + return tcp_sndbuf(_pcb); + } +#endif // ASYNC_TCP_SSL_ENABLED + return 0; +} + +void AsyncClient::ackPacket(struct pbuf * pb){ + if(!pb){ + return; + } + tcp_recved(_pcb, pb->len); + pbuf_free(pb); +} + +const char * AsyncClient::errorToString(err_t error) { + switch (error) { + case ERR_OK: return "No error, everything OK"; + case ERR_MEM: return "Out of memory error"; + case ERR_BUF: return "Buffer error"; + case ERR_TIMEOUT: return "Timeout"; + case ERR_RTE: return "Routing problem"; + case ERR_INPROGRESS: return "Operation in progress"; + case ERR_VAL: return "Illegal value"; + case ERR_WOULDBLOCK: return "Operation would block"; + case ERR_ABRT: return "Connection aborted"; + case ERR_RST: return "Connection reset"; + case ERR_CLSD: return "Connection closed"; + case ERR_CONN: return "Not connected"; + case ERR_ARG: return "Illegal argument"; + case ERR_USE: return "Address in use"; +#if defined(LWIP_VERSION_MAJOR) && (LWIP_VERSION_MAJOR > 1) + case ERR_ALREADY: return "Already connectioning"; +#endif + case ERR_IF: return "Low-level netif error"; + case ERR_ISCONN: return "Connection already established"; + case -55: return "DNS failed"; + default: return "Unknown error"; + } +} + +const char * AsyncClient::stateToString(){ + switch(state()){ + case 0: return "Closed"; + case 1: return "Listen"; + case 2: return "SYN Sent"; + case 3: return "SYN Received"; + case 4: return "Established"; + case 5: return "FIN Wait 1"; + case 6: return "FIN Wait 2"; + case 7: return "Close Wait"; + case 8: return "Closing"; + case 9: return "Last ACK"; + case 10: return "Time Wait"; + default: return "UNKNOWN"; + } +} + +/* + Async TCP Server +*/ +struct pending_pcb { + tcp_pcb* pcb; + pbuf *pb; + struct pending_pcb * next; +}; + +AsyncServer::AsyncServer(IPAddress addr, uint16_t port) + : _port(port) + , _addr(addr) + , _noDelay(false) + , _pcb(0) + , _connect_cb(0) + , _connect_cb_arg(0) +#if ASYNC_TCP_SSL_ENABLED + , _pending(NULL) + , _ssl_ctx(NULL) + , _file_cb(0) + , _file_cb_arg(0) +#endif +{ +#ifdef DEBUG_MORE + for (size_t i=0; inext; + if(p->pb){ + pbuf_free(p->pb); + } + free(p); + } + } + } +#endif +} + +void AsyncServer::setNoDelay(bool nodelay){ + _noDelay = nodelay; +} + +bool AsyncServer::getNoDelay(){ + return _noDelay; +} + +uint8_t AsyncServer::status(){ + if (!_pcb) + return 0; + return _pcb->state; +} + +err_t AsyncServer::_accept(tcp_pcb* pcb, err_t err){ + //http://savannah.nongnu.org/bugs/?43739 + if(NULL == pcb || ERR_OK != err){ + // https://www.nongnu.org/lwip/2_1_x/tcp_8h.html#a00517abce6856d6c82f0efebdafb734d + // An error code if there has been an error accepting. Only return ERR_ABRT + // if you have called tcp_abort from within the callback function! + // eg. 2.1.0 could call with error on failure to allocate pcb. + ASYNC_TCP_DEBUG("_accept:%s err: %ld\n", ((NULL == pcb) ? " NULL == pcb!," : ""), err); + ASYNC_TCP_ASSERT(ERR_ABRT != err); +#ifdef DEBUG_MORE + incEventCount(EE_ACCEPT_CB); +#endif + return ERR_OK; + } + + if(_connect_cb){ +#if ASYNC_TCP_SSL_ENABLED + if (_noDelay || _ssl_ctx) +#else + if (_noDelay) +#endif + tcp_nagle_disable(pcb); + else + tcp_nagle_enable(pcb); + +#if ASYNC_TCP_SSL_ENABLED + if(_ssl_ctx){ + if(tcp_ssl_has_client() || _pending){ + struct pending_pcb * new_item = (struct pending_pcb*)malloc(sizeof(struct pending_pcb)); + if(!new_item){ + ASYNC_TCP_DEBUG("### malloc new pending failed!\n"); + if(tcp_close(pcb) != ERR_OK){ + tcp_abort(pcb); + return ERR_ABRT; + } + return ERR_OK; + } + ASYNC_TCP_DEBUG("### put to wait: %d\n", _clients_waiting); + new_item->pcb = pcb; + new_item->pb = NULL; + new_item->next = NULL; + tcp_setprio(_pcb, TCP_PRIO_MIN); + tcp_arg(pcb, this); + tcp_poll(pcb, &_s_poll, 1); + tcp_recv(pcb, &_s_recv); + + if(_pending == NULL){ + _pending = new_item; + } else { + struct pending_pcb * p = _pending; + while(p->next != NULL) + p = p->next; + p->next = new_item; + } + } else { + AsyncClient *c = new (std::nothrow) AsyncClient(pcb, _ssl_ctx); + if(c){ + ASYNC_TCP_DEBUG("_accept[%u]: SSL connected\n", c->getConnectionId()); + c->onConnect([this](void * arg, AsyncClient *c){ + _connect_cb(_connect_cb_arg, c); + }, this); + } else { + ASYNC_TCP_DEBUG("_accept[_ssl_ctx]: new AsyncClient() failed, connection aborted!\n"); + if(tcp_close(pcb) != ERR_OK){ + tcp_abort(pcb); + return ERR_ABRT; + } + } + } + return ERR_OK; + } else { + AsyncClient *c = new (std::nothrow) AsyncClient(pcb, NULL); +#else + AsyncClient *c = new (std::nothrow) AsyncClient(pcb); +#endif + + if(c){ + auto errorTracker = c->getACErrorTracker(); +#ifdef DEBUG_MORE + errorTracker->onErrorEvent( + [](void *obj, size_t ee){ ((AsyncServer*)(obj))->incEventCount(ee); }, + this); +#endif + ASYNC_TCP_DEBUG("_accept[%u]: connected\n", errorTracker->getConnectionId()); + _connect_cb(_connect_cb_arg, c); + return errorTracker->getCallbackCloseError(); + } else { + ASYNC_TCP_DEBUG("_accept: new AsyncClient() failed, connection aborted!\n"); + if(tcp_close(pcb) != ERR_OK){ + tcp_abort(pcb); + return ERR_ABRT; + } + } +#if ASYNC_TCP_SSL_ENABLED + } +#endif + } + if(tcp_close(pcb) != ERR_OK){ + tcp_abort(pcb); + return ERR_ABRT; + } + return ERR_OK; +} + +err_t AsyncServer::_s_accept(void *arg, tcp_pcb* pcb, err_t err){ + return reinterpret_cast(arg)->_accept(pcb, err); +} + +#if ASYNC_TCP_SSL_ENABLED +err_t AsyncServer::_poll(tcp_pcb* pcb){ + if(!tcp_ssl_has_client() && _pending){ + struct pending_pcb * p = _pending; + if(p->pcb == pcb){ + _pending = _pending->next; + } else { + while(p->next && p->next->pcb != pcb) p = p->next; + if(!p->next) return 0; + struct pending_pcb * b = p->next; + p->next = b->next; + p = b; + } + ASYNC_TCP_DEBUG("### remove from wait: %d\n", _clients_waiting); + AsyncClient *c = new (std::nothrow) AsyncClient(pcb, _ssl_ctx); + if(c){ + c->onConnect([this](void * arg, AsyncClient *c){ + _connect_cb(_connect_cb_arg, c); + }, this); + if(p->pb) + c->_recv(pcb, p->pb, 0); + } + // Should there be error handling for when "new AsynClient" fails?? + free(p); + } + return ERR_OK; +} + +err_t AsyncServer::_recv(struct tcp_pcb *pcb, struct pbuf *pb, err_t err){ + if(!_pending) + return ERR_OK; + + struct pending_pcb * p; + + if(!pb){ + ASYNC_TCP_DEBUG("### close from wait: %d\n", _clients_waiting); + p = _pending; + if(p->pcb == pcb){ + _pending = _pending->next; + } else { + while(p->next && p->next->pcb != pcb) p = p->next; + if(!p->next) return 0; + struct pending_pcb * b = p->next; + p->next = b->next; + p = b; + } + if(p->pb){ + pbuf_free(p->pb); + } + free(p); + size_t err = tcp_close(pcb); + if (err != ERR_OK) { + tcp_abort(pcb); + return ERR_ABRT; + } + } else { + ASYNC_TCP_DEBUG("### wait _recv: %u %d\n", pb->tot_len, _clients_waiting); + p = _pending; + while(p && p->pcb != pcb) + p = p->next; + if(p){ + if(p->pb){ + pbuf_chain(p->pb, pb); + } else { + p->pb = pb; + } + } + } + return ERR_OK; +} + +int AsyncServer::_cert(const char *filename, uint8_t **buf){ + if(_file_cb){ + return _file_cb(_file_cb_arg, filename, buf); + } + *buf = 0; + return 0; +} + +int AsyncServer::_s_cert(void *arg, const char *filename, uint8_t **buf){ + return reinterpret_cast(arg)->_cert(filename, buf); +} + +err_t AsyncServer::_s_poll(void *arg, struct tcp_pcb *pcb){ + return reinterpret_cast(arg)->_poll(pcb); +} + +err_t AsyncServer::_s_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *pb, err_t err){ + return reinterpret_cast(arg)->_recv(pcb, pb, err); +} +#endif diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/src/ESPAsyncTCP.h b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/src/ESPAsyncTCP.h new file mode 100644 index 0000000..2d1f768 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/src/ESPAsyncTCP.h @@ -0,0 +1,327 @@ +/* + Asynchronous TCP library for Espressif MCUs + + Copyright (c) 2016 Hristo Gochkov. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef ASYNCTCP_H_ +#define ASYNCTCP_H_ + +#include +#include "IPAddress.h" +#include +#include + +extern "C" { + #include "lwip/init.h" + #include "lwip/err.h" + #include "lwip/pbuf.h" +}; + +class AsyncClient; +class AsyncServer; +class ACErrorTracker; + +#define ASYNC_MAX_ACK_TIME 5000 +#define ASYNC_WRITE_FLAG_COPY 0x01 //will allocate new buffer to hold the data while sending (else will hold reference to the data given) +#define ASYNC_WRITE_FLAG_MORE 0x02 //will not send PSH flag, meaning that there should be more data to be sent before the application should react. + +struct tcp_pcb; +struct ip_addr; +#if ASYNC_TCP_SSL_ENABLED +struct SSL_; +typedef struct SSL_ SSL; +struct SSL_CTX_; +typedef struct SSL_CTX_ SSL_CTX; +#endif + +typedef std::function AcConnectHandler; +typedef std::function AcAckHandler; +typedef std::function AcErrorHandler; +typedef std::function AcDataHandler; +typedef std::function AcPacketHandler; +typedef std::function AcTimeoutHandler; +typedef std::function AsNotifyHandler; + +enum error_events { + EE_OK = 0, + EE_ABORTED, // Callback or foreground aborted connections + EE_ERROR_CB, // Stack initiated aborts via error Callbacks. + EE_CONNECTED_CB, + EE_RECV_CB, + EE_ACCEPT_CB, + EE_MAX +}; +// DEBUG_MORE is for gathering more information on which CBs close events are +// occuring and count. +// #define DEBUG_MORE 1 +class ACErrorTracker { + private: + AsyncClient *_client; + err_t _close_error; + int _errored; +#if DEBUG_ESP_ASYNC_TCP + size_t _connectionId; +#endif +#ifdef DEBUG_MORE + AsNotifyHandler _error_event_cb; + void* _error_event_cb_arg; +#endif + + protected: + friend class AsyncClient; + friend class AsyncServer; +#ifdef DEBUG_MORE + void onErrorEvent(AsNotifyHandler cb, void *arg); +#endif +#if DEBUG_ESP_ASYNC_TCP + void setConnectionId(size_t id) { _connectionId=id;} + size_t getConnectionId(void) { return _connectionId;} +#endif + void setCloseError(err_t e); + void setErrored(size_t errorEvent); + err_t getCallbackCloseError(void); + void clearClient(void){ if (_client) _client = NULL;} + + public: + err_t getCloseError(void) const { return _close_error;} + bool hasClient(void) const { return (_client != NULL);} + ACErrorTracker(AsyncClient *c); + ~ACErrorTracker() {} +}; + +class AsyncClient { + protected: + friend class AsyncTCPbuffer; + friend class AsyncServer; + tcp_pcb* _pcb; + AcConnectHandler _connect_cb; + void* _connect_cb_arg; + AcConnectHandler _discard_cb; + void* _discard_cb_arg; + AcAckHandler _sent_cb; + void* _sent_cb_arg; + AcErrorHandler _error_cb; + void* _error_cb_arg; + AcDataHandler _recv_cb; + void* _recv_cb_arg; + AcPacketHandler _pb_cb; + void* _pb_cb_arg; + AcTimeoutHandler _timeout_cb; + void* _timeout_cb_arg; + AcConnectHandler _poll_cb; + void* _poll_cb_arg; + bool _pcb_busy; +#if ASYNC_TCP_SSL_ENABLED + bool _pcb_secure; + bool _handshake_done; +#endif + uint32_t _pcb_sent_at; + bool _close_pcb; + bool _ack_pcb; + uint32_t _tx_unacked_len; + uint32_t _tx_acked_len; + uint32_t _tx_unsent_len; + uint32_t _rx_ack_len; + uint32_t _rx_last_packet; + uint32_t _rx_since_timeout; + uint32_t _ack_timeout; + uint16_t _connect_port; + u8_t _recv_pbuf_flags; + std::shared_ptr _errorTracker; + + void _close(); + void _connected(std::shared_ptr& closeAbort, void* pcb, err_t err); + void _error(err_t err); +#if ASYNC_TCP_SSL_ENABLED + void _ssl_error(int8_t err); +#endif + void _poll(std::shared_ptr& closeAbort, tcp_pcb* pcb); + void _sent(std::shared_ptr& closeAbort, tcp_pcb* pcb, uint16_t len); +#if LWIP_VERSION_MAJOR == 1 + void _dns_found(struct ip_addr *ipaddr); +#else + void _dns_found(const ip_addr *ipaddr); +#endif + static err_t _s_poll(void *arg, struct tcp_pcb *tpcb); + static err_t _s_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *pb, err_t err); + static void _s_error(void *arg, err_t err); + static err_t _s_sent(void *arg, struct tcp_pcb *tpcb, uint16_t len); + static err_t _s_connected(void* arg, void* tpcb, err_t err); +#if LWIP_VERSION_MAJOR == 1 + static void _s_dns_found(const char *name, struct ip_addr *ipaddr, void *arg); +#else + static void _s_dns_found(const char *name, const ip_addr *ipaddr, void *arg); +#endif +#if ASYNC_TCP_SSL_ENABLED + static void _s_data(void *arg, struct tcp_pcb *tcp, uint8_t * data, size_t len); + static void _s_handshake(void *arg, struct tcp_pcb *tcp, SSL *ssl); + static void _s_ssl_error(void *arg, struct tcp_pcb *tcp, int8_t err); +#endif + std::shared_ptr getACErrorTracker(void) const { return _errorTracker; }; + void setCloseError(err_t e) const { _errorTracker->setCloseError(e);} + + public: + AsyncClient* prev; + AsyncClient* next; + +#if ASYNC_TCP_SSL_ENABLED + AsyncClient(tcp_pcb* pcb = 0, SSL_CTX * ssl_ctx = NULL); +#else + AsyncClient(tcp_pcb* pcb = 0); +#endif + ~AsyncClient(); + + AsyncClient & operator=(const AsyncClient &other); + AsyncClient & operator+=(const AsyncClient &other); + + bool operator==(const AsyncClient &other); + + bool operator!=(const AsyncClient &other) { + return !(*this == other); + } +#if ASYNC_TCP_SSL_ENABLED + bool connect(IPAddress ip, uint16_t port, bool secure=false); + bool connect(const char* host, uint16_t port, bool secure=false); +#else + bool connect(IPAddress ip, uint16_t port); + bool connect(const char* host, uint16_t port); +#endif + void close(bool now = false); + void stop(); + void abort(); + bool free(); + + bool canSend();//ack is not pending + size_t space(); + size_t add(const char* data, size_t size, uint8_t apiflags=0);//add for sending + bool send();//send all data added with the method above + size_t ack(size_t len); //ack data that you have not acked using the method below + void ackLater(){ _ack_pcb = false; } //will not ack the current packet. Call from onData + bool isRecvPush(){ return !!(_recv_pbuf_flags & PBUF_FLAG_PUSH); } +#if DEBUG_ESP_ASYNC_TCP + size_t getConnectionId(void) const { return _errorTracker->getConnectionId();} +#endif +#if ASYNC_TCP_SSL_ENABLED + SSL *getSSL(); +#endif + + size_t write(const char* data); + size_t write(const char* data, size_t size, uint8_t apiflags=0); //only when canSend() == true + + uint8_t state(); + bool connecting(); + bool connected(); + bool disconnecting(); + bool disconnected(); + bool freeable();//disconnected or disconnecting + + uint16_t getMss(); + uint32_t getRxTimeout(); + void setRxTimeout(uint32_t timeout);//no RX data timeout for the connection in seconds + uint32_t getAckTimeout(); + void setAckTimeout(uint32_t timeout);//no ACK timeout for the last sent packet in milliseconds + void setNoDelay(bool nodelay); + bool getNoDelay(); + uint32_t getRemoteAddress(); + uint16_t getRemotePort(); + uint32_t getLocalAddress(); + uint16_t getLocalPort(); + + IPAddress remoteIP(); + uint16_t remotePort(); + IPAddress localIP(); + uint16_t localPort(); + + void onConnect(AcConnectHandler cb, void* arg = 0); //on successful connect + void onDisconnect(AcConnectHandler cb, void* arg = 0); //disconnected + void onAck(AcAckHandler cb, void* arg = 0); //ack received + void onError(AcErrorHandler cb, void* arg = 0); //unsuccessful connect or error + void onData(AcDataHandler cb, void* arg = 0); //data received (called if onPacket is not used) + void onPacket(AcPacketHandler cb, void* arg = 0); //data received + void onTimeout(AcTimeoutHandler cb, void* arg = 0); //ack timeout + void onPoll(AcConnectHandler cb, void* arg = 0); //every 125ms when connected + void ackPacket(struct pbuf * pb); + + const char * errorToString(err_t error); + const char * stateToString(); + + void _recv(std::shared_ptr& closeAbort, tcp_pcb* pcb, pbuf* pb, err_t err); + err_t getCloseError(void) const { return _errorTracker->getCloseError();} +}; + +#if ASYNC_TCP_SSL_ENABLED +typedef std::function AcSSlFileHandler; +struct pending_pcb; +#endif + + +class AsyncServer { + protected: + uint16_t _port; + IPAddress _addr; + bool _noDelay; + tcp_pcb* _pcb; + AcConnectHandler _connect_cb; + void* _connect_cb_arg; +#if ASYNC_TCP_SSL_ENABLED + struct pending_pcb * _pending; + SSL_CTX * _ssl_ctx; + AcSSlFileHandler _file_cb; + void* _file_cb_arg; +#endif +#ifdef DEBUG_MORE + int _event_count[EE_MAX]; +#endif + + public: + + AsyncServer(IPAddress addr, uint16_t port); + AsyncServer(uint16_t port); + ~AsyncServer(); + void onClient(AcConnectHandler cb, void* arg); +#if ASYNC_TCP_SSL_ENABLED + void onSslFileRequest(AcSSlFileHandler cb, void* arg); + void beginSecure(const char *cert, const char *private_key_file, const char *password); +#endif + void begin(); + void end(); + void setNoDelay(bool nodelay); + bool getNoDelay(); + uint8_t status(); +#ifdef DEBUG_MORE + int getEventCount(size_t ee) const { return _event_count[ee];} +#endif + protected: + err_t _accept(tcp_pcb* newpcb, err_t err); + static err_t _s_accept(void *arg, tcp_pcb* newpcb, err_t err); +#ifdef DEBUG_MORE + int incEventCount(size_t ee) { return ++_event_count[ee];} +#endif +#if ASYNC_TCP_SSL_ENABLED + int _cert(const char *filename, uint8_t **buf); + err_t _poll(tcp_pcb* pcb); + err_t _recv(tcp_pcb *pcb, struct pbuf *pb, err_t err); + static int _s_cert(void *arg, const char *filename, uint8_t **buf); + static err_t _s_poll(void *arg, struct tcp_pcb *tpcb); + static err_t _s_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *pb, err_t err); +#endif +}; + + +#endif /* ASYNCTCP_H_ */ diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/src/ESPAsyncTCPbuffer.cpp b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/src/ESPAsyncTCPbuffer.cpp new file mode 100644 index 0000000..d2261da --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/src/ESPAsyncTCPbuffer.cpp @@ -0,0 +1,555 @@ +/** + * @file ESPAsyncTCPbuffer.cpp + * @date 22.01.2016 + * @author Markus Sattler + * + * Copyright (c) 2015 Markus Sattler. All rights reserved. + * This file is part of the Asynv TCP for ESP. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + + +#include +#include + +#include "ESPAsyncTCPbuffer.h" + + +AsyncTCPbuffer::AsyncTCPbuffer(AsyncClient* client) { + if(client == NULL) { + DEBUG_ASYNC_TCP("[A-TCP] client is null!!!\n"); + panic(); + } + + _client = client; + _TXbufferWrite = new (std::nothrow) cbuf(TCP_MSS); + _TXbufferRead = _TXbufferWrite; + _RXbuffer = new (std::nothrow) cbuf(100); + _RXmode = ATB_RX_MODE_FREE; + _rxSize = 0; + _rxTerminator = 0x00; + _rxReadBytesPtr = NULL; + _rxReadStringPtr = NULL; + _cbDisconnect = NULL; + + _cbRX = NULL; + _cbDone = NULL; + _attachCallbacks(); +} + +AsyncTCPbuffer::~AsyncTCPbuffer() { + if(_client) { + _client->close(); + } + + if(_RXbuffer) { + delete _RXbuffer; + _RXbuffer = NULL; + } + + if(_TXbufferWrite) { + // will be deleted in _TXbufferRead chain + _TXbufferWrite = NULL; + } + + if(_TXbufferRead) { + cbuf * next = _TXbufferRead->next; + delete _TXbufferRead; + while(next != NULL) { + _TXbufferRead = next; + next = _TXbufferRead->next; + delete _TXbufferRead; + } + _TXbufferRead = NULL; + } +} + +size_t AsyncTCPbuffer::write(String & data) { + return write(data.c_str(), data.length()); +} + +size_t AsyncTCPbuffer::write(uint8_t data) { + return write(&data, 1); +} + +size_t AsyncTCPbuffer::write(const char* data) { + return write((const uint8_t *) data, strlen(data)); +} + +size_t AsyncTCPbuffer::write(const char *data, size_t len) { + return write((const uint8_t *) data, len); +} + +/** + * write data in to buffer and try to send the data + * @param data + * @param len + * @return + */ +size_t AsyncTCPbuffer::write(const uint8_t *data, size_t len) { + if(_TXbufferWrite == NULL || _client == NULL || !_client->connected() || data == NULL || len == 0) { + return 0; + } + + size_t bytesLeft = len; + while(bytesLeft) { + size_t w = _TXbufferWrite->write((const char*) data, bytesLeft); + bytesLeft -= w; + data += w; + _sendBuffer(); + + // add new buffer since we have more data + if(_TXbufferWrite->full() && bytesLeft > 0) { + + // to less ram!!! + if(ESP.getFreeHeap() < 4096) { + DEBUG_ASYNC_TCP("[A-TCP] run out of Heap can not send all Data!\n"); + return (len - bytesLeft); + } + + cbuf * next = new (std::nothrow) cbuf(TCP_MSS); + if(next == NULL) { + DEBUG_ASYNC_TCP("[A-TCP] run out of Heap!\n"); + panic(); + } else { + DEBUG_ASYNC_TCP("[A-TCP] new cbuf\n"); + } + + // add new buffer to chain (current cbuf) + _TXbufferWrite->next = next; + + // move ptr for next data + _TXbufferWrite = next; + } + } + + return len; + +} + +/** + * wait until all data has send out + */ +void AsyncTCPbuffer::flush() { + while(!_TXbufferWrite->empty()) { + while(connected() && !_client->canSend()) { + delay(0); + } + if(!connected()) + return; + _sendBuffer(); + } +} + +void AsyncTCPbuffer::noCallback() { + _RXmode = ATB_RX_MODE_NONE; +} + +void AsyncTCPbuffer::readStringUntil(char terminator, String * str, AsyncTCPbufferDoneCb done) { + if(_client == NULL) { + return; + } + DEBUG_ASYNC_TCP("[A-TCP] readStringUntil terminator: %02X\n", terminator); + _RXmode = ATB_RX_MODE_NONE; + _cbDone = done; + _rxReadStringPtr = str; + _rxTerminator = terminator; + _rxSize = 0; + _RXmode = ATB_RX_MODE_TERMINATOR_STRING; +} + +/* + void AsyncTCPbuffer::readBytesUntil(char terminator, char *buffer, size_t length, AsyncTCPbufferDoneCb done) { + _RXmode = ATB_RX_MODE_NONE; + _cbDone = done; + _rxReadBytesPtr = (uint8_t *) buffer; + _rxTerminator = terminator; + _rxSize = length; + _RXmode = ATB_RX_MODE_TERMINATOR; + _handleRxBuffer(NULL, 0); + } + + void AsyncTCPbuffer::readBytesUntil(char terminator, uint8_t *buffer, size_t length, AsyncTCPbufferDoneCb done) { + readBytesUntil(terminator, (char *) buffer, length, done); + } + */ + +void AsyncTCPbuffer::readBytes(char *buffer, size_t length, AsyncTCPbufferDoneCb done) { + if(_client == NULL) { + return; + } + DEBUG_ASYNC_TCP("[A-TCP] readBytes length: %d\n", length); + _RXmode = ATB_RX_MODE_NONE; + _cbDone = done; + _rxReadBytesPtr = (uint8_t *) buffer; + _rxSize = length; + _RXmode = ATB_RX_MODE_READ_BYTES; +} + +void AsyncTCPbuffer::readBytes(uint8_t *buffer, size_t length, AsyncTCPbufferDoneCb done) { + readBytes((char *) buffer, length, done); +} + +void AsyncTCPbuffer::onData(AsyncTCPbufferDataCb cb) { + if(_client == NULL) { + return; + } + DEBUG_ASYNC_TCP("[A-TCP] onData\n"); + _RXmode = ATB_RX_MODE_NONE; + _cbDone = NULL; + _cbRX = cb; + _RXmode = ATB_RX_MODE_FREE; +} + +void AsyncTCPbuffer::onDisconnect(AsyncTCPbufferDisconnectCb cb) { + _cbDisconnect = cb; +} + +IPAddress AsyncTCPbuffer::remoteIP() { + if(!_client) { + return IPAddress(0U); + } + return _client->remoteIP(); +} + +uint16_t AsyncTCPbuffer::remotePort() { + if(!_client) { + return 0; + } + return _client->remotePort(); +} + +bool AsyncTCPbuffer::connected() { + if(!_client) { + return false; + } + return _client->connected(); +} + +void AsyncTCPbuffer::stop() { + + if(!_client) { + return; + } + _client->stop(); + _client = NULL; + + if(_cbDone) { + switch(_RXmode) { + case ATB_RX_MODE_READ_BYTES: + case ATB_RX_MODE_TERMINATOR: + case ATB_RX_MODE_TERMINATOR_STRING: + _RXmode = ATB_RX_MODE_NONE; + _cbDone(false, NULL); + break; + default: + break; + } + } + _RXmode = ATB_RX_MODE_NONE; +} + +void AsyncTCPbuffer::close() { + stop(); +} + + +///-------------------------------- + +/** + * attachCallbacks to AsyncClient class + */ +void AsyncTCPbuffer::_attachCallbacks() { + if(!_client) { + return; + } + DEBUG_ASYNC_TCP("[A-TCP] attachCallbacks\n"); + + _client->onPoll([](void *obj, AsyncClient* c) { + (void)c; + AsyncTCPbuffer* b = ((AsyncTCPbuffer*)(obj)); + if((b->_TXbufferRead != NULL) && !b->_TXbufferRead->empty()) { + b->_sendBuffer(); + } + // if(!b->_RXbuffer->empty()) { + // b->_handleRxBuffer(NULL, 0); + // } + }, this); + + _client->onAck([](void *obj, AsyncClient* c, size_t len, uint32_t time) { + (void)c; + (void)len; + (void)time; + DEBUG_ASYNC_TCP("[A-TCP] onAck\n"); + ((AsyncTCPbuffer*)(obj))->_sendBuffer(); + }, this); + + _client->onDisconnect([](void *obj, AsyncClient* c) { + DEBUG_ASYNC_TCP("[A-TCP] onDisconnect\n"); + AsyncTCPbuffer* b = ((AsyncTCPbuffer*)(obj)); + b->_client = NULL; + bool del = true; + if(b->_cbDisconnect) { + del = b->_cbDisconnect(b); + } + delete c; + if(del) { + delete b; + } + }, this); + + _client->onData([](void *obj, AsyncClient* c, void *buf, size_t len) { + (void)c; + AsyncTCPbuffer* b = ((AsyncTCPbuffer*)(obj)); + b->_rxData((uint8_t *)buf, len); + }, this); + + _client->onTimeout([](void *obj, AsyncClient* c, uint32_t time){ + (void)obj; + (void)time; + DEBUG_ASYNC_TCP("[A-TCP] onTimeout\n"); + c->close(); + }, this); + + DEBUG_ASYNC_TCP("[A-TCP] attachCallbacks Done.\n"); +} + +/** + * send TX buffer if possible + */ +void AsyncTCPbuffer::_sendBuffer() { + //DEBUG_ASYNC_TCP("[A-TCP] _sendBuffer...\n"); + size_t available = _TXbufferRead->available(); + if(available == 0 || _client == NULL || !_client->connected() || !_client->canSend()) { + return; + } + + while(connected() && (_client->space() > 0) && (_TXbufferRead->available() > 0) && _client->canSend()) { + + available = _TXbufferRead->available(); + + if(available > _client->space()) { + available = _client->space(); + } + + char *out = new (std::nothrow) char[available]; + if(out == NULL) { + DEBUG_ASYNC_TCP("[A-TCP] to less heap, try later.\n"); + return; + } + + // read data from buffer + _TXbufferRead->peek(out, available); + + // send data + size_t send = _client->write((const char*) out, available); + if(send != available) { + DEBUG_ASYNC_TCP("[A-TCP] write failed send: %d available: %d \n", send, available); + if(!connected()) { + DEBUG_ASYNC_TCP("[A-TCP] incomplete transfer, connection lost.\n"); + } + } + + // remove really send data from buffer + _TXbufferRead->remove(send); + + // if buffer is empty and there is a other buffer in chain delete the empty one + if(_TXbufferRead->available() == 0 && _TXbufferRead->next != NULL) { + cbuf * old = _TXbufferRead; + _TXbufferRead = _TXbufferRead->next; + delete old; + DEBUG_ASYNC_TCP("[A-TCP] delete cbuf\n"); + } + + delete out; + } + +} + +/** + * called on incoming data + * @param buf + * @param len + */ +void AsyncTCPbuffer::_rxData(uint8_t *buf, size_t len) { + if(!_client || !_client->connected()) { + DEBUG_ASYNC_TCP("[A-TCP] not connected!\n"); + return; + } + if(!_RXbuffer) { + DEBUG_ASYNC_TCP("[A-TCP] _rxData no _RXbuffer!\n"); + return; + } + DEBUG_ASYNC_TCP("[A-TCP] _rxData len: %d RXmode: %d\n", len, _RXmode); + + size_t handled = 0; + + if(_RXmode != ATB_RX_MODE_NONE) { + handled = _handleRxBuffer((uint8_t *) buf, len); + buf += handled; + len -= handled; + + // handle as much as possible before using the buffer + if(_RXbuffer->empty()) { + while(_RXmode != ATB_RX_MODE_NONE && handled != 0 && len > 0) { + handled = _handleRxBuffer(buf, len); + buf += handled; + len -= handled; + } + } + } + + if(len > 0) { + + if(_RXbuffer->room() < len) { + // to less space + DEBUG_ASYNC_TCP("[A-TCP] _rxData buffer full try resize\n"); + _RXbuffer->resizeAdd((len + _RXbuffer->room())); + + if(_RXbuffer->room() < len) { + DEBUG_ASYNC_TCP("[A-TCP] _rxData buffer to full can only handle %d!!!\n", _RXbuffer->room()); + } + } + + _RXbuffer->write((const char *) (buf), len); + } + + if(!_RXbuffer->empty() && _RXmode != ATB_RX_MODE_NONE) { + // handle as much as possible data in buffer + handled = _handleRxBuffer(NULL, 0); + while(_RXmode != ATB_RX_MODE_NONE && handled != 0) { + handled = _handleRxBuffer(NULL, 0); + } + } + + // clean up ram + if(_RXbuffer->empty() && _RXbuffer->room() != 100) { + _RXbuffer->resize(100); + } + +} + +/** + * + */ +size_t AsyncTCPbuffer::_handleRxBuffer(uint8_t *buf, size_t len) { + if(!_client || !_client->connected() || _RXbuffer == NULL) { + return 0; + } + + DEBUG_ASYNC_TCP("[A-TCP] _handleRxBuffer len: %d RXmode: %d\n", len, _RXmode); + + size_t BufferAvailable = _RXbuffer->available(); + size_t r = 0; + + if(_RXmode == ATB_RX_MODE_NONE) { + return 0; + } else if(_RXmode == ATB_RX_MODE_FREE) { + if(_cbRX == NULL) { + return 0; + } + + if(BufferAvailable > 0) { + uint8_t * b = new (std::nothrow) uint8_t[BufferAvailable]; + if(b == NULL){ + panic(); //TODO: What action should this be ? + } + _RXbuffer->peek((char *) b, BufferAvailable); + r = _cbRX(b, BufferAvailable); + _RXbuffer->remove(r); + } + + if(r == BufferAvailable && buf && (len > 0)) { + return _cbRX(buf, len); + } else { + return 0; + } + + } else if(_RXmode == ATB_RX_MODE_READ_BYTES) { + if(_rxReadBytesPtr == NULL || _cbDone == NULL) { + return 0; + } + + size_t newReadCount = 0; + + if(BufferAvailable) { + r = _RXbuffer->read((char *) _rxReadBytesPtr, _rxSize); + _rxSize -= r; + _rxReadBytesPtr += r; + } + + if(_RXbuffer->empty() && (len > 0) && buf) { + r = len; + if(r > _rxSize) { + r = _rxSize; + } + memcpy(_rxReadBytesPtr, buf, r); + _rxReadBytesPtr += r; + _rxSize -= r; + newReadCount += r; + } + + if(_rxSize == 0) { + _RXmode = ATB_RX_MODE_NONE; + _cbDone(true, NULL); + } + + // add left over bytes to Buffer + return newReadCount; + + } else if(_RXmode == ATB_RX_MODE_TERMINATOR) { + // TODO implement read terminator non string + + } else if(_RXmode == ATB_RX_MODE_TERMINATOR_STRING) { + if(_rxReadStringPtr == NULL || _cbDone == NULL) { + return 0; + } + + // handle Buffer + if(BufferAvailable > 0) { + while(!_RXbuffer->empty()) { + char c = _RXbuffer->read(); + if(c == _rxTerminator || c == 0x00) { + _RXmode = ATB_RX_MODE_NONE; + _cbDone(true, _rxReadStringPtr); + return 0; + } else { + (*_rxReadStringPtr) += c; + } + } + } + + if(_RXbuffer->empty() && (len > 0) && buf) { + size_t newReadCount = 0; + while(newReadCount < len) { + char c = (char) *buf; + buf++; + newReadCount++; + if(c == _rxTerminator || c == 0x00) { + _RXmode = ATB_RX_MODE_NONE; + _cbDone(true, _rxReadStringPtr); + return newReadCount; + } else { + (*_rxReadStringPtr) += c; + } + } + return newReadCount; + } + } + + return 0; +} diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/src/ESPAsyncTCPbuffer.h b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/src/ESPAsyncTCPbuffer.h new file mode 100644 index 0000000..08a57c7 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/src/ESPAsyncTCPbuffer.h @@ -0,0 +1,118 @@ +/** + * @file ESPAsyncTCPbuffer.h + * @date 22.01.2016 + * @author Markus Sattler + * + * Copyright (c) 2015 Markus Sattler. All rights reserved. + * This file is part of the Asynv TCP for ESP. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifndef ESPASYNCTCPBUFFER_H_ +#define ESPASYNCTCPBUFFER_H_ + +//#define DEBUG_ASYNC_TCP(...) while(((U0S >> USTXC) & 0x7F) != 0x00); os_printf( __VA_ARGS__ ); while(((U0S >> USTXC) & 0x7F) != 0x00) +//#define DEBUG_ASYNC_TCP ASYNC_TCP_DEBUG +#ifndef DEBUG_ASYNC_TCP +#define DEBUG_ASYNC_TCP(...) +#endif + +#include +#include + +#include "ESPAsyncTCP.h" + + + +typedef enum { + ATB_RX_MODE_NONE, + ATB_RX_MODE_FREE, + ATB_RX_MODE_READ_BYTES, + ATB_RX_MODE_TERMINATOR, + ATB_RX_MODE_TERMINATOR_STRING +} atbRxMode_t; + +class AsyncTCPbuffer: public Print { + + public: + + typedef std::function AsyncTCPbufferDataCb; + typedef std::function AsyncTCPbufferDoneCb; + typedef std::function AsyncTCPbufferDisconnectCb; + + AsyncTCPbuffer(AsyncClient* c); + virtual ~AsyncTCPbuffer(); + + size_t write(String & data); + size_t write(uint8_t data); + size_t write(const char* data); + size_t write(const char *data, size_t len); + size_t write(const uint8_t *data, size_t len); + + void flush(); + + void noCallback(); + + void readStringUntil(char terminator, String * str, AsyncTCPbufferDoneCb done); + + // TODO implement read terminator non string + //void readBytesUntil(char terminator, char *buffer, size_t length, AsyncTCPbufferDoneCb done); + //void readBytesUntil(char terminator, uint8_t *buffer, size_t length, AsyncTCPbufferDoneCb done); + + void readBytes(char *buffer, size_t length, AsyncTCPbufferDoneCb done); + void readBytes(uint8_t *buffer, size_t length, AsyncTCPbufferDoneCb done); + + // TODO implement + // void setTimeout(size_t timeout); + + void onData(AsyncTCPbufferDataCb cb); + void onDisconnect(AsyncTCPbufferDisconnectCb cb); + + IPAddress remoteIP(); + uint16_t remotePort(); + IPAddress localIP(); + uint16_t localPort(); + + bool connected(); + + void stop(); + void close(); + + protected: + AsyncClient* _client; + cbuf * _TXbufferRead; + cbuf * _TXbufferWrite; + cbuf * _RXbuffer; + atbRxMode_t _RXmode; + size_t _rxSize; + char _rxTerminator; + uint8_t * _rxReadBytesPtr; + String * _rxReadStringPtr; + + AsyncTCPbufferDataCb _cbRX; + AsyncTCPbufferDoneCb _cbDone; + AsyncTCPbufferDisconnectCb _cbDisconnect; + + void _attachCallbacks(); + void _sendBuffer(); + void _on_close(); + void _rxData(uint8_t *buf, size_t len); + size_t _handleRxBuffer(uint8_t *buf, size_t len); + +}; + +#endif /* ESPASYNCTCPBUFFER_H_ */ diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/src/SyncClient.cpp b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/src/SyncClient.cpp new file mode 100644 index 0000000..8335358 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/src/SyncClient.cpp @@ -0,0 +1,414 @@ +/* + Asynchronous TCP library for Espressif MCUs + + Copyright (c) 2016 Hristo Gochkov. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ +#include "Arduino.h" +#include "SyncClient.h" +#include "ESPAsyncTCP.h" +#include "cbuf.h" +#include + +#define DEBUG_ESP_SYNC_CLIENT +#if defined(DEBUG_ESP_SYNC_CLIENT) && !defined(SYNC_CLIENT_DEBUG) +#define SYNC_CLIENT_DEBUG( format, ...) DEBUG_GENERIC_P("[SYNC_CLIENT]", format, ##__VA_ARGS__) +#endif +#ifndef SYNC_CLIENT_DEBUG +#define SYNC_CLIENT_DEBUG(...) do { (void)0;} while(false) +#endif + +/* + Without LWIP_NETIF_TX_SINGLE_PBUF, all tcp_writes default to "no copy". + Referenced data must be preserved and free-ed from the specified tcp_sent() + callback. Alternative, tcp_writes need to use the TCP_WRITE_FLAG_COPY + attribute. +*/ +static_assert(LWIP_NETIF_TX_SINGLE_PBUF, "Required, tcp_write() must always copy."); + +SyncClient::SyncClient(size_t txBufLen) + : _client(NULL) + , _tx_buffer(NULL) + , _tx_buffer_size(txBufLen) + , _rx_buffer(NULL) + , _ref(NULL) +{ + ref(); +} + +SyncClient::SyncClient(AsyncClient *client, size_t txBufLen) + : _client(client) + , _tx_buffer(new (std::nothrow) cbuf(txBufLen)) + , _tx_buffer_size(txBufLen) + , _rx_buffer(NULL) + , _ref(NULL) +{ + if(ref() > 0 && _client != NULL) + _attachCallbacks(); +} + +SyncClient::~SyncClient(){ + if (0 == unref()) + _release(); +} + +void SyncClient::_release(){ + if(_client != NULL){ + _client->onData(NULL, NULL); + _client->onAck(NULL, NULL); + _client->onPoll(NULL, NULL); + _client->abort(); + _client = NULL; + } + if(_tx_buffer != NULL){ + cbuf *b = _tx_buffer; + _tx_buffer = NULL; + delete b; + } + while(_rx_buffer != NULL){ + cbuf *b = _rx_buffer; + _rx_buffer = _rx_buffer->next; + delete b; + } +} + +int SyncClient::ref(){ + if(_ref == NULL){ + _ref = new (std::nothrow) int; + if(_ref != NULL) + *_ref = 0; + else + return -1; + } + return (++*_ref); +} + +int SyncClient::unref(){ + int count = -1; + if (_ref != NULL) { + count = --*_ref; + if (0 == count) { + delete _ref; + _ref = NULL; + } + } + return count; +} + +#if ASYNC_TCP_SSL_ENABLED +int SyncClient::_connect(const IPAddress& ip, uint16_t port, bool secure){ +#else +int SyncClient::_connect(const IPAddress& ip, uint16_t port){ +#endif + if(connected()) + return 0; + if(_client != NULL) + delete _client; + + _client = new (std::nothrow) AsyncClient(); + if (_client == NULL) + return 0; + + _client->onConnect([](void *obj, AsyncClient *c){ ((SyncClient*)(obj))->_onConnect(c); }, this); + _attachCallbacks_Disconnect(); +#if ASYNC_TCP_SSL_ENABLED + if(_client->connect(ip, port, secure)){ +#else + if(_client->connect(ip, port)){ +#endif + while(_client != NULL && !_client->connected() && !_client->disconnecting()) + delay(1); + return connected(); + } + return 0; +} + +#if ASYNC_TCP_SSL_ENABLED +int SyncClient::connect(const char *host, uint16_t port, bool secure){ +#else +int SyncClient::connect(const char *host, uint16_t port){ +#endif + if(connected()) + return 0; + if(_client != NULL) + delete _client; + + _client = new (std::nothrow) AsyncClient(); + if (_client == NULL) + return 0; + + _client->onConnect([](void *obj, AsyncClient *c){ ((SyncClient*)(obj))->_onConnect(c); }, this); + _attachCallbacks_Disconnect(); +#if ASYNC_TCP_SSL_ENABLED + if(_client->connect(host, port, secure)){ +#else + if(_client->connect(host, port)){ +#endif + while(_client != NULL && !_client->connected() && !_client->disconnecting()) + delay(1); + return connected(); + } + return 0; +} +//#define SYNCCLIENT_NEW_OPERATOR_EQUAL +#ifdef SYNCCLIENT_NEW_OPERATOR_EQUAL +/* + New behavior for operator= + + Allow for the object to be placed on a queue and transfered to a new container + with buffers still in tact. Avoiding receive data drops. Transfers rx and tx + buffers. Supports return by value. + + Note, this is optional, the old behavior is the default. + +*/ +SyncClient & SyncClient::operator=(const SyncClient &other){ + int *rhsref = other._ref; + ++*rhsref; // Just in case the left and right side are the same object with different containers + if (0 == unref()) + _release(); + _ref = other._ref; + ref(); + --*rhsref; + // Why do I not test _tx_buffer for != NULL and free? + // I allow for the lh target container, to be a copy of an active + // connection. Thus we are just reusing the container. + // The above unref() handles releaseing the previous client of the container. + _tx_buffer_size = other._tx_buffer_size; + _tx_buffer = other._tx_buffer; + _client = other._client; + if (_client != NULL && _tx_buffer == NULL) + _tx_buffer = new (std::nothrow) cbuf(_tx_buffer_size); + + _rx_buffer = other._rx_buffer; + if(_client) + _attachCallbacks(); + return *this; +} +#else // ! SYNCCLIENT_NEW_OPERATOR_EQUAL +// This is the origianl logic with null checks +SyncClient & SyncClient::operator=(const SyncClient &other){ + if(_client != NULL){ + _client->abort(); + _client->free(); + _client = NULL; + } + _tx_buffer_size = other._tx_buffer_size; + if(_tx_buffer != NULL){ + cbuf *b = _tx_buffer; + _tx_buffer = NULL; + delete b; + } + while(_rx_buffer != NULL){ + cbuf *b = _rx_buffer; + _rx_buffer = b->next; + delete b; + } + if(other._client != NULL) + _tx_buffer = new (std::nothrow) cbuf(other._tx_buffer_size); + + _client = other._client; + if(_client) + _attachCallbacks(); + + return *this; +} +#endif + +void SyncClient::setTimeout(uint32_t seconds){ + if(_client != NULL) + _client->setRxTimeout(seconds); +} + +uint8_t SyncClient::status(){ + if(_client == NULL) + return 0; + return _client->state(); +} + +uint8_t SyncClient::connected(){ + return (_client != NULL && _client->connected()); +} + +bool SyncClient::stop(unsigned int maxWaitMs){ + (void)maxWaitMs; + if(_client != NULL) + _client->close(true); + return true; +} + +size_t SyncClient::_sendBuffer(){ + if(_client == NULL || _tx_buffer == NULL) + return 0; + size_t available = _tx_buffer->available(); + if(!connected() || !_client->canSend() || available == 0) + return 0; + size_t sendable = _client->space(); + if(sendable < available) + available= sendable; + char *out = new (std::nothrow) char[available]; + if(out == NULL) + return 0; + + _tx_buffer->read(out, available); + size_t sent = _client->write(out, available); + delete[] out; + return sent; +} + +void SyncClient::_onData(void *data, size_t len){ + _client->ackLater(); + cbuf *b = new (std::nothrow) cbuf(len+1); + if(b != NULL){ + b->write((const char *)data, len); + if(_rx_buffer == NULL) + _rx_buffer = b; + else { + cbuf *p = _rx_buffer; + while(p->next != NULL) + p = p->next; + p->next = b; + } + } else { + // We ran out of memory. This fail causes lost receive data. + // The connection should be closed in a manner that conveys something + // bad/abnormal has happened to the connection. Hence, we abort the + // connection to avoid possible data corruption. + // Note, callbacks maybe called. + _client->abort(); + } +} + +void SyncClient::_onDisconnect(){ + if(_client != NULL){ + _client = NULL; + } + if(_tx_buffer != NULL){ + cbuf *b = _tx_buffer; + _tx_buffer = NULL; + delete b; + } +} + +void SyncClient::_onConnect(AsyncClient *c){ + _client = c; + if(_tx_buffer != NULL){ + cbuf *b = _tx_buffer; + _tx_buffer = NULL; + delete b; + } + _tx_buffer = new (std::nothrow) cbuf(_tx_buffer_size); + _attachCallbacks_AfterConnected(); +} + +void SyncClient::_attachCallbacks(){ + _attachCallbacks_Disconnect(); + _attachCallbacks_AfterConnected(); +} + +void SyncClient::_attachCallbacks_AfterConnected(){ + _client->onAck([](void *obj, AsyncClient* c, size_t len, uint32_t time){ (void)c; (void)len; (void)time; ((SyncClient*)(obj))->_sendBuffer(); }, this); + _client->onData([](void *obj, AsyncClient* c, void *data, size_t len){ (void)c; ((SyncClient*)(obj))->_onData(data, len); }, this); + _client->onTimeout([](void *obj, AsyncClient* c, uint32_t time){ (void)obj; (void)time; c->close(); }, this); +} + +void SyncClient::_attachCallbacks_Disconnect(){ + _client->onDisconnect([](void *obj, AsyncClient* c){ ((SyncClient*)(obj))->_onDisconnect(); delete c; }, this); +} + +size_t SyncClient::write(uint8_t data){ + return write(&data, 1); +} + +size_t SyncClient::write(const uint8_t *data, size_t len){ + if(_tx_buffer == NULL || !connected()){ + return 0; + } + size_t toWrite = 0; + size_t toSend = len; + while(_tx_buffer->room() < toSend){ + toWrite = _tx_buffer->room(); + _tx_buffer->write((const char*)data, toWrite); + while(connected() && !_client->canSend()) + delay(0); + if(!connected()) + return 0; + _sendBuffer(); + toSend -= toWrite; + } + _tx_buffer->write((const char*)(data+(len - toSend)), toSend); + if(connected() && _client->canSend()) + _sendBuffer(); + return len; +} + +int SyncClient::available(){ + if(_rx_buffer == NULL) return 0; + size_t a = 0; + cbuf *b = _rx_buffer; + while(b != NULL){ + a += b->available(); + b = b->next; + } + return a; +} + +int SyncClient::peek(){ + if(_rx_buffer == NULL) return -1; + return _rx_buffer->peek(); +} + +int SyncClient::read(uint8_t *data, size_t len){ + if(_rx_buffer == NULL) return -1; + + size_t readSoFar = 0; + while(_rx_buffer != NULL && (len - readSoFar) >= _rx_buffer->available()){ + cbuf *b = _rx_buffer; + _rx_buffer = _rx_buffer->next; + size_t toRead = b->available(); + readSoFar += b->read((char*)(data+readSoFar), toRead); + if(connected()){ + _client->ack(b->size() - 1); + } + delete b; + } + if(_rx_buffer != NULL && readSoFar < len){ + readSoFar += _rx_buffer->read((char*)(data+readSoFar), (len - readSoFar)); + } + return readSoFar; +} + +int SyncClient::read(){ + uint8_t res = 0; + if(read(&res, 1) != 1) + return -1; + return res; +} + +bool SyncClient::flush(unsigned int maxWaitMs){ + (void)maxWaitMs; + if(_tx_buffer == NULL || !connected()) + return false; + if(_tx_buffer->available()){ + while(connected() && !_client->canSend()) + delay(0); + if(_client == NULL || _tx_buffer == NULL) + return false; + _sendBuffer(); + } + return true; +} diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/src/SyncClient.h b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/src/SyncClient.h new file mode 100644 index 0000000..cb568de --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/src/SyncClient.h @@ -0,0 +1,109 @@ +/* + Asynchronous TCP library for Espressif MCUs + + Copyright (c) 2016 Hristo Gochkov. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef SYNCCLIENT_H_ +#define SYNCCLIENT_H_ + +#include "Client.h" +// Needed for Arduino core releases prior to 2.5.0, because of changes +// made to accommodate Arduino core 2.5.0 +// CONST was 1st defined in Core 2.5.0 in IPAddress.h +#ifndef CONST +#define CONST +#endif +#include +class cbuf; +class AsyncClient; + +class SyncClient: public Client { + private: + AsyncClient *_client; + cbuf *_tx_buffer; + size_t _tx_buffer_size; + cbuf *_rx_buffer; + int *_ref; + + size_t _sendBuffer(); + void _onData(void *data, size_t len); + void _onConnect(AsyncClient *c); + void _onDisconnect(); + void _attachCallbacks(); + void _attachCallbacks_Disconnect(); + void _attachCallbacks_AfterConnected(); + void _release(); + + public: + SyncClient(size_t txBufLen = TCP_MSS); + SyncClient(AsyncClient *client, size_t txBufLen = TCP_MSS); + virtual ~SyncClient(); + + int ref(); + int unref(); + operator bool(){ return connected(); } + SyncClient & operator=(const SyncClient &other); + +#if ASYNC_TCP_SSL_ENABLED + int _connect(const IPAddress& ip, uint16_t port, bool secure); + int connect(CONST IPAddress& ip, uint16_t port, bool secure){ + return _connect(ip, port, secure); + } + int connect(IPAddress ip, uint16_t port, bool secure){ + return _connect(reinterpret_cast(ip), port, secure); + } + int connect(const char *host, uint16_t port, bool secure); + int connect(CONST IPAddress& ip, uint16_t port){ + return _connect(ip, port, false); + } + int connect(IPAddress ip, uint16_t port){ + return _connect(reinterpret_cast(ip), port, false); + } + int connect(const char *host, uint16_t port){ + return connect(host, port, false); + } +#else + int _connect(const IPAddress& ip, uint16_t port); + int connect(CONST IPAddress& ip, uint16_t port){ + return _connect(ip, port); + } + int connect(IPAddress ip, uint16_t port){ + return _connect(reinterpret_cast(ip), port); + } + int connect(const char *host, uint16_t port); +#endif + void setTimeout(uint32_t seconds); + + uint8_t status(); + uint8_t connected(); + + bool stop(unsigned int maxWaitMs); + bool flush(unsigned int maxWaitMs); + void stop() { (void)stop(0);} + void flush() { (void)flush(0);} + size_t write(uint8_t data); + size_t write(const uint8_t *data, size_t len); + + int available(); + int peek(); + int read(); + int read(uint8_t *data, size_t len); +}; + +#endif /* SYNCCLIENT_H_ */ diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/src/async_config.h b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/src/async_config.h new file mode 100644 index 0000000..ca6912f --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/src/async_config.h @@ -0,0 +1,38 @@ +#ifndef LIBRARIES_ESPASYNCTCP_SRC_ASYNC_CONFIG_H_ +#define LIBRARIES_ESPASYNCTCP_SRC_ASYNC_CONFIG_H_ + +#ifndef ASYNC_TCP_SSL_ENABLED +#define ASYNC_TCP_SSL_ENABLED 0 +#endif + +#ifndef TCP_MSS +// May have been definded as a -DTCP_MSS option on the compile line or not. +// Arduino core 2.3.0 or earlier does not do the -DTCP_MSS option. +// Later versions may set this option with info from board.txt. +// However, Core 2.4.0 and up board.txt does not define TCP_MSS for lwIP v1.4 +#define TCP_MSS (1460) +#endif + +// #define ASYNC_TCP_DEBUG(...) ets_printf(__VA_ARGS__) +// #define TCP_SSL_DEBUG(...) ets_printf(__VA_ARGS__) +// #define ASYNC_TCP_ASSERT( a ) do{ if(!(a)){ets_printf("ASSERT: %s %u \n", __FILE__, __LINE__);}}while(0) + +// Starting with Arduino Core 2.4.0 and up the define of DEBUG_ESP_PORT +// can be handled through the Arduino IDE Board options instead of here. +// #define DEBUG_ESP_PORT Serial + +// #define DEBUG_ESP_ASYNC_TCP 1 +// #define DEBUG_ESP_TCP_SSL 1 +#include + +#ifndef ASYNC_TCP_ASSERT +#define ASYNC_TCP_ASSERT(...) do { (void)0;} while(false) +#endif +#ifndef ASYNC_TCP_DEBUG +#define ASYNC_TCP_DEBUG(...) do { (void)0;} while(false) +#endif +#ifndef TCP_SSL_DEBUG +#define TCP_SSL_DEBUG(...) do { (void)0;} while(false) +#endif + +#endif /* LIBRARIES_ESPASYNCTCP_SRC_ASYNC_CONFIG_H_ */ diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/src/tcp_axtls.c b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/src/tcp_axtls.c new file mode 100644 index 0000000..cdbdf41 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/src/tcp_axtls.c @@ -0,0 +1,588 @@ +/* + Asynchronous TCP library for Espressif MCUs + + Copyright (c) 2016 Hristo Gochkov. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ +/* + * Compatibility for AxTLS with LWIP raw tcp mode (http://lwip.wikia.com/wiki/Raw/TCP) + * Original Code and Inspiration: Slavey Karadzhov + */ +#include +#if ASYNC_TCP_SSL_ENABLED + +#include "lwip/opt.h" +#include "lwip/tcp.h" +#include "lwip/inet.h" +#include +#include +#include +#include +#include + +uint8_t * default_private_key = NULL; +uint16_t default_private_key_len = 0; + +uint8_t * default_certificate = NULL; +uint16_t default_certificate_len = 0; + +static uint8_t _tcp_ssl_has_client = 0; + +SSL_CTX * tcp_ssl_new_server_ctx(const char *cert, const char *private_key_file, const char *password){ + uint32_t options = SSL_CONNECT_IN_PARTS; + SSL_CTX *ssl_ctx; + + if(private_key_file){ + options |= SSL_NO_DEFAULT_KEY; + } + + if ((ssl_ctx = ssl_ctx_new(options, SSL_DEFAULT_SVR_SESS)) == NULL){ + TCP_SSL_DEBUG("tcp_ssl_new_server_ctx: failed to allocate context\n"); + return NULL; + } + + if (private_key_file){ + int obj_type = SSL_OBJ_RSA_KEY; + if (strstr(private_key_file, ".p8")) + obj_type = SSL_OBJ_PKCS8; + else if (strstr(private_key_file, ".p12")) + obj_type = SSL_OBJ_PKCS12; + + if (ssl_obj_load(ssl_ctx, obj_type, private_key_file, password)){ + TCP_SSL_DEBUG("tcp_ssl_new_server_ctx: load private key '%s' failed\n", private_key_file); + return NULL; + } + } + + if (cert){ + if (ssl_obj_load(ssl_ctx, SSL_OBJ_X509_CERT, cert, NULL)){ + TCP_SSL_DEBUG("tcp_ssl_new_server_ctx: load certificate '%s' failed\n", cert); + return NULL; + } + } + return ssl_ctx; +} + +struct tcp_ssl_pcb { + struct tcp_pcb *tcp; + int fd; + SSL_CTX* ssl_ctx; + SSL *ssl; + uint8_t type; + int handshake; + void * arg; + tcp_ssl_data_cb_t on_data; + tcp_ssl_handshake_cb_t on_handshake; + tcp_ssl_error_cb_t on_error; + int last_wr; + struct pbuf *tcp_pbuf; + int pbuf_offset; + struct tcp_ssl_pcb * next; +}; + +typedef struct tcp_ssl_pcb tcp_ssl_t; + +static tcp_ssl_t * tcp_ssl_array = NULL; +static int tcp_ssl_next_fd = 0; + +uint8_t tcp_ssl_has_client(){ + return _tcp_ssl_has_client; +} + +tcp_ssl_t * tcp_ssl_new(struct tcp_pcb *tcp) { + + if(tcp_ssl_next_fd < 0){ + tcp_ssl_next_fd = 0;//overflow + } + + tcp_ssl_t * new_item = (tcp_ssl_t*)malloc(sizeof(tcp_ssl_t)); + if(!new_item){ + TCP_SSL_DEBUG("tcp_ssl_new: failed to allocate tcp_ssl\n"); + return NULL; + } + + new_item->tcp = tcp; + new_item->handshake = SSL_NOT_OK; + new_item->arg = NULL; + new_item->on_data = NULL; + new_item->on_handshake = NULL; + new_item->on_error = NULL; + new_item->tcp_pbuf = NULL; + new_item->pbuf_offset = 0; + new_item->next = NULL; + new_item->ssl_ctx = NULL; + new_item->ssl = NULL; + new_item->type = TCP_SSL_TYPE_CLIENT; + new_item->fd = tcp_ssl_next_fd++; + + if(tcp_ssl_array == NULL){ + tcp_ssl_array = new_item; + } else { + tcp_ssl_t * item = tcp_ssl_array; + while(item->next != NULL) + item = item->next; + item->next = new_item; + } + + TCP_SSL_DEBUG("tcp_ssl_new: %d\n", new_item->fd); + return new_item; +} + +tcp_ssl_t* tcp_ssl_get(struct tcp_pcb *tcp) { + if(tcp == NULL) { + return NULL; + } + tcp_ssl_t * item = tcp_ssl_array; + while(item && item->tcp != tcp){ + item = item->next; + } + return item; +} + +int tcp_ssl_new_client(struct tcp_pcb *tcp){ + SSL_CTX* ssl_ctx; + tcp_ssl_t * tcp_ssl; + + if(tcp == NULL) { + return -1; + } + + if(tcp_ssl_get(tcp) != NULL){ + TCP_SSL_DEBUG("tcp_ssl_new_client: tcp_ssl already exists\n"); + return -1; + } + + ssl_ctx = ssl_ctx_new(SSL_CONNECT_IN_PARTS | SSL_SERVER_VERIFY_LATER, 1); + if(ssl_ctx == NULL){ + TCP_SSL_DEBUG("tcp_ssl_new_client: failed to allocate ssl context\n"); + return -1; + } + + tcp_ssl = tcp_ssl_new(tcp); + if(tcp_ssl == NULL){ + ssl_ctx_free(ssl_ctx); + return -1; + } + + tcp_ssl->ssl_ctx = ssl_ctx; + + tcp_ssl->ssl = ssl_client_new(ssl_ctx, tcp_ssl->fd, NULL, 0, NULL); + if(tcp_ssl->ssl == NULL){ + TCP_SSL_DEBUG("tcp_ssl_new_client: failed to allocate ssl\n"); + tcp_ssl_free(tcp); + return -1; + } + + return tcp_ssl->fd; +} + +int tcp_ssl_new_server(struct tcp_pcb *tcp, SSL_CTX* ssl_ctx){ + tcp_ssl_t * tcp_ssl; + + if(tcp == NULL) { + return -1; + } + + if(ssl_ctx == NULL){ + return -1; + } + + if(tcp_ssl_get(tcp) != NULL){ + TCP_SSL_DEBUG("tcp_ssl_new_server: tcp_ssl already exists\n"); + return -1; + } + + tcp_ssl = tcp_ssl_new(tcp); + if(tcp_ssl == NULL){ + return -1; + } + + tcp_ssl->type = TCP_SSL_TYPE_SERVER; + tcp_ssl->ssl_ctx = ssl_ctx; + + _tcp_ssl_has_client = 1; + tcp_ssl->ssl = ssl_server_new(ssl_ctx, tcp_ssl->fd); + if(tcp_ssl->ssl == NULL){ + TCP_SSL_DEBUG("tcp_ssl_new_server: failed to allocate ssl\n"); + tcp_ssl_free(tcp); + return -1; + } + + return tcp_ssl->fd; +} + +int tcp_ssl_free(struct tcp_pcb *tcp) { + + if(tcp == NULL) { + return -1; + } + + tcp_ssl_t * item = tcp_ssl_array; + + if(item->tcp == tcp){ + tcp_ssl_array = tcp_ssl_array->next; + if(item->tcp_pbuf != NULL){ + pbuf_free(item->tcp_pbuf); + } + TCP_SSL_DEBUG("tcp_ssl_free: %d\n", item->fd); + if(item->ssl) + ssl_free(item->ssl); + if(item->type == TCP_SSL_TYPE_CLIENT && item->ssl_ctx) + ssl_ctx_free(item->ssl_ctx); + if(item->type == TCP_SSL_TYPE_SERVER) + _tcp_ssl_has_client = 0; + free(item); + return 0; + } + + while(item->next && item->next->tcp != tcp) + item = item->next; + + if(item->next == NULL){ + return ERR_TCP_SSL_INVALID_CLIENTFD_DATA;//item not found + } + + tcp_ssl_t * i = item->next; + item->next = i->next; + if(i->tcp_pbuf != NULL){ + pbuf_free(i->tcp_pbuf); + } + TCP_SSL_DEBUG("tcp_ssl_free: %d\n", i->fd); + if(i->ssl) + ssl_free(i->ssl); + if(i->type == TCP_SSL_TYPE_CLIENT && i->ssl_ctx) + ssl_ctx_free(i->ssl_ctx); + if(i->type == TCP_SSL_TYPE_SERVER) + _tcp_ssl_has_client = 0; + free(i); + return 0; +} + +#ifdef AXTLS_2_0_0_SNDBUF +int tcp_ssl_sndbuf(struct tcp_pcb *tcp){ + int expected; + int available; + int result = -1; + + if(tcp == NULL) { + return result; + } + tcp_ssl_t * tcp_ssl = tcp_ssl_get(tcp); + if(!tcp_ssl){ + TCP_SSL_DEBUG("tcp_ssl_sndbuf: tcp_ssl is NULL\n"); + return result; + } + available = tcp_sndbuf(tcp); + if(!available){ + TCP_SSL_DEBUG("tcp_ssl_sndbuf: tcp_sndbuf is zero\n"); + return 0; + } + result = available; + while((expected = ssl_calculate_write_length(tcp_ssl->ssl, result)) > available){ + result -= (expected - available) + 4; + } + + if(expected > 0){ + //TCP_SSL_DEBUG("tcp_ssl_sndbuf: tcp_sndbuf is %d from %d\n", result, available); + return result; + } + + return 0; +} +#endif + +int tcp_ssl_write(struct tcp_pcb *tcp, uint8_t *data, size_t len) { + if(tcp == NULL) { + return -1; + } + tcp_ssl_t * tcp_ssl = tcp_ssl_get(tcp); + if(!tcp_ssl){ + TCP_SSL_DEBUG("tcp_ssl_write: tcp_ssl is NULL\n"); + return 0; + } + tcp_ssl->last_wr = 0; + +#ifdef AXTLS_2_0_0_SNDBUF + int expected_len = ssl_calculate_write_length(tcp_ssl->ssl, len); + int available_len = tcp_sndbuf(tcp); + if(expected_len < 0 || expected_len > available_len){ + TCP_SSL_DEBUG("tcp_ssl_write: data will not fit! %u < %d(%u)\r\n", available_len, expected_len, len); + return -1; + } +#endif + + int rc = ssl_write(tcp_ssl->ssl, data, len); + + //TCP_SSL_DEBUG("tcp_ssl_write: %u -> %d (%d)\r\n", len, tcp_ssl->last_wr, rc); + + if (rc < 0){ + if(rc != SSL_CLOSE_NOTIFY) { + TCP_SSL_DEBUG("tcp_ssl_write error: %d\r\n", rc); + } + return rc; + } + + return tcp_ssl->last_wr; +} + +/** + * Reads data from the SSL over TCP stream. Returns decrypted data. + * @param tcp_pcb *tcp - pointer to the raw tcp object + * @param pbuf *p - pointer to the buffer with the TCP packet data + * + * @return int + * 0 - when everything is fine but there are no symbols to process yet + * < 0 - when there is an error + * > 0 - the length of the clear text characters that were read + */ +int tcp_ssl_read(struct tcp_pcb *tcp, struct pbuf *p) { + if(tcp == NULL) { + return -1; + } + tcp_ssl_t* fd_data = NULL; + + int read_bytes = 0; + int total_bytes = 0; + uint8_t *read_buf; + + fd_data = tcp_ssl_get(tcp); + if(fd_data == NULL) { + TCP_SSL_DEBUG("tcp_ssl_read: tcp_ssl is NULL\n"); + return ERR_TCP_SSL_INVALID_CLIENTFD_DATA; + } + + if(p == NULL) { + TCP_SSL_DEBUG("tcp_ssl_read:p == NULL\n"); + return ERR_TCP_SSL_INVALID_DATA; + } + + //TCP_SSL_DEBUG("READY TO READ SOME DATA\n"); + + fd_data->tcp_pbuf = p; + fd_data->pbuf_offset = 0; + + do { + read_bytes = ssl_read(fd_data->ssl, &read_buf); + //TCP_SSL_DEBUG("tcp_ssl_ssl_read: %d\n", read_bytes); + if(read_bytes < SSL_OK) { + if(read_bytes != SSL_CLOSE_NOTIFY) { + TCP_SSL_DEBUG("tcp_ssl_read: read error: %d\n", read_bytes); + } + total_bytes = read_bytes; + break; + } else if(read_bytes > 0){ + if(fd_data->on_data){ + fd_data->on_data(fd_data->arg, tcp, read_buf, read_bytes); + } + total_bytes+= read_bytes; + } else { + if(fd_data->handshake != SSL_OK) { + fd_data->handshake = ssl_handshake_status(fd_data->ssl); + if(fd_data->handshake == SSL_OK){ + //TCP_SSL_DEBUG("tcp_ssl_read: handshake OK\n"); + if(fd_data->on_handshake) + fd_data->on_handshake(fd_data->arg, fd_data->tcp, fd_data->ssl); + } else if(fd_data->handshake != SSL_NOT_OK){ + TCP_SSL_DEBUG("tcp_ssl_read: handshake error: %d\n", fd_data->handshake); + if(fd_data->on_error) + fd_data->on_error(fd_data->arg, fd_data->tcp, fd_data->handshake); + return fd_data->handshake; + } + } + } + } while (p->tot_len - fd_data->pbuf_offset > 0); + + tcp_recved(tcp, p->tot_len); + fd_data->tcp_pbuf = NULL; + pbuf_free(p); + + return total_bytes; +} + +SSL * tcp_ssl_get_ssl(struct tcp_pcb *tcp){ + tcp_ssl_t * tcp_ssl = tcp_ssl_get(tcp); + if(tcp_ssl){ + return tcp_ssl->ssl; + } + return NULL; +} + +bool tcp_ssl_has(struct tcp_pcb *tcp){ + return tcp_ssl_get(tcp) != NULL; +} + +int tcp_ssl_is_server(struct tcp_pcb *tcp){ + tcp_ssl_t * tcp_ssl = tcp_ssl_get(tcp); + if(tcp_ssl){ + return tcp_ssl->type; + } + return -1; +} + +void tcp_ssl_arg(struct tcp_pcb *tcp, void * arg){ + tcp_ssl_t * item = tcp_ssl_get(tcp); + if(item) { + item->arg = arg; + } +} + +void tcp_ssl_data(struct tcp_pcb *tcp, tcp_ssl_data_cb_t arg){ + tcp_ssl_t * item = tcp_ssl_get(tcp); + if(item) { + item->on_data = arg; + } +} + +void tcp_ssl_handshake(struct tcp_pcb *tcp, tcp_ssl_handshake_cb_t arg){ + tcp_ssl_t * item = tcp_ssl_get(tcp); + if(item) { + item->on_handshake = arg; + } +} + +void tcp_ssl_err(struct tcp_pcb *tcp, tcp_ssl_error_cb_t arg){ + tcp_ssl_t * item = tcp_ssl_get(tcp); + if(item) { + item->on_error = arg; + } +} + +static tcp_ssl_file_cb_t _tcp_ssl_file_cb = NULL; +static void * _tcp_ssl_file_arg = NULL; + +void tcp_ssl_file(tcp_ssl_file_cb_t cb, void * arg){ + _tcp_ssl_file_cb = cb; + _tcp_ssl_file_arg = arg; +} + +int ax_get_file(const char *filename, uint8_t **buf) { + //TCP_SSL_DEBUG("ax_get_file: %s\n", filename); + if(_tcp_ssl_file_cb){ + return _tcp_ssl_file_cb(_tcp_ssl_file_arg, filename, buf); + } + *buf = 0; + return 0; +} + +tcp_ssl_t* tcp_ssl_get_by_fd(int fd) { + tcp_ssl_t * item = tcp_ssl_array; + while(item && item->fd != fd){ + item = item->next; + } + return item; +} +/* + * The LWIP tcp raw version of the SOCKET_WRITE(A, B, C) + */ +int ax_port_write(int fd, uint8_t *data, uint16_t len) { + tcp_ssl_t *fd_data = NULL; + int tcp_len = 0; + err_t err = ERR_OK; + + //TCP_SSL_DEBUG("ax_port_write: %d, %d\n", fd, len); + + fd_data = tcp_ssl_get_by_fd(fd); + if(fd_data == NULL) { + //TCP_SSL_DEBUG("ax_port_write: tcp_ssl[%d] is NULL\n", fd); + return ERR_MEM; + } + + if (data == NULL || len == 0) { + return 0; + } + + if (tcp_sndbuf(fd_data->tcp) < len) { + tcp_len = tcp_sndbuf(fd_data->tcp); + if(tcp_len == 0) { + TCP_SSL_DEBUG("ax_port_write: tcp_sndbuf is zero: %d\n", len); + return ERR_MEM; + } + } else { + tcp_len = len; + } + + if (tcp_len > 2 * fd_data->tcp->mss) { + tcp_len = 2 * fd_data->tcp->mss; + } + + err = tcp_write(fd_data->tcp, data, tcp_len, TCP_WRITE_FLAG_COPY); + if(err < ERR_OK) { + if (err == ERR_MEM) { + TCP_SSL_DEBUG("ax_port_write: No memory %d (%d)\n", tcp_len, len); + return err; + } + TCP_SSL_DEBUG("ax_port_write: tcp_write error: %d\n", err); + return err; + } else if (err == ERR_OK) { + //TCP_SSL_DEBUG("ax_port_write: tcp_output: %d / %d\n", tcp_len, len); + err = tcp_output(fd_data->tcp); + if(err != ERR_OK) { + TCP_SSL_DEBUG("ax_port_write: tcp_output err: %d\n", err); + return err; + } + } + + fd_data->last_wr += tcp_len; + + return tcp_len; +} + +/* + * The LWIP tcp raw version of the SOCKET_READ(A, B, C) + */ +int ax_port_read(int fd, uint8_t *data, int len) { + tcp_ssl_t *fd_data = NULL; + uint8_t *read_buf = NULL; + uint8_t *pread_buf = NULL; + u16_t recv_len = 0; + + //TCP_SSL_DEBUG("ax_port_read: %d, %d\n", fd, len); + + fd_data = tcp_ssl_get_by_fd(fd); + if (fd_data == NULL) { + TCP_SSL_DEBUG("ax_port_read: tcp_ssl[%d] is NULL\n", fd); + return ERR_TCP_SSL_INVALID_CLIENTFD_DATA; + } + + if(fd_data->tcp_pbuf == NULL || fd_data->tcp_pbuf->tot_len == 0) { + return 0; + } + + read_buf =(uint8_t*)calloc(fd_data->tcp_pbuf->len + 1, sizeof(uint8_t)); + pread_buf = read_buf; + if (pread_buf != NULL){ + recv_len = pbuf_copy_partial(fd_data->tcp_pbuf, read_buf, len, fd_data->pbuf_offset); + fd_data->pbuf_offset += recv_len; + } + + if (recv_len != 0) { + memcpy(data, read_buf, recv_len); + } + + if(len < recv_len) { + TCP_SSL_DEBUG("ax_port_read: got %d bytes more than expected\n", recv_len - len); + } + + free(pread_buf); + pread_buf = NULL; + + return recv_len; +} + +void ax_wdt_feed() {} + +#endif diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/src/tcp_axtls.h b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/src/tcp_axtls.h new file mode 100644 index 0000000..118e36f --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/src/tcp_axtls.h @@ -0,0 +1,98 @@ +/* + Asynchronous TCP library for Espressif MCUs + + Copyright (c) 2016 Hristo Gochkov. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ +/* + * Compatibility for AxTLS with LWIP raw tcp mode (http://lwip.wikia.com/wiki/Raw/TCP) + * Original Code and Inspiration: Slavey Karadzhov + */ + +#ifndef LWIPR_COMPAT_H +#define LWIPR_COMPAT_H + +#include + +#if ASYNC_TCP_SSL_ENABLED + +#include "lwipopts.h" +/* + * All those functions will run only if LWIP tcp raw mode is used + */ +#if LWIP_RAW==1 + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include "include/ssl.h" + +#define ERR_TCP_SSL_INVALID_SSL -101 +#define ERR_TCP_SSL_INVALID_TCP -102 +#define ERR_TCP_SSL_INVALID_CLIENTFD -103 +#define ERR_TCP_SSL_INVALID_CLIENTFD_DATA -104 +#define ERR_TCP_SSL_INVALID_DATA -105 + +#define TCP_SSL_TYPE_CLIENT 0 +#define TCP_SSL_TYPE_SERVER 1 + +#define tcp_ssl_ssl_write(A, B, C) tcp_ssl_write(A, B, C) +#define tcp_ssl_ssl_read(A, B) tcp_ssl_read(A, B) + +typedef void (* tcp_ssl_data_cb_t)(void *arg, struct tcp_pcb *tcp, uint8_t * data, size_t len); +typedef void (* tcp_ssl_handshake_cb_t)(void *arg, struct tcp_pcb *tcp, SSL *ssl); +typedef void (* tcp_ssl_error_cb_t)(void *arg, struct tcp_pcb *tcp, int8_t error); +typedef int (* tcp_ssl_file_cb_t)(void *arg, const char *filename, uint8_t **buf); + +uint8_t tcp_ssl_has_client(); + +int tcp_ssl_new_client(struct tcp_pcb *tcp); + +SSL_CTX * tcp_ssl_new_server_ctx(const char *cert, const char *private_key_file, const char *password); +int tcp_ssl_new_server(struct tcp_pcb *tcp, SSL_CTX* ssl_ctx); +int tcp_ssl_is_server(struct tcp_pcb *tcp); + +int tcp_ssl_free(struct tcp_pcb *tcp); +int tcp_ssl_read(struct tcp_pcb *tcp, struct pbuf *p); + +#ifdef AXTLS_2_0_0_SNDBUF +int tcp_ssl_sndbuf(struct tcp_pcb *tcp); +#endif + +int tcp_ssl_write(struct tcp_pcb *tcp, uint8_t *data, size_t len); + +void tcp_ssl_file(tcp_ssl_file_cb_t cb, void * arg); + +void tcp_ssl_arg(struct tcp_pcb *tcp, void * arg); +void tcp_ssl_data(struct tcp_pcb *tcp, tcp_ssl_data_cb_t arg); +void tcp_ssl_handshake(struct tcp_pcb *tcp, tcp_ssl_handshake_cb_t arg); +void tcp_ssl_err(struct tcp_pcb *tcp, tcp_ssl_error_cb_t arg); + +SSL * tcp_ssl_get_ssl(struct tcp_pcb *tcp); +bool tcp_ssl_has(struct tcp_pcb *tcp); + +#ifdef __cplusplus +} +#endif + +#endif /* LWIP_RAW==1 */ + +#endif /* ASYNC_TCP_SSL_ENABLED */ + +#endif /* LWIPR_COMPAT_H */ diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/ssl/gen_server_cert.sh b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/ssl/gen_server_cert.sh new file mode 100644 index 0000000..fd749ed --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/ssl/gen_server_cert.sh @@ -0,0 +1,36 @@ +#!/bin/bash + +cat > ca_cert.conf << EOF +[ req ] +distinguished_name = req_distinguished_name +prompt = no + +[ req_distinguished_name ] + O = Espressif Systems +EOF + +openssl genrsa -out axTLS.ca_key.pem 2048 +openssl req -new -config ./ca_cert.conf -key axTLS.ca_key.pem -out axTLS.ca_x509.req +openssl x509 -req -sha1 -days 5000 -signkey axTLS.ca_key.pem -CAkey axTLS.ca_key.pem -in axTLS.ca_x509.req -out axTLS.ca_x509.pem + +cat > certs.conf << EOF +[ req ] +distinguished_name = req_distinguished_name +prompt = no + +[ req_distinguished_name ] + O = axTLS on ESP8266 + CN = esp8266.local +EOF + +openssl genrsa -out axTLS.key_1024.pem 1024 +openssl req -new -config ./certs.conf -key axTLS.key_1024.pem -out axTLS.x509_1024.req +openssl x509 -req -sha1 -CAcreateserial -days 5000 -CA axTLS.ca_x509.pem -CAkey axTLS.ca_key.pem -in axTLS.x509_1024.req -out axTLS.x509_1024.pem + +openssl rsa -outform DER -in axTLS.key_1024.pem -out axTLS.key_1024 +openssl x509 -outform DER -in axTLS.x509_1024.pem -out axTLS.x509_1024.cer + +cat axTLS.key_1024 > server.key +cat axTLS.x509_1024.cer > server.cer + +rm axTLS.* ca_cert.conf certs.conf diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/ssl/server.cer b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/ssl/server.cer new file mode 100644 index 0000000..b5e5f24 Binary files /dev/null and b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/ssl/server.cer differ diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/ssl/server.key b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/ssl/server.key new file mode 100644 index 0000000..1b7095f Binary files /dev/null and b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncTCP/ssl/server.key differ diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/CMakeLists.txt b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/CMakeLists.txt new file mode 100644 index 0000000..64292ec --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/CMakeLists.txt @@ -0,0 +1,17 @@ +set(COMPONENT_SRCDIRS + "src" +) + +set(COMPONENT_ADD_INCLUDEDIRS + "src" +) + +set(COMPONENT_REQUIRES + "arduino-esp32" + "AsyncTCP" +) + +register_component() + +target_compile_definitions(${COMPONENT_TARGET} PUBLIC -DESP32) +target_compile_options(${COMPONENT_TARGET} PRIVATE -fno-rtti) diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/README.md b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/README.md new file mode 100644 index 0000000..d6dd320 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/README.md @@ -0,0 +1,1521 @@ +# ESPAsyncWebServer +[![Build Status](https://travis-ci.org/me-no-dev/ESPAsyncWebServer.svg?branch=master)](https://travis-ci.org/me-no-dev/ESPAsyncWebServer) ![](https://github.com/me-no-dev/ESPAsyncWebServer/workflows/ESP%20Async%20Web%20Server%20CI/badge.svg) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/395dd42cfc674e6ca2e326af3af80ffc)](https://www.codacy.com/manual/me-no-dev/ESPAsyncWebServer?utm_source=github.com&utm_medium=referral&utm_content=me-no-dev/ESPAsyncWebServer&utm_campaign=Badge_Grade) + +For help and support [![Join the chat at https://gitter.im/me-no-dev/ESPAsyncWebServer](https://badges.gitter.im/me-no-dev/ESPAsyncWebServer.svg)](https://gitter.im/me-no-dev/ESPAsyncWebServer?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) + +Async HTTP and WebSocket Server for ESP8266 Arduino + +For ESP8266 it requires [ESPAsyncTCP](https://github.com/me-no-dev/ESPAsyncTCP) +To use this library you might need to have the latest git versions of [ESP8266](https://github.com/esp8266/Arduino) Arduino Core + +For ESP32 it requires [AsyncTCP](https://github.com/me-no-dev/AsyncTCP) to work +To use this library you might need to have the latest git versions of [ESP32](https://github.com/espressif/arduino-esp32) Arduino Core + +## Table of contents +- [ESPAsyncWebServer](#espasyncwebserver) + - [Table of contents](#table-of-contents) + - [Installation](#installation) + - [Using PlatformIO](#using-platformio) + - [Why should you care](#why-should-you-care) + - [Important things to remember](#important-things-to-remember) + - [Principles of operation](#principles-of-operation) + - [The Async Web server](#the-async-web-server) + - [Request Life Cycle](#request-life-cycle) + - [Rewrites and how do they work](#rewrites-and-how-do-they-work) + - [Handlers and how do they work](#handlers-and-how-do-they-work) + - [Responses and how do they work](#responses-and-how-do-they-work) + - [Template processing](#template-processing) + - [Libraries and projects that use AsyncWebServer](#libraries-and-projects-that-use-asyncwebserver) + - [Request Variables](#request-variables) + - [Common Variables](#common-variables) + - [Headers](#headers) + - [GET, POST and FILE parameters](#get-post-and-file-parameters) + - [FILE Upload handling](#file-upload-handling) + - [Body data handling](#body-data-handling) + - [JSON body handling with ArduinoJson](#json-body-handling-with-arduinojson) + - [Responses](#responses) + - [Redirect to another URL](#redirect-to-another-url) + - [Basic response with HTTP Code](#basic-response-with-http-code) + - [Basic response with HTTP Code and extra headers](#basic-response-with-http-code-and-extra-headers) + - [Basic response with string content](#basic-response-with-string-content) + - [Basic response with string content and extra headers](#basic-response-with-string-content-and-extra-headers) + - [Send large webpage from PROGMEM](#send-large-webpage-from-progmem) + - [Send large webpage from PROGMEM and extra headers](#send-large-webpage-from-progmem-and-extra-headers) + - [Send large webpage from PROGMEM containing templates](#send-large-webpage-from-progmem-containing-templates) + - [Send large webpage from PROGMEM containing templates and extra headers](#send-large-webpage-from-progmem-containing-templates-and-extra-headers) + - [Send binary content from PROGMEM](#send-binary-content-from-progmem) + - [Respond with content coming from a Stream](#respond-with-content-coming-from-a-stream) + - [Respond with content coming from a Stream and extra headers](#respond-with-content-coming-from-a-stream-and-extra-headers) + - [Respond with content coming from a Stream containing templates](#respond-with-content-coming-from-a-stream-containing-templates) + - [Respond with content coming from a Stream containing templates and extra headers](#respond-with-content-coming-from-a-stream-containing-templates-and-extra-headers) + - [Respond with content coming from a File](#respond-with-content-coming-from-a-file) + - [Respond with content coming from a File and extra headers](#respond-with-content-coming-from-a-file-and-extra-headers) + - [Respond with content coming from a File containing templates](#respond-with-content-coming-from-a-file-containing-templates) + - [Respond with content using a callback](#respond-with-content-using-a-callback) + - [Respond with content using a callback and extra headers](#respond-with-content-using-a-callback-and-extra-headers) + - [Respond with content using a callback containing templates](#respond-with-content-using-a-callback-containing-templates) + - [Respond with content using a callback containing templates and extra headers](#respond-with-content-using-a-callback-containing-templates-and-extra-headers) + - [Chunked Response](#chunked-response) + - [Chunked Response containing templates](#chunked-response-containing-templates) + - [Print to response](#print-to-response) + - [ArduinoJson Basic Response](#arduinojson-basic-response) + - [ArduinoJson Advanced Response](#arduinojson-advanced-response) + - [Serving static files](#serving-static-files) + - [Serving specific file by name](#serving-specific-file-by-name) + - [Serving files in directory](#serving-files-in-directory) + - [Serving static files with authentication](#serving-static-files-with-authentication) + - [Specifying Cache-Control header](#specifying-cache-control-header) + - [Specifying Date-Modified header](#specifying-date-modified-header) + - [Specifying Template Processor callback](#specifying-template-processor-callback) + - [Param Rewrite With Matching](#param-rewrite-with-matching) + - [Using filters](#using-filters) + - [Serve different site files in AP mode](#serve-different-site-files-in-ap-mode) + - [Rewrite to different index on AP](#rewrite-to-different-index-on-ap) + - [Serving different hosts](#serving-different-hosts) + - [Determine interface inside callbacks](#determine-interface-inside-callbacks) + - [Bad Responses](#bad-responses) + - [Respond with content using a callback without content length to HTTP/1.0 clients](#respond-with-content-using-a-callback-without-content-length-to-http10-clients) + - [Async WebSocket Plugin](#async-websocket-plugin) + - [Async WebSocket Event](#async-websocket-event) + - [Methods for sending data to a socket client](#methods-for-sending-data-to-a-socket-client) + - [Direct access to web socket message buffer](#direct-access-to-web-socket-message-buffer) + - [Limiting the number of web socket clients](#limiting-the-number-of-web-socket-clients) + - [Async Event Source Plugin](#async-event-source-plugin) + - [Setup Event Source on the server](#setup-event-source-on-the-server) + - [Setup Event Source in the browser](#setup-event-source-in-the-browser) + - [Scanning for available WiFi Networks](#scanning-for-available-wifi-networks) + - [Remove handlers and rewrites](#remove-handlers-and-rewrites) + - [Setting up the server](#setting-up-the-server) + - [Setup global and class functions as request handlers](#setup-global-and-class-functions-as-request-handlers) + - [Methods for controlling websocket connections](#methods-for-controlling-websocket-connections) + - [Adding Default Headers](#adding-default-headers) + - [Path variable](#path-variable) + +## Installation + +### Using PlatformIO + +[PlatformIO](http://platformio.org) is an open source ecosystem for IoT development with cross platform build system, library manager and full support for Espressif ESP8266/ESP32 development. It works on the popular host OS: Mac OS X, Windows, Linux 32/64, Linux ARM (like Raspberry Pi, BeagleBone, CubieBoard). + +1. Install [PlatformIO IDE](http://platformio.org/platformio-ide) +2. Create new project using "PlatformIO Home > New Project" +3. Update dev/platform to staging version: + - [Instruction for Espressif 8266](http://docs.platformio.org/en/latest/platforms/espressif8266.html#using-arduino-framework-with-staging-version) + - [Instruction for Espressif 32](http://docs.platformio.org/en/latest/platforms/espressif32.html#using-arduino-framework-with-staging-version) + 4. Add "ESP Async WebServer" to project using [Project Configuration File `platformio.ini`](http://docs.platformio.org/page/projectconf.html) and [lib_deps](http://docs.platformio.org/page/projectconf/section_env_library.html#lib-deps) option: + +```ini +[env:myboard] +platform = espressif... +board = ... +framework = arduino + +# using the latest stable version +lib_deps = ESP Async WebServer + +# or using GIT Url (the latest development version) +lib_deps = https://github.com/me-no-dev/ESPAsyncWebServer.git +``` + 5. Happy coding with PlatformIO! + +## Why should you care +- Using asynchronous network means that you can handle more than one connection at the same time +- You are called once the request is ready and parsed +- When you send the response, you are immediately ready to handle other connections + while the server is taking care of sending the response in the background +- Speed is OMG +- Easy to use API, HTTP Basic and Digest MD5 Authentication (default), ChunkedResponse +- Easily extendible to handle any type of content +- Supports Continue 100 +- Async WebSocket plugin offering different locations without extra servers or ports +- Async EventSource (Server-Sent Events) plugin to send events to the browser +- URL Rewrite plugin for conditional and permanent url rewrites +- ServeStatic plugin that supports cache, Last-Modified, default index and more +- Simple template processing engine to handle templates + +## Important things to remember +- This is fully asynchronous server and as such does not run on the loop thread. +- You can not use yield or delay or any function that uses them inside the callbacks +- The server is smart enough to know when to close the connection and free resources +- You can not send more than one response to a single request + +## Principles of operation + +### The Async Web server +- Listens for connections +- Wraps the new clients into ```Request``` +- Keeps track of clients and cleans memory +- Manages ```Rewrites``` and apply them on the request url +- Manages ```Handlers``` and attaches them to Requests + +### Request Life Cycle +- TCP connection is received by the server +- The connection is wrapped inside ```Request``` object +- When the request head is received (type, url, get params, http version and host), + the server goes through all ```Rewrites``` (in the order they were added) to rewrite the url and inject query parameters, + next, it goes through all attached ```Handlers```(in the order they were added) trying to find one + that ```canHandle``` the given request. If none are found, the default(catch-all) handler is attached. +- The rest of the request is received, calling the ```handleUpload``` or ```handleBody``` methods of the ```Handler``` if they are needed (POST+File/Body) +- When the whole request is parsed, the result is given to the ```handleRequest``` method of the ```Handler``` and is ready to be responded to +- In the ```handleRequest``` method, to the ```Request``` is attached a ```Response``` object (see below) that will serve the response data back to the client +- When the ```Response``` is sent, the client is closed and freed from the memory + +### Rewrites and how do they work +- The ```Rewrites``` are used to rewrite the request url and/or inject get parameters for a specific request url path. +- All ```Rewrites``` are evaluated on the request in the order they have been added to the server. +- The ```Rewrite``` will change the request url only if the request url (excluding get parameters) is fully match + the rewrite url, and when the optional ```Filter``` callback return true. +- Setting a ```Filter``` to the ```Rewrite``` enables to control when to apply the rewrite, decision can be based on + request url, http version, request host/port/target host, get parameters or the request client's localIP or remoteIP. +- Two filter callbacks are provided: ```ON_AP_FILTER``` to execute the rewrite when request is made to the AP interface, + ```ON_STA_FILTER``` to execute the rewrite when request is made to the STA interface. +- The ```Rewrite``` can specify a target url with optional get parameters, e.g. ```/to-url?with=params``` + +### Handlers and how do they work +- The ```Handlers``` are used for executing specific actions to particular requests +- One ```Handler``` instance can be attached to any request and lives together with the server +- Setting a ```Filter``` to the ```Handler``` enables to control when to apply the handler, decision can be based on + request url, http version, request host/port/target host, get parameters or the request client's localIP or remoteIP. +- Two filter callbacks are provided: ```ON_AP_FILTER``` to execute the rewrite when request is made to the AP interface, + ```ON_STA_FILTER``` to execute the rewrite when request is made to the STA interface. +- The ```canHandle``` method is used for handler specific control on whether the requests can be handled + and for declaring any interesting headers that the ```Request``` should parse. Decision can be based on request + method, request url, http version, request host/port/target host and get parameters +- Once a ```Handler``` is attached to given ```Request``` (```canHandle``` returned true) + that ```Handler``` takes care to receive any file/data upload and attach a ```Response``` + once the ```Request``` has been fully parsed +- ```Handlers``` are evaluated in the order they are attached to the server. The ```canHandle``` is called only + if the ```Filter``` that was set to the ```Handler``` return true. +- The first ```Handler``` that can handle the request is selected, not further ```Filter``` and ```canHandle``` are called. + +### Responses and how do they work +- The ```Response``` objects are used to send the response data back to the client +- The ```Response``` object lives with the ```Request``` and is freed on end or disconnect +- Different techniques are used depending on the response type to send the data in packets + returning back almost immediately and sending the next packet when this one is received. + Any time in between is spent to run the user loop and handle other network packets +- Responding asynchronously is probably the most difficult thing for most to understand +- Many different options exist for the user to make responding a background task + +### Template processing +- ESPAsyncWebserver contains simple template processing engine. +- Template processing can be added to most response types. +- Currently it supports only replacing template placeholders with actual values. No conditional processing, cycles, etc. +- Placeholders are delimited with ```%``` symbols. Like this: ```%TEMPLATE_PLACEHOLDER%```. +- It works by extracting placeholder name from response text and passing it to user provided function which should return actual value to be used instead of placeholder. +- Since it's user provided function, it is possible for library users to implement conditional processing and cycles themselves. +- Since it's impossible to know the actual response size after template processing step in advance (and, therefore, to include it in response headers), the response becomes [chunked](#chunked-response). + +## Libraries and projects that use AsyncWebServer +- [WebSocketToSerial](https://github.com/hallard/WebSocketToSerial) - Debug serial devices through the web browser +- [Sattrack](https://github.com/Hopperpop/Sattrack) - Track the ISS with ESP8266 +- [ESP Radio](https://github.com/Edzelf/Esp-radio) - Icecast radio based on ESP8266 and VS1053 +- [VZero](https://github.com/andig/vzero) - the Wireless zero-config controller for volkszaehler.org +- [ESPurna](https://bitbucket.org/xoseperez/espurna) - ESPurna ("spark" in Catalan) is a custom C firmware for ESP8266 based smart switches. It was originally developed with the ITead Sonoff in mind. +- [fauxmoESP](https://bitbucket.org/xoseperez/fauxmoesp) - Belkin WeMo emulator library for ESP8266. +- [ESP-RFID](https://github.com/omersiar/esp-rfid) - MFRC522 RFID Access Control Management project for ESP8266. + +## Request Variables + +### Common Variables +```cpp +request->version(); // uint8_t: 0 = HTTP/1.0, 1 = HTTP/1.1 +request->method(); // enum: HTTP_GET, HTTP_POST, HTTP_DELETE, HTTP_PUT, HTTP_PATCH, HTTP_HEAD, HTTP_OPTIONS +request->url(); // String: URL of the request (not including host, port or GET parameters) +request->host(); // String: The requested host (can be used for virtual hosting) +request->contentType(); // String: ContentType of the request (not avaiable in Handler::canHandle) +request->contentLength(); // size_t: ContentLength of the request (not avaiable in Handler::canHandle) +request->multipart(); // bool: True if the request has content type "multipart" +``` + +### Headers +```cpp +//List all collected headers +int headers = request->headers(); +int i; +for(i=0;igetHeader(i); + Serial.printf("HEADER[%s]: %s\n", h->name().c_str(), h->value().c_str()); +} + +//get specific header by name +if(request->hasHeader("MyHeader")){ + AsyncWebHeader* h = request->getHeader("MyHeader"); + Serial.printf("MyHeader: %s\n", h->value().c_str()); +} + +//List all collected headers (Compatibility) +int headers = request->headers(); +int i; +for(i=0;iheaderName(i).c_str(), request->header(i).c_str()); +} + +//get specific header by name (Compatibility) +if(request->hasHeader("MyHeader")){ + Serial.printf("MyHeader: %s\n", request->header("MyHeader").c_str()); +} +``` + +### GET, POST and FILE parameters +```cpp +//List all parameters +int params = request->params(); +for(int i=0;igetParam(i); + if(p->isFile()){ //p->isPost() is also true + Serial.printf("FILE[%s]: %s, size: %u\n", p->name().c_str(), p->value().c_str(), p->size()); + } else if(p->isPost()){ + Serial.printf("POST[%s]: %s\n", p->name().c_str(), p->value().c_str()); + } else { + Serial.printf("GET[%s]: %s\n", p->name().c_str(), p->value().c_str()); + } +} + +//Check if GET parameter exists +if(request->hasParam("download")) + AsyncWebParameter* p = request->getParam("download"); + +//Check if POST (but not File) parameter exists +if(request->hasParam("download", true)) + AsyncWebParameter* p = request->getParam("download", true); + +//Check if FILE was uploaded +if(request->hasParam("download", true, true)) + AsyncWebParameter* p = request->getParam("download", true, true); + +//List all parameters (Compatibility) +int args = request->args(); +for(int i=0;iargName(i).c_str(), request->arg(i).c_str()); +} + +//Check if parameter exists (Compatibility) +if(request->hasArg("download")) + String arg = request->arg("download"); +``` + +### FILE Upload handling +```cpp +void handleUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final){ + if(!index){ + Serial.printf("UploadStart: %s\n", filename.c_str()); + } + for(size_t i=0; i(); + // ... +}); +server.addHandler(handler); +``` + +## Responses +### Redirect to another URL +```cpp +//to local url +request->redirect("/login"); + +//to external url +request->redirect("http://esp8266.com"); +``` + +### Basic response with HTTP Code +```cpp +request->send(404); //Sends 404 File Not Found +``` + +### Basic response with HTTP Code and extra headers +```cpp +AsyncWebServerResponse *response = request->beginResponse(404); //Sends 404 File Not Found +response->addHeader("Server","ESP Async Web Server"); +request->send(response); +``` + +### Basic response with string content +```cpp +request->send(200, "text/plain", "Hello World!"); +``` + +### Basic response with string content and extra headers +```cpp +AsyncWebServerResponse *response = request->beginResponse(200, "text/plain", "Hello World!"); +response->addHeader("Server","ESP Async Web Server"); +request->send(response); +``` + +### Send large webpage from PROGMEM +```cpp +const char index_html[] PROGMEM = "..."; // large char array, tested with 14k +request->send_P(200, "text/html", index_html); +``` + +### Send large webpage from PROGMEM and extra headers +```cpp +const char index_html[] PROGMEM = "..."; // large char array, tested with 14k +AsyncWebServerResponse *response = request->beginResponse_P(200, "text/html", index_html); +response->addHeader("Server","ESP Async Web Server"); +request->send(response); +``` + +### Send large webpage from PROGMEM containing templates +```cpp +String processor(const String& var) +{ + if(var == "HELLO_FROM_TEMPLATE") + return F("Hello world!"); + return String(); +} + +// ... + +const char index_html[] PROGMEM = "..."; // large char array, tested with 14k +request->send_P(200, "text/html", index_html, processor); +``` + +### Send large webpage from PROGMEM containing templates and extra headers +```cpp +String processor(const String& var) +{ + if(var == "HELLO_FROM_TEMPLATE") + return F("Hello world!"); + return String(); +} + +// ... + +const char index_html[] PROGMEM = "..."; // large char array, tested with 14k +AsyncWebServerResponse *response = request->beginResponse_P(200, "text/html", index_html, processor); +response->addHeader("Server","ESP Async Web Server"); +request->send(response); +``` + +### Send binary content from PROGMEM +```cpp + +//File: favicon.ico.gz, Size: 726 +#define favicon_ico_gz_len 726 +const uint8_t favicon_ico_gz[] PROGMEM = { + 0x1F, 0x8B, 0x08, 0x08, 0x0B, 0x87, 0x90, 0x57, 0x00, 0x03, 0x66, 0x61, 0x76, 0x69, 0x63, 0x6F, + 0x6E, 0x2E, 0x69, 0x63, 0x6F, 0x00, 0xCD, 0x53, 0x5F, 0x48, 0x9A, 0x51, 0x14, 0xBF, 0x62, 0x6D, + 0x86, 0x96, 0xA9, 0x64, 0xD3, 0xFE, 0xA8, 0x99, 0x65, 0x1A, 0xB4, 0x8A, 0xA8, 0x51, 0x54, 0x23, + 0xA8, 0x11, 0x49, 0x51, 0x8A, 0x34, 0x62, 0x93, 0x85, 0x31, 0x58, 0x44, 0x12, 0x45, 0x2D, 0x58, + 0xF5, 0x52, 0x41, 0x10, 0x23, 0x82, 0xA0, 0x20, 0x98, 0x2F, 0xC1, 0x26, 0xED, 0xA1, 0x20, 0x89, + 0x04, 0xD7, 0x83, 0x58, 0x20, 0x28, 0x04, 0xAB, 0xD1, 0x9B, 0x8C, 0xE5, 0xC3, 0x60, 0x32, 0x64, + 0x0E, 0x56, 0xBF, 0x9D, 0xEF, 0xF6, 0x30, 0x82, 0xED, 0xAD, 0x87, 0xDD, 0x8F, 0xF3, 0xDD, 0x8F, + 0x73, 0xCF, 0xEF, 0x9C, 0xDF, 0x39, 0xBF, 0xFB, 0x31, 0x26, 0xA2, 0x27, 0x37, 0x97, 0xD1, 0x5B, + 0xCF, 0x9E, 0x67, 0x30, 0xA6, 0x66, 0x8C, 0x99, 0xC9, 0xC8, 0x45, 0x9E, 0x6B, 0x3F, 0x5F, 0x74, + 0xA6, 0x94, 0x5E, 0xDB, 0xFF, 0xB2, 0xE6, 0xE7, 0xE7, 0xF9, 0xDE, 0xD6, 0xD6, 0x96, 0xDB, 0xD8, + 0xD8, 0x78, 0xBF, 0xA1, 0xA1, 0xC1, 0xDA, 0xDC, 0xDC, 0x2C, 0xEB, 0xED, 0xED, 0x15, 0x9B, 0xCD, + 0xE6, 0x4A, 0x83, 0xC1, 0xE0, 0x2E, 0x29, 0x29, 0x99, 0xD6, 0x6A, 0xB5, 0x4F, 0x75, 0x3A, 0x9D, + 0x61, 0x75, 0x75, 0x95, 0xB5, 0xB7, 0xB7, 0xDF, 0xC8, 0xD1, 0xD4, 0xD4, 0xF4, 0xB0, 0xBA, 0xBA, + 0xFA, 0x83, 0xD5, 0x6A, 0xFD, 0x5A, 0x5E, 0x5E, 0x9E, 0x28, 0x2D, 0x2D, 0x0D, 0x10, 0xC6, 0x4B, + 0x98, 0x78, 0x5E, 0x5E, 0xDE, 0x95, 0x42, 0xA1, 0x40, 0x4E, 0x4E, 0xCE, 0x65, 0x76, 0x76, 0xF6, + 0x47, 0xB5, 0x5A, 0x6D, 0x4F, 0x26, 0x93, 0xA2, 0xD6, 0xD6, 0x56, 0x8E, 0x6D, 0x69, 0x69, 0xD1, + 0x11, 0x36, 0x62, 0xB1, 0x58, 0x60, 0x32, 0x99, 0xA0, 0xD7, 0xEB, 0x51, 0x58, 0x58, 0x88, 0xFC, + 0xFC, 0x7C, 0x10, 0x16, 0x02, 0x56, 0x2E, 0x97, 0x43, 0x2A, 0x95, 0x42, 0x2C, 0x16, 0x23, 0x33, + 0x33, 0x33, 0xAE, 0x52, 0xA9, 0x1E, 0x64, 0x65, 0x65, 0x71, 0x7C, 0x7D, 0x7D, 0xBD, 0x93, 0xEA, + 0xFE, 0x30, 0x1A, 0x8D, 0xE8, 0xEC, 0xEC, 0xC4, 0xE2, 0xE2, 0x22, 0x6A, 0x6A, 0x6A, 0x40, 0x39, + 0x41, 0xB5, 0x38, 0x4E, 0xC8, 0x33, 0x3C, 0x3C, 0x0C, 0x87, 0xC3, 0xC1, 0x6B, 0x54, 0x54, 0x54, + 0xBC, 0xE9, 0xEB, 0xEB, 0x93, 0x5F, 0x5C, 0x5C, 0x30, 0x8A, 0x9D, 0x2E, 0x2B, 0x2B, 0xBB, 0xA2, + 0x3E, 0x41, 0xBD, 0x21, 0x1E, 0x8F, 0x63, 0x6A, 0x6A, 0x0A, 0x81, 0x40, 0x00, 0x94, 0x1B, 0x3D, + 0x3D, 0x3D, 0x42, 0x3C, 0x96, 0x96, 0x96, 0x70, 0x7E, 0x7E, 0x8E, 0xE3, 0xE3, 0x63, 0xF8, 0xFD, + 0xFE, 0xB4, 0xD7, 0xEB, 0xF5, 0x8F, 0x8F, 0x8F, 0x5B, 0x68, 0x5E, 0x6F, 0x05, 0xCE, 0xB4, 0xE3, + 0xE8, 0xE8, 0x08, 0x27, 0x27, 0x27, 0xD8, 0xDF, 0xDF, 0xC7, 0xD9, 0xD9, 0x19, 0x6C, 0x36, 0x1B, + 0x36, 0x36, 0x36, 0x38, 0x9F, 0x85, 0x85, 0x05, 0xAC, 0xAF, 0xAF, 0x23, 0x1A, 0x8D, 0x22, 0x91, + 0x48, 0x20, 0x16, 0x8B, 0xFD, 0xDA, 0xDA, 0xDA, 0x7A, 0x41, 0x33, 0x7E, 0x57, 0x50, 0x50, 0x80, + 0x89, 0x89, 0x09, 0x84, 0xC3, 0x61, 0x6C, 0x6F, 0x6F, 0x23, 0x12, 0x89, 0xE0, 0xE0, 0xE0, 0x00, + 0x43, 0x43, 0x43, 0x58, 0x5E, 0x5E, 0xE6, 0x9C, 0x7D, 0x3E, 0x1F, 0x46, 0x47, 0x47, 0x79, 0xBE, + 0xBD, 0xBD, 0x3D, 0xE1, 0x3C, 0x1D, 0x0C, 0x06, 0x9F, 0x10, 0xB7, 0xC7, 0x84, 0x4F, 0xF6, 0xF7, + 0xF7, 0x63, 0x60, 0x60, 0x00, 0x83, 0x83, 0x83, 0x18, 0x19, 0x19, 0xC1, 0xDC, 0xDC, 0x1C, 0x8F, + 0x17, 0x7C, 0xA4, 0x27, 0xE7, 0x34, 0x39, 0x39, 0x89, 0x9D, 0x9D, 0x1D, 0x6E, 0x54, 0xE3, 0x13, + 0xE5, 0x34, 0x11, 0x37, 0x49, 0x51, 0x51, 0xD1, 0x4B, 0xA5, 0x52, 0xF9, 0x45, 0x26, 0x93, 0x5D, + 0x0A, 0xF3, 0x92, 0x48, 0x24, 0xA0, 0x6F, 0x14, 0x17, 0x17, 0xA3, 0xB6, 0xB6, 0x16, 0x5D, 0x5D, + 0x5D, 0x7C, 0x1E, 0xBB, 0xBB, 0xBB, 0x9C, 0xD7, 0xE1, 0xE1, 0x21, 0x42, 0xA1, 0xD0, 0x6B, 0xD2, + 0x45, 0x4C, 0x33, 0x12, 0x34, 0xCC, 0xA0, 0x19, 0x54, 0x92, 0x56, 0x0E, 0xD2, 0xD9, 0x43, 0xF8, + 0xCF, 0x82, 0x56, 0xC2, 0xDC, 0xEB, 0xEA, 0xEA, 0x38, 0x7E, 0x6C, 0x6C, 0x4C, 0xE0, 0xFE, 0x9D, + 0xB8, 0xBF, 0xA7, 0xFA, 0xAF, 0x56, 0x56, 0x56, 0xEE, 0x6D, 0x6E, 0x6E, 0xDE, 0xB8, 0x47, 0x55, + 0x55, 0x55, 0x6C, 0x66, 0x66, 0x46, 0x44, 0xDA, 0x3B, 0x34, 0x1A, 0x4D, 0x94, 0xB0, 0x3F, 0x09, + 0x7B, 0x45, 0xBD, 0xA5, 0x5D, 0x2E, 0x57, 0x8C, 0x7A, 0x73, 0xD9, 0xED, 0xF6, 0x3B, 0x84, 0xFF, + 0xE7, 0x7D, 0xA6, 0x3A, 0x2C, 0x95, 0x4A, 0xB1, 0x8E, 0x8E, 0x0E, 0x6D, 0x77, 0x77, 0xB7, 0xCD, + 0xE9, 0x74, 0x3E, 0x73, 0xBB, 0xDD, 0x8F, 0x3C, 0x1E, 0x8F, 0xE6, 0xF4, 0xF4, 0x94, 0xAD, 0xAD, + 0xAD, 0xDD, 0xDE, 0xCF, 0x73, 0x0B, 0x0B, 0xB8, 0xB6, 0xE0, 0x5D, 0xC6, 0x66, 0xC5, 0xE4, 0x10, + 0x4C, 0xF4, 0xF7, 0xD8, 0x59, 0xF2, 0x7F, 0xA3, 0xB8, 0xB4, 0xFC, 0x0F, 0xEE, 0x37, 0x70, 0xEC, + 0x16, 0x4A, 0x7E, 0x04, 0x00, 0x00 +}; + +AsyncWebServerResponse *response = request->beginResponse_P(200, "image/x-icon", favicon_ico_gz, favicon_ico_gz_len); +response->addHeader("Content-Encoding", "gzip"); +request->send(response); +``` + +### Respond with content coming from a Stream +```cpp +//read 12 bytes from Serial and send them as Content Type text/plain +request->send(Serial, "text/plain", 12); +``` + +### Respond with content coming from a Stream and extra headers +```cpp +//read 12 bytes from Serial and send them as Content Type text/plain +AsyncWebServerResponse *response = request->beginResponse(Serial, "text/plain", 12); +response->addHeader("Server","ESP Async Web Server"); +request->send(response); +``` + +### Respond with content coming from a Stream containing templates +```cpp +String processor(const String& var) +{ + if(var == "HELLO_FROM_TEMPLATE") + return F("Hello world!"); + return String(); +} + +// ... + +//read 12 bytes from Serial and send them as Content Type text/plain +request->send(Serial, "text/plain", 12, processor); +``` + +### Respond with content coming from a Stream containing templates and extra headers +```cpp +String processor(const String& var) +{ + if(var == "HELLO_FROM_TEMPLATE") + return F("Hello world!"); + return String(); +} + +// ... + +//read 12 bytes from Serial and send them as Content Type text/plain +AsyncWebServerResponse *response = request->beginResponse(Serial, "text/plain", 12, processor); +response->addHeader("Server","ESP Async Web Server"); +request->send(response); +``` + +### Respond with content coming from a File +```cpp +//Send index.htm with default content type +request->send(SPIFFS, "/index.htm"); + +//Send index.htm as text +request->send(SPIFFS, "/index.htm", "text/plain"); + +//Download index.htm +request->send(SPIFFS, "/index.htm", String(), true); +``` + +### Respond with content coming from a File and extra headers +```cpp +//Send index.htm with default content type +AsyncWebServerResponse *response = request->beginResponse(SPIFFS, "/index.htm"); + +//Send index.htm as text +AsyncWebServerResponse *response = request->beginResponse(SPIFFS, "/index.htm", "text/plain"); + +//Download index.htm +AsyncWebServerResponse *response = request->beginResponse(SPIFFS, "/index.htm", String(), true); + +response->addHeader("Server","ESP Async Web Server"); +request->send(response); +``` + +### Respond with content coming from a File containing templates +Internally uses [Chunked Response](#chunked-response). + +Index.htm contents: +``` +%HELLO_FROM_TEMPLATE% +``` + +Somewhere in source files: +```cpp +String processor(const String& var) +{ + if(var == "HELLO_FROM_TEMPLATE") + return F("Hello world!"); + return String(); +} + +// ... + +//Send index.htm with template processor function +request->send(SPIFFS, "/index.htm", String(), false, processor); +``` + +### Respond with content using a callback +```cpp +//send 128 bytes as plain text +request->send("text/plain", 128, [](uint8_t *buffer, size_t maxLen, size_t index) -> size_t { + //Write up to "maxLen" bytes into "buffer" and return the amount written. + //index equals the amount of bytes that have been already sent + //You will not be asked for more bytes once the content length has been reached. + //Keep in mind that you can not delay or yield waiting for more data! + //Send what you currently have and you will be asked for more again + return mySource.read(buffer, maxLen); +}); +``` + +### Respond with content using a callback and extra headers +```cpp +//send 128 bytes as plain text +AsyncWebServerResponse *response = request->beginResponse("text/plain", 128, [](uint8_t *buffer, size_t maxLen, size_t index) -> size_t { + //Write up to "maxLen" bytes into "buffer" and return the amount written. + //index equals the amount of bytes that have been already sent + //You will not be asked for more bytes once the content length has been reached. + //Keep in mind that you can not delay or yield waiting for more data! + //Send what you currently have and you will be asked for more again + return mySource.read(buffer, maxLen); +}); +response->addHeader("Server","ESP Async Web Server"); +request->send(response); +``` + +### Respond with content using a callback containing templates +```cpp +String processor(const String& var) +{ + if(var == "HELLO_FROM_TEMPLATE") + return F("Hello world!"); + return String(); +} + +// ... + +//send 128 bytes as plain text +request->send("text/plain", 128, [](uint8_t *buffer, size_t maxLen, size_t index) -> size_t { + //Write up to "maxLen" bytes into "buffer" and return the amount written. + //index equals the amount of bytes that have been already sent + //You will not be asked for more bytes once the content length has been reached. + //Keep in mind that you can not delay or yield waiting for more data! + //Send what you currently have and you will be asked for more again + return mySource.read(buffer, maxLen); +}, processor); +``` + +### Respond with content using a callback containing templates and extra headers +```cpp +String processor(const String& var) +{ + if(var == "HELLO_FROM_TEMPLATE") + return F("Hello world!"); + return String(); +} + +// ... + +//send 128 bytes as plain text +AsyncWebServerResponse *response = request->beginResponse("text/plain", 128, [](uint8_t *buffer, size_t maxLen, size_t index) -> size_t { + //Write up to "maxLen" bytes into "buffer" and return the amount written. + //index equals the amount of bytes that have been already sent + //You will not be asked for more bytes once the content length has been reached. + //Keep in mind that you can not delay or yield waiting for more data! + //Send what you currently have and you will be asked for more again + return mySource.read(buffer, maxLen); +}, processor); +response->addHeader("Server","ESP Async Web Server"); +request->send(response); +``` + +### Chunked Response +Used when content length is unknown. Works best if the client supports HTTP/1.1 +```cpp +AsyncWebServerResponse *response = request->beginChunkedResponse("text/plain", [](uint8_t *buffer, size_t maxLen, size_t index) -> size_t { + //Write up to "maxLen" bytes into "buffer" and return the amount written. + //index equals the amount of bytes that have been already sent + //You will be asked for more data until 0 is returned + //Keep in mind that you can not delay or yield waiting for more data! + return mySource.read(buffer, maxLen); +}); +response->addHeader("Server","ESP Async Web Server"); +request->send(response); +``` + +### Chunked Response containing templates +Used when content length is unknown. Works best if the client supports HTTP/1.1 +```cpp +String processor(const String& var) +{ + if(var == "HELLO_FROM_TEMPLATE") + return F("Hello world!"); + return String(); +} + +// ... + +AsyncWebServerResponse *response = request->beginChunkedResponse("text/plain", [](uint8_t *buffer, size_t maxLen, size_t index) -> size_t { + //Write up to "maxLen" bytes into "buffer" and return the amount written. + //index equals the amount of bytes that have been already sent + //You will be asked for more data until 0 is returned + //Keep in mind that you can not delay or yield waiting for more data! + return mySource.read(buffer, maxLen); +}, processor); +response->addHeader("Server","ESP Async Web Server"); +request->send(response); +``` + +### Print to response +```cpp +AsyncResponseStream *response = request->beginResponseStream("text/html"); +response->addHeader("Server","ESP Async Web Server"); +response->printf("Webpage at %s", request->url().c_str()); + +response->print("

Hello "); +response->print(request->client()->remoteIP()); +response->print("

"); + +response->print("

General

"); +response->print("
    "); +response->printf("
  • Version: HTTP/1.%u
  • ", request->version()); +response->printf("
  • Method: %s
  • ", request->methodToString()); +response->printf("
  • URL: %s
  • ", request->url().c_str()); +response->printf("
  • Host: %s
  • ", request->host().c_str()); +response->printf("
  • ContentType: %s
  • ", request->contentType().c_str()); +response->printf("
  • ContentLength: %u
  • ", request->contentLength()); +response->printf("
  • Multipart: %s
  • ", request->multipart()?"true":"false"); +response->print("
"); + +response->print("

Headers

"); +response->print("
    "); +int headers = request->headers(); +for(int i=0;igetHeader(i); + response->printf("
  • %s: %s
  • ", h->name().c_str(), h->value().c_str()); +} +response->print("
"); + +response->print("

Parameters

"); +response->print("
    "); +int params = request->params(); +for(int i=0;igetParam(i); + if(p->isFile()){ + response->printf("
  • FILE[%s]: %s, size: %u
  • ", p->name().c_str(), p->value().c_str(), p->size()); + } else if(p->isPost()){ + response->printf("
  • POST[%s]: %s
  • ", p->name().c_str(), p->value().c_str()); + } else { + response->printf("
  • GET[%s]: %s
  • ", p->name().c_str(), p->value().c_str()); + } +} +response->print("
"); + +response->print(""); +//send the response last +request->send(response); +``` + +### ArduinoJson Basic Response +This way of sending Json is great for when the result is below 4KB +```cpp +#include "AsyncJson.h" +#include "ArduinoJson.h" + + +AsyncResponseStream *response = request->beginResponseStream("application/json"); +DynamicJsonBuffer jsonBuffer; +JsonObject &root = jsonBuffer.createObject(); +root["heap"] = ESP.getFreeHeap(); +root["ssid"] = WiFi.SSID(); +root.printTo(*response); +request->send(response); +``` + +### ArduinoJson Advanced Response +This response can handle really large Json objects (tested to 40KB) +There isn't any noticeable speed decrease for small results with the method above +Since ArduinoJson does not allow reading parts of the string, the whole Json has to +be passed every time a chunks needs to be sent, which shows speed decrease proportional +to the resulting json packets +```cpp +#include "AsyncJson.h" +#include "ArduinoJson.h" + + +AsyncJsonResponse * response = new AsyncJsonResponse(); +response->addHeader("Server","ESP Async Web Server"); +JsonObject& root = response->getRoot(); +root["heap"] = ESP.getFreeHeap(); +root["ssid"] = WiFi.SSID(); +response->setLength(); +request->send(response); +``` + +## Serving static files +In addition to serving files from SPIFFS as described above, the server provide a dedicated handler that optimize the +performance of serving files from SPIFFS - ```AsyncStaticWebHandler```. Use ```server.serveStatic()``` function to +initialize and add a new instance of ```AsyncStaticWebHandler``` to the server. +The Handler will not handle the request if the file does not exists, e.g. the server will continue to look for another +handler that can handle the request. +Notice that you can chain setter functions to setup the handler, or keep a pointer to change it at a later time. + +### Serving specific file by name +```cpp +// Serve the file "/www/page.htm" when request url is "/page.htm" +server.serveStatic("/page.htm", SPIFFS, "/www/page.htm"); +``` + +### Serving files in directory +To serve files in a directory, the path to the files should specify a directory in SPIFFS and ends with "/". +```cpp +// Serve files in directory "/www/" when request url starts with "/" +// Request to the root or none existing files will try to server the defualt +// file name "index.htm" if exists +server.serveStatic("/", SPIFFS, "/www/"); + +// Server with different default file +server.serveStatic("/", SPIFFS, "/www/").setDefaultFile("default.html"); +``` + +### Serving static files with authentication + +```cpp +server + .serveStatic("/", SPIFFS, "/www/") + .setDefaultFile("default.html") + .setAuthentication("user", "pass"); +``` + +### Specifying Cache-Control header +It is possible to specify Cache-Control header value to reduce the number of calls to the server once the client loaded +the files. For more information on Cache-Control values see [Cache-Control](https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9) +```cpp +// Cache responses for 10 minutes (600 seconds) +server.serveStatic("/", SPIFFS, "/www/").setCacheControl("max-age=600"); + +//*** Change Cache-Control after server setup *** + +// During setup - keep a pointer to the handler +AsyncStaticWebHandler* handler = &server.serveStatic("/", SPIFFS, "/www/").setCacheControl("max-age=600"); + +// At a later event - change Cache-Control +handler->setCacheControl("max-age=30"); +``` + +### Specifying Date-Modified header +It is possible to specify Date-Modified header to enable the server to return Not-Modified (304) response for requests +with "If-Modified-Since" header with the same value, instead of responding with the actual file content. +```cpp +// Update the date modified string every time files are updated +server.serveStatic("/", SPIFFS, "/www/").setLastModified("Mon, 20 Jun 2016 14:00:00 GMT"); + +//*** Chage last modified value at a later stage *** + +// During setup - read last modified value from config or EEPROM +String date_modified = loadDateModified(); +AsyncStaticWebHandler* handler = &server.serveStatic("/", SPIFFS, "/www/"); +handler->setLastModified(date_modified); + +// At a later event when files are updated +String date_modified = getNewDateModfied(); +saveDateModified(date_modified); // Save for next reset +handler->setLastModified(date_modified); +``` + +### Specifying Template Processor callback +It is possible to specify template processor for static files. For information on template processor see +[Respond with content coming from a File containing templates](#respond-with-content-coming-from-a-file-containing-templates). +```cpp +String processor(const String& var) +{ + if(var == "HELLO_FROM_TEMPLATE") + return F("Hello world!"); + return String(); +} + +// ... + +server.serveStatic("/", SPIFFS, "/www/").setTemplateProcessor(processor); +``` + +## Param Rewrite With Matching +It is possible to rewrite the request url with parameter matchg. Here is an example with one parameter: +Rewrite for example "/radio/{frequence}" -> "/radio?f={frequence}" + +```cpp +class OneParamRewrite : public AsyncWebRewrite +{ + protected: + String _urlPrefix; + int _paramIndex; + String _paramsBackup; + + public: + OneParamRewrite(const char* from, const char* to) + : AsyncWebRewrite(from, to) { + + _paramIndex = _from.indexOf('{'); + + if( _paramIndex >=0 && _from.endsWith("}")) { + _urlPrefix = _from.substring(0, _paramIndex); + int index = _params.indexOf('{'); + if(index >= 0) { + _params = _params.substring(0, index); + } + } else { + _urlPrefix = _from; + } + _paramsBackup = _params; + } + + bool match(AsyncWebServerRequest *request) override { + if(request->url().startsWith(_urlPrefix)) { + if(_paramIndex >= 0) { + _params = _paramsBackup + request->url().substring(_paramIndex); + } else { + _params = _paramsBackup; + } + return true; + + } else { + return false; + } + } +}; +``` + +Usage: + +```cpp + server.addRewrite( new OneParamRewrite("/radio/{frequence}", "/radio?f={frequence}") ); +``` + +## Using filters +Filters can be set to `Rewrite` or `Handler` in order to control when to apply the rewrite and consider the handler. +A filter is a callback function that evaluates the request and return a boolean `true` to include the item +or `false` to exclude it. +Two filter callback are provided for convince: +* `ON_STA_FILTER` - return true when requests are made to the STA (station mode) interface. +* `ON_AP_FILTER` - return true when requests are made to the AP (access point) interface. + +### Serve different site files in AP mode +```cpp +server.serveStatic("/", SPIFFS, "/www/").setFilter(ON_STA_FILTER); +server.serveStatic("/", SPIFFS, "/ap/").setFilter(ON_AP_FILTER); +``` + +### Rewrite to different index on AP +```cpp +// Serve the file "/www/index-ap.htm" in AP, and the file "/www/index.htm" on STA +server.rewrite("/", "index.htm"); +server.rewrite("/index.htm", "index-ap.htm").setFilter(ON_AP_FILTER); +server.serveStatic("/", SPIFFS, "/www/"); +``` + +### Serving different hosts +```cpp +// Filter callback using request host +bool filterOnHost1(AsyncWebServerRequest *request) { return request->host() == "host1"; } + +// Server setup: server files in "/host1/" to requests for "host1", and files in "/www/" otherwise. +server.serveStatic("/", SPIFFS, "/host1/").setFilter(filterOnHost1); +server.serveStatic("/", SPIFFS, "/www/"); +``` + +### Determine interface inside callbacks +```cpp + String RedirectUrl = "http://"; + if (ON_STA_FILTER(request)) { + RedirectUrl += WiFi.localIP().toString(); + } else { + RedirectUrl += WiFi.softAPIP().toString(); + } + RedirectUrl += "/index.htm"; + request->redirect(RedirectUrl); +``` + +## Bad Responses +Some responses are implemented, but you should not use them, because they do not conform to HTTP. +The following example will lead to unclean close of the connection and more time wasted +than providing the length of the content + +### Respond with content using a callback without content length to HTTP/1.0 clients +```cpp +//This is used as fallback for chunked responses to HTTP/1.0 Clients +request->send("text/plain", 0, [](uint8_t *buffer, size_t maxLen, size_t index) -> size_t { + //Write up to "maxLen" bytes into "buffer" and return the amount written. + //You will be asked for more data until 0 is returned + //Keep in mind that you can not delay or yield waiting for more data! + return mySource.read(buffer, maxLen); +}); +``` + +## Async WebSocket Plugin +The server includes a web socket plugin which lets you define different WebSocket locations to connect to +without starting another listening service or using different port + +### Async WebSocket Event +```cpp + +void onEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len){ + if(type == WS_EVT_CONNECT){ + //client connected + os_printf("ws[%s][%u] connect\n", server->url(), client->id()); + client->printf("Hello Client %u :)", client->id()); + client->ping(); + } else if(type == WS_EVT_DISCONNECT){ + //client disconnected + os_printf("ws[%s][%u] disconnect: %u\n", server->url(), client->id()); + } else if(type == WS_EVT_ERROR){ + //error was received from the other end + os_printf("ws[%s][%u] error(%u): %s\n", server->url(), client->id(), *((uint16_t*)arg), (char*)data); + } else if(type == WS_EVT_PONG){ + //pong message was received (in response to a ping request maybe) + os_printf("ws[%s][%u] pong[%u]: %s\n", server->url(), client->id(), len, (len)?(char*)data:""); + } else if(type == WS_EVT_DATA){ + //data packet + AwsFrameInfo * info = (AwsFrameInfo*)arg; + if(info->final && info->index == 0 && info->len == len){ + //the whole message is in a single frame and we got all of it's data + os_printf("ws[%s][%u] %s-message[%llu]: ", server->url(), client->id(), (info->opcode == WS_TEXT)?"text":"binary", info->len); + if(info->opcode == WS_TEXT){ + data[len] = 0; + os_printf("%s\n", (char*)data); + } else { + for(size_t i=0; i < info->len; i++){ + os_printf("%02x ", data[i]); + } + os_printf("\n"); + } + if(info->opcode == WS_TEXT) + client->text("I got your text message"); + else + client->binary("I got your binary message"); + } else { + //message is comprised of multiple frames or the frame is split into multiple packets + if(info->index == 0){ + if(info->num == 0) + os_printf("ws[%s][%u] %s-message start\n", server->url(), client->id(), (info->message_opcode == WS_TEXT)?"text":"binary"); + os_printf("ws[%s][%u] frame[%u] start[%llu]\n", server->url(), client->id(), info->num, info->len); + } + + os_printf("ws[%s][%u] frame[%u] %s[%llu - %llu]: ", server->url(), client->id(), info->num, (info->message_opcode == WS_TEXT)?"text":"binary", info->index, info->index + len); + if(info->message_opcode == WS_TEXT){ + data[len] = 0; + os_printf("%s\n", (char*)data); + } else { + for(size_t i=0; i < len; i++){ + os_printf("%02x ", data[i]); + } + os_printf("\n"); + } + + if((info->index + len) == info->len){ + os_printf("ws[%s][%u] frame[%u] end[%llu]\n", server->url(), client->id(), info->num, info->len); + if(info->final){ + os_printf("ws[%s][%u] %s-message end\n", server->url(), client->id(), (info->message_opcode == WS_TEXT)?"text":"binary"); + if(info->message_opcode == WS_TEXT) + client->text("I got your text message"); + else + client->binary("I got your binary message"); + } + } + } + } +} +``` + +### Methods for sending data to a socket client +```cpp + + + +//Server methods +AsyncWebSocket ws("/ws"); +//printf to a client +ws.printf((uint32_t)client_id, arguments...); +//printf to all clients +ws.printfAll(arguments...); +//printf_P to a client +ws.printf_P((uint32_t)client_id, PSTR(format), arguments...); +//printfAll_P to all clients +ws.printfAll_P(PSTR(format), arguments...); +//send text to a client +ws.text((uint32_t)client_id, (char*)text); +ws.text((uint32_t)client_id, (uint8_t*)text, (size_t)len); +//send text from PROGMEM to a client +ws.text((uint32_t)client_id, PSTR("text")); +const char flash_text[] PROGMEM = "Text to send" +ws.text((uint32_t)client_id, FPSTR(flash_text)); +//send text to all clients +ws.textAll((char*)text); +ws.textAll((uint8_t*)text, (size_t)len); +//send binary to a client +ws.binary((uint32_t)client_id, (char*)binary); +ws.binary((uint32_t)client_id, (uint8_t*)binary, (size_t)len); +//send binary from PROGMEM to a client +const uint8_t flash_binary[] PROGMEM = { 0x01, 0x02, 0x03, 0x04 }; +ws.binary((uint32_t)client_id, flash_binary, 4); +//send binary to all clients +ws.binaryAll((char*)binary); +ws.binaryAll((uint8_t*)binary, (size_t)len); +//HTTP Authenticate before switch to Websocket protocol +ws.setAuthentication("user", "pass"); + +//client methods +AsyncWebSocketClient * client; +//printf +client->printf(arguments...); +//printf_P +client->printf_P(PSTR(format), arguments...); +//send text +client->text((char*)text); +client->text((uint8_t*)text, (size_t)len); +//send text from PROGMEM +client->text(PSTR("text")); +const char flash_text[] PROGMEM = "Text to send"; +client->text(FPSTR(flash_text)); +//send binary +client->binary((char*)binary); +client->binary((uint8_t*)binary, (size_t)len); +//send binary from PROGMEM +const uint8_t flash_binary[] PROGMEM = { 0x01, 0x02, 0x03, 0x04 }; +client->binary(flash_binary, 4); +``` + +### Direct access to web socket message buffer +When sending a web socket message using the above methods a buffer is created. Under certain circumstances you might want to manipulate or populate this buffer directly from your application, for example to prevent unnecessary duplications of the data. This example below shows how to create a buffer and print data to it from an ArduinoJson object then send it. + +```cpp +void sendDataWs(AsyncWebSocketClient * client) +{ + DynamicJsonBuffer jsonBuffer; + JsonObject& root = jsonBuffer.createObject(); + root["a"] = "abc"; + root["b"] = "abcd"; + root["c"] = "abcde"; + root["d"] = "abcdef"; + root["e"] = "abcdefg"; + size_t len = root.measureLength(); + AsyncWebSocketMessageBuffer * buffer = ws.makeBuffer(len); // creates a buffer (len + 1) for you. + if (buffer) { + root.printTo((char *)buffer->get(), len + 1); + if (client) { + client->text(buffer); + } else { + ws.textAll(buffer); + } + } +} +``` + +### Limiting the number of web socket clients +Browsers sometimes do not correctly close the websocket connection, even when the close() function is called in javascript. This will eventually exhaust the web server's resources and will cause the server to crash. Periodically calling the cleanClients() function from the main loop() function limits the number of clients by closing the oldest client when the maximum number of clients has been exceeded. This can called be every cycle, however, if you wish to use less power, then calling as infrequently as once per second is sufficient. + +```cpp +void loop(){ + ws.cleanupClients(); +} +``` + + +## Async Event Source Plugin +The server includes EventSource (Server-Sent Events) plugin which can be used to send short text events to the browser. +Difference between EventSource and WebSockets is that EventSource is single direction, text-only protocol. + +### Setup Event Source on the server +```cpp +AsyncWebServer server(80); +AsyncEventSource events("/events"); + +void setup(){ + // setup ...... + events.onConnect([](AsyncEventSourceClient *client){ + if(client->lastId()){ + Serial.printf("Client reconnected! Last message ID that it gat is: %u\n", client->lastId()); + } + //send event with message "hello!", id current millis + // and set reconnect delay to 1 second + client->send("hello!",NULL,millis(),1000); + }); + //HTTP Basic authentication + events.setAuthentication("user", "pass"); + server.addHandler(&events); + // setup ...... +} + +void loop(){ + if(eventTriggered){ // your logic here + //send event "myevent" + events.send("my event content","myevent",millis()); + } +} +``` + +### Setup Event Source in the browser +```javascript +if (!!window.EventSource) { + var source = new EventSource('/events'); + + source.addEventListener('open', function(e) { + console.log("Events Connected"); + }, false); + + source.addEventListener('error', function(e) { + if (e.target.readyState != EventSource.OPEN) { + console.log("Events Disconnected"); + } + }, false); + + source.addEventListener('message', function(e) { + console.log("message", e.data); + }, false); + + source.addEventListener('myevent', function(e) { + console.log("myevent", e.data); + }, false); +} +``` + +## Scanning for available WiFi Networks +```cpp +//First request will return 0 results unless you start scan from somewhere else (loop/setup) +//Do not request more often than 3-5 seconds +server.on("/scan", HTTP_GET, [](AsyncWebServerRequest *request){ + String json = "["; + int n = WiFi.scanComplete(); + if(n == -2){ + WiFi.scanNetworks(true); + } else if(n){ + for (int i = 0; i < n; ++i){ + if(i) json += ","; + json += "{"; + json += "\"rssi\":"+String(WiFi.RSSI(i)); + json += ",\"ssid\":\""+WiFi.SSID(i)+"\""; + json += ",\"bssid\":\""+WiFi.BSSIDstr(i)+"\""; + json += ",\"channel\":"+String(WiFi.channel(i)); + json += ",\"secure\":"+String(WiFi.encryptionType(i)); + json += ",\"hidden\":"+String(WiFi.isHidden(i)?"true":"false"); + json += "}"; + } + WiFi.scanDelete(); + if(WiFi.scanComplete() == -2){ + WiFi.scanNetworks(true); + } + } + json += "]"; + request->send(200, "application/json", json); + json = String(); +}); +``` + +## Remove handlers and rewrites + +Server goes through handlers in same order as they were added. You can't simple add handler with same path to override them. +To remove handler: +```arduino +// save callback for particular URL path +auto handler = server.on("/some/path", [](AsyncWebServerRequest *request){ + //do something useful +}); +// when you don't need handler anymore remove it +server.removeHandler(&handler); + +// same with rewrites +server.removeRewrite(&someRewrite); + +server.onNotFound([](AsyncWebServerRequest *request){ + request->send(404); +}); + +// remove server.onNotFound handler +server.onNotFound(NULL); + +// remove all rewrites, handlers and onNotFound/onFileUpload/onRequestBody callbacks +server.reset(); +``` + +## Setting up the server +```cpp +#include "ESPAsyncTCP.h" +#include "ESPAsyncWebServer.h" + +AsyncWebServer server(80); +AsyncWebSocket ws("/ws"); // access at ws://[esp ip]/ws +AsyncEventSource events("/events"); // event source (Server-Sent events) + +const char* ssid = "your-ssid"; +const char* password = "your-pass"; +const char* http_username = "admin"; +const char* http_password = "admin"; + +//flag to use from web update to reboot the ESP +bool shouldReboot = false; + +void onRequest(AsyncWebServerRequest *request){ + //Handle Unknown Request + request->send(404); +} + +void onBody(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total){ + //Handle body +} + +void onUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final){ + //Handle upload +} + +void onEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len){ + //Handle WebSocket event +} + +void setup(){ + Serial.begin(115200); + WiFi.mode(WIFI_STA); + WiFi.begin(ssid, password); + if (WiFi.waitForConnectResult() != WL_CONNECTED) { + Serial.printf("WiFi Failed!\n"); + return; + } + + // attach AsyncWebSocket + ws.onEvent(onEvent); + server.addHandler(&ws); + + // attach AsyncEventSource + server.addHandler(&events); + + // respond to GET requests on URL /heap + server.on("/heap", HTTP_GET, [](AsyncWebServerRequest *request){ + request->send(200, "text/plain", String(ESP.getFreeHeap())); + }); + + // upload a file to /upload + server.on("/upload", HTTP_POST, [](AsyncWebServerRequest *request){ + request->send(200); + }, onUpload); + + // send a file when /index is requested + server.on("/index", HTTP_ANY, [](AsyncWebServerRequest *request){ + request->send(SPIFFS, "/index.htm"); + }); + + // HTTP basic authentication + server.on("/login", HTTP_GET, [](AsyncWebServerRequest *request){ + if(!request->authenticate(http_username, http_password)) + return request->requestAuthentication(); + request->send(200, "text/plain", "Login Success!"); + }); + + // Simple Firmware Update Form + server.on("/update", HTTP_GET, [](AsyncWebServerRequest *request){ + request->send(200, "text/html", "
"); + }); + server.on("/update", HTTP_POST, [](AsyncWebServerRequest *request){ + shouldReboot = !Update.hasError(); + AsyncWebServerResponse *response = request->beginResponse(200, "text/plain", shouldReboot?"OK":"FAIL"); + response->addHeader("Connection", "close"); + request->send(response); + },[](AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final){ + if(!index){ + Serial.printf("Update Start: %s\n", filename.c_str()); + Update.runAsync(true); + if(!Update.begin((ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000)){ + Update.printError(Serial); + } + } + if(!Update.hasError()){ + if(Update.write(data, len) != len){ + Update.printError(Serial); + } + } + if(final){ + if(Update.end(true)){ + Serial.printf("Update Success: %uB\n", index+len); + } else { + Update.printError(Serial); + } + } + }); + + // attach filesystem root at URL /fs + server.serveStatic("/fs", SPIFFS, "/"); + + // Catch-All Handlers + // Any request that can not find a Handler that canHandle it + // ends in the callbacks below. + server.onNotFound(onRequest); + server.onFileUpload(onUpload); + server.onRequestBody(onBody); + + server.begin(); +} + +void loop(){ + if(shouldReboot){ + Serial.println("Rebooting..."); + delay(100); + ESP.restart(); + } + static char temp[128]; + sprintf(temp, "Seconds since boot: %u", millis()/1000); + events.send(temp, "time"); //send event "time" +} +``` + +### Setup global and class functions as request handlers + +```cpp +#include +#include +#include +#include + +void handleRequest(AsyncWebServerRequest *request){} + +class WebClass { +public : + AsyncWebServer classWebServer = AsyncWebServer(81); + + WebClass(){}; + + void classRequest (AsyncWebServerRequest *request){} + + void begin(){ + // attach global request handler + classWebServer.on("/example", HTTP_ANY, handleRequest); + + // attach class request handler + classWebServer.on("/example", HTTP_ANY, std::bind(&WebClass::classRequest, this, std::placeholders::_1)); + } +}; + +AsyncWebServer globalWebServer(80); +WebClass webClassInstance; + +void setup() { + // attach global request handler + globalWebServer.on("/example", HTTP_ANY, handleRequest); + + // attach class request handler + globalWebServer.on("/example", HTTP_ANY, std::bind(&WebClass::classRequest, webClassInstance, std::placeholders::_1)); +} + +void loop() { + +} +``` + +### Methods for controlling websocket connections + +```cpp + // Disable client connections if it was activated + if ( ws.enabled() ) + ws.enable(false); + + // enable client connections if it was disabled + if ( !ws.enabled() ) + ws.enable(true); +``` + +Example of OTA code + +```cpp + // OTA callbacks + ArduinoOTA.onStart([]() { + // Clean SPIFFS + SPIFFS.end(); + + // Disable client connections + ws.enable(false); + + // Advertise connected clients what's going on + ws.textAll("OTA Update Started"); + + // Close them + ws.closeAll(); + + }); + +``` + +### Adding Default Headers + +In some cases, such as when working with CORS, or with some sort of custom authentication system, +you might need to define a header that should get added to all responses (including static, websocket and EventSource). +The DefaultHeaders singleton allows you to do this. + +Example: + +```cpp +DefaultHeaders::Instance().addHeader("Access-Control-Allow-Origin", "*"); +webServer.begin(); +``` + +*NOTE*: You will still need to respond to the OPTIONS method for CORS pre-flight in most cases. (unless you are only using GET) + +This is one option: + +```cpp +webServer.onNotFound([](AsyncWebServerRequest *request) { + if (request->method() == HTTP_OPTIONS) { + request->send(200); + } else { + request->send(404); + } +}); +``` + +### Path variable + +With path variable you can create a custom regex rule for a specific parameter in a route. +For example we want a `sensorId` parameter in a route rule to match only a integer. + +```cpp + server.on("^\\/sensor\\/([0-9]+)$", HTTP_GET, [] (AsyncWebServerRequest *request) { + String sensorId = request->pathArg(0); + }); +``` +*NOTE*: All regex patterns starts with `^` and ends with `$` + +To enable the `Path variable` support, you have to define the buildflag `-DASYNCWEBSERVER_REGEX`. + + +For Arduino IDE create/update `platform.local.txt`: + +`Windows`: C:\Users\(username)\AppData\Local\Arduino15\packages\\`{espxxxx}`\hardware\\`espxxxx`\\`{version}`\platform.local.txt + +`Linux`: ~/.arduino15/packages/`{espxxxx}`/hardware/`{espxxxx}`/`{version}`/platform.local.txt + +Add/Update the following line: +``` + compiler.cpp.extra_flags=-DDASYNCWEBSERVER_REGEX +``` + +For platformio modify `platformio.ini`: +```ini +[env:myboard] +build_flags = + -DASYNCWEBSERVER_REGEX +``` +*NOTE*: By enabling `ASYNCWEBSERVER_REGEX`, `` will be included. This will add an 100k to your binary. diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/_config.yml b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/_config.yml new file mode 100644 index 0000000..c419263 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/_config.yml @@ -0,0 +1 @@ +theme: jekyll-theme-cayman \ No newline at end of file diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/component.mk b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/component.mk new file mode 100644 index 0000000..bb5bb16 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/component.mk @@ -0,0 +1,3 @@ +COMPONENT_ADD_INCLUDEDIRS := src +COMPONENT_SRCDIRS := src +CXXFLAGS += -fno-rtti diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/examples/CaptivePortal/CaptivePortal.ino b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/examples/CaptivePortal/CaptivePortal.ino new file mode 100644 index 0000000..f97f142 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/examples/CaptivePortal/CaptivePortal.ino @@ -0,0 +1,47 @@ +#include +#ifdef ESP32 +#include +#include +#elif defined(ESP8266) +#include +#include +#endif +#include "ESPAsyncWebServer.h" + +DNSServer dnsServer; +AsyncWebServer server(80); + +class CaptiveRequestHandler : public AsyncWebHandler { +public: + CaptiveRequestHandler() {} + virtual ~CaptiveRequestHandler() {} + + bool canHandle(AsyncWebServerRequest *request){ + //request->addInterestingHeader("ANY"); + return true; + } + + void handleRequest(AsyncWebServerRequest *request) { + AsyncResponseStream *response = request->beginResponseStream("text/html"); + response->print("Captive Portal"); + response->print("

This is out captive portal front page.

"); + response->printf("

You were trying to reach: http://%s%s

", request->host().c_str(), request->url().c_str()); + response->printf("

Try opening this link instead

", WiFi.softAPIP().toString().c_str()); + response->print(""); + request->send(response); + } +}; + + +void setup(){ + //your other setup stuff... + WiFi.softAP("esp-captive"); + dnsServer.start(53, "*", WiFi.softAPIP()); + server.addHandler(new CaptiveRequestHandler()).setFilter(ON_AP_FILTER);//only when requested from AP + //more handlers... + server.begin(); +} + +void loop(){ + dnsServer.processNextRequest(); +} diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/examples/ESP_AsyncFSBrowser/ESP_AsyncFSBrowser.ino b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/examples/ESP_AsyncFSBrowser/ESP_AsyncFSBrowser.ino new file mode 100644 index 0000000..bf42d00 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/examples/ESP_AsyncFSBrowser/ESP_AsyncFSBrowser.ino @@ -0,0 +1,221 @@ +#include +#ifdef ESP32 +#include +#include +#include +#include +#include +#elif defined(ESP8266) +#include +#include +#include +#endif +#include +#include + +// SKETCH BEGIN +AsyncWebServer server(80); +AsyncWebSocket ws("/ws"); +AsyncEventSource events("/events"); + +void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len){ + if(type == WS_EVT_CONNECT){ + Serial.printf("ws[%s][%u] connect\n", server->url(), client->id()); + client->printf("Hello Client %u :)", client->id()); + client->ping(); + } else if(type == WS_EVT_DISCONNECT){ + Serial.printf("ws[%s][%u] disconnect\n", server->url(), client->id()); + } else if(type == WS_EVT_ERROR){ + Serial.printf("ws[%s][%u] error(%u): %s\n", server->url(), client->id(), *((uint16_t*)arg), (char*)data); + } else if(type == WS_EVT_PONG){ + Serial.printf("ws[%s][%u] pong[%u]: %s\n", server->url(), client->id(), len, (len)?(char*)data:""); + } else if(type == WS_EVT_DATA){ + AwsFrameInfo * info = (AwsFrameInfo*)arg; + String msg = ""; + if(info->final && info->index == 0 && info->len == len){ + //the whole message is in a single frame and we got all of it's data + Serial.printf("ws[%s][%u] %s-message[%llu]: ", server->url(), client->id(), (info->opcode == WS_TEXT)?"text":"binary", info->len); + + if(info->opcode == WS_TEXT){ + for(size_t i=0; i < info->len; i++) { + msg += (char) data[i]; + } + } else { + char buff[3]; + for(size_t i=0; i < info->len; i++) { + sprintf(buff, "%02x ", (uint8_t) data[i]); + msg += buff ; + } + } + Serial.printf("%s\n",msg.c_str()); + + if(info->opcode == WS_TEXT) + client->text("I got your text message"); + else + client->binary("I got your binary message"); + } else { + //message is comprised of multiple frames or the frame is split into multiple packets + if(info->index == 0){ + if(info->num == 0) + Serial.printf("ws[%s][%u] %s-message start\n", server->url(), client->id(), (info->message_opcode == WS_TEXT)?"text":"binary"); + Serial.printf("ws[%s][%u] frame[%u] start[%llu]\n", server->url(), client->id(), info->num, info->len); + } + + Serial.printf("ws[%s][%u] frame[%u] %s[%llu - %llu]: ", server->url(), client->id(), info->num, (info->message_opcode == WS_TEXT)?"text":"binary", info->index, info->index + len); + + if(info->opcode == WS_TEXT){ + for(size_t i=0; i < len; i++) { + msg += (char) data[i]; + } + } else { + char buff[3]; + for(size_t i=0; i < len; i++) { + sprintf(buff, "%02x ", (uint8_t) data[i]); + msg += buff ; + } + } + Serial.printf("%s\n",msg.c_str()); + + if((info->index + len) == info->len){ + Serial.printf("ws[%s][%u] frame[%u] end[%llu]\n", server->url(), client->id(), info->num, info->len); + if(info->final){ + Serial.printf("ws[%s][%u] %s-message end\n", server->url(), client->id(), (info->message_opcode == WS_TEXT)?"text":"binary"); + if(info->message_opcode == WS_TEXT) + client->text("I got your text message"); + else + client->binary("I got your binary message"); + } + } + } + } +} + + +const char* ssid = "*******"; +const char* password = "*******"; +const char * hostName = "esp-async"; +const char* http_username = "admin"; +const char* http_password = "admin"; + +void setup(){ + Serial.begin(115200); + Serial.setDebugOutput(true); + WiFi.mode(WIFI_AP_STA); + WiFi.softAP(hostName); + WiFi.begin(ssid, password); + if (WiFi.waitForConnectResult() != WL_CONNECTED) { + Serial.printf("STA: Failed!\n"); + WiFi.disconnect(false); + delay(1000); + WiFi.begin(ssid, password); + } + + //Send OTA events to the browser + ArduinoOTA.onStart([]() { events.send("Update Start", "ota"); }); + ArduinoOTA.onEnd([]() { events.send("Update End", "ota"); }); + ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { + char p[32]; + sprintf(p, "Progress: %u%%\n", (progress/(total/100))); + events.send(p, "ota"); + }); + ArduinoOTA.onError([](ota_error_t error) { + if(error == OTA_AUTH_ERROR) events.send("Auth Failed", "ota"); + else if(error == OTA_BEGIN_ERROR) events.send("Begin Failed", "ota"); + else if(error == OTA_CONNECT_ERROR) events.send("Connect Failed", "ota"); + else if(error == OTA_RECEIVE_ERROR) events.send("Recieve Failed", "ota"); + else if(error == OTA_END_ERROR) events.send("End Failed", "ota"); + }); + ArduinoOTA.setHostname(hostName); + ArduinoOTA.begin(); + + MDNS.addService("http","tcp",80); + + SPIFFS.begin(); + + ws.onEvent(onWsEvent); + server.addHandler(&ws); + + events.onConnect([](AsyncEventSourceClient *client){ + client->send("hello!",NULL,millis(),1000); + }); + server.addHandler(&events); + +#ifdef ESP32 + server.addHandler(new SPIFFSEditor(SPIFFS, http_username,http_password)); +#elif defined(ESP8266) + server.addHandler(new SPIFFSEditor(http_username,http_password)); +#endif + + server.on("/heap", HTTP_GET, [](AsyncWebServerRequest *request){ + request->send(200, "text/plain", String(ESP.getFreeHeap())); + }); + + server.serveStatic("/", SPIFFS, "/").setDefaultFile("index.htm"); + + server.onNotFound([](AsyncWebServerRequest *request){ + Serial.printf("NOT_FOUND: "); + if(request->method() == HTTP_GET) + Serial.printf("GET"); + else if(request->method() == HTTP_POST) + Serial.printf("POST"); + else if(request->method() == HTTP_DELETE) + Serial.printf("DELETE"); + else if(request->method() == HTTP_PUT) + Serial.printf("PUT"); + else if(request->method() == HTTP_PATCH) + Serial.printf("PATCH"); + else if(request->method() == HTTP_HEAD) + Serial.printf("HEAD"); + else if(request->method() == HTTP_OPTIONS) + Serial.printf("OPTIONS"); + else + Serial.printf("UNKNOWN"); + Serial.printf(" http://%s%s\n", request->host().c_str(), request->url().c_str()); + + if(request->contentLength()){ + Serial.printf("_CONTENT_TYPE: %s\n", request->contentType().c_str()); + Serial.printf("_CONTENT_LENGTH: %u\n", request->contentLength()); + } + + int headers = request->headers(); + int i; + for(i=0;igetHeader(i); + Serial.printf("_HEADER[%s]: %s\n", h->name().c_str(), h->value().c_str()); + } + + int params = request->params(); + for(i=0;igetParam(i); + if(p->isFile()){ + Serial.printf("_FILE[%s]: %s, size: %u\n", p->name().c_str(), p->value().c_str(), p->size()); + } else if(p->isPost()){ + Serial.printf("_POST[%s]: %s\n", p->name().c_str(), p->value().c_str()); + } else { + Serial.printf("_GET[%s]: %s\n", p->name().c_str(), p->value().c_str()); + } + } + + request->send(404); + }); + server.onFileUpload([](AsyncWebServerRequest *request, const String& filename, size_t index, uint8_t *data, size_t len, bool final){ + if(!index) + Serial.printf("UploadStart: %s\n", filename.c_str()); + Serial.printf("%s", (const char*)data); + if(final) + Serial.printf("UploadEnd: %s (%u)\n", filename.c_str(), index+len); + }); + server.onRequestBody([](AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total){ + if(!index) + Serial.printf("BodyStart: %u\n", total); + Serial.printf("%s", (const char*)data); + if(index + len == total) + Serial.printf("BodyEnd: %u\n", total); + }); + server.begin(); +} + +void loop(){ + ArduinoOTA.handle(); + ws.cleanupClients(); +} diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/examples/ESP_AsyncFSBrowser/data/ace.js.gz b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/examples/ESP_AsyncFSBrowser/data/ace.js.gz new file mode 100644 index 0000000..7b175c1 Binary files /dev/null and b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/examples/ESP_AsyncFSBrowser/data/ace.js.gz differ diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/examples/ESP_AsyncFSBrowser/data/ext-searchbox.js.gz b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/examples/ESP_AsyncFSBrowser/data/ext-searchbox.js.gz new file mode 100644 index 0000000..cf5b49f Binary files /dev/null and b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/examples/ESP_AsyncFSBrowser/data/ext-searchbox.js.gz differ diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/examples/ESP_AsyncFSBrowser/data/favicon.ico b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/examples/ESP_AsyncFSBrowser/data/favicon.ico new file mode 100644 index 0000000..71b25fe Binary files /dev/null and b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/examples/ESP_AsyncFSBrowser/data/favicon.ico differ diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/examples/ESP_AsyncFSBrowser/data/index.htm b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/examples/ESP_AsyncFSBrowser/data/index.htm new file mode 100644 index 0000000..28f47e9 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/examples/ESP_AsyncFSBrowser/data/index.htm @@ -0,0 +1,131 @@ + + + + + + WebSocketTester + + + + +

+    
+ $ +
+ + diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/examples/ESP_AsyncFSBrowser/data/mode-css.js.gz b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/examples/ESP_AsyncFSBrowser/data/mode-css.js.gz new file mode 100644 index 0000000..ebd6fe9 Binary files /dev/null and b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/examples/ESP_AsyncFSBrowser/data/mode-css.js.gz differ diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/examples/ESP_AsyncFSBrowser/data/mode-html.js.gz b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/examples/ESP_AsyncFSBrowser/data/mode-html.js.gz new file mode 100644 index 0000000..26b5353 Binary files /dev/null and b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/examples/ESP_AsyncFSBrowser/data/mode-html.js.gz differ diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/examples/ESP_AsyncFSBrowser/data/mode-javascript.js.gz b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/examples/ESP_AsyncFSBrowser/data/mode-javascript.js.gz new file mode 100644 index 0000000..c0451c1 Binary files /dev/null and b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/examples/ESP_AsyncFSBrowser/data/mode-javascript.js.gz differ diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/examples/ESP_AsyncFSBrowser/data/worker-html.js.gz b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/examples/ESP_AsyncFSBrowser/data/worker-html.js.gz new file mode 100644 index 0000000..ec8aa87 Binary files /dev/null and b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/examples/ESP_AsyncFSBrowser/data/worker-html.js.gz differ diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/examples/regex_patterns/regex_patterns.ino b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/examples/regex_patterns/regex_patterns.ino new file mode 100644 index 0000000..fb01306 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/examples/regex_patterns/regex_patterns.ino @@ -0,0 +1,77 @@ +// +// A simple server implementation with regex routes: +// * serve static messages +// * read GET and POST parameters +// * handle missing pages / 404s +// + +// Add buildflag ASYNCWEBSERVER_REGEX to enable the regex support + +// For platformio: platformio.ini: +// build_flags = +// -DASYNCWEBSERVER_REGEX + +// For arduino IDE: create/update platform.local.txt +// Windows: C:\Users\(username)\AppData\Local\Arduino15\packages\espxxxx\hardware\espxxxx\{version}\platform.local.txt +// Linux: ~/.arduino15/packages/espxxxx/hardware/espxxxx/{version}/platform.local.txt +// +// compiler.cpp.extra_flags=-DASYNCWEBSERVER_REGEX=1 + +#include +#ifdef ESP32 +#include +#include +#elif defined(ESP8266) +#include +#include +#endif +#include + +AsyncWebServer server(80); + +const char* ssid = "YOUR_SSID"; +const char* password = "YOUR_PASSWORD"; + +const char* PARAM_MESSAGE = "message"; + +void notFound(AsyncWebServerRequest *request) { + request->send(404, "text/plain", "Not found"); +} + +void setup() { + + Serial.begin(115200); + WiFi.mode(WIFI_STA); + WiFi.begin(ssid, password); + if (WiFi.waitForConnectResult() != WL_CONNECTED) { + Serial.printf("WiFi Failed!\n"); + return; + } + + Serial.print("IP Address: "); + Serial.println(WiFi.localIP()); + + server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ + request->send(200, "text/plain", "Hello, world"); + }); + + // Send a GET request to /sensor/ + server.on("^\\/sensor\\/([0-9]+)$", HTTP_GET, [] (AsyncWebServerRequest *request) { + String sensorNumber = request->pathArg(0); + request->send(200, "text/plain", "Hello, sensor: " + sensorNumber); + }); + + // Send a GET request to /sensor//action/ + server.on("^\\/sensor\\/([0-9]+)\\/action\\/([a-zA-Z0-9]+)$", HTTP_GET, [] (AsyncWebServerRequest *request) { + String sensorNumber = request->pathArg(0); + String action = request->pathArg(1); + request->send(200, "text/plain", "Hello, sensor: " + sensorNumber + ", with action: " + action); + }); + + server.onNotFound(notFound); + + server.begin(); +} + +void loop() { +} diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/examples/simple_server/simple_server.ino b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/examples/simple_server/simple_server.ino new file mode 100644 index 0000000..bdbcf60 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/examples/simple_server/simple_server.ino @@ -0,0 +1,74 @@ +// +// A simple server implementation showing how to: +// * serve static messages +// * read GET and POST parameters +// * handle missing pages / 404s +// + +#include +#ifdef ESP32 +#include +#include +#elif defined(ESP8266) +#include +#include +#endif +#include + +AsyncWebServer server(80); + +const char* ssid = "YOUR_SSID"; +const char* password = "YOUR_PASSWORD"; + +const char* PARAM_MESSAGE = "message"; + +void notFound(AsyncWebServerRequest *request) { + request->send(404, "text/plain", "Not found"); +} + +void setup() { + + Serial.begin(115200); + WiFi.mode(WIFI_STA); + WiFi.begin(ssid, password); + if (WiFi.waitForConnectResult() != WL_CONNECTED) { + Serial.printf("WiFi Failed!\n"); + return; + } + + Serial.print("IP Address: "); + Serial.println(WiFi.localIP()); + + server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ + request->send(200, "text/plain", "Hello, world"); + }); + + // Send a GET request to /get?message= + server.on("/get", HTTP_GET, [] (AsyncWebServerRequest *request) { + String message; + if (request->hasParam(PARAM_MESSAGE)) { + message = request->getParam(PARAM_MESSAGE)->value(); + } else { + message = "No message sent"; + } + request->send(200, "text/plain", "Hello, GET: " + message); + }); + + // Send a POST request to /post with a form field message set to + server.on("/post", HTTP_POST, [](AsyncWebServerRequest *request){ + String message; + if (request->hasParam(PARAM_MESSAGE, true)) { + message = request->getParam(PARAM_MESSAGE, true)->value(); + } else { + message = "No message sent"; + } + request->send(200, "text/plain", "Hello, POST: " + message); + }); + + server.onNotFound(notFound); + + server.begin(); +} + +void loop() { +} \ No newline at end of file diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/keywords.txt b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/keywords.txt new file mode 100644 index 0000000..c391e6c --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/keywords.txt @@ -0,0 +1,3 @@ +JsonArray KEYWORD1 +add KEYWORD2 +createArray KEYWORD3 diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/library.json b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/library.json new file mode 100644 index 0000000..5565927 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/library.json @@ -0,0 +1,33 @@ +{ + "name":"ESP Async WebServer", + "description":"Asynchronous HTTP and WebSocket Server Library for ESP8266 and ESP32", + "keywords":"http,async,websocket,webserver", + "authors": + { + "name": "Hristo Gochkov", + "maintainer": true + }, + "repository": + { + "type": "git", + "url": "https://github.com/me-no-dev/ESPAsyncWebServer.git" + }, + "version": "1.2.3", + "license": "LGPL-3.0", + "frameworks": "arduino", + "platforms": ["espressif8266", "espressif32"], + "dependencies": [ + { + "name": "ESPAsyncTCP", + "platforms": "espressif8266" + }, + { + "name": "AsyncTCP", + "platforms": "espressif32" + }, + { + "name": "Hash", + "platforms": "espressif8266" + } + ] +} diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/library.properties b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/library.properties new file mode 100644 index 0000000..401b041 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/library.properties @@ -0,0 +1,9 @@ +name=ESP Async WebServer +version=1.2.3 +author=Me-No-Dev +maintainer=Me-No-Dev +sentence=Async Web Server for ESP8266 and ESP31B +paragraph=Async Web Server for ESP8266 and ESP31B +category=Other +url=https://github.com/me-no-dev/ESPAsyncWebServer +architectures=* diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/AsyncEventSource.cpp b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/AsyncEventSource.cpp new file mode 100644 index 0000000..f2914df --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/AsyncEventSource.cpp @@ -0,0 +1,368 @@ +/* + Asynchronous WebServer library for Espressif MCUs + + Copyright (c) 2016 Hristo Gochkov. All rights reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ +#include "Arduino.h" +#include "AsyncEventSource.h" + +static String generateEventMessage(const char *message, const char *event, uint32_t id, uint32_t reconnect){ + String ev = ""; + + if(reconnect){ + ev += "retry: "; + ev += String(reconnect); + ev += "\r\n"; + } + + if(id){ + ev += "id: "; + ev += String(id); + ev += "\r\n"; + } + + if(event != NULL){ + ev += "event: "; + ev += String(event); + ev += "\r\n"; + } + + if(message != NULL){ + size_t messageLen = strlen(message); + char * lineStart = (char *)message; + char * lineEnd; + do { + char * nextN = strchr(lineStart, '\n'); + char * nextR = strchr(lineStart, '\r'); + if(nextN == NULL && nextR == NULL){ + size_t llen = ((char *)message + messageLen) - lineStart; + char * ldata = (char *)malloc(llen+1); + if(ldata != NULL){ + memcpy(ldata, lineStart, llen); + ldata[llen] = 0; + ev += "data: "; + ev += ldata; + ev += "\r\n\r\n"; + free(ldata); + } + lineStart = (char *)message + messageLen; + } else { + char * nextLine = NULL; + if(nextN != NULL && nextR != NULL){ + if(nextR < nextN){ + lineEnd = nextR; + if(nextN == (nextR + 1)) + nextLine = nextN + 1; + else + nextLine = nextR + 1; + } else { + lineEnd = nextN; + if(nextR == (nextN + 1)) + nextLine = nextR + 1; + else + nextLine = nextN + 1; + } + } else if(nextN != NULL){ + lineEnd = nextN; + nextLine = nextN + 1; + } else { + lineEnd = nextR; + nextLine = nextR + 1; + } + + size_t llen = lineEnd - lineStart; + char * ldata = (char *)malloc(llen+1); + if(ldata != NULL){ + memcpy(ldata, lineStart, llen); + ldata[llen] = 0; + ev += "data: "; + ev += ldata; + ev += "\r\n"; + free(ldata); + } + lineStart = nextLine; + if(lineStart == ((char *)message + messageLen)) + ev += "\r\n"; + } + } while(lineStart < ((char *)message + messageLen)); + } + + return ev; +} + +// Message + +AsyncEventSourceMessage::AsyncEventSourceMessage(const char * data, size_t len) +: _data(nullptr), _len(len), _sent(0), _acked(0) +{ + _data = (uint8_t*)malloc(_len+1); + if(_data == nullptr){ + _len = 0; + } else { + memcpy(_data, data, len); + _data[_len] = 0; + } +} + +AsyncEventSourceMessage::~AsyncEventSourceMessage() { + if(_data != NULL) + free(_data); +} + +size_t AsyncEventSourceMessage::ack(size_t len, uint32_t time) { + (void)time; + // If the whole message is now acked... + if(_acked + len > _len){ + // Return the number of extra bytes acked (they will be carried on to the next message) + const size_t extra = _acked + len - _len; + _acked = _len; + return extra; + } + // Return that no extra bytes left. + _acked += len; + return 0; +} + +size_t AsyncEventSourceMessage::send(AsyncClient *client) { + const size_t len = _len - _sent; + if(client->space() < len){ + return 0; + } + size_t sent = client->add((const char *)_data, len); + if(client->canSend()) + client->send(); + _sent += sent; + return sent; +} + +// Client + +AsyncEventSourceClient::AsyncEventSourceClient(AsyncWebServerRequest *request, AsyncEventSource *server) +: _messageQueue(LinkedList([](AsyncEventSourceMessage *m){ delete m; })) +{ + _client = request->client(); + _server = server; + _lastId = 0; + if(request->hasHeader("Last-Event-ID")) + _lastId = atoi(request->getHeader("Last-Event-ID")->value().c_str()); + + _client->setRxTimeout(0); + _client->onError(NULL, NULL); + _client->onAck([](void *r, AsyncClient* c, size_t len, uint32_t time){ (void)c; ((AsyncEventSourceClient*)(r))->_onAck(len, time); }, this); + _client->onPoll([](void *r, AsyncClient* c){ (void)c; ((AsyncEventSourceClient*)(r))->_onPoll(); }, this); + _client->onData(NULL, NULL); + _client->onTimeout([this](void *r, AsyncClient* c __attribute__((unused)), uint32_t time){ ((AsyncEventSourceClient*)(r))->_onTimeout(time); }, this); + _client->onDisconnect([this](void *r, AsyncClient* c){ ((AsyncEventSourceClient*)(r))->_onDisconnect(); delete c; }, this); + + _server->_addClient(this); + delete request; +} + +AsyncEventSourceClient::~AsyncEventSourceClient(){ + _messageQueue.free(); + close(); +} + +void AsyncEventSourceClient::_queueMessage(AsyncEventSourceMessage *dataMessage){ + if(dataMessage == NULL) + return; + if(!connected()){ + delete dataMessage; + return; + } + if(_messageQueue.length() >= SSE_MAX_QUEUED_MESSAGES){ + ets_printf("ERROR: Too many messages queued\n"); + delete dataMessage; + } else { + _messageQueue.add(dataMessage); + } + if(_client->canSend()) + _runQueue(); +} + +void AsyncEventSourceClient::_onAck(size_t len, uint32_t time){ + while(len && !_messageQueue.isEmpty()){ + len = _messageQueue.front()->ack(len, time); + if(_messageQueue.front()->finished()) + _messageQueue.remove(_messageQueue.front()); + } + + _runQueue(); +} + +void AsyncEventSourceClient::_onPoll(){ + if(!_messageQueue.isEmpty()){ + _runQueue(); + } +} + + +void AsyncEventSourceClient::_onTimeout(uint32_t time __attribute__((unused))){ + _client->close(true); +} + +void AsyncEventSourceClient::_onDisconnect(){ + _client = NULL; + _server->_handleDisconnect(this); +} + +void AsyncEventSourceClient::close(){ + if(_client != NULL) + _client->close(); +} + +void AsyncEventSourceClient::write(const char * message, size_t len){ + _queueMessage(new AsyncEventSourceMessage(message, len)); +} + +void AsyncEventSourceClient::send(const char *message, const char *event, uint32_t id, uint32_t reconnect){ + String ev = generateEventMessage(message, event, id, reconnect); + _queueMessage(new AsyncEventSourceMessage(ev.c_str(), ev.length())); +} + +void AsyncEventSourceClient::_runQueue(){ + while(!_messageQueue.isEmpty() && _messageQueue.front()->finished()){ + _messageQueue.remove(_messageQueue.front()); + } + + for(auto i = _messageQueue.begin(); i != _messageQueue.end(); ++i) + { + if(!(*i)->sent()) + (*i)->send(_client); + } +} + + +// Handler + +AsyncEventSource::AsyncEventSource(const String& url) + : _url(url) + , _clients(LinkedList([](AsyncEventSourceClient *c){ delete c; })) + , _connectcb(NULL) +{} + +AsyncEventSource::~AsyncEventSource(){ + close(); +} + +void AsyncEventSource::onConnect(ArEventHandlerFunction cb){ + _connectcb = cb; +} + +void AsyncEventSource::_addClient(AsyncEventSourceClient * client){ + /*char * temp = (char *)malloc(2054); + if(temp != NULL){ + memset(temp+1,' ',2048); + temp[0] = ':'; + temp[2049] = '\r'; + temp[2050] = '\n'; + temp[2051] = '\r'; + temp[2052] = '\n'; + temp[2053] = 0; + client->write((const char *)temp, 2053); + free(temp); + }*/ + + _clients.add(client); + if(_connectcb) + _connectcb(client); +} + +void AsyncEventSource::_handleDisconnect(AsyncEventSourceClient * client){ + _clients.remove(client); +} + +void AsyncEventSource::close(){ + for(const auto &c: _clients){ + if(c->connected()) + c->close(); + } +} + +// pmb fix +size_t AsyncEventSource::avgPacketsWaiting() const { + if(_clients.isEmpty()) + return 0; + + size_t aql=0; + uint32_t nConnectedClients=0; + + for(const auto &c: _clients){ + if(c->connected()) { + aql+=c->packetsWaiting(); + ++nConnectedClients; + } + } +// return aql / nConnectedClients; + return ((aql) + (nConnectedClients/2))/(nConnectedClients); // round up +} + +void AsyncEventSource::send(const char *message, const char *event, uint32_t id, uint32_t reconnect){ + + + String ev = generateEventMessage(message, event, id, reconnect); + for(const auto &c: _clients){ + if(c->connected()) { + c->write(ev.c_str(), ev.length()); + } + } +} + +size_t AsyncEventSource::count() const { + return _clients.count_if([](AsyncEventSourceClient *c){ + return c->connected(); + }); +} + +bool AsyncEventSource::canHandle(AsyncWebServerRequest *request){ + if(request->method() != HTTP_GET || !request->url().equals(_url)) { + return false; + } + request->addInterestingHeader("Last-Event-ID"); + return true; +} + +void AsyncEventSource::handleRequest(AsyncWebServerRequest *request){ + if((_username != "" && _password != "") && !request->authenticate(_username.c_str(), _password.c_str())) + return request->requestAuthentication(); + request->send(new AsyncEventSourceResponse(this)); +} + +// Response + +AsyncEventSourceResponse::AsyncEventSourceResponse(AsyncEventSource *server){ + _server = server; + _code = 200; + _contentType = "text/event-stream"; + _sendContentLength = false; + addHeader("Cache-Control", "no-cache"); + addHeader("Connection","keep-alive"); +} + +void AsyncEventSourceResponse::_respond(AsyncWebServerRequest *request){ + String out = _assembleHead(request->version()); + request->client()->write(out.c_str(), _headLength); + _state = RESPONSE_WAIT_ACK; +} + +size_t AsyncEventSourceResponse::_ack(AsyncWebServerRequest *request, size_t len, uint32_t time __attribute__((unused))){ + if(len){ + new AsyncEventSourceClient(request, _server); + } + return 0; +} + diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/AsyncEventSource.h b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/AsyncEventSource.h new file mode 100644 index 0000000..b097fa6 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/AsyncEventSource.h @@ -0,0 +1,133 @@ +/* + Asynchronous WebServer library for Espressif MCUs + + Copyright (c) 2016 Hristo Gochkov. All rights reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ +#ifndef ASYNCEVENTSOURCE_H_ +#define ASYNCEVENTSOURCE_H_ + +#include +#ifdef ESP32 +#include +#define SSE_MAX_QUEUED_MESSAGES 32 +#else +#include +#define SSE_MAX_QUEUED_MESSAGES 8 +#endif +#include + +#include "AsyncWebSynchronization.h" + +#ifdef ESP8266 +#include +#ifdef CRYPTO_HASH_h // include Hash.h from espressif framework if the first include was from the crypto library +#include <../src/Hash.h> +#endif +#endif + +#ifdef ESP32 +#define DEFAULT_MAX_SSE_CLIENTS 8 +#else +#define DEFAULT_MAX_SSE_CLIENTS 4 +#endif + +class AsyncEventSource; +class AsyncEventSourceResponse; +class AsyncEventSourceClient; +typedef std::function ArEventHandlerFunction; + +class AsyncEventSourceMessage { + private: + uint8_t * _data; + size_t _len; + size_t _sent; + //size_t _ack; + size_t _acked; + public: + AsyncEventSourceMessage(const char * data, size_t len); + ~AsyncEventSourceMessage(); + size_t ack(size_t len, uint32_t time __attribute__((unused))); + size_t send(AsyncClient *client); + bool finished(){ return _acked == _len; } + bool sent() { return _sent == _len; } +}; + +class AsyncEventSourceClient { + private: + AsyncClient *_client; + AsyncEventSource *_server; + uint32_t _lastId; + LinkedList _messageQueue; + void _queueMessage(AsyncEventSourceMessage *dataMessage); + void _runQueue(); + + public: + + AsyncEventSourceClient(AsyncWebServerRequest *request, AsyncEventSource *server); + ~AsyncEventSourceClient(); + + AsyncClient* client(){ return _client; } + void close(); + void write(const char * message, size_t len); + void send(const char *message, const char *event=NULL, uint32_t id=0, uint32_t reconnect=0); + bool connected() const { return (_client != NULL) && _client->connected(); } + uint32_t lastId() const { return _lastId; } + size_t packetsWaiting() const { return _messageQueue.length(); } + + //system callbacks (do not call) + void _onAck(size_t len, uint32_t time); + void _onPoll(); + void _onTimeout(uint32_t time); + void _onDisconnect(); +}; + +class AsyncEventSource: public AsyncWebHandler { + private: + String _url; + LinkedList _clients; + ArEventHandlerFunction _connectcb; + public: + AsyncEventSource(const String& url); + ~AsyncEventSource(); + + const char * url() const { return _url.c_str(); } + void close(); + void onConnect(ArEventHandlerFunction cb); + void send(const char *message, const char *event=NULL, uint32_t id=0, uint32_t reconnect=0); + size_t count() const; //number clinets connected + size_t avgPacketsWaiting() const; + + //system callbacks (do not call) + void _addClient(AsyncEventSourceClient * client); + void _handleDisconnect(AsyncEventSourceClient * client); + virtual bool canHandle(AsyncWebServerRequest *request) override final; + virtual void handleRequest(AsyncWebServerRequest *request) override final; +}; + +class AsyncEventSourceResponse: public AsyncWebServerResponse { + private: + String _content; + AsyncEventSource *_server; + public: + AsyncEventSourceResponse(AsyncEventSource *server); + void _respond(AsyncWebServerRequest *request); + size_t _ack(AsyncWebServerRequest *request, size_t len, uint32_t time); + bool _sourceValid() const { return true; } +}; + + +#endif /* ASYNCEVENTSOURCE_H_ */ diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/AsyncJson.h b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/AsyncJson.h new file mode 100644 index 0000000..27b4a26 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/AsyncJson.h @@ -0,0 +1,252 @@ +// AsyncJson.h +/* + Async Response to use with ArduinoJson and AsyncWebServer + Written by Andrew Melvin (SticilFace) with help from me-no-dev and BBlanchon. + + Example of callback in use + + server.on("/json", HTTP_ANY, [](AsyncWebServerRequest * request) { + + AsyncJsonResponse * response = new AsyncJsonResponse(); + JsonObject& root = response->getRoot(); + root["key1"] = "key number one"; + JsonObject& nested = root.createNestedObject("nested"); + nested["key1"] = "key number one"; + + response->setLength(); + request->send(response); + }); + + -------------------- + + Async Request to use with ArduinoJson and AsyncWebServer + Written by Arsène von Wyss (avonwyss) + + Example + + AsyncCallbackJsonWebHandler* handler = new AsyncCallbackJsonWebHandler("/rest/endpoint"); + handler->onRequest([](AsyncWebServerRequest *request, JsonVariant &json) { + JsonObject& jsonObj = json.as(); + // ... + }); + server.addHandler(handler); + +*/ +#ifndef ASYNC_JSON_H_ +#define ASYNC_JSON_H_ +#include +#include +#include + +#if ARDUINOJSON_VERSION_MAJOR == 5 + #define ARDUINOJSON_5_COMPATIBILITY +#else + #define DYNAMIC_JSON_DOCUMENT_SIZE 1024 +#endif + +constexpr const char* JSON_MIMETYPE = "application/json"; + +/* + * Json Response + * */ + +class ChunkPrint : public Print { + private: + uint8_t* _destination; + size_t _to_skip; + size_t _to_write; + size_t _pos; + public: + ChunkPrint(uint8_t* destination, size_t from, size_t len) + : _destination(destination), _to_skip(from), _to_write(len), _pos{0} {} + virtual ~ChunkPrint(){} + size_t write(uint8_t c){ + if (_to_skip > 0) { + _to_skip--; + return 1; + } else if (_to_write > 0) { + _to_write--; + _destination[_pos++] = c; + return 1; + } + return 0; + } + size_t write(const uint8_t *buffer, size_t size) + { + return this->Print::write(buffer, size); + } +}; + +class AsyncJsonResponse: public AsyncAbstractResponse { + protected: + +#ifdef ARDUINOJSON_5_COMPATIBILITY + DynamicJsonBuffer _jsonBuffer; +#else + DynamicJsonDocument _jsonBuffer; +#endif + + JsonVariant _root; + bool _isValid; + + public: + +#ifdef ARDUINOJSON_5_COMPATIBILITY + AsyncJsonResponse(bool isArray=false): _isValid{false} { + _code = 200; + _contentType = JSON_MIMETYPE; + if(isArray) + _root = _jsonBuffer.createArray(); + else + _root = _jsonBuffer.createObject(); + } +#else + AsyncJsonResponse(bool isArray=false, size_t maxJsonBufferSize = DYNAMIC_JSON_DOCUMENT_SIZE) : _jsonBuffer(maxJsonBufferSize), _isValid{false} { + _code = 200; + _contentType = JSON_MIMETYPE; + if(isArray) + _root = _jsonBuffer.createNestedArray(); + else + _root = _jsonBuffer.createNestedObject(); + } +#endif + + ~AsyncJsonResponse() {} + JsonVariant & getRoot() { return _root; } + bool _sourceValid() const { return _isValid; } + size_t setLength() { + +#ifdef ARDUINOJSON_5_COMPATIBILITY + _contentLength = _root.measureLength(); +#else + _contentLength = measureJson(_root); +#endif + + if (_contentLength) { _isValid = true; } + return _contentLength; + } + + size_t getSize() { return _jsonBuffer.size(); } + + size_t _fillBuffer(uint8_t *data, size_t len){ + ChunkPrint dest(data, _sentLength, len); + +#ifdef ARDUINOJSON_5_COMPATIBILITY + _root.printTo( dest ) ; +#else + serializeJson(_root, dest); +#endif + return len; + } +}; + +class PrettyAsyncJsonResponse: public AsyncJsonResponse { +public: +#ifdef ARDUINOJSON_5_COMPATIBILITY + PrettyAsyncJsonResponse (bool isArray=false) : AsyncJsonResponse{isArray} {} +#else + PrettyAsyncJsonResponse (bool isArray=false, size_t maxJsonBufferSize = DYNAMIC_JSON_DOCUMENT_SIZE) : AsyncJsonResponse{isArray, maxJsonBufferSize} {} +#endif + size_t setLength () { +#ifdef ARDUINOJSON_5_COMPATIBILITY + _contentLength = _root.measurePrettyLength (); +#else + _contentLength = measureJsonPretty(_root); +#endif + if (_contentLength) {_isValid = true;} + return _contentLength; + } + size_t _fillBuffer (uint8_t *data, size_t len) { + ChunkPrint dest (data, _sentLength, len); +#ifdef ARDUINOJSON_5_COMPATIBILITY + _root.prettyPrintTo (dest); +#else + serializeJsonPretty(_root, dest); +#endif + return len; + } +}; + +typedef std::function ArJsonRequestHandlerFunction; + +class AsyncCallbackJsonWebHandler: public AsyncWebHandler { +private: +protected: + const String _uri; + WebRequestMethodComposite _method; + ArJsonRequestHandlerFunction _onRequest; + size_t _contentLength; +#ifndef ARDUINOJSON_5_COMPATIBILITY + const size_t maxJsonBufferSize; +#endif + size_t _maxContentLength; +public: +#ifdef ARDUINOJSON_5_COMPATIBILITY + AsyncCallbackJsonWebHandler(const String& uri, ArJsonRequestHandlerFunction onRequest) + : _uri(uri), _method(HTTP_POST|HTTP_PUT|HTTP_PATCH), _onRequest(onRequest), _maxContentLength(16384) {} +#else + AsyncCallbackJsonWebHandler(const String& uri, ArJsonRequestHandlerFunction onRequest, size_t maxJsonBufferSize=DYNAMIC_JSON_DOCUMENT_SIZE) + : _uri(uri), _method(HTTP_POST|HTTP_PUT|HTTP_PATCH), _onRequest(onRequest), maxJsonBufferSize(maxJsonBufferSize), _maxContentLength(16384) {} +#endif + + void setMethod(WebRequestMethodComposite method){ _method = method; } + void setMaxContentLength(int maxContentLength){ _maxContentLength = maxContentLength; } + void onRequest(ArJsonRequestHandlerFunction fn){ _onRequest = fn; } + + virtual bool canHandle(AsyncWebServerRequest *request) override final{ + if(!_onRequest) + return false; + + if(!(_method & request->method())) + return false; + + if(_uri.length() && (_uri != request->url() && !request->url().startsWith(_uri+"/"))) + return false; + + if ( !request->contentType().equalsIgnoreCase(JSON_MIMETYPE) ) + return false; + + request->addInterestingHeader("ANY"); + return true; + } + + virtual void handleRequest(AsyncWebServerRequest *request) override final { + if(_onRequest) { + if (request->_tempObject != NULL) { + +#ifdef ARDUINOJSON_5_COMPATIBILITY + DynamicJsonBuffer jsonBuffer; + JsonVariant json = jsonBuffer.parse((uint8_t*)(request->_tempObject)); + if (json.success()) { +#else + DynamicJsonDocument jsonBuffer(this->maxJsonBufferSize); + DeserializationError error = deserializeJson(jsonBuffer, (uint8_t*)(request->_tempObject)); + if(!error) { + JsonVariant json = jsonBuffer.as(); +#endif + + _onRequest(request, json); + return; + } + } + request->send(_contentLength > _maxContentLength ? 413 : 400); + } else { + request->send(500); + } + } + virtual void handleUpload(AsyncWebServerRequest *request, const String& filename, size_t index, uint8_t *data, size_t len, bool final) override final { + } + virtual void handleBody(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total) override final { + if (_onRequest) { + _contentLength = total; + if (total > 0 && request->_tempObject == NULL && total < _maxContentLength) { + request->_tempObject = malloc(total); + } + if (request->_tempObject != NULL) { + memcpy((uint8_t*)(request->_tempObject) + index, data, len); + } + } + } + virtual bool isRequestHandlerTrivial() override final {return _onRequest ? false : true;} +}; +#endif diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/AsyncWebSocket.cpp b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/AsyncWebSocket.cpp new file mode 100644 index 0000000..52dcd75 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/AsyncWebSocket.cpp @@ -0,0 +1,1303 @@ +/* + Asynchronous WebServer library for Espressif MCUs + + Copyright (c) 2016 Hristo Gochkov. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ +#include "Arduino.h" +#include "AsyncWebSocket.h" + +#include + +#ifndef ESP8266 +extern "C" { +typedef struct { + uint32_t state[5]; + uint32_t count[2]; + unsigned char buffer[64]; +} SHA1_CTX; + +void SHA1Transform(uint32_t state[5], const unsigned char buffer[64]); +void SHA1Init(SHA1_CTX* context); +void SHA1Update(SHA1_CTX* context, const unsigned char* data, uint32_t len); +void SHA1Final(unsigned char digest[20], SHA1_CTX* context); +} +#else +#include +#endif + +#define MAX_PRINTF_LEN 64 + +size_t webSocketSendFrameWindow(AsyncClient *client){ + if(!client->canSend()) + return 0; + size_t space = client->space(); + if(space < 9) + return 0; + return space - 8; +} + +size_t webSocketSendFrame(AsyncClient *client, bool final, uint8_t opcode, bool mask, uint8_t *data, size_t len){ + if(!client->canSend()) + return 0; + size_t space = client->space(); + if(space < 2) + return 0; + uint8_t mbuf[4] = {0,0,0,0}; + uint8_t headLen = 2; + if(len && mask){ + headLen += 4; + mbuf[0] = rand() % 0xFF; + mbuf[1] = rand() % 0xFF; + mbuf[2] = rand() % 0xFF; + mbuf[3] = rand() % 0xFF; + } + if(len > 125) + headLen += 2; + if(space < headLen) + return 0; + space -= headLen; + + if(len > space) len = space; + + uint8_t *buf = (uint8_t*)malloc(headLen); + if(buf == NULL){ + //os_printf("could not malloc %u bytes for frame header\n", headLen); + return 0; + } + + buf[0] = opcode & 0x0F; + if(final) + buf[0] |= 0x80; + if(len < 126) + buf[1] = len & 0x7F; + else { + buf[1] = 126; + buf[2] = (uint8_t)((len >> 8) & 0xFF); + buf[3] = (uint8_t)(len & 0xFF); + } + if(len && mask){ + buf[1] |= 0x80; + memcpy(buf + (headLen - 4), mbuf, 4); + } + if(client->add((const char *)buf, headLen) != headLen){ + //os_printf("error adding %lu header bytes\n", headLen); + free(buf); + return 0; + } + free(buf); + + if(len){ + if(len && mask){ + size_t i; + for(i=0;iadd((const char *)data, len) != len){ + //os_printf("error adding %lu data bytes\n", len); + return 0; + } + } + if(!client->send()){ + //os_printf("error sending frame: %lu\n", headLen+len); + return 0; + } + return len; +} + + +/* + * AsyncWebSocketMessageBuffer + */ + + + +AsyncWebSocketMessageBuffer::AsyncWebSocketMessageBuffer() + :_data(nullptr) + ,_len(0) + ,_lock(false) + ,_count(0) +{ + +} + +AsyncWebSocketMessageBuffer::AsyncWebSocketMessageBuffer(uint8_t * data, size_t size) + :_data(nullptr) + ,_len(size) + ,_lock(false) + ,_count(0) +{ + + if (!data) { + return; + } + + _data = new uint8_t[_len + 1]; + + if (_data) { + memcpy(_data, data, _len); + _data[_len] = 0; + } +} + + +AsyncWebSocketMessageBuffer::AsyncWebSocketMessageBuffer(size_t size) + :_data(nullptr) + ,_len(size) + ,_lock(false) + ,_count(0) +{ + _data = new uint8_t[_len + 1]; + + if (_data) { + _data[_len] = 0; + } + +} + +AsyncWebSocketMessageBuffer::AsyncWebSocketMessageBuffer(const AsyncWebSocketMessageBuffer & copy) + :_data(nullptr) + ,_len(0) + ,_lock(false) + ,_count(0) +{ + _len = copy._len; + _lock = copy._lock; + _count = 0; + + if (_len) { + _data = new uint8_t[_len + 1]; + _data[_len] = 0; + } + + if (_data) { + memcpy(_data, copy._data, _len); + _data[_len] = 0; + } + +} + +AsyncWebSocketMessageBuffer::AsyncWebSocketMessageBuffer(AsyncWebSocketMessageBuffer && copy) + :_data(nullptr) + ,_len(0) + ,_lock(false) + ,_count(0) +{ + _len = copy._len; + _lock = copy._lock; + _count = 0; + + if (copy._data) { + _data = copy._data; + copy._data = nullptr; + } + +} + +AsyncWebSocketMessageBuffer::~AsyncWebSocketMessageBuffer() +{ + if (_data) { + delete[] _data; + } +} + +bool AsyncWebSocketMessageBuffer::reserve(size_t size) +{ + _len = size; + + if (_data) { + delete[] _data; + _data = nullptr; + } + + _data = new uint8_t[_len + 1]; + + if (_data) { + _data[_len] = 0; + return true; + } else { + return false; + } + +} + + + +/* + * Control Frame + */ + +class AsyncWebSocketControl { + private: + uint8_t _opcode; + uint8_t *_data; + size_t _len; + bool _mask; + bool _finished; + public: + AsyncWebSocketControl(uint8_t opcode, uint8_t *data=NULL, size_t len=0, bool mask=false) + :_opcode(opcode) + ,_len(len) + ,_mask(len && mask) + ,_finished(false) + { + if(data == NULL) + _len = 0; + if(_len){ + if(_len > 125) + _len = 125; + _data = (uint8_t*)malloc(_len); + if(_data == NULL) + _len = 0; + else memcpy(_data, data, len); + } else _data = NULL; + } + virtual ~AsyncWebSocketControl(){ + if(_data != NULL) + free(_data); + } + virtual bool finished() const { return _finished; } + uint8_t opcode(){ return _opcode; } + uint8_t len(){ return _len + 2; } + size_t send(AsyncClient *client){ + _finished = true; + return webSocketSendFrame(client, true, _opcode & 0x0F, _mask, _data, _len); + } +}; + +/* + * Basic Buffered Message + */ + + +AsyncWebSocketBasicMessage::AsyncWebSocketBasicMessage(const char * data, size_t len, uint8_t opcode, bool mask) + :_len(len) + ,_sent(0) + ,_ack(0) + ,_acked(0) +{ + _opcode = opcode & 0x07; + _mask = mask; + _data = (uint8_t*)malloc(_len+1); + if(_data == NULL){ + _len = 0; + _status = WS_MSG_ERROR; + } else { + _status = WS_MSG_SENDING; + memcpy(_data, data, _len); + _data[_len] = 0; + } +} +AsyncWebSocketBasicMessage::AsyncWebSocketBasicMessage(uint8_t opcode, bool mask) + :_len(0) + ,_sent(0) + ,_ack(0) + ,_acked(0) + ,_data(NULL) +{ + _opcode = opcode & 0x07; + _mask = mask; + +} + + +AsyncWebSocketBasicMessage::~AsyncWebSocketBasicMessage() { + if(_data != NULL) + free(_data); +} + + void AsyncWebSocketBasicMessage::ack(size_t len, uint32_t time) { + (void)time; + _acked += len; + if(_sent == _len && _acked == _ack){ + _status = WS_MSG_SENT; + } +} + size_t AsyncWebSocketBasicMessage::send(AsyncClient *client) { + if(_status != WS_MSG_SENDING) + return 0; + if(_acked < _ack){ + return 0; + } + if(_sent == _len){ + if(_acked == _ack) + _status = WS_MSG_SENT; + return 0; + } + if(_sent > _len){ + _status = WS_MSG_ERROR; + return 0; + } + + size_t toSend = _len - _sent; + size_t window = webSocketSendFrameWindow(client); + + if(window < toSend) { + toSend = window; + } + + _sent += toSend; + _ack += toSend + ((toSend < 126)?2:4) + (_mask * 4); + + bool final = (_sent == _len); + uint8_t* dPtr = (uint8_t*)(_data + (_sent - toSend)); + uint8_t opCode = (toSend && _sent == toSend)?_opcode:(uint8_t)WS_CONTINUATION; + + size_t sent = webSocketSendFrame(client, final, opCode, _mask, dPtr, toSend); + _status = WS_MSG_SENDING; + if(toSend && sent != toSend){ + _sent -= (toSend - sent); + _ack -= (toSend - sent); + } + return sent; +} + +// bool AsyncWebSocketBasicMessage::reserve(size_t size) { +// if (size) { +// _data = (uint8_t*)malloc(size +1); +// if (_data) { +// memset(_data, 0, size); +// _len = size; +// _status = WS_MSG_SENDING; +// return true; +// } +// } +// return false; +// } + + +/* + * AsyncWebSocketMultiMessage Message + */ + + +AsyncWebSocketMultiMessage::AsyncWebSocketMultiMessage(AsyncWebSocketMessageBuffer * buffer, uint8_t opcode, bool mask) + :_len(0) + ,_sent(0) + ,_ack(0) + ,_acked(0) + ,_WSbuffer(nullptr) +{ + + _opcode = opcode & 0x07; + _mask = mask; + + if (buffer) { + _WSbuffer = buffer; + (*_WSbuffer)++; + _data = buffer->get(); + _len = buffer->length(); + _status = WS_MSG_SENDING; + //ets_printf("M: %u\n", _len); + } else { + _status = WS_MSG_ERROR; + } + +} + + +AsyncWebSocketMultiMessage::~AsyncWebSocketMultiMessage() { + if (_WSbuffer) { + (*_WSbuffer)--; // decreases the counter. + } +} + + void AsyncWebSocketMultiMessage::ack(size_t len, uint32_t time) { + (void)time; + _acked += len; + if(_sent >= _len && _acked >= _ack){ + _status = WS_MSG_SENT; + } + //ets_printf("A: %u\n", len); +} + size_t AsyncWebSocketMultiMessage::send(AsyncClient *client) { + if(_status != WS_MSG_SENDING) + return 0; + if(_acked < _ack){ + return 0; + } + if(_sent == _len){ + _status = WS_MSG_SENT; + return 0; + } + if(_sent > _len){ + _status = WS_MSG_ERROR; + //ets_printf("E: %u > %u\n", _sent, _len); + return 0; + } + + size_t toSend = _len - _sent; + size_t window = webSocketSendFrameWindow(client); + + if(window < toSend) { + toSend = window; + } + + _sent += toSend; + _ack += toSend + ((toSend < 126)?2:4) + (_mask * 4); + + //ets_printf("W: %u %u\n", _sent - toSend, toSend); + + bool final = (_sent == _len); + uint8_t* dPtr = (uint8_t*)(_data + (_sent - toSend)); + uint8_t opCode = (toSend && _sent == toSend)?_opcode:(uint8_t)WS_CONTINUATION; + + size_t sent = webSocketSendFrame(client, final, opCode, _mask, dPtr, toSend); + _status = WS_MSG_SENDING; + if(toSend && sent != toSend){ + //ets_printf("E: %u != %u\n", toSend, sent); + _sent -= (toSend - sent); + _ack -= (toSend - sent); + } + //ets_printf("S: %u %u\n", _sent, sent); + return sent; +} + + +/* + * Async WebSocket Client + */ + const char * AWSC_PING_PAYLOAD = "ESPAsyncWebServer-PING"; + const size_t AWSC_PING_PAYLOAD_LEN = 22; + +AsyncWebSocketClient::AsyncWebSocketClient(AsyncWebServerRequest *request, AsyncWebSocket *server) + : _controlQueue(LinkedList([](AsyncWebSocketControl *c){ delete c; })) + , _messageQueue(LinkedList([](AsyncWebSocketMessage *m){ delete m; })) + , _tempObject(NULL) +{ + _client = request->client(); + _server = server; + _clientId = _server->_getNextId(); + _status = WS_CONNECTED; + _pstate = 0; + _lastMessageTime = millis(); + _keepAlivePeriod = 0; + _client->setRxTimeout(0); + _client->onError([](void *r, AsyncClient* c, int8_t error){ (void)c; ((AsyncWebSocketClient*)(r))->_onError(error); }, this); + _client->onAck([](void *r, AsyncClient* c, size_t len, uint32_t time){ (void)c; ((AsyncWebSocketClient*)(r))->_onAck(len, time); }, this); + _client->onDisconnect([](void *r, AsyncClient* c){ ((AsyncWebSocketClient*)(r))->_onDisconnect(); delete c; }, this); + _client->onTimeout([](void *r, AsyncClient* c, uint32_t time){ (void)c; ((AsyncWebSocketClient*)(r))->_onTimeout(time); }, this); + _client->onData([](void *r, AsyncClient* c, void *buf, size_t len){ (void)c; ((AsyncWebSocketClient*)(r))->_onData(buf, len); }, this); + _client->onPoll([](void *r, AsyncClient* c){ (void)c; ((AsyncWebSocketClient*)(r))->_onPoll(); }, this); + _server->_addClient(this); + _server->_handleEvent(this, WS_EVT_CONNECT, request, NULL, 0); + delete request; +} + +AsyncWebSocketClient::~AsyncWebSocketClient(){ + _messageQueue.free(); + _controlQueue.free(); + _server->_handleEvent(this, WS_EVT_DISCONNECT, NULL, NULL, 0); +} + +void AsyncWebSocketClient::_onAck(size_t len, uint32_t time){ + _lastMessageTime = millis(); + if(!_controlQueue.isEmpty()){ + auto head = _controlQueue.front(); + if(head->finished()){ + len -= head->len(); + if(_status == WS_DISCONNECTING && head->opcode() == WS_DISCONNECT){ + _controlQueue.remove(head); + _status = WS_DISCONNECTED; + _client->close(true); + return; + } + _controlQueue.remove(head); + } + } + if(len && !_messageQueue.isEmpty()){ + _messageQueue.front()->ack(len, time); + } + _server->_cleanBuffers(); + _runQueue(); +} + +void AsyncWebSocketClient::_onPoll(){ + if(_client->canSend() && (!_controlQueue.isEmpty() || !_messageQueue.isEmpty())){ + _runQueue(); + } else if(_keepAlivePeriod > 0 && _controlQueue.isEmpty() && _messageQueue.isEmpty() && (millis() - _lastMessageTime) >= _keepAlivePeriod){ + ping((uint8_t *)AWSC_PING_PAYLOAD, AWSC_PING_PAYLOAD_LEN); + } +} + +void AsyncWebSocketClient::_runQueue(){ + while(!_messageQueue.isEmpty() && _messageQueue.front()->finished()){ + _messageQueue.remove(_messageQueue.front()); + } + + if(!_controlQueue.isEmpty() && (_messageQueue.isEmpty() || _messageQueue.front()->betweenFrames()) && webSocketSendFrameWindow(_client) > (size_t)(_controlQueue.front()->len() - 1)){ + _controlQueue.front()->send(_client); + } else if(!_messageQueue.isEmpty() && _messageQueue.front()->betweenFrames() && webSocketSendFrameWindow(_client)){ + _messageQueue.front()->send(_client); + } +} + +bool AsyncWebSocketClient::queueIsFull(){ + if((_messageQueue.length() >= WS_MAX_QUEUED_MESSAGES) || (_status != WS_CONNECTED) ) return true; + return false; +} + +void AsyncWebSocketClient::_queueMessage(AsyncWebSocketMessage *dataMessage){ + if(dataMessage == NULL) + return; + if(_status != WS_CONNECTED){ + delete dataMessage; + return; + } + if(_messageQueue.length() >= WS_MAX_QUEUED_MESSAGES){ + ets_printf("ERROR: Too many messages queued\n"); + delete dataMessage; + } else { + _messageQueue.add(dataMessage); + } + if(_client->canSend()) + _runQueue(); +} + +void AsyncWebSocketClient::_queueControl(AsyncWebSocketControl *controlMessage){ + if(controlMessage == NULL) + return; + _controlQueue.add(controlMessage); + if(_client->canSend()) + _runQueue(); +} + +void AsyncWebSocketClient::close(uint16_t code, const char * message){ + if(_status != WS_CONNECTED) + return; + if(code){ + uint8_t packetLen = 2; + if(message != NULL){ + size_t mlen = strlen(message); + if(mlen > 123) mlen = 123; + packetLen += mlen; + } + char * buf = (char*)malloc(packetLen); + if(buf != NULL){ + buf[0] = (uint8_t)(code >> 8); + buf[1] = (uint8_t)(code & 0xFF); + if(message != NULL){ + memcpy(buf+2, message, packetLen -2); + } + _queueControl(new AsyncWebSocketControl(WS_DISCONNECT,(uint8_t*)buf,packetLen)); + free(buf); + return; + } + } + _queueControl(new AsyncWebSocketControl(WS_DISCONNECT)); +} + +void AsyncWebSocketClient::ping(uint8_t *data, size_t len){ + if(_status == WS_CONNECTED) + _queueControl(new AsyncWebSocketControl(WS_PING, data, len)); +} + +void AsyncWebSocketClient::_onError(int8_t){} + +void AsyncWebSocketClient::_onTimeout(uint32_t time){ + (void)time; + _client->close(true); +} + +void AsyncWebSocketClient::_onDisconnect(){ + _client = NULL; + _server->_handleDisconnect(this); +} + +void AsyncWebSocketClient::_onData(void *pbuf, size_t plen){ + _lastMessageTime = millis(); + uint8_t *data = (uint8_t*)pbuf; + while(plen > 0){ + if(!_pstate){ + const uint8_t *fdata = data; + _pinfo.index = 0; + _pinfo.final = (fdata[0] & 0x80) != 0; + _pinfo.opcode = fdata[0] & 0x0F; + _pinfo.masked = (fdata[1] & 0x80) != 0; + _pinfo.len = fdata[1] & 0x7F; + data += 2; + plen -= 2; + if(_pinfo.len == 126){ + _pinfo.len = fdata[3] | (uint16_t)(fdata[2]) << 8; + data += 2; + plen -= 2; + } else if(_pinfo.len == 127){ + _pinfo.len = fdata[9] | (uint16_t)(fdata[8]) << 8 | (uint32_t)(fdata[7]) << 16 | (uint32_t)(fdata[6]) << 24 | (uint64_t)(fdata[5]) << 32 | (uint64_t)(fdata[4]) << 40 | (uint64_t)(fdata[3]) << 48 | (uint64_t)(fdata[2]) << 56; + data += 8; + plen -= 8; + } + + if(_pinfo.masked){ + memcpy(_pinfo.mask, data, 4); + data += 4; + plen -= 4; + } + } + + const size_t datalen = std::min((size_t)(_pinfo.len - _pinfo.index), plen); + const auto datalast = data[datalen]; + + if(_pinfo.masked){ + for(size_t i=0;i_handleEvent(this, WS_EVT_DATA, (void *)&_pinfo, (uint8_t*)data, datalen); + + _pinfo.index += datalen; + } else if((datalen + _pinfo.index) == _pinfo.len){ + _pstate = 0; + if(_pinfo.opcode == WS_DISCONNECT){ + if(datalen){ + uint16_t reasonCode = (uint16_t)(data[0] << 8) + data[1]; + char * reasonString = (char*)(data+2); + if(reasonCode > 1001){ + _server->_handleEvent(this, WS_EVT_ERROR, (void *)&reasonCode, (uint8_t*)reasonString, strlen(reasonString)); + } + } + if(_status == WS_DISCONNECTING){ + _status = WS_DISCONNECTED; + _client->close(true); + } else { + _status = WS_DISCONNECTING; + _client->ackLater(); + _queueControl(new AsyncWebSocketControl(WS_DISCONNECT, data, datalen)); + } + } else if(_pinfo.opcode == WS_PING){ + _queueControl(new AsyncWebSocketControl(WS_PONG, data, datalen)); + } else if(_pinfo.opcode == WS_PONG){ + if(datalen != AWSC_PING_PAYLOAD_LEN || memcmp(AWSC_PING_PAYLOAD, data, AWSC_PING_PAYLOAD_LEN) != 0) + _server->_handleEvent(this, WS_EVT_PONG, NULL, data, datalen); + } else if(_pinfo.opcode < 8){//continuation or text/binary frame + _server->_handleEvent(this, WS_EVT_DATA, (void *)&_pinfo, data, datalen); + } + } else { + //os_printf("frame error: len: %u, index: %llu, total: %llu\n", datalen, _pinfo.index, _pinfo.len); + //what should we do? + break; + } + + // restore byte as _handleEvent may have added a null terminator i.e., data[len] = 0; + if (datalen > 0) + data[datalen] = datalast; + + data += datalen; + plen -= datalen; + } +} + +size_t AsyncWebSocketClient::printf(const char *format, ...) { + va_list arg; + va_start(arg, format); + char* temp = new char[MAX_PRINTF_LEN]; + if(!temp){ + va_end(arg); + return 0; + } + char* buffer = temp; + size_t len = vsnprintf(temp, MAX_PRINTF_LEN, format, arg); + va_end(arg); + + if (len > (MAX_PRINTF_LEN - 1)) { + buffer = new char[len + 1]; + if (!buffer) { + delete[] temp; + return 0; + } + va_start(arg, format); + vsnprintf(buffer, len + 1, format, arg); + va_end(arg); + } + text(buffer, len); + if (buffer != temp) { + delete[] buffer; + } + delete[] temp; + return len; +} + +#ifndef ESP32 +size_t AsyncWebSocketClient::printf_P(PGM_P formatP, ...) { + va_list arg; + va_start(arg, formatP); + char* temp = new char[MAX_PRINTF_LEN]; + if(!temp){ + va_end(arg); + return 0; + } + char* buffer = temp; + size_t len = vsnprintf_P(temp, MAX_PRINTF_LEN, formatP, arg); + va_end(arg); + + if (len > (MAX_PRINTF_LEN - 1)) { + buffer = new char[len + 1]; + if (!buffer) { + delete[] temp; + return 0; + } + va_start(arg, formatP); + vsnprintf_P(buffer, len + 1, formatP, arg); + va_end(arg); + } + text(buffer, len); + if (buffer != temp) { + delete[] buffer; + } + delete[] temp; + return len; +} +#endif + +void AsyncWebSocketClient::text(const char * message, size_t len){ + _queueMessage(new AsyncWebSocketBasicMessage(message, len)); +} +void AsyncWebSocketClient::text(const char * message){ + text(message, strlen(message)); +} +void AsyncWebSocketClient::text(uint8_t * message, size_t len){ + text((const char *)message, len); +} +void AsyncWebSocketClient::text(char * message){ + text(message, strlen(message)); +} +void AsyncWebSocketClient::text(const String &message){ + text(message.c_str(), message.length()); +} +void AsyncWebSocketClient::text(const __FlashStringHelper *data){ + PGM_P p = reinterpret_cast(data); + size_t n = 0; + while (1) { + if (pgm_read_byte(p+n) == 0) break; + n += 1; + } + char * message = (char*) malloc(n+1); + if(message){ + for(size_t b=0; b(data); + char * message = (char*) malloc(len); + if(message){ + for(size_t b=0; bremoteIP(); +} + +uint16_t AsyncWebSocketClient::remotePort() { + if(!_client) { + return 0; + } + return _client->remotePort(); +} + + + +/* + * Async Web Socket - Each separate socket location + */ + +AsyncWebSocket::AsyncWebSocket(const String& url) + :_url(url) + ,_clients(LinkedList([](AsyncWebSocketClient *c){ delete c; })) + ,_cNextId(1) + ,_enabled(true) + ,_buffers(LinkedList([](AsyncWebSocketMessageBuffer *b){ delete b; })) +{ + _eventHandler = NULL; +} + +AsyncWebSocket::~AsyncWebSocket(){} + +void AsyncWebSocket::_handleEvent(AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len){ + if(_eventHandler != NULL){ + _eventHandler(this, client, type, arg, data, len); + } +} + +void AsyncWebSocket::_addClient(AsyncWebSocketClient * client){ + _clients.add(client); +} + +void AsyncWebSocket::_handleDisconnect(AsyncWebSocketClient * client){ + + _clients.remove_first([=](AsyncWebSocketClient * c){ + return c->id() == client->id(); + }); +} + +bool AsyncWebSocket::availableForWriteAll(){ + for(const auto& c: _clients){ + if(c->queueIsFull()) return false; + } + return true; +} + +bool AsyncWebSocket::availableForWrite(uint32_t id){ + for(const auto& c: _clients){ + if(c->queueIsFull() && (c->id() == id )) return false; + } + return true; +} + +size_t AsyncWebSocket::count() const { + return _clients.count_if([](AsyncWebSocketClient * c){ + return c->status() == WS_CONNECTED; + }); +} + +AsyncWebSocketClient * AsyncWebSocket::client(uint32_t id){ + for(const auto &c: _clients){ + if(c->id() == id && c->status() == WS_CONNECTED){ + return c; + } + } + return nullptr; +} + + +void AsyncWebSocket::close(uint32_t id, uint16_t code, const char * message){ + AsyncWebSocketClient * c = client(id); + if(c) + c->close(code, message); +} + +void AsyncWebSocket::closeAll(uint16_t code, const char * message){ + for(const auto& c: _clients){ + if(c->status() == WS_CONNECTED) + c->close(code, message); + } +} + +void AsyncWebSocket::cleanupClients(uint16_t maxClients) +{ + if (count() > maxClients){ + _clients.front()->close(); + } +} + +void AsyncWebSocket::ping(uint32_t id, uint8_t *data, size_t len){ + AsyncWebSocketClient * c = client(id); + if(c) + c->ping(data, len); +} + +void AsyncWebSocket::pingAll(uint8_t *data, size_t len){ + for(const auto& c: _clients){ + if(c->status() == WS_CONNECTED) + c->ping(data, len); + } +} + +void AsyncWebSocket::text(uint32_t id, const char * message, size_t len){ + AsyncWebSocketClient * c = client(id); + if(c) + c->text(message, len); +} + +void AsyncWebSocket::textAll(AsyncWebSocketMessageBuffer * buffer){ + if (!buffer) return; + buffer->lock(); + for(const auto& c: _clients){ + if(c->status() == WS_CONNECTED){ + c->text(buffer); + } + } + buffer->unlock(); + _cleanBuffers(); +} + + +void AsyncWebSocket::textAll(const char * message, size_t len){ + AsyncWebSocketMessageBuffer * WSBuffer = makeBuffer((uint8_t *)message, len); + textAll(WSBuffer); +} + +void AsyncWebSocket::binary(uint32_t id, const char * message, size_t len){ + AsyncWebSocketClient * c = client(id); + if(c) + c->binary(message, len); +} + +void AsyncWebSocket::binaryAll(const char * message, size_t len){ + AsyncWebSocketMessageBuffer * buffer = makeBuffer((uint8_t *)message, len); + binaryAll(buffer); +} + +void AsyncWebSocket::binaryAll(AsyncWebSocketMessageBuffer * buffer) +{ + if (!buffer) return; + buffer->lock(); + for(const auto& c: _clients){ + if(c->status() == WS_CONNECTED) + c->binary(buffer); + } + buffer->unlock(); + _cleanBuffers(); +} + +void AsyncWebSocket::message(uint32_t id, AsyncWebSocketMessage *message){ + AsyncWebSocketClient * c = client(id); + if(c) + c->message(message); +} + +void AsyncWebSocket::messageAll(AsyncWebSocketMultiMessage *message){ + for(const auto& c: _clients){ + if(c->status() == WS_CONNECTED) + c->message(message); + } + _cleanBuffers(); +} + +size_t AsyncWebSocket::printf(uint32_t id, const char *format, ...){ + AsyncWebSocketClient * c = client(id); + if(c){ + va_list arg; + va_start(arg, format); + size_t len = c->printf(format, arg); + va_end(arg); + return len; + } + return 0; +} + +size_t AsyncWebSocket::printfAll(const char *format, ...) { + va_list arg; + char* temp = new char[MAX_PRINTF_LEN]; + if(!temp){ + return 0; + } + va_start(arg, format); + size_t len = vsnprintf(temp, MAX_PRINTF_LEN, format, arg); + va_end(arg); + delete[] temp; + + AsyncWebSocketMessageBuffer * buffer = makeBuffer(len); + if (!buffer) { + return 0; + } + + va_start(arg, format); + vsnprintf( (char *)buffer->get(), len + 1, format, arg); + va_end(arg); + + textAll(buffer); + return len; +} + +#ifndef ESP32 +size_t AsyncWebSocket::printf_P(uint32_t id, PGM_P formatP, ...){ + AsyncWebSocketClient * c = client(id); + if(c != NULL){ + va_list arg; + va_start(arg, formatP); + size_t len = c->printf_P(formatP, arg); + va_end(arg); + return len; + } + return 0; +} +#endif + +size_t AsyncWebSocket::printfAll_P(PGM_P formatP, ...) { + va_list arg; + char* temp = new char[MAX_PRINTF_LEN]; + if(!temp){ + return 0; + } + va_start(arg, formatP); + size_t len = vsnprintf_P(temp, MAX_PRINTF_LEN, formatP, arg); + va_end(arg); + delete[] temp; + + AsyncWebSocketMessageBuffer * buffer = makeBuffer(len + 1); + if (!buffer) { + return 0; + } + + va_start(arg, formatP); + vsnprintf_P((char *)buffer->get(), len + 1, formatP, arg); + va_end(arg); + + textAll(buffer); + return len; +} + +void AsyncWebSocket::text(uint32_t id, const char * message){ + text(id, message, strlen(message)); +} +void AsyncWebSocket::text(uint32_t id, uint8_t * message, size_t len){ + text(id, (const char *)message, len); +} +void AsyncWebSocket::text(uint32_t id, char * message){ + text(id, message, strlen(message)); +} +void AsyncWebSocket::text(uint32_t id, const String &message){ + text(id, message.c_str(), message.length()); +} +void AsyncWebSocket::text(uint32_t id, const __FlashStringHelper *message){ + AsyncWebSocketClient * c = client(id); + if(c != NULL) + c->text(message); +} +void AsyncWebSocket::textAll(const char * message){ + textAll(message, strlen(message)); +} +void AsyncWebSocket::textAll(uint8_t * message, size_t len){ + textAll((const char *)message, len); +} +void AsyncWebSocket::textAll(char * message){ + textAll(message, strlen(message)); +} +void AsyncWebSocket::textAll(const String &message){ + textAll(message.c_str(), message.length()); +} +void AsyncWebSocket::textAll(const __FlashStringHelper *message){ + for(const auto& c: _clients){ + if(c->status() == WS_CONNECTED) + c->text(message); + } +} +void AsyncWebSocket::binary(uint32_t id, const char * message){ + binary(id, message, strlen(message)); +} +void AsyncWebSocket::binary(uint32_t id, uint8_t * message, size_t len){ + binary(id, (const char *)message, len); +} +void AsyncWebSocket::binary(uint32_t id, char * message){ + binary(id, message, strlen(message)); +} +void AsyncWebSocket::binary(uint32_t id, const String &message){ + binary(id, message.c_str(), message.length()); +} +void AsyncWebSocket::binary(uint32_t id, const __FlashStringHelper *message, size_t len){ + AsyncWebSocketClient * c = client(id); + if(c != NULL) + c-> binary(message, len); +} +void AsyncWebSocket::binaryAll(const char * message){ + binaryAll(message, strlen(message)); +} +void AsyncWebSocket::binaryAll(uint8_t * message, size_t len){ + binaryAll((const char *)message, len); +} +void AsyncWebSocket::binaryAll(char * message){ + binaryAll(message, strlen(message)); +} +void AsyncWebSocket::binaryAll(const String &message){ + binaryAll(message.c_str(), message.length()); +} +void AsyncWebSocket::binaryAll(const __FlashStringHelper *message, size_t len){ + for(const auto& c: _clients){ + if(c->status() == WS_CONNECTED) + c-> binary(message, len); + } + } + +const char * WS_STR_CONNECTION = "Connection"; +const char * WS_STR_UPGRADE = "Upgrade"; +const char * WS_STR_ORIGIN = "Origin"; +const char * WS_STR_VERSION = "Sec-WebSocket-Version"; +const char * WS_STR_KEY = "Sec-WebSocket-Key"; +const char * WS_STR_PROTOCOL = "Sec-WebSocket-Protocol"; +const char * WS_STR_ACCEPT = "Sec-WebSocket-Accept"; +const char * WS_STR_UUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; + +bool AsyncWebSocket::canHandle(AsyncWebServerRequest *request){ + if(!_enabled) + return false; + + if(request->method() != HTTP_GET || !request->url().equals(_url) || !request->isExpectedRequestedConnType(RCT_WS)) + return false; + + request->addInterestingHeader(WS_STR_CONNECTION); + request->addInterestingHeader(WS_STR_UPGRADE); + request->addInterestingHeader(WS_STR_ORIGIN); + request->addInterestingHeader(WS_STR_VERSION); + request->addInterestingHeader(WS_STR_KEY); + request->addInterestingHeader(WS_STR_PROTOCOL); + return true; +} + +void AsyncWebSocket::handleRequest(AsyncWebServerRequest *request){ + if(!request->hasHeader(WS_STR_VERSION) || !request->hasHeader(WS_STR_KEY)){ + request->send(400); + return; + } + if((_username != "" && _password != "") && !request->authenticate(_username.c_str(), _password.c_str())){ + return request->requestAuthentication(); + } + AsyncWebHeader* version = request->getHeader(WS_STR_VERSION); + if(version->value().toInt() != 13){ + AsyncWebServerResponse *response = request->beginResponse(400); + response->addHeader(WS_STR_VERSION,"13"); + request->send(response); + return; + } + AsyncWebHeader* key = request->getHeader(WS_STR_KEY); + AsyncWebServerResponse *response = new AsyncWebSocketResponse(key->value(), this); + if(request->hasHeader(WS_STR_PROTOCOL)){ + AsyncWebHeader* protocol = request->getHeader(WS_STR_PROTOCOL); + //ToDo: check protocol + response->addHeader(WS_STR_PROTOCOL, protocol->value()); + } + request->send(response); +} + +AsyncWebSocketMessageBuffer * AsyncWebSocket::makeBuffer(size_t size) +{ + AsyncWebSocketMessageBuffer * buffer = new AsyncWebSocketMessageBuffer(size); + if (buffer) { + AsyncWebLockGuard l(_lock); + _buffers.add(buffer); + } + return buffer; +} + +AsyncWebSocketMessageBuffer * AsyncWebSocket::makeBuffer(uint8_t * data, size_t size) +{ + AsyncWebSocketMessageBuffer * buffer = new AsyncWebSocketMessageBuffer(data, size); + + if (buffer) { + AsyncWebLockGuard l(_lock); + _buffers.add(buffer); + } + + return buffer; +} + +void AsyncWebSocket::_cleanBuffers() +{ + AsyncWebLockGuard l(_lock); + + for(AsyncWebSocketMessageBuffer * c: _buffers){ + if(c && c->canDelete()){ + _buffers.remove(c); + } + } +} + +AsyncWebSocket::AsyncWebSocketClientLinkedList AsyncWebSocket::getClients() const { + return _clients; +} + +/* + * Response to Web Socket request - sends the authorization and detaches the TCP Client from the web server + * Authentication code from https://github.com/Links2004/arduinoWebSockets/blob/master/src/WebSockets.cpp#L480 + */ + +AsyncWebSocketResponse::AsyncWebSocketResponse(const String& key, AsyncWebSocket *server){ + _server = server; + _code = 101; + _sendContentLength = false; + + uint8_t * hash = (uint8_t*)malloc(20); + if(hash == NULL){ + _state = RESPONSE_FAILED; + return; + } + char * buffer = (char *) malloc(33); + if(buffer == NULL){ + free(hash); + _state = RESPONSE_FAILED; + return; + } +#ifdef ESP8266 + sha1(key + WS_STR_UUID, hash); +#else + (String&)key += WS_STR_UUID; + SHA1_CTX ctx; + SHA1Init(&ctx); + SHA1Update(&ctx, (const unsigned char*)key.c_str(), key.length()); + SHA1Final(hash, &ctx); +#endif + base64_encodestate _state; + base64_init_encodestate(&_state); + int len = base64_encode_block((const char *) hash, 20, buffer, &_state); + len = base64_encode_blockend((buffer + len), &_state); + addHeader(WS_STR_CONNECTION, WS_STR_UPGRADE); + addHeader(WS_STR_UPGRADE, "websocket"); + addHeader(WS_STR_ACCEPT,buffer); + free(buffer); + free(hash); +} + +void AsyncWebSocketResponse::_respond(AsyncWebServerRequest *request){ + if(_state == RESPONSE_FAILED){ + request->client()->close(true); + return; + } + String out = _assembleHead(request->version()); + request->client()->write(out.c_str(), _headLength); + _state = RESPONSE_WAIT_ACK; +} + +size_t AsyncWebSocketResponse::_ack(AsyncWebServerRequest *request, size_t len, uint32_t time){ + (void)time; + if(len){ + new AsyncWebSocketClient(request, _server); + } + return 0; +} diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/AsyncWebSocket.h b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/AsyncWebSocket.h new file mode 100644 index 0000000..5b03ace --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/AsyncWebSocket.h @@ -0,0 +1,350 @@ +/* + Asynchronous WebServer library for Espressif MCUs + + Copyright (c) 2016 Hristo Gochkov. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ +#ifndef ASYNCWEBSOCKET_H_ +#define ASYNCWEBSOCKET_H_ + +#include +#ifdef ESP32 +#include +#define WS_MAX_QUEUED_MESSAGES 32 +#else +#include +#define WS_MAX_QUEUED_MESSAGES 8 +#endif +#include + +#include "AsyncWebSynchronization.h" + +#ifdef ESP8266 +#include +#ifdef CRYPTO_HASH_h // include Hash.h from espressif framework if the first include was from the crypto library +#include <../src/Hash.h> +#endif +#endif + +#ifdef ESP32 +#define DEFAULT_MAX_WS_CLIENTS 8 +#else +#define DEFAULT_MAX_WS_CLIENTS 4 +#endif + +class AsyncWebSocket; +class AsyncWebSocketResponse; +class AsyncWebSocketClient; +class AsyncWebSocketControl; + +typedef struct { + /** Message type as defined by enum AwsFrameType. + * Note: Applications will only see WS_TEXT and WS_BINARY. + * All other types are handled by the library. */ + uint8_t message_opcode; + /** Frame number of a fragmented message. */ + uint32_t num; + /** Is this the last frame in a fragmented message ?*/ + uint8_t final; + /** Is this frame masked? */ + uint8_t masked; + /** Message type as defined by enum AwsFrameType. + * This value is the same as message_opcode for non-fragmented + * messages, but may also be WS_CONTINUATION in a fragmented message. */ + uint8_t opcode; + /** Length of the current frame. + * This equals the total length of the message if num == 0 && final == true */ + uint64_t len; + /** Mask key */ + uint8_t mask[4]; + /** Offset of the data inside the current frame. */ + uint64_t index; +} AwsFrameInfo; + +typedef enum { WS_DISCONNECTED, WS_CONNECTED, WS_DISCONNECTING } AwsClientStatus; +typedef enum { WS_CONTINUATION, WS_TEXT, WS_BINARY, WS_DISCONNECT = 0x08, WS_PING, WS_PONG } AwsFrameType; +typedef enum { WS_MSG_SENDING, WS_MSG_SENT, WS_MSG_ERROR } AwsMessageStatus; +typedef enum { WS_EVT_CONNECT, WS_EVT_DISCONNECT, WS_EVT_PONG, WS_EVT_ERROR, WS_EVT_DATA } AwsEventType; + +class AsyncWebSocketMessageBuffer { + private: + uint8_t * _data; + size_t _len; + bool _lock; + uint32_t _count; + + public: + AsyncWebSocketMessageBuffer(); + AsyncWebSocketMessageBuffer(size_t size); + AsyncWebSocketMessageBuffer(uint8_t * data, size_t size); + AsyncWebSocketMessageBuffer(const AsyncWebSocketMessageBuffer &); + AsyncWebSocketMessageBuffer(AsyncWebSocketMessageBuffer &&); + ~AsyncWebSocketMessageBuffer(); + void operator ++(int i) { (void)i; _count++; } + void operator --(int i) { (void)i; if (_count > 0) { _count--; } ; } + bool reserve(size_t size); + void lock() { _lock = true; } + void unlock() { _lock = false; } + uint8_t * get() { return _data; } + size_t length() { return _len; } + uint32_t count() { return _count; } + bool canDelete() { return (!_count && !_lock); } + + friend AsyncWebSocket; + +}; + +class AsyncWebSocketMessage { + protected: + uint8_t _opcode; + bool _mask; + AwsMessageStatus _status; + public: + AsyncWebSocketMessage():_opcode(WS_TEXT),_mask(false),_status(WS_MSG_ERROR){} + virtual ~AsyncWebSocketMessage(){} + virtual void ack(size_t len __attribute__((unused)), uint32_t time __attribute__((unused))){} + virtual size_t send(AsyncClient *client __attribute__((unused))){ return 0; } + virtual bool finished(){ return _status != WS_MSG_SENDING; } + virtual bool betweenFrames() const { return false; } +}; + +class AsyncWebSocketBasicMessage: public AsyncWebSocketMessage { + private: + size_t _len; + size_t _sent; + size_t _ack; + size_t _acked; + uint8_t * _data; +public: + AsyncWebSocketBasicMessage(const char * data, size_t len, uint8_t opcode=WS_TEXT, bool mask=false); + AsyncWebSocketBasicMessage(uint8_t opcode=WS_TEXT, bool mask=false); + virtual ~AsyncWebSocketBasicMessage() override; + virtual bool betweenFrames() const override { return _acked == _ack; } + virtual void ack(size_t len, uint32_t time) override ; + virtual size_t send(AsyncClient *client) override ; +}; + +class AsyncWebSocketMultiMessage: public AsyncWebSocketMessage { + private: + uint8_t * _data; + size_t _len; + size_t _sent; + size_t _ack; + size_t _acked; + AsyncWebSocketMessageBuffer * _WSbuffer; +public: + AsyncWebSocketMultiMessage(AsyncWebSocketMessageBuffer * buffer, uint8_t opcode=WS_TEXT, bool mask=false); + virtual ~AsyncWebSocketMultiMessage() override; + virtual bool betweenFrames() const override { return _acked == _ack; } + virtual void ack(size_t len, uint32_t time) override ; + virtual size_t send(AsyncClient *client) override ; +}; + +class AsyncWebSocketClient { + private: + AsyncClient *_client; + AsyncWebSocket *_server; + uint32_t _clientId; + AwsClientStatus _status; + + LinkedList _controlQueue; + LinkedList _messageQueue; + + uint8_t _pstate; + AwsFrameInfo _pinfo; + + uint32_t _lastMessageTime; + uint32_t _keepAlivePeriod; + + void _queueMessage(AsyncWebSocketMessage *dataMessage); + void _queueControl(AsyncWebSocketControl *controlMessage); + void _runQueue(); + + public: + void *_tempObject; + + AsyncWebSocketClient(AsyncWebServerRequest *request, AsyncWebSocket *server); + ~AsyncWebSocketClient(); + + //client id increments for the given server + uint32_t id(){ return _clientId; } + AwsClientStatus status(){ return _status; } + AsyncClient* client(){ return _client; } + AsyncWebSocket *server(){ return _server; } + AwsFrameInfo const &pinfo() const { return _pinfo; } + + IPAddress remoteIP(); + uint16_t remotePort(); + + //control frames + void close(uint16_t code=0, const char * message=NULL); + void ping(uint8_t *data=NULL, size_t len=0); + + //set auto-ping period in seconds. disabled if zero (default) + void keepAlivePeriod(uint16_t seconds){ + _keepAlivePeriod = seconds * 1000; + } + uint16_t keepAlivePeriod(){ + return (uint16_t)(_keepAlivePeriod / 1000); + } + + //data packets + void message(AsyncWebSocketMessage *message){ _queueMessage(message); } + bool queueIsFull(); + + size_t printf(const char *format, ...) __attribute__ ((format (printf, 2, 3))); +#ifndef ESP32 + size_t printf_P(PGM_P formatP, ...) __attribute__ ((format (printf, 2, 3))); +#endif + void text(const char * message, size_t len); + void text(const char * message); + void text(uint8_t * message, size_t len); + void text(char * message); + void text(const String &message); + void text(const __FlashStringHelper *data); + void text(AsyncWebSocketMessageBuffer *buffer); + + void binary(const char * message, size_t len); + void binary(const char * message); + void binary(uint8_t * message, size_t len); + void binary(char * message); + void binary(const String &message); + void binary(const __FlashStringHelper *data, size_t len); + void binary(AsyncWebSocketMessageBuffer *buffer); + + bool canSend() { return _messageQueue.length() < WS_MAX_QUEUED_MESSAGES; } + + //system callbacks (do not call) + void _onAck(size_t len, uint32_t time); + void _onError(int8_t); + void _onPoll(); + void _onTimeout(uint32_t time); + void _onDisconnect(); + void _onData(void *pbuf, size_t plen); +}; + +typedef std::function AwsEventHandler; + +//WebServer Handler implementation that plays the role of a socket server +class AsyncWebSocket: public AsyncWebHandler { + public: + typedef LinkedList AsyncWebSocketClientLinkedList; + private: + String _url; + AsyncWebSocketClientLinkedList _clients; + uint32_t _cNextId; + AwsEventHandler _eventHandler; + bool _enabled; + AsyncWebLock _lock; + + public: + AsyncWebSocket(const String& url); + ~AsyncWebSocket(); + const char * url() const { return _url.c_str(); } + void enable(bool e){ _enabled = e; } + bool enabled() const { return _enabled; } + bool availableForWriteAll(); + bool availableForWrite(uint32_t id); + + size_t count() const; + AsyncWebSocketClient * client(uint32_t id); + bool hasClient(uint32_t id){ return client(id) != NULL; } + + void close(uint32_t id, uint16_t code=0, const char * message=NULL); + void closeAll(uint16_t code=0, const char * message=NULL); + void cleanupClients(uint16_t maxClients = DEFAULT_MAX_WS_CLIENTS); + + void ping(uint32_t id, uint8_t *data=NULL, size_t len=0); + void pingAll(uint8_t *data=NULL, size_t len=0); // done + + void text(uint32_t id, const char * message, size_t len); + void text(uint32_t id, const char * message); + void text(uint32_t id, uint8_t * message, size_t len); + void text(uint32_t id, char * message); + void text(uint32_t id, const String &message); + void text(uint32_t id, const __FlashStringHelper *message); + + void textAll(const char * message, size_t len); + void textAll(const char * message); + void textAll(uint8_t * message, size_t len); + void textAll(char * message); + void textAll(const String &message); + void textAll(const __FlashStringHelper *message); // need to convert + void textAll(AsyncWebSocketMessageBuffer * buffer); + + void binary(uint32_t id, const char * message, size_t len); + void binary(uint32_t id, const char * message); + void binary(uint32_t id, uint8_t * message, size_t len); + void binary(uint32_t id, char * message); + void binary(uint32_t id, const String &message); + void binary(uint32_t id, const __FlashStringHelper *message, size_t len); + + void binaryAll(const char * message, size_t len); + void binaryAll(const char * message); + void binaryAll(uint8_t * message, size_t len); + void binaryAll(char * message); + void binaryAll(const String &message); + void binaryAll(const __FlashStringHelper *message, size_t len); + void binaryAll(AsyncWebSocketMessageBuffer * buffer); + + void message(uint32_t id, AsyncWebSocketMessage *message); + void messageAll(AsyncWebSocketMultiMessage *message); + + size_t printf(uint32_t id, const char *format, ...) __attribute__ ((format (printf, 3, 4))); + size_t printfAll(const char *format, ...) __attribute__ ((format (printf, 2, 3))); +#ifndef ESP32 + size_t printf_P(uint32_t id, PGM_P formatP, ...) __attribute__ ((format (printf, 3, 4))); +#endif + size_t printfAll_P(PGM_P formatP, ...) __attribute__ ((format (printf, 2, 3))); + + //event listener + void onEvent(AwsEventHandler handler){ + _eventHandler = handler; + } + + //system callbacks (do not call) + uint32_t _getNextId(){ return _cNextId++; } + void _addClient(AsyncWebSocketClient * client); + void _handleDisconnect(AsyncWebSocketClient * client); + void _handleEvent(AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len); + virtual bool canHandle(AsyncWebServerRequest *request) override final; + virtual void handleRequest(AsyncWebServerRequest *request) override final; + + + // messagebuffer functions/objects. + AsyncWebSocketMessageBuffer * makeBuffer(size_t size = 0); + AsyncWebSocketMessageBuffer * makeBuffer(uint8_t * data, size_t size); + LinkedList _buffers; + void _cleanBuffers(); + + AsyncWebSocketClientLinkedList getClients() const; +}; + +//WebServer response to authenticate the socket and detach the tcp client from the web server request +class AsyncWebSocketResponse: public AsyncWebServerResponse { + private: + String _content; + AsyncWebSocket *_server; + public: + AsyncWebSocketResponse(const String& key, AsyncWebSocket *server); + void _respond(AsyncWebServerRequest *request); + size_t _ack(AsyncWebServerRequest *request, size_t len, uint32_t time); + bool _sourceValid() const { return true; } +}; + + +#endif /* ASYNCWEBSOCKET_H_ */ diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/AsyncWebSynchronization.h b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/AsyncWebSynchronization.h new file mode 100644 index 0000000..f36c52d --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/AsyncWebSynchronization.h @@ -0,0 +1,87 @@ +#ifndef ASYNCWEBSYNCHRONIZATION_H_ +#define ASYNCWEBSYNCHRONIZATION_H_ + +// Synchronisation is only available on ESP32, as the ESP8266 isn't using FreeRTOS by default + +#include + +#ifdef ESP32 + +// This is the ESP32 version of the Sync Lock, using the FreeRTOS Semaphore +class AsyncWebLock +{ +private: + SemaphoreHandle_t _lock; + mutable void *_lockedBy; + +public: + AsyncWebLock() { + _lock = xSemaphoreCreateBinary(); + _lockedBy = NULL; + xSemaphoreGive(_lock); + } + + ~AsyncWebLock() { + vSemaphoreDelete(_lock); + } + + bool lock() const { + extern void *pxCurrentTCB; + if (_lockedBy != pxCurrentTCB) { + xSemaphoreTake(_lock, portMAX_DELAY); + _lockedBy = pxCurrentTCB; + return true; + } + return false; + } + + void unlock() const { + _lockedBy = NULL; + xSemaphoreGive(_lock); + } +}; + +#else + +// This is the 8266 version of the Sync Lock which is currently unimplemented +class AsyncWebLock +{ + +public: + AsyncWebLock() { + } + + ~AsyncWebLock() { + } + + bool lock() const { + return false; + } + + void unlock() const { + } +}; +#endif + +class AsyncWebLockGuard +{ +private: + const AsyncWebLock *_lock; + +public: + AsyncWebLockGuard(const AsyncWebLock &l) { + if (l.lock()) { + _lock = &l; + } else { + _lock = NULL; + } + } + + ~AsyncWebLockGuard() { + if (_lock) { + _lock->unlock(); + } + } +}; + +#endif // ASYNCWEBSYNCHRONIZATION_H_ \ No newline at end of file diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/ESPAsyncWebServer.h b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/ESPAsyncWebServer.h new file mode 100644 index 0000000..7cd21aa --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/ESPAsyncWebServer.h @@ -0,0 +1,471 @@ +/* + Asynchronous WebServer library for Espressif MCUs + + Copyright (c) 2016 Hristo Gochkov. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ +#ifndef _ESPAsyncWebServer_H_ +#define _ESPAsyncWebServer_H_ + +#include "Arduino.h" + +#include +#include "FS.h" + +#include "StringArray.h" + +#ifdef ESP32 +#include +#include +#elif defined(ESP8266) +#include +#include +#else +#error Platform not supported +#endif + +#ifdef ASYNCWEBSERVER_REGEX +#define ASYNCWEBSERVER_REGEX_ATTRIBUTE +#else +#define ASYNCWEBSERVER_REGEX_ATTRIBUTE __attribute__((warning("ASYNCWEBSERVER_REGEX not defined"))) +#endif + +#define DEBUGF(...) //Serial.printf(__VA_ARGS__) + +class AsyncWebServer; +class AsyncWebServerRequest; +class AsyncWebServerResponse; +class AsyncWebHeader; +class AsyncWebParameter; +class AsyncWebRewrite; +class AsyncWebHandler; +class AsyncStaticWebHandler; +class AsyncCallbackWebHandler; +class AsyncResponseStream; + +#ifndef WEBSERVER_H +typedef enum { + HTTP_GET = 0b00000001, + HTTP_POST = 0b00000010, + HTTP_DELETE = 0b00000100, + HTTP_PUT = 0b00001000, + HTTP_PATCH = 0b00010000, + HTTP_HEAD = 0b00100000, + HTTP_OPTIONS = 0b01000000, + HTTP_ANY = 0b01111111, +} WebRequestMethod; +#endif + +//if this value is returned when asked for data, packet will not be sent and you will be asked for data again +#define RESPONSE_TRY_AGAIN 0xFFFFFFFF + +typedef uint8_t WebRequestMethodComposite; +typedef std::function ArDisconnectHandler; + +/* + * PARAMETER :: Chainable object to hold GET/POST and FILE parameters + * */ + +class AsyncWebParameter { + private: + String _name; + String _value; + size_t _size; + bool _isForm; + bool _isFile; + + public: + + AsyncWebParameter(const String& name, const String& value, bool form=false, bool file=false, size_t size=0): _name(name), _value(value), _size(size), _isForm(form), _isFile(file){} + const String& name() const { return _name; } + const String& value() const { return _value; } + size_t size() const { return _size; } + bool isPost() const { return _isForm; } + bool isFile() const { return _isFile; } +}; + +/* + * HEADER :: Chainable object to hold the headers + * */ + +class AsyncWebHeader { + private: + String _name; + String _value; + + public: + AsyncWebHeader(const String& name, const String& value): _name(name), _value(value){} + AsyncWebHeader(const String& data): _name(), _value(){ + if(!data) return; + int index = data.indexOf(':'); + if (index < 0) return; + _name = data.substring(0, index); + _value = data.substring(index + 2); + } + ~AsyncWebHeader(){} + const String& name() const { return _name; } + const String& value() const { return _value; } + String toString() const { return String(_name+": "+_value+"\r\n"); } +}; + +/* + * REQUEST :: Each incoming Client is wrapped inside a Request and both live together until disconnect + * */ + +typedef enum { RCT_NOT_USED = -1, RCT_DEFAULT = 0, RCT_HTTP, RCT_WS, RCT_EVENT, RCT_MAX } RequestedConnectionType; + +typedef std::function AwsResponseFiller; +typedef std::function AwsTemplateProcessor; + +class AsyncWebServerRequest { + using File = fs::File; + using FS = fs::FS; + friend class AsyncWebServer; + friend class AsyncCallbackWebHandler; + private: + AsyncClient* _client; + AsyncWebServer* _server; + AsyncWebHandler* _handler; + AsyncWebServerResponse* _response; + StringArray _interestingHeaders; + ArDisconnectHandler _onDisconnectfn; + + String _temp; + uint8_t _parseState; + + uint8_t _version; + WebRequestMethodComposite _method; + String _url; + String _host; + String _contentType; + String _boundary; + String _authorization; + RequestedConnectionType _reqconntype; + void _removeNotInterestingHeaders(); + bool _isDigest; + bool _isMultipart; + bool _isPlainPost; + bool _expectingContinue; + size_t _contentLength; + size_t _parsedLength; + + LinkedList _headers; + LinkedList _params; + LinkedList _pathParams; + + uint8_t _multiParseState; + uint8_t _boundaryPosition; + size_t _itemStartIndex; + size_t _itemSize; + String _itemName; + String _itemFilename; + String _itemType; + String _itemValue; + uint8_t *_itemBuffer; + size_t _itemBufferIndex; + bool _itemIsFile; + + void _onPoll(); + void _onAck(size_t len, uint32_t time); + void _onError(int8_t error); + void _onTimeout(uint32_t time); + void _onDisconnect(); + void _onData(void *buf, size_t len); + + void _addParam(AsyncWebParameter*); + void _addPathParam(const char *param); + + bool _parseReqHead(); + bool _parseReqHeader(); + void _parseLine(); + void _parsePlainPostChar(uint8_t data); + void _parseMultipartPostByte(uint8_t data, bool last); + void _addGetParams(const String& params); + + void _handleUploadStart(); + void _handleUploadByte(uint8_t data, bool last); + void _handleUploadEnd(); + + public: + File _tempFile; + void *_tempObject; + + AsyncWebServerRequest(AsyncWebServer*, AsyncClient*); + ~AsyncWebServerRequest(); + + AsyncClient* client(){ return _client; } + uint8_t version() const { return _version; } + WebRequestMethodComposite method() const { return _method; } + const String& url() const { return _url; } + const String& host() const { return _host; } + const String& contentType() const { return _contentType; } + size_t contentLength() const { return _contentLength; } + bool multipart() const { return _isMultipart; } + const char * methodToString() const; + const char * requestedConnTypeToString() const; + RequestedConnectionType requestedConnType() const { return _reqconntype; } + bool isExpectedRequestedConnType(RequestedConnectionType erct1, RequestedConnectionType erct2 = RCT_NOT_USED, RequestedConnectionType erct3 = RCT_NOT_USED); + void onDisconnect (ArDisconnectHandler fn); + + //hash is the string representation of: + // base64(user:pass) for basic or + // user:realm:md5(user:realm:pass) for digest + bool authenticate(const char * hash); + bool authenticate(const char * username, const char * password, const char * realm = NULL, bool passwordIsHash = false); + void requestAuthentication(const char * realm = NULL, bool isDigest = true); + + void setHandler(AsyncWebHandler *handler){ _handler = handler; } + void addInterestingHeader(const String& name); + + void redirect(const String& url); + + void send(AsyncWebServerResponse *response); + void send(int code, const String& contentType=String(), const String& content=String()); + void send(FS &fs, const String& path, const String& contentType=String(), bool download=false, AwsTemplateProcessor callback=nullptr); + void send(File content, const String& path, const String& contentType=String(), bool download=false, AwsTemplateProcessor callback=nullptr); + void send(Stream &stream, const String& contentType, size_t len, AwsTemplateProcessor callback=nullptr); + void send(const String& contentType, size_t len, AwsResponseFiller callback, AwsTemplateProcessor templateCallback=nullptr); + void sendChunked(const String& contentType, AwsResponseFiller callback, AwsTemplateProcessor templateCallback=nullptr); + void send_P(int code, const String& contentType, const uint8_t * content, size_t len, AwsTemplateProcessor callback=nullptr); + void send_P(int code, const String& contentType, PGM_P content, AwsTemplateProcessor callback=nullptr); + + AsyncWebServerResponse *beginResponse(int code, const String& contentType=String(), const String& content=String()); + AsyncWebServerResponse *beginResponse(FS &fs, const String& path, const String& contentType=String(), bool download=false, AwsTemplateProcessor callback=nullptr); + AsyncWebServerResponse *beginResponse(File content, const String& path, const String& contentType=String(), bool download=false, AwsTemplateProcessor callback=nullptr); + AsyncWebServerResponse *beginResponse(Stream &stream, const String& contentType, size_t len, AwsTemplateProcessor callback=nullptr); + AsyncWebServerResponse *beginResponse(const String& contentType, size_t len, AwsResponseFiller callback, AwsTemplateProcessor templateCallback=nullptr); + AsyncWebServerResponse *beginChunkedResponse(const String& contentType, AwsResponseFiller callback, AwsTemplateProcessor templateCallback=nullptr); + AsyncResponseStream *beginResponseStream(const String& contentType, size_t bufferSize=1460); + AsyncWebServerResponse *beginResponse_P(int code, const String& contentType, const uint8_t * content, size_t len, AwsTemplateProcessor callback=nullptr); + AsyncWebServerResponse *beginResponse_P(int code, const String& contentType, PGM_P content, AwsTemplateProcessor callback=nullptr); + + size_t headers() const; // get header count + bool hasHeader(const String& name) const; // check if header exists + bool hasHeader(const __FlashStringHelper * data) const; // check if header exists + + AsyncWebHeader* getHeader(const String& name) const; + AsyncWebHeader* getHeader(const __FlashStringHelper * data) const; + AsyncWebHeader* getHeader(size_t num) const; + + size_t params() const; // get arguments count + bool hasParam(const String& name, bool post=false, bool file=false) const; + bool hasParam(const __FlashStringHelper * data, bool post=false, bool file=false) const; + + AsyncWebParameter* getParam(const String& name, bool post=false, bool file=false) const; + AsyncWebParameter* getParam(const __FlashStringHelper * data, bool post, bool file) const; + AsyncWebParameter* getParam(size_t num) const; + + size_t args() const { return params(); } // get arguments count + const String& arg(const String& name) const; // get request argument value by name + const String& arg(const __FlashStringHelper * data) const; // get request argument value by F(name) + const String& arg(size_t i) const; // get request argument value by number + const String& argName(size_t i) const; // get request argument name by number + bool hasArg(const char* name) const; // check if argument exists + bool hasArg(const __FlashStringHelper * data) const; // check if F(argument) exists + + const String& ASYNCWEBSERVER_REGEX_ATTRIBUTE pathArg(size_t i) const; + + const String& header(const char* name) const;// get request header value by name + const String& header(const __FlashStringHelper * data) const;// get request header value by F(name) + const String& header(size_t i) const; // get request header value by number + const String& headerName(size_t i) const; // get request header name by number + String urlDecode(const String& text) const; +}; + +/* + * FILTER :: Callback to filter AsyncWebRewrite and AsyncWebHandler (done by the Server) + * */ + +typedef std::function ArRequestFilterFunction; + +bool ON_STA_FILTER(AsyncWebServerRequest *request); + +bool ON_AP_FILTER(AsyncWebServerRequest *request); + +/* + * REWRITE :: One instance can be handle any Request (done by the Server) + * */ + +class AsyncWebRewrite { + protected: + String _from; + String _toUrl; + String _params; + ArRequestFilterFunction _filter; + public: + AsyncWebRewrite(const char* from, const char* to): _from(from), _toUrl(to), _params(String()), _filter(NULL){ + int index = _toUrl.indexOf('?'); + if (index > 0) { + _params = _toUrl.substring(index +1); + _toUrl = _toUrl.substring(0, index); + } + } + virtual ~AsyncWebRewrite(){} + AsyncWebRewrite& setFilter(ArRequestFilterFunction fn) { _filter = fn; return *this; } + bool filter(AsyncWebServerRequest *request) const { return _filter == NULL || _filter(request); } + const String& from(void) const { return _from; } + const String& toUrl(void) const { return _toUrl; } + const String& params(void) const { return _params; } + virtual bool match(AsyncWebServerRequest *request) { return from() == request->url() && filter(request); } +}; + +/* + * HANDLER :: One instance can be attached to any Request (done by the Server) + * */ + +class AsyncWebHandler { + protected: + ArRequestFilterFunction _filter; + String _username; + String _password; + public: + AsyncWebHandler():_username(""), _password(""){} + AsyncWebHandler& setFilter(ArRequestFilterFunction fn) { _filter = fn; return *this; } + AsyncWebHandler& setAuthentication(const char *username, const char *password){ _username = String(username);_password = String(password); return *this; }; + bool filter(AsyncWebServerRequest *request){ return _filter == NULL || _filter(request); } + virtual ~AsyncWebHandler(){} + virtual bool canHandle(AsyncWebServerRequest *request __attribute__((unused))){ + return false; + } + virtual void handleRequest(AsyncWebServerRequest *request __attribute__((unused))){} + virtual void handleUpload(AsyncWebServerRequest *request __attribute__((unused)), const String& filename __attribute__((unused)), size_t index __attribute__((unused)), uint8_t *data __attribute__((unused)), size_t len __attribute__((unused)), bool final __attribute__((unused))){} + virtual void handleBody(AsyncWebServerRequest *request __attribute__((unused)), uint8_t *data __attribute__((unused)), size_t len __attribute__((unused)), size_t index __attribute__((unused)), size_t total __attribute__((unused))){} + virtual bool isRequestHandlerTrivial(){return true;} +}; + +/* + * RESPONSE :: One instance is created for each Request (attached by the Handler) + * */ + +typedef enum { + RESPONSE_SETUP, RESPONSE_HEADERS, RESPONSE_CONTENT, RESPONSE_WAIT_ACK, RESPONSE_END, RESPONSE_FAILED +} WebResponseState; + +class AsyncWebServerResponse { + protected: + int _code; + LinkedList _headers; + String _contentType; + size_t _contentLength; + bool _sendContentLength; + bool _chunked; + size_t _headLength; + size_t _sentLength; + size_t _ackedLength; + size_t _writtenLength; + WebResponseState _state; + const char* _responseCodeToString(int code); + + public: + AsyncWebServerResponse(); + virtual ~AsyncWebServerResponse(); + virtual void setCode(int code); + virtual void setContentLength(size_t len); + virtual void setContentType(const String& type); + virtual void addHeader(const String& name, const String& value); + virtual String _assembleHead(uint8_t version); + virtual bool _started() const; + virtual bool _finished() const; + virtual bool _failed() const; + virtual bool _sourceValid() const; + virtual void _respond(AsyncWebServerRequest *request); + virtual size_t _ack(AsyncWebServerRequest *request, size_t len, uint32_t time); +}; + +/* + * SERVER :: One instance + * */ + +typedef std::function ArRequestHandlerFunction; +typedef std::function ArUploadHandlerFunction; +typedef std::function ArBodyHandlerFunction; + +class AsyncWebServer { + protected: + AsyncServer _server; + LinkedList _rewrites; + LinkedList _handlers; + AsyncCallbackWebHandler* _catchAllHandler; + + public: + AsyncWebServer(uint16_t port); + ~AsyncWebServer(); + + void begin(); + void end(); + +#if ASYNC_TCP_SSL_ENABLED + void onSslFileRequest(AcSSlFileHandler cb, void* arg); + void beginSecure(const char *cert, const char *private_key_file, const char *password); +#endif + + AsyncWebRewrite& addRewrite(AsyncWebRewrite* rewrite); + bool removeRewrite(AsyncWebRewrite* rewrite); + AsyncWebRewrite& rewrite(const char* from, const char* to); + + AsyncWebHandler& addHandler(AsyncWebHandler* handler); + bool removeHandler(AsyncWebHandler* handler); + + AsyncCallbackWebHandler& on(const char* uri, ArRequestHandlerFunction onRequest); + AsyncCallbackWebHandler& on(const char* uri, WebRequestMethodComposite method, ArRequestHandlerFunction onRequest); + AsyncCallbackWebHandler& on(const char* uri, WebRequestMethodComposite method, ArRequestHandlerFunction onRequest, ArUploadHandlerFunction onUpload); + AsyncCallbackWebHandler& on(const char* uri, WebRequestMethodComposite method, ArRequestHandlerFunction onRequest, ArUploadHandlerFunction onUpload, ArBodyHandlerFunction onBody); + + AsyncStaticWebHandler& serveStatic(const char* uri, fs::FS& fs, const char* path, const char* cache_control = NULL); + + void onNotFound(ArRequestHandlerFunction fn); //called when handler is not assigned + void onFileUpload(ArUploadHandlerFunction fn); //handle file uploads + void onRequestBody(ArBodyHandlerFunction fn); //handle posts with plain body content (JSON often transmitted this way as a request) + + void reset(); //remove all writers and handlers, with onNotFound/onFileUpload/onRequestBody + + void _handleDisconnect(AsyncWebServerRequest *request); + void _attachHandler(AsyncWebServerRequest *request); + void _rewriteRequest(AsyncWebServerRequest *request); +}; + +class DefaultHeaders { + using headers_t = LinkedList; + headers_t _headers; + + DefaultHeaders() + :_headers(headers_t([](AsyncWebHeader *h){ delete h; })) + {} +public: + using ConstIterator = headers_t::ConstIterator; + + void addHeader(const String& name, const String& value){ + _headers.add(new AsyncWebHeader(name, value)); + } + + ConstIterator begin() const { return _headers.begin(); } + ConstIterator end() const { return _headers.end(); } + + DefaultHeaders(DefaultHeaders const &) = delete; + DefaultHeaders &operator=(DefaultHeaders const &) = delete; + static DefaultHeaders &Instance() { + static DefaultHeaders instance; + return instance; + } +}; + +#include "WebResponseImpl.h" +#include "WebHandlerImpl.h" +#include "AsyncWebSocket.h" +#include "AsyncEventSource.h" + +#endif /* _AsyncWebServer_H_ */ diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/SPIFFSEditor.cpp b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/SPIFFSEditor.cpp new file mode 100644 index 0000000..a84fa87 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/SPIFFSEditor.cpp @@ -0,0 +1,544 @@ +#include "SPIFFSEditor.h" +#include + +//File: edit.htm.gz, Size: 4151 +#define edit_htm_gz_len 4151 +const uint8_t edit_htm_gz[] PROGMEM = { + 0x1F, 0x8B, 0x08, 0x08, 0xB8, 0x94, 0xB1, 0x59, 0x00, 0x03, 0x65, 0x64, 0x69, 0x74, 0x2E, 0x68, + 0x74, 0x6D, 0x00, 0xB5, 0x3A, 0x0B, 0x7B, 0xDA, 0xB8, 0xB2, 0x7F, 0xC5, 0x71, 0xCF, 0x66, 0xED, + 0x83, 0x31, 0x90, 0xA4, 0xD9, 0xD6, 0xC4, 0xC9, 0x42, 0x92, 0x36, 0x6D, 0xF3, 0x6A, 0x80, 0xB6, + 0x69, 0x4F, 0xEE, 0x7E, 0xC2, 0x16, 0xA0, 0xC6, 0x96, 0x5D, 0x5B, 0x0E, 0x49, 0x59, 0xFE, 0xFB, + 0x9D, 0x91, 0x6C, 0xB0, 0x09, 0x69, 0x77, 0xCF, 0xBD, 0xBB, 0xDD, 0x2D, 0x92, 0x46, 0x33, 0x9A, + 0x19, 0xCD, 0x53, 0xDE, 0xBD, 0x8D, 0xA3, 0x8B, 0xC3, 0xFE, 0xF5, 0xE5, 0xB1, 0x36, 0x11, 0x61, + 0xB0, 0xBF, 0x87, 0x7F, 0x6B, 0x01, 0xE1, 0x63, 0x97, 0xF2, 0xFD, 0x3D, 0xC1, 0x44, 0x40, 0xF7, + 0x8F, 0x7B, 0x97, 0xDA, 0xB1, 0xCF, 0x44, 0x94, 0xEC, 0x35, 0xD4, 0xCA, 0x5E, 0x2A, 0x1E, 0x02, + 0xAA, 0x85, 0xD4, 0x67, 0xC4, 0x4D, 0xBD, 0x84, 0xC2, 0x66, 0xDB, 0x0B, 0x67, 0xDF, 0xEB, 0x8C, + 0xFB, 0xF4, 0xDE, 0xD9, 0x6E, 0x36, 0xDB, 0x71, 0x94, 0x32, 0xC1, 0x22, 0xEE, 0x90, 0x61, 0x1A, + 0x05, 0x99, 0xA0, 0xED, 0x80, 0x8E, 0x84, 0xF3, 0x3C, 0xBE, 0x6F, 0x0F, 0xA3, 0xC4, 0xA7, 0x89, + 0xD3, 0x8A, 0xEF, 0x35, 0x00, 0x31, 0x5F, 0x7B, 0xB6, 0xB3, 0xB3, 0xD3, 0x1E, 0x12, 0xEF, 0x76, + 0x9C, 0x44, 0x19, 0xF7, 0xEB, 0x5E, 0x14, 0x44, 0x89, 0xF3, 0x6C, 0xF4, 0x1C, 0xFF, 0xB4, 0x7D, + 0x96, 0xC6, 0x01, 0x79, 0x70, 0x78, 0xC4, 0x29, 0xE0, 0xDE, 0xD7, 0xD3, 0x09, 0xF1, 0xA3, 0xA9, + 0xD3, 0xD4, 0x9A, 0x5A, 0xAB, 0x09, 0x44, 0x92, 0xF1, 0x90, 0x18, 0x4D, 0x0B, 0xFF, 0xD8, 0x3B, + 0x66, 0x7B, 0x14, 0x71, 0x51, 0x4F, 0xD9, 0x77, 0xEA, 0xB4, 0xB6, 0xE0, 0x34, 0x39, 0x1D, 0x91, + 0x90, 0x05, 0x0F, 0x4E, 0x4A, 0x78, 0x5A, 0x4F, 0x69, 0xC2, 0x46, 0x6A, 0x79, 0x4A, 0xD9, 0x78, + 0x22, 0x9C, 0xDF, 0x9A, 0xCD, 0x39, 0xF0, 0xAF, 0x65, 0xC1, 0x2C, 0x60, 0x29, 0x20, 0xA3, 0x78, + 0xEA, 0x3C, 0x11, 0xC5, 0x4E, 0x53, 0xB1, 0xDE, 0x6C, 0x87, 0x24, 0x19, 0x33, 0x0E, 0x83, 0x98, + 0xF8, 0x3E, 0xE3, 0x63, 0x47, 0xA1, 0x05, 0x6C, 0xB6, 0x90, 0x36, 0xA1, 0x01, 0x11, 0xEC, 0x8E, + 0xB6, 0x43, 0xC6, 0xEB, 0x53, 0xE6, 0x8B, 0x89, 0xB3, 0x0B, 0x3C, 0xB6, 0xBD, 0x2C, 0x49, 0x41, + 0xA6, 0x38, 0x62, 0x5C, 0xD0, 0x44, 0xA2, 0xA5, 0x31, 0xE1, 0xB3, 0x5C, 0x54, 0x54, 0x40, 0x21, + 0x27, 0xE3, 0x01, 0xE3, 0xB4, 0x3E, 0x0C, 0x22, 0xEF, 0x76, 0x71, 0xD2, 0x6E, 0x7C, 0x9F, 0x9F, + 0xE5, 0x4C, 0xA2, 0x3B, 0x9A, 0xCC, 0x96, 0xEA, 0x92, 0xD8, 0x15, 0x60, 0x85, 0x34, 0xA5, 0x74, + 0x6E, 0x8B, 0xBB, 0x0C, 0xA0, 0x96, 0xFC, 0x05, 0x29, 0x17, 0xFC, 0x2F, 0x45, 0x5A, 0x11, 0x5C, + 0xA1, 0x30, 0x1E, 0x67, 0x62, 0xF6, 0xF8, 0x2A, 0xA3, 0x98, 0x78, 0x4C, 0x3C, 0xA0, 0xFC, 0xB0, + 0x6D, 0x86, 0xBA, 0x04, 0xAC, 0x24, 0x24, 0x81, 0x86, 0x3A, 0xD7, 0x3E, 0xD0, 0xC4, 0x27, 0x9C, + 0x58, 0x9D, 0x84, 0x91, 0xC0, 0xEA, 0x2D, 0xB5, 0x5E, 0x0F, 0xA3, 0xEF, 0xF5, 0x0C, 0xC6, 0x30, + 0x0F, 0xA8, 0x27, 0x94, 0x92, 0xE1, 0x1E, 0x86, 0xB7, 0x4C, 0x3C, 0x06, 0x3C, 0x5A, 0x28, 0xA9, + 0x4B, 0x2A, 0x69, 0xA2, 0x2E, 0xB0, 0x25, 0xD5, 0x83, 0x1C, 0x4B, 0xC9, 0x95, 0x50, 0xF5, 0x61, + 0x24, 0x44, 0x14, 0x4A, 0x93, 0x5B, 0x08, 0xAC, 0x49, 0xAB, 0x79, 0xF1, 0xE8, 0x46, 0xD6, 0x6B, + 0xBF, 0x44, 0xBE, 0x0D, 0x7A, 0x15, 0xCC, 0x23, 0x41, 0x9D, 0x04, 0x6C, 0xCC, 0x9D, 0x90, 0xF9, + 0x7E, 0x40, 0x4B, 0x56, 0xEB, 0x64, 0x49, 0x60, 0xF8, 0x44, 0x10, 0x87, 0x85, 0x64, 0x4C, 0x1B, + 0x31, 0x1F, 0x03, 0x34, 0xA5, 0xBB, 0x3B, 0x16, 0xFB, 0xD0, 0xBD, 0xB8, 0x9A, 0x36, 0xDF, 0xBD, + 0x1E, 0x47, 0x1D, 0xF8, 0xE7, 0xBC, 0x37, 0x98, 0x1C, 0x0F, 0xC6, 0x30, 0xEA, 0xE2, 0xB4, 0xF3, + 0xFE, 0xB0, 0xF3, 0x1E, 0x7E, 0x0E, 0x5B, 0xB5, 0xAF, 0xA3, 0x6F, 0xB8, 0xD0, 0x7D, 0xED, 0x77, + 0xFB, 0x83, 0xE3, 0x4E, 0xE7, 0x5D, 0xE3, 0xCD, 0xF9, 0xF4, 0xE3, 0xBB, 0x5D, 0x04, 0x77, 0x83, + 0xE6, 0xD5, 0x87, 0x49, 0x73, 0xB0, 0xF5, 0x32, 0xF4, 0x4F, 0xFC, 0x89, 0x17, 0x0E, 0x3A, 0xEF, + 0x3F, 0x5E, 0xDD, 0x5D, 0x87, 0x83, 0x71, 0xEF, 0x63, 0x6B, 0xF2, 0x79, 0xEB, 0x43, 0xEF, 0xF3, + 0xC7, 0x57, 0xB7, 0xF4, 0xD3, 0xC9, 0xDB, 0xCF, 0xFD, 0x29, 0x20, 0x1C, 0x45, 0xBD, 0xC1, 0x55, + 0xF7, 0x43, 0x77, 0xFC, 0xB9, 0xEB, 0x1D, 0xDF, 0x0F, 0x83, 0xF3, 0xEE, 0xEB, 0xCE, 0xB0, 0xB3, + 0xE5, 0x51, 0x3A, 0xEE, 0x5F, 0x75, 0xB3, 0x37, 0xEF, 0x2E, 0xC6, 0x8C, 0x4D, 0x7A, 0x9F, 0xCF, + 0xFB, 0xDE, 0xE1, 0xF3, 0xD3, 0xC1, 0x49, 0x87, 0x4D, 0xCE, 0xDF, 0x5E, 0x35, 0x6F, 0x5F, 0xBF, + 0x3B, 0x3C, 0xF2, 0xAE, 0xDF, 0x5E, 0xEF, 0x1E, 0x6D, 0x37, 0x7E, 0xFB, 0xED, 0xCC, 0xBF, 0x60, + 0xBC, 0x7F, 0xF7, 0xBD, 0x33, 0x3E, 0x9C, 0xBE, 0x78, 0x48, 0xFB, 0x93, 0x37, 0x77, 0xBC, 0xF1, + 0x21, 0xFA, 0xFA, 0xE6, 0xE1, 0x0C, 0xFE, 0xBB, 0xBC, 0xAC, 0x0D, 0x7B, 0xAD, 0x74, 0xF0, 0xFE, + 0xCD, 0x87, 0xAD, 0xF4, 0xE5, 0xF3, 0xB8, 0x7B, 0x74, 0x74, 0x17, 0x0E, 0x2F, 0x1B, 0xA1, 0x7F, + 0x3B, 0x12, 0x2F, 0xB6, 0x45, 0x7C, 0x3D, 0xCE, 0x3E, 0x7F, 0x7B, 0xFE, 0x76, 0xD2, 0xB8, 0xA0, + 0xE4, 0x7A, 0x52, 0x7B, 0xF8, 0xFE, 0xF0, 0x62, 0xD2, 0x3F, 0xB9, 0x3B, 0x0F, 0xC8, 0xFD, 0xF9, + 0xB9, 0xF7, 0x3D, 0xAC, 0x05, 0xE4, 0xE5, 0x45, 0x3F, 0x20, 0x49, 0x6B, 0xE0, 0x77, 0x1A, 0xB5, + 0xC3, 0xAD, 0xCE, 0x8E, 0x48, 0xAE, 0x0E, 0xF9, 0xD1, 0xF6, 0xD7, 0xDE, 0x8B, 0x6E, 0xB7, 0x15, + 0x0D, 0xBF, 0x6D, 0xBD, 0xBE, 0xDD, 0x7D, 0x3D, 0xD8, 0x7D, 0x3F, 0x7C, 0xDF, 0xE9, 0xED, 0x74, + 0x07, 0xE4, 0xBA, 0xF7, 0xBE, 0x33, 0xDA, 0x19, 0x4E, 0x26, 0xEF, 0xDE, 0xF5, 0x5F, 0xF9, 0x9D, + 0xEF, 0x49, 0xE7, 0x62, 0xDA, 0xB9, 0x3F, 0x1E, 0x74, 0x4E, 0x6A, 0xEF, 0x8E, 0xCF, 0x9A, 0xAD, + 0xDE, 0xF5, 0xF6, 0xF8, 0x6C, 0x77, 0xDA, 0x4D, 0x8F, 0x3B, 0xEF, 0xBB, 0xCD, 0xF1, 0xDB, 0x5A, + 0x48, 0x3E, 0x47, 0x87, 0xDB, 0xE3, 0x37, 0xBB, 0xEC, 0xF2, 0x9A, 0x74, 0xDE, 0x74, 0xDF, 0xA6, + 0xEC, 0x2A, 0x3C, 0x19, 0x34, 0x3B, 0x9D, 0xD3, 0x0B, 0xFA, 0xEA, 0x70, 0x9B, 0xBC, 0xDB, 0xF2, + 0x3E, 0x82, 0xFE, 0x07, 0x9F, 0xE8, 0x6F, 0xB5, 0xCE, 0xF4, 0xA2, 0x19, 0x78, 0x2F, 0x69, 0xFF, + 0xE4, 0xBA, 0x2F, 0x6F, 0xE7, 0x38, 0x78, 0xD5, 0xBF, 0xED, 0x65, 0xEF, 0xC3, 0xC3, 0x43, 0x53, + 0xE3, 0x51, 0x3D, 0xA1, 0x31, 0x25, 0xA2, 0x1C, 0xAE, 0x16, 0xFE, 0x01, 0xB6, 0xB5, 0xB4, 0xC2, + 0xDC, 0x4F, 0x05, 0xBD, 0x17, 0x75, 0x9F, 0x7A, 0x51, 0x42, 0xE4, 0x1E, 0x40, 0xA0, 0x09, 0x9A, + 0xD8, 0xFC, 0x77, 0x19, 0x3F, 0x35, 0x15, 0x3F, 0x35, 0xC2, 0x7D, 0xCD, 0x28, 0x1C, 0x01, 0x83, + 0x87, 0x4F, 0xEF, 0x98, 0x47, 0xEB, 0x31, 0xBB, 0xA7, 0x41, 0x5D, 0x22, 0x3B, 0x4D, 0x73, 0x26, + 0xFD, 0xAD, 0xD8, 0x46, 0x38, 0x98, 0x9A, 0xA4, 0x5A, 0x2C, 0xF8, 0x5F, 0x89, 0x47, 0x21, 0xB0, + 0x81, 0xCB, 0x84, 0xF8, 0xAB, 0x7C, 0x27, 0x4A, 0xEA, 0xC3, 0x6C, 0x3C, 0x62, 0xF7, 0xE0, 0xD0, + 0x23, 0xC6, 0x99, 0xA0, 0x5A, 0x2B, 0x9D, 0xFF, 0x5E, 0x90, 0xB9, 0xA5, 0x0F, 0xA3, 0x84, 0x84, + 0x34, 0xD5, 0xFE, 0x22, 0x99, 0xD9, 0x28, 0x89, 0xC2, 0x65, 0x10, 0x99, 0x8B, 0xA8, 0x34, 0x99, + 0xCF, 0x9F, 0x65, 0x71, 0x10, 0x11, 0x10, 0x73, 0x4D, 0xE4, 0x50, 0xF1, 0x34, 0x91, 0x6E, 0xB5, + 0x88, 0xAB, 0xB9, 0x9B, 0x6D, 0xA1, 0x5B, 0x96, 0xDD, 0x7A, 0x6B, 0x67, 0xE9, 0xBA, 0x75, 0xB9, + 0x17, 0xE3, 0xFD, 0x9A, 0x4C, 0x81, 0xF1, 0xA0, 0x14, 0xEE, 0x9E, 0x09, 0x50, 0xE9, 0x13, 0x87, + 0xCB, 0x43, 0xF2, 0xC8, 0xB0, 0x60, 0x40, 0x05, 0xEA, 0x96, 0x8C, 0xD4, 0x85, 0x24, 0xB0, 0x6F, + 0xFE, 0x8C, 0xCA, 0xBC, 0x67, 0x3D, 0x8B, 0x13, 0xB8, 0x0D, 0x3A, 0xFD, 0x11, 0xCD, 0x42, 0xA6, + 0x2A, 0x6D, 0x45, 0x53, 0x65, 0xBC, 0x5C, 0x84, 0x65, 0xDA, 0x93, 0xBC, 0x16, 0xA4, 0x1F, 0x4B, + 0x05, 0xE0, 0x05, 0x37, 0xCF, 0x91, 0x9B, 0x1F, 0x6A, 0x75, 0x7B, 0xF7, 0x97, 0x9C, 0x87, 0x9D, + 0xE6, 0x2F, 0x73, 0x3B, 0xDF, 0x5B, 0xA4, 0xE4, 0x56, 0x13, 0xFE, 0x29, 0x32, 0xEF, 0x8B, 0x25, + 0x0B, 0xC3, 0xE7, 0xF8, 0xA7, 0x60, 0x10, 0xE9, 0x94, 0x80, 0xDB, 0x3B, 0x2F, 0x5F, 0xF8, 0xC3, + 0x02, 0x98, 0x0B, 0xF6, 0x24, 0x3C, 0x21, 0x3E, 0xCB, 0x52, 0xE7, 0x79, 0xF3, 0x97, 0x5C, 0x9F, + 0x5B, 0x3B, 0x28, 0xFB, 0xE2, 0x2E, 0x71, 0xB2, 0xB4, 0xD8, 0x34, 0x66, 0x5C, 0xDB, 0x4A, 0x35, + 0xBC, 0x6F, 0x92, 0x2C, 0x0C, 0xB3, 0x92, 0xED, 0xE7, 0xBF, 0x2F, 0x4D, 0x13, 0xF7, 0xCF, 0x9A, + 0xBF, 0xCC, 0x44, 0x02, 0xD9, 0x64, 0x04, 0xB9, 0xC6, 0x49, 0x22, 0x41, 0x04, 0x35, 0x9A, 0xE6, + 0x1C, 0x84, 0x5B, 0x03, 0xD8, 0xDE, 0x6D, 0xFA, 0x74, 0x6C, 0xCE, 0xE7, 0x7B, 0x0D, 0x99, 0xD7, + 0xA0, 0x6C, 0xF1, 0x12, 0x16, 0x8B, 0xFD, 0x51, 0xC6, 0x3D, 0xE4, 0x41, 0x1B, 0x53, 0x83, 0x9A, + 0xB3, 0x84, 0x8A, 0x2C, 0xE1, 0x9A, 0x1F, 0x79, 0x19, 0x1A, 0xBB, 0x3D, 0xA6, 0xE2, 0x58, 0xD9, + 0x7D, 0xF7, 0xE1, 0x8D, 0x0F, 0x3B, 0xE6, 0x0B, 0x04, 0x6F, 0x2D, 0x02, 0x38, 0x30, 0x9C, 0x97, + 0xE3, 0x54, 0xF6, 0x43, 0x82, 0x01, 0x22, 0xEF, 0xE8, 0x83, 0x41, 0x2D, 0xB1, 0x40, 0xA4, 0x36, + 0xAE, 0x1B, 0xC5, 0x2E, 0x80, 0x71, 0x73, 0x76, 0x07, 0x4A, 0x20, 0x2E, 0xFD, 0x22, 0x6E, 0x2C, + 0xE6, 0x72, 0xF8, 0x69, 0xE7, 0xBB, 0xC9, 0x1E, 0x3B, 0xA8, 0xB7, 0x1C, 0xB2, 0xCF, 0x0E, 0x5A, + 0xE0, 0x5E, 0x65, 0x6E, 0xE4, 0xB9, 0xAF, 0x58, 0x40, 0x07, 0xB9, 0xC3, 0xE1, 0x31, 0x48, 0x6C, + 0xB1, 0x85, 0x28, 0xE2, 0x5B, 0xCD, 0xE6, 0x86, 0x4B, 0x0F, 0x48, 0x00, 0x39, 0xCC, 0xD0, 0x8F, + 0xAF, 0xAE, 0x2E, 0xAE, 0xBE, 0xE8, 0x35, 0x5A, 0xD3, 0x6F, 0x1C, 0x4D, 0xAF, 0x71, 0xD3, 0x11, + 0x76, 0x42, 0x47, 0x09, 0x4D, 0x27, 0x97, 0x44, 0x4C, 0x8C, 0xD4, 0xBE, 0x23, 0x41, 0x56, 0x16, + 0x84, 0xA1, 0xDC, 0xC8, 0xA2, 0x70, 0x39, 0x9D, 0x6A, 0xAF, 0x40, 0xCD, 0x47, 0x90, 0xEA, 0xDA, + 0xC2, 0x26, 0x71, 0x4C, 0xB9, 0x6F, 0xE8, 0x31, 0x20, 0xEA, 0x16, 0x35, 0xAD, 0x84, 0x7E, 0xCB, + 0x68, 0x2A, 0x52, 0x1B, 0x2C, 0xD7, 0xD0, 0x2F, 0x07, 0x7D, 0xDD, 0xD2, 0x1B, 0xE8, 0x47, 0x3A, + 0xF0, 0x46, 0xCC, 0x39, 0x52, 0x89, 0x5C, 0xD0, 0xA4, 0x3E, 0xCC, 0xC0, 0xA0, 0xB8, 0x6E, 0xB6, + 0x23, 0x9B, 0x71, 0x4E, 0x93, 0x93, 0xFE, 0xD9, 0xA9, 0xAB, 0x5F, 0x29, 0x46, 0xB4, 0x53, 0x28, + 0x48, 0x74, 0x4B, 0x5E, 0x51, 0x7E, 0xC8, 0xE1, 0x84, 0x05, 0xBE, 0x11, 0x99, 0x6D, 0x24, 0xE1, + 0x49, 0x12, 0xB2, 0x40, 0x01, 0x0A, 0x9E, 0x2D, 0x1E, 0x62, 0xEA, 0xEA, 0x23, 0x50, 0x86, 0x6E, + 0x79, 0x76, 0x98, 0x05, 0x82, 0xC5, 0x01, 0x75, 0x37, 0x5A, 0x30, 0xE3, 0x60, 0x41, 0xAE, 0x8E, + 0xB9, 0x19, 0x61, 0xCC, 0x77, 0x75, 0x15, 0xA1, 0xF2, 0xB8, 0xB6, 0xEE, 0x14, 0x4F, 0x9D, 0x92, + 0x56, 0x4E, 0x49, 0xCB, 0xB8, 0x4A, 0xE0, 0x34, 0x3F, 0x18, 0xC3, 0x3C, 0xCE, 0xD4, 0x51, 0x05, + 0xCC, 0xA7, 0x23, 0x02, 0x9C, 0x7C, 0x40, 0x6D, 0xBA, 0x7A, 0x63, 0xDD, 0x41, 0xA9, 0x3A, 0xC8, + 0xAF, 0x6A, 0xC4, 0x2F, 0x6B, 0x44, 0xDD, 0xEE, 0x3A, 0x64, 0x5F, 0x21, 0x07, 0x55, 0xE4, 0xA0, + 0x8C, 0x7C, 0x28, 0x8D, 0x64, 0x1D, 0x72, 0xA0, 0x90, 0x93, 0x8A, 0x88, 0x89, 0x14, 0x51, 0x85, + 0xBD, 0x3A, 0x6A, 0x13, 0x05, 0xD2, 0xAD, 0xA4, 0x22, 0x66, 0x62, 0x83, 0x97, 0x92, 0x61, 0x40, + 0x7D, 0x77, 0xA3, 0x09, 0x33, 0x2C, 0xB6, 0xDD, 0xAD, 0xE6, 0x9A, 0x33, 0x12, 0x75, 0x46, 0x56, + 0x65, 0x30, 0x2B, 0x33, 0xA8, 0xF5, 0xC8, 0x1D, 0xD5, 0xD6, 0x31, 0x98, 0x99, 0x56, 0x60, 0x47, + 0xDC, 0x0B, 0x98, 0x77, 0xEB, 0x2E, 0xBD, 0xC5, 0x9C, 0xB1, 0x85, 0x85, 0x5A, 0x5C, 0x06, 0xBA, + 0x01, 0x94, 0x5E, 0x8B, 0xA5, 0x7C, 0x80, 0xFA, 0x9E, 0x5B, 0xD9, 0x5A, 0x02, 0xDC, 0xA6, 0xF7, + 0xD4, 0x3B, 0x8C, 0xC2, 0x90, 0xA0, 0xED, 0xA6, 0xC0, 0x41, 0x3E, 0xD1, 0xCD, 0xB9, 0x15, 0xAD, + 0xC5, 0x79, 0xC2, 0x45, 0x2C, 0x7F, 0x3D, 0x8B, 0x23, 0x03, 0x5C, 0xCE, 0xF5, 0x6C, 0xD4, 0x61, + 0x6A, 0x83, 0x1E, 0xC7, 0x62, 0xF2, 0x13, 0x17, 0x2A, 0x0C, 0x54, 0xA2, 0x7C, 0x69, 0xDE, 0x58, + 0x0B, 0x91, 0x56, 0x7C, 0xEA, 0xA2, 0xB7, 0xE2, 0x54, 0xA8, 0xBC, 0x8A, 0x5D, 0x9A, 0x4B, 0x1D, + 0x94, 0x61, 0xB9, 0xBD, 0x2F, 0xA0, 0xFA, 0x7C, 0x0E, 0xE7, 0x01, 0xFF, 0x13, 0x68, 0xF9, 0xE8, + 0x5F, 0x17, 0x60, 0xC9, 0xA3, 0x34, 0x78, 0x8B, 0xBB, 0x0D, 0xE3, 0xC0, 0xF9, 0x8F, 0x6D, 0x7C, + 0xF9, 0x1F, 0xFB, 0xA6, 0x66, 0x9A, 0x07, 0xFF, 0x6A, 0x48, 0x0D, 0x1B, 0xC2, 0xFC, 0xD2, 0xBA, + 0xB1, 0x08, 0x80, 0xED, 0x7F, 0x9B, 0xFF, 0xB1, 0x25, 0xB8, 0x02, 0x6B, 0xDF, 0x45, 0x90, 0x49, + 0xF0, 0x24, 0x34, 0xB0, 0x68, 0xA4, 0x91, 0xCD, 0x4D, 0x43, 0xB8, 0xA4, 0x72, 0x8D, 0x35, 0x51, + 0xD3, 0x6D, 0x88, 0x53, 0x50, 0x5B, 0xAC, 0x04, 0xBF, 0x3E, 0x24, 0x7A, 0x15, 0x5B, 0x17, 0x00, + 0xC9, 0x3D, 0xCA, 0x0C, 0x3D, 0x22, 0x97, 0x52, 0xCB, 0x0C, 0x02, 0x42, 0xA7, 0x89, 0xE7, 0x2A, + 0xAD, 0x1D, 0x14, 0x30, 0x17, 0xA2, 0xE0, 0xBC, 0x1C, 0x2D, 0x15, 0xEA, 0xAA, 0xFD, 0x17, 0x0A, + 0xA3, 0xD6, 0x12, 0x8A, 0x04, 0x31, 0xAD, 0xD8, 0x79, 0xC6, 0x72, 0x75, 0x4C, 0x59, 0xBA, 0x35, + 0x59, 0x5D, 0x96, 0xAD, 0x04, 0xAE, 0x2F, 0x8D, 0xFE, 0xD7, 0x3D, 0x16, 0x8E, 0xB5, 0x12, 0x3F, + 0xF8, 0x97, 0xFB, 0x2B, 0x46, 0xE4, 0xCD, 0x3F, 0xBC, 0x21, 0x70, 0x05, 0xA6, 0x41, 0x6D, 0x1E, + 0x4D, 0x0D, 0xB3, 0xF6, 0xAB, 0xAE, 0x49, 0x8A, 0xAE, 0x1E, 0x92, 0xFB, 0xBC, 0xA7, 0xC4, 0x8C, + 0xD7, 0xD6, 0x70, 0x5E, 0xB4, 0x28, 0xF9, 0x82, 0xEC, 0xE6, 0x48, 0x26, 0xA2, 0xB6, 0x56, 0x64, + 0x52, 0xD5, 0xCA, 0xE8, 0x5A, 0x63, 0xFF, 0xD7, 0x4A, 0x40, 0xB7, 0x98, 0xBA, 0x4E, 0x15, 0x8C, + 0xB3, 0x00, 0x1C, 0x93, 0x3E, 0x1D, 0x69, 0x03, 0x26, 0x03, 0x75, 0x35, 0x46, 0x5A, 0x81, 0xC1, + 0xCC, 0x03, 0xC3, 0x2B, 0xFB, 0xF3, 0x1E, 0x16, 0xBF, 0xFB, 0x97, 0xAA, 0xAA, 0x81, 0xD4, 0x8B, + 0x33, 0x5D, 0x59, 0x59, 0xD5, 0x4B, 0xE0, 0xD2, 0x08, 0xA0, 0x5B, 0x8B, 0x3C, 0x3A, 0x8C, 0xFC, + 0x87, 0x52, 0xF6, 0x4D, 0xBB, 0x0F, 0x87, 0x01, 0x49, 0xD3, 0x73, 0xB8, 0x01, 0x43, 0xF7, 0x42, + 0x50, 0xB8, 0xB2, 0xC2, 0xFD, 0xE6, 0xE6, 0x66, 0x15, 0x29, 0xA1, 0x21, 0x14, 0xDB, 0x8A, 0x2B, + 0xF0, 0x49, 0xD3, 0xF1, 0x81, 0x30, 0x18, 0xD2, 0x1A, 0xC6, 0xF0, 0x25, 0xE3, 0x47, 0x5C, 0x71, + 0xF4, 0xF4, 0x22, 0xA6, 0xFC, 0x33, 0xDC, 0x95, 0x32, 0xCB, 0x1A, 0xAD, 0xA6, 0x68, 0xFA, 0x8F, + 0xD8, 0x3E, 0xCA, 0x0D, 0x76, 0xC1, 0x7A, 0xBA, 0x56, 0xA1, 0xFC, 0x9F, 0x61, 0xB9, 0x94, 0x28, + 0xD6, 0x70, 0x9C, 0x40, 0x80, 0x5A, 0xC3, 0x31, 0xC4, 0x1A, 0x41, 0x17, 0xFC, 0x26, 0x6B, 0xF9, + 0xCD, 0xFE, 0x19, 0x7E, 0x97, 0x76, 0x1E, 0x15, 0x25, 0x91, 0xAA, 0xAF, 0x50, 0x02, 0x9F, 0xDD, + 0xE9, 0xA6, 0x15, 0xB9, 0x55, 0x0A, 0x50, 0x1B, 0x46, 0x41, 0xD0, 0x8F, 0xE2, 0x83, 0x27, 0xD6, + 0x9D, 0xC5, 0x7A, 0x31, 0xC8, 0xD9, 0x5C, 0x6E, 0xB1, 0xBC, 0xB5, 0x44, 0x4F, 0xA1, 0xEC, 0x5F, + 0x4B, 0x15, 0x01, 0x3F, 0x23, 0x8B, 0x7B, 0xAC, 0xD4, 0xA5, 0x36, 0x28, 0x0F, 0x56, 0x3F, 0xD5, + 0x3C, 0xCB, 0x5F, 0xCC, 0xAE, 0x6B, 0x51, 0x9B, 0xC0, 0x38, 0x57, 0x92, 0x8B, 0x4A, 0xB2, 0xC8, + 0x13, 0x01, 0xA8, 0x58, 0xC7, 0x2E, 0xC4, 0x4D, 0x6B, 0x7A, 0x7C, 0xBF, 0x5C, 0x83, 0xC2, 0xDF, + 0xF5, 0xD5, 0x12, 0x33, 0x08, 0xC4, 0xD3, 0x95, 0x4B, 0x29, 0x5F, 0x37, 0x29, 0x8A, 0x0E, 0x62, + 0x47, 0xA3, 0x51, 0x4A, 0xC5, 0x47, 0x0C, 0x49, 0x56, 0xB2, 0x98, 0x9F, 0xC8, 0x90, 0x04, 0x8C, + 0x45, 0x3C, 0x8C, 0xB2, 0x94, 0x46, 0x99, 0xA8, 0xA4, 0x16, 0x63, 0x21, 0xCC, 0x5E, 0xFA, 0xE7, + 0x9F, 0x8B, 0xC9, 0x7E, 0x5A, 0x0B, 0x96, 0xD3, 0xEB, 0x3D, 0xBF, 0x34, 0xD9, 0xF7, 0x6B, 0x89, + 0xB9, 0x7A, 0xE9, 0xFF, 0x67, 0x4B, 0x21, 0x65, 0x4B, 0xF1, 0xB0, 0x54, 0x2E, 0x62, 0x62, 0x29, + 0xE6, 0xC9, 0x82, 0x91, 0x97, 0x7C, 0x16, 0x0D, 0x1A, 0x2B, 0x25, 0x55, 0x9E, 0x97, 0x7D, 0x95, + 0x43, 0x40, 0x59, 0x71, 0xE5, 0x35, 0x11, 0x06, 0x34, 0xE0, 0x63, 0x64, 0xF2, 0x41, 0xEB, 0xA7, + 0xD1, 0x94, 0x26, 0x87, 0x24, 0xA5, 0x06, 0x24, 0xCD, 0x65, 0xDC, 0x41, 0xA8, 0xE9, 0x04, 0xEB, + 0x76, 0x6D, 0x6E, 0x12, 0x05, 0xCE, 0x33, 0x77, 0xC4, 0xB1, 0x26, 0x03, 0xF9, 0xB2, 0xCA, 0x09, + 0xD4, 0xC6, 0xBE, 0x12, 0xA4, 0x3E, 0x52, 0x25, 0xA8, 0x61, 0x5A, 0xD0, 0x76, 0xC0, 0x35, 0x5F, + 0x26, 0x51, 0x4C, 0xC6, 0xB2, 0x07, 0x83, 0x35, 0x74, 0x0F, 0xA4, 0x66, 0x6D, 0x34, 0x91, 0x60, + 0xA9, 0x73, 0x29, 0xFC, 0x66, 0xD9, 0xC2, 0x70, 0x4B, 0x57, 0xC9, 0xB0, 0xBD, 0xF4, 0xA5, 0x35, + 0x59, 0x83, 0xE0, 0x0B, 0x6C, 0x62, 0xE0, 0x1E, 0x68, 0x64, 0xF2, 0x7B, 0x00, 0x77, 0x6B, 0xB6, + 0xA3, 0x3D, 0xD6, 0x8E, 0x6A, 0x35, 0x53, 0x55, 0xE9, 0xAE, 0x0B, 0x6D, 0x4E, 0x74, 0x23, 0x0B, + 0x4B, 0x10, 0xAA, 0x9A, 0x59, 0x0C, 0x38, 0x1B, 0x81, 0xAA, 0xBA, 0xC0, 0x11, 0xD6, 0x98, 0x66, + 0xA9, 0x23, 0xF1, 0x97, 0x1D, 0xC9, 0x13, 0xB5, 0x07, 0x95, 0xF5, 0x05, 0xD4, 0x31, 0xAB, 0x25, + 0x86, 0x30, 0xD3, 0x29, 0x13, 0xDE, 0x04, 0x03, 0x90, 0x07, 0x5A, 0xD5, 0x05, 0x14, 0xB5, 0x8E, + 0x1C, 0x4D, 0x44, 0xB8, 0x1C, 0x05, 0xF9, 0xF0, 0x6B, 0x9A, 0x0F, 0xBC, 0xB4, 0x18, 0xDD, 0x97, + 0x80, 0x50, 0xD2, 0xE6, 0xE0, 0x88, 0x8F, 0xF2, 0x21, 0xF4, 0xB2, 0x05, 0x9D, 0x02, 0x58, 0xFC, + 0xC6, 0x71, 0x3E, 0x8A, 0x27, 0xC5, 0x68, 0x42, 0xEF, 0x17, 0x78, 0x51, 0x01, 0xF5, 0xA9, 0xEE, + 0x28, 0x1B, 0xDB, 0x68, 0xCE, 0xF3, 0x41, 0x6B, 0x29, 0x7F, 0xF0, 0xFF, 0x28, 0x7F, 0xCC, 0xC7, + 0x85, 0x34, 0x71, 0x31, 0x1A, 0xB3, 0x42, 0x96, 0x61, 0x18, 0xFF, 0x90, 0x93, 0xA4, 0xD4, 0x13, + 0x97, 0x7A, 0x5A, 0xF1, 0xB3, 0xB6, 0x53, 0x98, 0x8E, 0x31, 0xAA, 0xF8, 0xE3, 0xC8, 0xF6, 0xF0, + 0xF7, 0x3C, 0xF2, 0x65, 0x6D, 0x69, 0x5A, 0xA1, 0x31, 0x82, 0x3A, 0x57, 0x37, 0xCB, 0x7E, 0x9A, + 0xFD, 0xB7, 0xAD, 0xE8, 0xD1, 0xF1, 0xE9, 0x71, 0xFF, 0xB8, 0x5C, 0x38, 0x23, 0xE7, 0x25, 0x93, + 0x8A, 0x2B, 0x5D, 0xFA, 0xB2, 0x22, 0x80, 0x02, 0x1B, 0x45, 0x01, 0x7B, 0xDD, 0xDC, 0x54, 0x7E, + 0xF1, 0xB6, 0x77, 0x71, 0x6E, 0xC7, 0x24, 0x01, 0x8F, 0x24, 0x15, 0xE6, 0xC2, 0x82, 0x44, 0xF9, + 0xE0, 0xD7, 0xC7, 0xA5, 0x72, 0x5D, 0x7E, 0x61, 0x70, 0xC4, 0xDC, 0x52, 0xA7, 0xA9, 0x7E, 0x78, + 0xE2, 0x62, 0x5D, 0x99, 0xBF, 0x04, 0x41, 0x72, 0x1A, 0x2D, 0x13, 0x55, 0x11, 0x67, 0x46, 0xE5, + 0x30, 0x2F, 0xEE, 0xB2, 0x75, 0x0D, 0xD3, 0xC8, 0xB4, 0xC4, 0x84, 0xA5, 0xE5, 0x46, 0xA5, 0x12, + 0x14, 0xFE, 0xA2, 0xB6, 0xE7, 0x8B, 0x91, 0x24, 0xB7, 0x5A, 0x73, 0xAB, 0x6F, 0x41, 0x2A, 0x3E, + 0x58, 0x04, 0x23, 0x66, 0x39, 0xDB, 0x16, 0x77, 0xA3, 0x43, 0xEE, 0x61, 0x5C, 0x7F, 0xBA, 0x35, + 0x78, 0xD2, 0x3C, 0x79, 0x61, 0x9E, 0xFC, 0xB1, 0x7B, 0x2E, 0x1C, 0x45, 0xF9, 0xDA, 0xE2, 0x98, + 0xF6, 0x10, 0x58, 0xBB, 0x6D, 0x2F, 0x7D, 0x18, 0x20, 0xD2, 0x83, 0xCB, 0x00, 0xF4, 0x63, 0x58, + 0xFF, 0x4A, 0xEE, 0x88, 0x7A, 0x09, 0xAA, 0xA2, 0xAD, 0x73, 0x54, 0xD8, 0xEE, 0xFD, 0x81, 0xA3, + 0xF2, 0xCE, 0x65, 0x18, 0x48, 0x97, 0xC3, 0x92, 0x37, 0x8B, 0x75, 0xC1, 0x61, 0x19, 0x31, 0x64, + 0x6C, 0x00, 0xE3, 0xCD, 0x5D, 0x49, 0x13, 0xD5, 0x1C, 0xB4, 0xF0, 0x1B, 0x08, 0x8A, 0x4F, 0x39, + 0xCE, 0x9A, 0x38, 0xAD, 0x62, 0x72, 0xC5, 0x23, 0xC8, 0x4A, 0x67, 0x89, 0xC0, 0x6E, 0x10, 0x0D, + 0x0D, 0x7C, 0x64, 0x9A, 0xA1, 0xB6, 0x1D, 0x3E, 0x37, 0xD7, 0xBC, 0xD9, 0x54, 0xFA, 0x4B, 0x62, + 0x79, 0xD5, 0xB0, 0x8B, 0x1C, 0x56, 0xCC, 0x75, 0x7D, 0x1F, 0xF4, 0xA3, 0x4E, 0x29, 0xAF, 0x48, + 0xA4, 0x53, 0xD1, 0x83, 0xC4, 0x86, 0xA2, 0x41, 0xBE, 0x91, 0x40, 0x44, 0x72, 0x4A, 0x33, 0x5D, + 0xC7, 0xCA, 0xD2, 0x0B, 0x28, 0x49, 0x7A, 0xB2, 0x73, 0x95, 0x49, 0x6B, 0x25, 0x06, 0xFE, 0xC8, + 0xD7, 0xF0, 0xC7, 0xA1, 0xD0, 0xA3, 0x83, 0x9B, 0x49, 0x2B, 0x83, 0xA4, 0x23, 0x64, 0x83, 0xA9, + 0x37, 0xE4, 0xBB, 0xA8, 0x2D, 0x2F, 0xCB, 0xB4, 0x16, 0x50, 0x70, 0x71, 0x83, 0xBB, 0x11, 0x30, + 0x52, 0x5A, 0xC4, 0x9E, 0x94, 0xA8, 0xC7, 0x8F, 0x10, 0x1F, 0x53, 0x4A, 0x20, 0x06, 0x20, 0xA6, + 0x40, 0xD0, 0xA7, 0x42, 0x8A, 0x54, 0xE6, 0x92, 0x53, 0x2A, 0x20, 0xCA, 0x48, 0xCD, 0xE2, 0xC1, + 0x85, 0x78, 0xD4, 0x46, 0xD6, 0x80, 0xFD, 0xDC, 0xBD, 0x73, 0x33, 0xDE, 0x90, 0x68, 0x09, 0x56, + 0x36, 0x3D, 0x9A, 0xA6, 0x52, 0x5C, 0x54, 0xC7, 0x19, 0xF8, 0xA8, 0xA1, 0x03, 0x5A, 0x23, 0x84, + 0x11, 0x1E, 0x84, 0x8A, 0x01, 0x40, 0x7F, 0x42, 0xC3, 0x1C, 0x22, 0x70, 0x08, 0x20, 0x82, 0xA0, + 0x7F, 0x49, 0x0D, 0xF7, 0x64, 0x05, 0xC9, 0xF8, 0xD8, 0x6D, 0x35, 0xF0, 0x9D, 0x66, 0x95, 0xEC, + 0x20, 0xA5, 0xBD, 0x68, 0x24, 0xFA, 0x64, 0x98, 0x1A, 0x50, 0x00, 0xAC, 0xD9, 0x01, 0xA0, 0x1E, + 0x24, 0x5E, 0x63, 0x2B, 0x3F, 0xEF, 0x04, 0x2A, 0xBB, 0x00, 0xAB, 0xBB, 0x8E, 0x87, 0x5F, 0x39, + 0x4F, 0x19, 0xA7, 0x39, 0x26, 0x00, 0x7B, 0x93, 0x68, 0x7A, 0x99, 0x30, 0x2E, 0xCE, 0x64, 0x1B, + 0x6A, 0x6C, 0xB4, 0xE4, 0xF5, 0xA9, 0x87, 0x15, 0x79, 0x3F, 0xC5, 0x8B, 0xCB, 0x0C, 0xF3, 0xBA, + 0x53, 0x79, 0x77, 0xB1, 0x86, 0x70, 0x21, 0x50, 0x66, 0x38, 0xB3, 0x29, 0x74, 0xB0, 0xFA, 0xA1, + 0x48, 0x82, 0x7A, 0x4F, 0xB7, 0x42, 0xE2, 0xC1, 0x44, 0xED, 0x81, 0xF9, 0xDC, 0xC2, 0xD8, 0xE1, + 0x94, 0x83, 0x5A, 0x0A, 0xB5, 0x02, 0x45, 0xC6, 0x95, 0xCD, 0x98, 0x35, 0x1D, 0x6A, 0x58, 0x88, + 0x61, 0xE0, 0xAF, 0xFE, 0x05, 0x0F, 0x1E, 0x1C, 0xC8, 0x55, 0x3F, 0xE1, 0x23, 0xE3, 0x7E, 0xF4, + 0x23, 0x3E, 0x3E, 0xAF, 0xF0, 0xF1, 0x79, 0x1D, 0x1F, 0xB4, 0xAA, 0x3C, 0x98, 0x0C, 0x80, 0xEC, + 0x19, 0xE1, 0x64, 0x4C, 0x13, 0x58, 0xC0, 0x43, 0x50, 0x25, 0x7F, 0x8B, 0xB3, 0x84, 0xFE, 0x98, + 0xB3, 0xDE, 0x84, 0x8D, 0xC4, 0x23, 0xFE, 0x8A, 0xD5, 0xFF, 0x82, 0x4B, 0x3C, 0x70, 0x3D, 0x97, + 0x79, 0x6D, 0x5A, 0x49, 0x28, 0x3F, 0x7E, 0x2B, 0x91, 0x7E, 0xE4, 0x42, 0x78, 0xA9, 0x38, 0xC8, + 0xDF, 0xB7, 0xF4, 0x00, 0xBC, 0x11, 0xF8, 0x29, 0x35, 0x75, 0xBC, 0x0B, 0xA5, 0xFC, 0x29, 0x30, + 0x64, 0xA8, 0xC0, 0x47, 0xDD, 0xD9, 0xDC, 0x12, 0xAE, 0x01, 0x8A, 0xF1, 0xA3, 0x29, 0xB0, 0xEA, + 0xC9, 0x02, 0xD7, 0x9E, 0x40, 0x26, 0x04, 0x91, 0xE0, 0x48, 0xC8, 0xA7, 0x8D, 0x2F, 0x07, 0x9B, + 0x37, 0x35, 0xC8, 0x43, 0x2E, 0xFC, 0x98, 0x2E, 0x0C, 0x36, 0x6F, 0xFE, 0x6D, 0x36, 0xC6, 0xCC, + 0x5A, 0x76, 0xA4, 0x96, 0x4C, 0xF6, 0xF4, 0x0B, 0xBF, 0x71, 0x09, 0x48, 0x5D, 0x49, 0x78, 0x45, + 0x34, 0x03, 0x6B, 0x43, 0x61, 0xE1, 0x07, 0xFF, 0x47, 0x09, 0xF8, 0x91, 0x9E, 0x07, 0xCE, 0xBD, + 0xE6, 0x3D, 0x5E, 0x2F, 0x3E, 0x85, 0xE9, 0x56, 0xE9, 0xC1, 0x4A, 0xC7, 0xEF, 0x53, 0x3A, 0x76, + 0x59, 0xA2, 0x14, 0x4A, 0x14, 0x59, 0x88, 0x1A, 0x6A, 0x50, 0x0E, 0x51, 0x98, 0x89, 0x17, 0xCD, + 0x81, 0x02, 0x9B, 0x73, 0x34, 0x5B, 0x3A, 0x02, 0x0F, 0xF4, 0xF5, 0x45, 0xEE, 0xFC, 0x74, 0x76, + 0x7A, 0x22, 0x44, 0x7C, 0xA5, 0x62, 0x22, 0xD0, 0xAA, 0x2E, 0x2C, 0x2F, 0xCF, 0x9C, 0x89, 0xE4, + 0xA1, 0x28, 0x75, 0x30, 0x31, 0x28, 0x87, 0xFE, 0x74, 0x31, 0xFC, 0x0A, 0x71, 0xD6, 0xD0, 0xCF, + 0x52, 0x48, 0x58, 0x5B, 0x36, 0xA2, 0xF7, 0xFB, 0x97, 0xF6, 0xAE, 0xDD, 0x84, 0xBA, 0x00, 0xB4, + 0x0A, 0x69, 0x19, 0xEE, 0x7D, 0xFE, 0xB7, 0x90, 0xB7, 0xFF, 0x1E, 0x32, 0x83, 0xA8, 0x95, 0x42, + 0x58, 0x2A, 0xF0, 0xAB, 0xB8, 0x93, 0x24, 0x9A, 0x4A, 0xB4, 0xE3, 0x24, 0xC1, 0x4B, 0xE9, 0x43, + 0x85, 0xA2, 0x0D, 0x61, 0x31, 0xA5, 0x89, 0xE6, 0x47, 0x34, 0xD5, 0x78, 0x24, 0xB4, 0x34, 0x8B, + 0x63, 0x68, 0x5C, 0x56, 0xF4, 0x61, 0xEB, 0xC5, 0xEB, 0xCB, 0xFB, 0x8C, 0x66, 0xD4, 0xCF, 0x97, + 0x69, 0x52, 0xD1, 0x0B, 0x56, 0x50, 0xDF, 0x10, 0xEE, 0x7E, 0xB9, 0xC9, 0xEB, 0xA9, 0x8C, 0x73, + 0x8C, 0xA2, 0x1B, 0x2D, 0x35, 0x07, 0xE9, 0x26, 0x40, 0xD5, 0xE5, 0x59, 0x10, 0xCC, 0xDB, 0x2B, + 0xB4, 0xA0, 0xF1, 0x8A, 0x44, 0x24, 0x9F, 0xCB, 0x67, 0x7F, 0xE4, 0xC9, 0xA9, 0xE2, 0x82, 0x50, + 0xF2, 0x54, 0xA9, 0x36, 0xAD, 0x0D, 0x63, 0x83, 0x6A, 0x8C, 0xA7, 0x82, 0x70, 0x0F, 0xAF, 0x51, + 0xE9, 0xC2, 0x2C, 0x6A, 0x29, 0xDC, 0xDE, 0x46, 0x5F, 0xCB, 0x6D, 0xE9, 0x89, 0x7C, 0x2A, 0x25, + 0xE3, 0xAE, 0xAE, 0x63, 0x55, 0x45, 0xB1, 0x3E, 0x25, 0x61, 0x5A, 0x26, 0x5B, 0x54, 0x06, 0x26, + 0x77, 0x0B, 0x70, 0x9B, 0x06, 0x29, 0x1C, 0xBD, 0x7E, 0x7F, 0xCE, 0x46, 0xD1, 0xCE, 0x11, 0x80, + 0x69, 0xC5, 0x3E, 0x93, 0xD7, 0xE0, 0x24, 0xCC, 0x73, 0x07, 0x32, 0xE9, 0x4A, 0x03, 0x0E, 0xA9, + 0x98, 0x44, 0xFE, 0x81, 0x7E, 0xA0, 0x3B, 0x3A, 0xFC, 0xBB, 0x09, 0x35, 0x47, 0xCD, 0xA5, 0xD0, + 0xA4, 0xFA, 0x74, 0x70, 0xF5, 0x06, 0xC2, 0x53, 0x0C, 0xA5, 0x01, 0x17, 0x50, 0x34, 0xD7, 0x74, + 0x7C, 0x7A, 0x7D, 0x0C, 0x29, 0xC8, 0x7F, 0x21, 0x37, 0x66, 0xBB, 0xAA, 0x6C, 0xB8, 0xF3, 0xEA, + 0x75, 0x56, 0x2E, 0x03, 0x7A, 0x61, 0x8C, 0x58, 0x0F, 0x29, 0x7E, 0xFB, 0x7B, 0xF4, 0x9E, 0x8D, + 0x15, 0xD2, 0x6A, 0x5D, 0x6F, 0xCE, 0x76, 0x90, 0x67, 0x89, 0xD5, 0x43, 0x2C, 0x70, 0x97, 0x1F, + 0x29, 0x59, 0x95, 0x35, 0xDC, 0xF6, 0x48, 0x10, 0xE0, 0xC7, 0x5A, 0x03, 0x1B, 0x6A, 0x22, 0xB2, + 0xD4, 0x42, 0x22, 0x29, 0x08, 0x90, 0xD2, 0x3E, 0x84, 0x39, 0xD3, 0x92, 0x65, 0x86, 0xB2, 0xA1, + 0xBC, 0xFF, 0xC5, 0x9A, 0xA3, 0x64, 0x46, 0xE8, 0xCE, 0xF9, 0x6C, 0x73, 0x53, 0xD8, 0x85, 0x99, + 0x18, 0x05, 0x52, 0x8A, 0x01, 0x1C, 0x9A, 0x7D, 0x68, 0x2D, 0x8C, 0xB2, 0x90, 0x58, 0xAB, 0x3D, + 0xD2, 0xB6, 0x51, 0x55, 0x03, 0x54, 0x7C, 0x46, 0x01, 0x03, 0xCE, 0xB2, 0x24, 0x80, 0xA8, 0x8B, + 0x39, 0xBA, 0xB2, 0x2D, 0xC5, 0xBA, 0xD0, 0x84, 0x0E, 0xEC, 0x67, 0xC8, 0x12, 0x95, 0x97, 0xAD, + 0xA2, 0x27, 0x12, 0xC5, 0x77, 0x95, 0x9E, 0xC8, 0x6F, 0xE5, 0x84, 0xAA, 0xC8, 0x77, 0x88, 0x2F, + 0x13, 0x5C, 0xD4, 0xD1, 0x13, 0xA0, 0x24, 0x83, 0x52, 0x34, 0x60, 0x2A, 0x2C, 0x37, 0xEE, 0xEB, + 0xD3, 0xE9, 0xB4, 0x8E, 0xDF, 0x6A, 0xEB, 0x70, 0x82, 0xB2, 0x02, 0x5F, 0x5F, 0xC7, 0x21, 0x47, + 0x15, 0x58, 0xF8, 0x6E, 0xE1, 0xAC, 0xBA, 0xE8, 0x42, 0x7F, 0x2B, 0xDE, 0xD4, 0xAA, 0xD2, 0x59, + 0xE1, 0x73, 0x79, 0xDB, 0x7B, 0x3B, 0x2B, 0x20, 0x32, 0xC4, 0xAF, 0xB2, 0x90, 0x69, 0x20, 0x0D, + 0x3B, 0xE5, 0x46, 0x56, 0x25, 0x85, 0x65, 0x5C, 0xB0, 0xE3, 0x2C, 0x9D, 0x18, 0x33, 0x60, 0xDD, + 0x11, 0x96, 0xD2, 0x95, 0x43, 0x2D, 0x65, 0xB7, 0x0E, 0xB7, 0x0A, 0xFB, 0x70, 0x30, 0x83, 0x94, + 0x79, 0xFB, 0xF3, 0x4F, 0x39, 0x5B, 0xDE, 0xF6, 0x92, 0x62, 0x71, 0xE1, 0xF3, 0xFC, 0xA9, 0x35, + 0xAF, 0x69, 0xA5, 0xD1, 0xAF, 0xC4, 0x97, 0xBD, 0x46, 0xFE, 0x19, 0x3B, 0xFF, 0x9C, 0xAD, 0x81, + 0xB1, 0x43, 0x23, 0x2A, 0xDC, 0x4C, 0x8C, 0xEA, 0x2F, 0x34, 0xE6, 0x63, 0x79, 0x29, 0xBF, 0x2D, + 0xA0, 0x54, 0xA9, 0xD3, 0x68, 0x78, 0x3E, 0xFF, 0x9A, 0x42, 0x19, 0x1D, 0x65, 0xFE, 0x28, 0x20, + 0x09, 0xC5, 0x82, 0xA3, 0x41, 0xBE, 0x92, 0xFB, 0x46, 0xC0, 0x86, 0x69, 0x03, 0x93, 0x6D, 0xCB, + 0xDE, 0xB2, 0x77, 0x71, 0x64, 0x7F, 0x4D, 0xF7, 0x57, 0x4F, 0xD8, 0x5F, 0x34, 0x69, 0x58, 0x0B, + 0xE7, 0xB5, 0xAB, 0x8A, 0x4D, 0x6A, 0x83, 0xFB, 0xC4, 0xA7, 0x70, 0x3D, 0x6F, 0xB3, 0xCC, 0xB6, + 0x1A, 0xE4, 0x5F, 0x60, 0xD4, 0x31, 0xBA, 0x95, 0x2F, 0x92, 0xF4, 0x81, 0x7B, 0x18, 0x5B, 0x17, + 0x54, 0x26, 0x70, 0x49, 0xD5, 0x87, 0x34, 0xB9, 0xD3, 0x9C, 0x2F, 0x39, 0xC3, 0xB7, 0x3C, 0xA8, + 0x03, 0xE4, 0x37, 0x9C, 0x72, 0x39, 0xB0, 0xBF, 0x07, 0x5D, 0x33, 0x2A, 0x41, 0x79, 0xB1, 0x26, + 0x9B, 0xE6, 0x7C, 0x02, 0x82, 0x01, 0x70, 0xB1, 0xA3, 0x48, 0xCD, 0x2B, 0xCB, 0x98, 0x9B, 0x57, + 0x96, 0x54, 0xE2, 0x5F, 0x59, 0xCC, 0xDB, 0x9F, 0xFC, 0xDB, 0x4C, 0xF9, 0x7F, 0x5B, 0x28, 0x36, + 0x32, 0xF9, 0xE1, 0x09, 0xF7, 0x56, 0x3F, 0x45, 0xAD, 0x47, 0x51, 0xBB, 0xF7, 0xFF, 0x17, 0x53, + 0xE8, 0x9D, 0x36, 0x92, 0x29, 0x00, 0x00 +}; + +#define SPIFFS_MAXLENGTH_FILEPATH 32 +const char *excludeListFile = "/.exclude.files"; + +typedef struct ExcludeListS { + char *item; + ExcludeListS *next; +} ExcludeList; + +static ExcludeList *excludes = NULL; + +static bool matchWild(const char *pattern, const char *testee) { + const char *nxPat = NULL, *nxTst = NULL; + + while (*testee) { + if (( *pattern == '?' ) || (*pattern == *testee)){ + pattern++;testee++; + continue; + } + if (*pattern=='*'){ + nxPat=pattern++; nxTst=testee; + continue; + } + if (nxPat){ + pattern = nxPat+1; testee=++nxTst; + continue; + } + return false; + } + while (*pattern=='*'){pattern++;} + return (*pattern == 0); +} + +static bool addExclude(const char *item){ + size_t len = strlen(item); + if(!len){ + return false; + } + ExcludeList *e = (ExcludeList *)malloc(sizeof(ExcludeList)); + if(!e){ + return false; + } + e->item = (char *)malloc(len+1); + if(!e->item){ + free(e); + return false; + } + memcpy(e->item, item, len+1); + e->next = excludes; + excludes = e; + return true; +} + +static void loadExcludeList(fs::FS &_fs, const char *filename){ + static char linebuf[SPIFFS_MAXLENGTH_FILEPATH]; + fs::File excludeFile=_fs.open(filename, "r"); + if(!excludeFile){ + //addExclude("/*.js.gz"); + return; + } +#ifdef ESP32 + if(excludeFile.isDirectory()){ + excludeFile.close(); + return; + } +#endif + if (excludeFile.size() > 0){ + uint8_t idx; + bool isOverflowed = false; + while (excludeFile.available()){ + linebuf[0] = '\0'; + idx = 0; + int lastChar; + do { + lastChar = excludeFile.read(); + if(lastChar != '\r'){ + linebuf[idx++] = (char) lastChar; + } + } while ((lastChar >= 0) && (lastChar != '\n') && (idx < SPIFFS_MAXLENGTH_FILEPATH)); + + if(isOverflowed){ + isOverflowed = (lastChar != '\n'); + continue; + } + isOverflowed = (idx >= SPIFFS_MAXLENGTH_FILEPATH); + linebuf[idx-1] = '\0'; + if(!addExclude(linebuf)){ + excludeFile.close(); + return; + } + } + } + excludeFile.close(); +} + +static bool isExcluded(fs::FS &_fs, const char *filename) { + if(excludes == NULL){ + loadExcludeList(_fs, excludeListFile); + } + ExcludeList *e = excludes; + while(e){ + if (matchWild(e->item, filename)){ + return true; + } + e = e->next; + } + return false; +} + +// WEB HANDLER IMPLEMENTATION + +#ifdef ESP32 +SPIFFSEditor::SPIFFSEditor(const fs::FS& fs, const String& username, const String& password) +#else +SPIFFSEditor::SPIFFSEditor(const String& username, const String& password, const fs::FS& fs) +#endif +:_fs(fs) +,_username(username) +,_password(password) +,_authenticated(false) +,_startTime(0) +{} + +bool SPIFFSEditor::canHandle(AsyncWebServerRequest *request){ + if(request->url().equalsIgnoreCase("/edit")){ + if(request->method() == HTTP_GET){ + if(request->hasParam("list")) + return true; + if(request->hasParam("edit")){ + request->_tempFile = _fs.open(request->arg("edit"), "r"); + if(!request->_tempFile){ + return false; + } +#ifdef ESP32 + if(request->_tempFile.isDirectory()){ + request->_tempFile.close(); + return false; + } +#endif + } + if(request->hasParam("download")){ + request->_tempFile = _fs.open(request->arg("download"), "r"); + if(!request->_tempFile){ + return false; + } +#ifdef ESP32 + if(request->_tempFile.isDirectory()){ + request->_tempFile.close(); + return false; + } +#endif + } + request->addInterestingHeader("If-Modified-Since"); + return true; + } + else if(request->method() == HTTP_POST) + return true; + else if(request->method() == HTTP_DELETE) + return true; + else if(request->method() == HTTP_PUT) + return true; + + } + return false; +} + + +void SPIFFSEditor::handleRequest(AsyncWebServerRequest *request){ + if(_username.length() && _password.length() && !request->authenticate(_username.c_str(), _password.c_str())) + return request->requestAuthentication(); + + if(request->method() == HTTP_GET){ + if(request->hasParam("list")){ + String path = request->getParam("list")->value(); +#ifdef ESP32 + File dir = _fs.open(path); +#else + Dir dir = _fs.openDir(path); +#endif + path = String(); + String output = "["; +#ifdef ESP32 + File entry = dir.openNextFile(); + while(entry){ +#else + while(dir.next()){ + fs::File entry = dir.openFile("r"); +#endif + if (isExcluded(_fs, entry.name())) { +#ifdef ESP32 + entry = dir.openNextFile(); +#endif + continue; + } + if (output != "[") output += ','; + output += "{\"type\":\""; + output += "file"; + output += "\",\"name\":\""; + output += String(entry.name()); + output += "\",\"size\":"; + output += String(entry.size()); + output += "}"; +#ifdef ESP32 + entry = dir.openNextFile(); +#else + entry.close(); +#endif + } +#ifdef ESP32 + dir.close(); +#endif + output += "]"; + request->send(200, "application/json", output); + output = String(); + } + else if(request->hasParam("edit") || request->hasParam("download")){ + request->send(request->_tempFile, request->_tempFile.name(), String(), request->hasParam("download")); + } + else { + const char * buildTime = __DATE__ " " __TIME__ " GMT"; + if (request->header("If-Modified-Since").equals(buildTime)) { + request->send(304); + } else { + AsyncWebServerResponse *response = request->beginResponse_P(200, "text/html", edit_htm_gz, edit_htm_gz_len); + response->addHeader("Content-Encoding", "gzip"); + response->addHeader("Last-Modified", buildTime); + request->send(response); + } + } + } else if(request->method() == HTTP_DELETE){ + if(request->hasParam("path", true)){ + _fs.remove(request->getParam("path", true)->value()); + request->send(200, "", "DELETE: "+request->getParam("path", true)->value()); + } else + request->send(404); + } else if(request->method() == HTTP_POST){ + if(request->hasParam("data", true, true) && _fs.exists(request->getParam("data", true, true)->value())) + request->send(200, "", "UPLOADED: "+request->getParam("data", true, true)->value()); + else + request->send(500); + } else if(request->method() == HTTP_PUT){ + if(request->hasParam("path", true)){ + String filename = request->getParam("path", true)->value(); + if(_fs.exists(filename)){ + request->send(200); + } else { + fs::File f = _fs.open(filename, "w"); + if(f){ + f.write((uint8_t)0x00); + f.close(); + request->send(200, "", "CREATE: "+filename); + } else { + request->send(500); + } + } + } else + request->send(400); + } +} + +void SPIFFSEditor::handleUpload(AsyncWebServerRequest *request, const String& filename, size_t index, uint8_t *data, size_t len, bool final){ + if(!index){ + if(!_username.length() || request->authenticate(_username.c_str(),_password.c_str())){ + _authenticated = true; + request->_tempFile = _fs.open(filename, "w"); + _startTime = millis(); + } + } + if(_authenticated && request->_tempFile){ + if(len){ + request->_tempFile.write(data,len); + } + if(final){ + request->_tempFile.close(); + } + } +} diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/SPIFFSEditor.h b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/SPIFFSEditor.h new file mode 100644 index 0000000..3586429 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/SPIFFSEditor.h @@ -0,0 +1,24 @@ +#ifndef SPIFFSEditor_H_ +#define SPIFFSEditor_H_ +#include + +class SPIFFSEditor: public AsyncWebHandler { + private: + fs::FS _fs; + String _username; + String _password; + bool _authenticated; + uint32_t _startTime; + public: +#ifdef ESP32 + SPIFFSEditor(const fs::FS& fs, const String& username=String(), const String& password=String()); +#else + SPIFFSEditor(const String& username=String(), const String& password=String(), const fs::FS& fs=SPIFFS); +#endif + virtual bool canHandle(AsyncWebServerRequest *request) override final; + virtual void handleRequest(AsyncWebServerRequest *request) override final; + virtual void handleUpload(AsyncWebServerRequest *request, const String& filename, size_t index, uint8_t *data, size_t len, bool final) override final; + virtual bool isRequestHandlerTrivial() override final {return false;} +}; + +#endif diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/StringArray.h b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/StringArray.h new file mode 100644 index 0000000..4c0aa70 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/StringArray.h @@ -0,0 +1,193 @@ +/* + Asynchronous WebServer library for Espressif MCUs + + Copyright (c) 2016 Hristo Gochkov. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ +#ifndef STRINGARRAY_H_ +#define STRINGARRAY_H_ + +#include "stddef.h" +#include "WString.h" + +template +class LinkedListNode { + T _value; + public: + LinkedListNode* next; + LinkedListNode(const T val): _value(val), next(nullptr) {} + ~LinkedListNode(){} + const T& value() const { return _value; }; + T& value(){ return _value; } +}; + +template class Item = LinkedListNode> +class LinkedList { + public: + typedef Item ItemType; + typedef std::function OnRemove; + typedef std::function Predicate; + private: + ItemType* _root; + OnRemove _onRemove; + + class Iterator { + ItemType* _node; + public: + Iterator(ItemType* current = nullptr) : _node(current) {} + Iterator(const Iterator& i) : _node(i._node) {} + Iterator& operator ++() { _node = _node->next; return *this; } + bool operator != (const Iterator& i) const { return _node != i._node; } + const T& operator * () const { return _node->value(); } + const T* operator -> () const { return &_node->value(); } + }; + + public: + typedef const Iterator ConstIterator; + ConstIterator begin() const { return ConstIterator(_root); } + ConstIterator end() const { return ConstIterator(nullptr); } + + LinkedList(OnRemove onRemove) : _root(nullptr), _onRemove(onRemove) {} + ~LinkedList(){} + void add(const T& t){ + auto it = new ItemType(t); + if(!_root){ + _root = it; + } else { + auto i = _root; + while(i->next) i = i->next; + i->next = it; + } + } + T& front() const { + return _root->value(); + } + + bool isEmpty() const { + return _root == nullptr; + } + size_t length() const { + size_t i = 0; + auto it = _root; + while(it){ + i++; + it = it->next; + } + return i; + } + size_t count_if(Predicate predicate) const { + size_t i = 0; + auto it = _root; + while(it){ + if (!predicate){ + i++; + } + else if (predicate(it->value())) { + i++; + } + it = it->next; + } + return i; + } + const T* nth(size_t N) const { + size_t i = 0; + auto it = _root; + while(it){ + if(i++ == N) + return &(it->value()); + it = it->next; + } + return nullptr; + } + bool remove(const T& t){ + auto it = _root; + auto pit = _root; + while(it){ + if(it->value() == t){ + if(it == _root){ + _root = _root->next; + } else { + pit->next = it->next; + } + + if (_onRemove) { + _onRemove(it->value()); + } + + delete it; + return true; + } + pit = it; + it = it->next; + } + return false; + } + bool remove_first(Predicate predicate){ + auto it = _root; + auto pit = _root; + while(it){ + if(predicate(it->value())){ + if(it == _root){ + _root = _root->next; + } else { + pit->next = it->next; + } + if (_onRemove) { + _onRemove(it->value()); + } + delete it; + return true; + } + pit = it; + it = it->next; + } + return false; + } + + void free(){ + while(_root != nullptr){ + auto it = _root; + _root = _root->next; + if (_onRemove) { + _onRemove(it->value()); + } + delete it; + } + _root = nullptr; + } +}; + + +class StringArray : public LinkedList { +public: + + StringArray() : LinkedList(nullptr) {} + + bool containsIgnoreCase(const String& str){ + for (const auto& s : *this) { + if (str.equalsIgnoreCase(s)) { + return true; + } + } + return false; + } +}; + + + + +#endif /* STRINGARRAY_H_ */ diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/WebAuthentication.cpp b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/WebAuthentication.cpp new file mode 100644 index 0000000..2feca54 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/WebAuthentication.cpp @@ -0,0 +1,235 @@ +/* + Asynchronous WebServer library for Espressif MCUs + + Copyright (c) 2016 Hristo Gochkov. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ +#include "WebAuthentication.h" +#include +#ifdef ESP32 +#include "mbedtls/md5.h" +#else +#include "md5.h" +#endif + + +// Basic Auth hash = base64("username:password") + +bool checkBasicAuthentication(const char * hash, const char * username, const char * password){ + if(username == NULL || password == NULL || hash == NULL) + return false; + + size_t toencodeLen = strlen(username)+strlen(password)+1; + size_t encodedLen = base64_encode_expected_len(toencodeLen); + if(strlen(hash) != encodedLen) + return false; + + char *toencode = new char[toencodeLen+1]; + if(toencode == NULL){ + return false; + } + char *encoded = new char[base64_encode_expected_len(toencodeLen)+1]; + if(encoded == NULL){ + delete[] toencode; + return false; + } + sprintf(toencode, "%s:%s", username, password); + if(base64_encode_chars(toencode, toencodeLen, encoded) > 0 && memcmp(hash, encoded, encodedLen) == 0){ + delete[] toencode; + delete[] encoded; + return true; + } + delete[] toencode; + delete[] encoded; + return false; +} + +static bool getMD5(uint8_t * data, uint16_t len, char * output){//33 bytes or more +#ifdef ESP32 + mbedtls_md5_context _ctx; +#else + md5_context_t _ctx; +#endif + uint8_t i; + uint8_t * _buf = (uint8_t*)malloc(16); + if(_buf == NULL) + return false; + memset(_buf, 0x00, 16); +#ifdef ESP32 + mbedtls_md5_init(&_ctx); + mbedtls_md5_starts(&_ctx); + mbedtls_md5_update(&_ctx, data, len); + mbedtls_md5_finish(&_ctx, _buf); +#else + MD5Init(&_ctx); + MD5Update(&_ctx, data, len); + MD5Final(_buf, &_ctx); +#endif + for(i = 0; i < 16; i++) { + sprintf(output + (i * 2), "%02x", _buf[i]); + } + free(_buf); + return true; +} + +static String genRandomMD5(){ +#ifdef ESP8266 + uint32_t r = RANDOM_REG32; +#else + uint32_t r = rand(); +#endif + char * out = (char*)malloc(33); + if(out == NULL || !getMD5((uint8_t*)(&r), 4, out)) + return ""; + String res = String(out); + free(out); + return res; +} + +static String stringMD5(const String& in){ + char * out = (char*)malloc(33); + if(out == NULL || !getMD5((uint8_t*)(in.c_str()), in.length(), out)) + return ""; + String res = String(out); + free(out); + return res; +} + +String generateDigestHash(const char * username, const char * password, const char * realm){ + if(username == NULL || password == NULL || realm == NULL){ + return ""; + } + char * out = (char*)malloc(33); + String res = String(username); + res.concat(":"); + res.concat(realm); + res.concat(":"); + String in = res; + in.concat(password); + if(out == NULL || !getMD5((uint8_t*)(in.c_str()), in.length(), out)) + return ""; + res.concat(out); + free(out); + return res; +} + +String requestDigestAuthentication(const char * realm){ + String header = "realm=\""; + if(realm == NULL) + header.concat("asyncesp"); + else + header.concat(realm); + header.concat( "\", qop=\"auth\", nonce=\""); + header.concat(genRandomMD5()); + header.concat("\", opaque=\""); + header.concat(genRandomMD5()); + header.concat("\""); + return header; +} + +bool checkDigestAuthentication(const char * header, const char * method, const char * username, const char * password, const char * realm, bool passwordIsHash, const char * nonce, const char * opaque, const char * uri){ + if(username == NULL || password == NULL || header == NULL || method == NULL){ + //os_printf("AUTH FAIL: missing requred fields\n"); + return false; + } + + String myHeader = String(header); + int nextBreak = myHeader.indexOf(","); + if(nextBreak < 0){ + //os_printf("AUTH FAIL: no variables\n"); + return false; + } + + String myUsername = String(); + String myRealm = String(); + String myNonce = String(); + String myUri = String(); + String myResponse = String(); + String myQop = String(); + String myNc = String(); + String myCnonce = String(); + + myHeader += ", "; + do { + String avLine = myHeader.substring(0, nextBreak); + avLine.trim(); + myHeader = myHeader.substring(nextBreak+1); + nextBreak = myHeader.indexOf(","); + + int eqSign = avLine.indexOf("="); + if(eqSign < 0){ + //os_printf("AUTH FAIL: no = sign\n"); + return false; + } + String varName = avLine.substring(0, eqSign); + avLine = avLine.substring(eqSign + 1); + if(avLine.startsWith("\"")){ + avLine = avLine.substring(1, avLine.length() - 1); + } + + if(varName.equals("username")){ + if(!avLine.equals(username)){ + //os_printf("AUTH FAIL: username\n"); + return false; + } + myUsername = avLine; + } else if(varName.equals("realm")){ + if(realm != NULL && !avLine.equals(realm)){ + //os_printf("AUTH FAIL: realm\n"); + return false; + } + myRealm = avLine; + } else if(varName.equals("nonce")){ + if(nonce != NULL && !avLine.equals(nonce)){ + //os_printf("AUTH FAIL: nonce\n"); + return false; + } + myNonce = avLine; + } else if(varName.equals("opaque")){ + if(opaque != NULL && !avLine.equals(opaque)){ + //os_printf("AUTH FAIL: opaque\n"); + return false; + } + } else if(varName.equals("uri")){ + if(uri != NULL && !avLine.equals(uri)){ + //os_printf("AUTH FAIL: uri\n"); + return false; + } + myUri = avLine; + } else if(varName.equals("response")){ + myResponse = avLine; + } else if(varName.equals("qop")){ + myQop = avLine; + } else if(varName.equals("nc")){ + myNc = avLine; + } else if(varName.equals("cnonce")){ + myCnonce = avLine; + } + } while(nextBreak > 0); + + String ha1 = (passwordIsHash) ? String(password) : stringMD5(myUsername + ":" + myRealm + ":" + String(password)); + String ha2 = String(method) + ":" + myUri; + String response = ha1 + ":" + myNonce + ":" + myNc + ":" + myCnonce + ":" + myQop + ":" + stringMD5(ha2); + + if(myResponse.equals(stringMD5(response))){ + //os_printf("AUTH SUCCESS\n"); + return true; + } + + //os_printf("AUTH FAIL: password\n"); + return false; +} diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/WebAuthentication.h b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/WebAuthentication.h new file mode 100644 index 0000000..ff68265 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/WebAuthentication.h @@ -0,0 +1,34 @@ +/* + Asynchronous WebServer library for Espressif MCUs + + Copyright (c) 2016 Hristo Gochkov. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef WEB_AUTHENTICATION_H_ +#define WEB_AUTHENTICATION_H_ + +#include "Arduino.h" + +bool checkBasicAuthentication(const char * header, const char * username, const char * password); +String requestDigestAuthentication(const char * realm); +bool checkDigestAuthentication(const char * header, const char * method, const char * username, const char * password, const char * realm, bool passwordIsHash, const char * nonce, const char * opaque, const char * uri); + +//for storing hashed versions on the device that can be authenticated against +String generateDigestHash(const char * username, const char * password, const char * realm); + +#endif diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/WebHandlerImpl.h b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/WebHandlerImpl.h new file mode 100644 index 0000000..d121fa7 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/WebHandlerImpl.h @@ -0,0 +1,138 @@ +/* + Asynchronous WebServer library for Espressif MCUs + + Copyright (c) 2016 Hristo Gochkov. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ +#ifndef ASYNCWEBSERVERHANDLERIMPL_H_ +#define ASYNCWEBSERVERHANDLERIMPL_H_ + +#include +#ifdef ASYNCWEBSERVER_REGEX +#include +#endif + +#include "stddef.h" +#include + +class AsyncStaticWebHandler: public AsyncWebHandler { + using File = fs::File; + using FS = fs::FS; + private: + bool _getFile(AsyncWebServerRequest *request); + bool _fileExists(AsyncWebServerRequest *request, const String& path); + uint8_t _countBits(const uint8_t value) const; + protected: + FS _fs; + String _uri; + String _path; + String _default_file; + String _cache_control; + String _last_modified; + AwsTemplateProcessor _callback; + bool _isDir; + bool _gzipFirst; + uint8_t _gzipStats; + public: + AsyncStaticWebHandler(const char* uri, FS& fs, const char* path, const char* cache_control); + virtual bool canHandle(AsyncWebServerRequest *request) override final; + virtual void handleRequest(AsyncWebServerRequest *request) override final; + AsyncStaticWebHandler& setIsDir(bool isDir); + AsyncStaticWebHandler& setDefaultFile(const char* filename); + AsyncStaticWebHandler& setCacheControl(const char* cache_control); + AsyncStaticWebHandler& setLastModified(const char* last_modified); + AsyncStaticWebHandler& setLastModified(struct tm* last_modified); + #ifdef ESP8266 + AsyncStaticWebHandler& setLastModified(time_t last_modified); + AsyncStaticWebHandler& setLastModified(); //sets to current time. Make sure sntp is runing and time is updated + #endif + AsyncStaticWebHandler& setTemplateProcessor(AwsTemplateProcessor newCallback) {_callback = newCallback; return *this;} +}; + +class AsyncCallbackWebHandler: public AsyncWebHandler { + private: + protected: + String _uri; + WebRequestMethodComposite _method; + ArRequestHandlerFunction _onRequest; + ArUploadHandlerFunction _onUpload; + ArBodyHandlerFunction _onBody; + bool _isRegex; + public: + AsyncCallbackWebHandler() : _uri(), _method(HTTP_ANY), _onRequest(NULL), _onUpload(NULL), _onBody(NULL), _isRegex(false) {} + void setUri(const String& uri){ + _uri = uri; + _isRegex = uri.startsWith("^") && uri.endsWith("$"); + } + void setMethod(WebRequestMethodComposite method){ _method = method; } + void onRequest(ArRequestHandlerFunction fn){ _onRequest = fn; } + void onUpload(ArUploadHandlerFunction fn){ _onUpload = fn; } + void onBody(ArBodyHandlerFunction fn){ _onBody = fn; } + + virtual bool canHandle(AsyncWebServerRequest *request) override final{ + + if(!_onRequest) + return false; + + if(!(_method & request->method())) + return false; + +#ifdef ASYNCWEBSERVER_REGEX + if (_isRegex) { + std::regex pattern(_uri.c_str()); + std::smatch matches; + std::string s(request->url().c_str()); + if(std::regex_search(s, matches, pattern)) { + for (size_t i = 1; i < matches.size(); ++i) { // start from 1 + request->_addPathParam(matches[i].str().c_str()); + } + } else { + return false; + } + } else +#endif + if (_uri.length() && _uri.endsWith("*")) { + String uriTemplate = String(_uri); + uriTemplate = uriTemplate.substring(0, uriTemplate.length() - 1); + if (!request->url().startsWith(uriTemplate)) + return false; + } + else if(_uri.length() && (_uri != request->url() && !request->url().startsWith(_uri+"/"))) + return false; + + request->addInterestingHeader("ANY"); + return true; + } + + virtual void handleRequest(AsyncWebServerRequest *request) override final { + if(_onRequest) + _onRequest(request); + else + request->send(500); + } + virtual void handleUpload(AsyncWebServerRequest *request, const String& filename, size_t index, uint8_t *data, size_t len, bool final) override final { + if(_onUpload) + _onUpload(request, filename, index, data, len, final); + } + virtual void handleBody(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total) override final { + if(_onBody) + _onBody(request, data, len, index, total); + } + virtual bool isRequestHandlerTrivial() override final {return _onRequest ? false : true;} +}; + +#endif /* ASYNCWEBSERVERHANDLERIMPL_H_ */ diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/WebHandlers.cpp b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/WebHandlers.cpp new file mode 100644 index 0000000..1f435e6 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/WebHandlers.cpp @@ -0,0 +1,220 @@ +/* + Asynchronous WebServer library for Espressif MCUs + + Copyright (c) 2016 Hristo Gochkov. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ +#include "ESPAsyncWebServer.h" +#include "WebHandlerImpl.h" + +AsyncStaticWebHandler::AsyncStaticWebHandler(const char* uri, FS& fs, const char* path, const char* cache_control) + : _fs(fs), _uri(uri), _path(path), _default_file("index.htm"), _cache_control(cache_control), _last_modified(""), _callback(nullptr) +{ + // Ensure leading '/' + if (_uri.length() == 0 || _uri[0] != '/') _uri = "/" + _uri; + if (_path.length() == 0 || _path[0] != '/') _path = "/" + _path; + + // If path ends with '/' we assume a hint that this is a directory to improve performance. + // However - if it does not end with '/' we, can't assume a file, path can still be a directory. + _isDir = _path[_path.length()-1] == '/'; + + // Remove the trailing '/' so we can handle default file + // Notice that root will be "" not "/" + if (_uri[_uri.length()-1] == '/') _uri = _uri.substring(0, _uri.length()-1); + if (_path[_path.length()-1] == '/') _path = _path.substring(0, _path.length()-1); + + // Reset stats + _gzipFirst = false; + _gzipStats = 0xF8; +} + +AsyncStaticWebHandler& AsyncStaticWebHandler::setIsDir(bool isDir){ + _isDir = isDir; + return *this; +} + +AsyncStaticWebHandler& AsyncStaticWebHandler::setDefaultFile(const char* filename){ + _default_file = String(filename); + return *this; +} + +AsyncStaticWebHandler& AsyncStaticWebHandler::setCacheControl(const char* cache_control){ + _cache_control = String(cache_control); + return *this; +} + +AsyncStaticWebHandler& AsyncStaticWebHandler::setLastModified(const char* last_modified){ + _last_modified = String(last_modified); + return *this; +} + +AsyncStaticWebHandler& AsyncStaticWebHandler::setLastModified(struct tm* last_modified){ + char result[30]; + strftime (result,30,"%a, %d %b %Y %H:%M:%S %Z", last_modified); + return setLastModified((const char *)result); +} + +#ifdef ESP8266 +AsyncStaticWebHandler& AsyncStaticWebHandler::setLastModified(time_t last_modified){ + return setLastModified((struct tm *)gmtime(&last_modified)); +} + +AsyncStaticWebHandler& AsyncStaticWebHandler::setLastModified(){ + time_t last_modified; + if(time(&last_modified) == 0) //time is not yet set + return *this; + return setLastModified(last_modified); +} +#endif +bool AsyncStaticWebHandler::canHandle(AsyncWebServerRequest *request){ + if(request->method() != HTTP_GET + || !request->url().startsWith(_uri) + || !request->isExpectedRequestedConnType(RCT_DEFAULT, RCT_HTTP) + ){ + return false; + } + if (_getFile(request)) { + // We interested in "If-Modified-Since" header to check if file was modified + if (_last_modified.length()) + request->addInterestingHeader("If-Modified-Since"); + + if(_cache_control.length()) + request->addInterestingHeader("If-None-Match"); + + DEBUGF("[AsyncStaticWebHandler::canHandle] TRUE\n"); + return true; + } + + return false; +} + +bool AsyncStaticWebHandler::_getFile(AsyncWebServerRequest *request) +{ + // Remove the found uri + String path = request->url().substring(_uri.length()); + + // We can skip the file check and look for default if request is to the root of a directory or that request path ends with '/' + bool canSkipFileCheck = (_isDir && path.length() == 0) || (path.length() && path[path.length()-1] == '/'); + + path = _path + path; + + // Do we have a file or .gz file + if (!canSkipFileCheck && _fileExists(request, path)) + return true; + + // Can't handle if not default file + if (_default_file.length() == 0) + return false; + + // Try to add default file, ensure there is a trailing '/' ot the path. + if (path.length() == 0 || path[path.length()-1] != '/') + path += "/"; + path += _default_file; + + return _fileExists(request, path); +} + +#ifdef ESP32 +#define FILE_IS_REAL(f) (f == true && !f.isDirectory()) +#else +#define FILE_IS_REAL(f) (f == true) +#endif + +bool AsyncStaticWebHandler::_fileExists(AsyncWebServerRequest *request, const String& path) +{ + bool fileFound = false; + bool gzipFound = false; + + String gzip = path + ".gz"; + + if (_gzipFirst) { + request->_tempFile = _fs.open(gzip, "r"); + gzipFound = FILE_IS_REAL(request->_tempFile); + if (!gzipFound){ + request->_tempFile = _fs.open(path, "r"); + fileFound = FILE_IS_REAL(request->_tempFile); + } + } else { + request->_tempFile = _fs.open(path, "r"); + fileFound = FILE_IS_REAL(request->_tempFile); + if (!fileFound){ + request->_tempFile = _fs.open(gzip, "r"); + gzipFound = FILE_IS_REAL(request->_tempFile); + } + } + + bool found = fileFound || gzipFound; + + if (found) { + // Extract the file name from the path and keep it in _tempObject + size_t pathLen = path.length(); + char * _tempPath = (char*)malloc(pathLen+1); + snprintf(_tempPath, pathLen+1, "%s", path.c_str()); + request->_tempObject = (void*)_tempPath; + + // Calculate gzip statistic + _gzipStats = (_gzipStats << 1) + (gzipFound ? 1 : 0); + if (_gzipStats == 0x00) _gzipFirst = false; // All files are not gzip + else if (_gzipStats == 0xFF) _gzipFirst = true; // All files are gzip + else _gzipFirst = _countBits(_gzipStats) > 4; // IF we have more gzip files - try gzip first + } + + return found; +} + +uint8_t AsyncStaticWebHandler::_countBits(const uint8_t value) const +{ + uint8_t w = value; + uint8_t n; + for (n=0; w!=0; n++) w&=w-1; + return n; +} + +void AsyncStaticWebHandler::handleRequest(AsyncWebServerRequest *request) +{ + // Get the filename from request->_tempObject and free it + String filename = String((char*)request->_tempObject); + free(request->_tempObject); + request->_tempObject = NULL; + if((_username != "" && _password != "") && !request->authenticate(_username.c_str(), _password.c_str())) + return request->requestAuthentication(); + + if (request->_tempFile == true) { + String etag = String(request->_tempFile.size()); + if (_last_modified.length() && _last_modified == request->header("If-Modified-Since")) { + request->_tempFile.close(); + request->send(304); // Not modified + } else if (_cache_control.length() && request->hasHeader("If-None-Match") && request->header("If-None-Match").equals(etag)) { + request->_tempFile.close(); + AsyncWebServerResponse * response = new AsyncBasicResponse(304); // Not modified + response->addHeader("Cache-Control", _cache_control); + response->addHeader("ETag", etag); + request->send(response); + } else { + AsyncWebServerResponse * response = new AsyncFileResponse(request->_tempFile, filename, String(), false, _callback); + if (_last_modified.length()) + response->addHeader("Last-Modified", _last_modified); + if (_cache_control.length()){ + response->addHeader("Cache-Control", _cache_control); + response->addHeader("ETag", etag); + } + request->send(response); + } + } else { + request->send(404); + } +} diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/WebRequest.cpp b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/WebRequest.cpp new file mode 100644 index 0000000..bbce5ca --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/WebRequest.cpp @@ -0,0 +1,1008 @@ +/* + Asynchronous WebServer library for Espressif MCUs + + Copyright (c) 2016 Hristo Gochkov. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ +#include "ESPAsyncWebServer.h" +#include "WebResponseImpl.h" +#include "WebAuthentication.h" + +#ifndef ESP8266 +#define os_strlen strlen +#endif + +static const String SharedEmptyString = String(); + +#define __is_param_char(c) ((c) && ((c) != '{') && ((c) != '[') && ((c) != '&') && ((c) != '=')) + +enum { PARSE_REQ_START, PARSE_REQ_HEADERS, PARSE_REQ_BODY, PARSE_REQ_END, PARSE_REQ_FAIL }; + +AsyncWebServerRequest::AsyncWebServerRequest(AsyncWebServer* s, AsyncClient* c) + : _client(c) + , _server(s) + , _handler(NULL) + , _response(NULL) + , _temp() + , _parseState(0) + , _version(0) + , _method(HTTP_ANY) + , _url() + , _host() + , _contentType() + , _boundary() + , _authorization() + , _reqconntype(RCT_HTTP) + , _isDigest(false) + , _isMultipart(false) + , _isPlainPost(false) + , _expectingContinue(false) + , _contentLength(0) + , _parsedLength(0) + , _headers(LinkedList([](AsyncWebHeader *h){ delete h; })) + , _params(LinkedList([](AsyncWebParameter *p){ delete p; })) + , _pathParams(LinkedList([](String *p){ delete p; })) + , _multiParseState(0) + , _boundaryPosition(0) + , _itemStartIndex(0) + , _itemSize(0) + , _itemName() + , _itemFilename() + , _itemType() + , _itemValue() + , _itemBuffer(0) + , _itemBufferIndex(0) + , _itemIsFile(false) + , _tempObject(NULL) +{ + c->onError([](void *r, AsyncClient* c, int8_t error){ (void)c; AsyncWebServerRequest *req = (AsyncWebServerRequest*)r; req->_onError(error); }, this); + c->onAck([](void *r, AsyncClient* c, size_t len, uint32_t time){ (void)c; AsyncWebServerRequest *req = (AsyncWebServerRequest*)r; req->_onAck(len, time); }, this); + c->onDisconnect([](void *r, AsyncClient* c){ AsyncWebServerRequest *req = (AsyncWebServerRequest*)r; req->_onDisconnect(); delete c; }, this); + c->onTimeout([](void *r, AsyncClient* c, uint32_t time){ (void)c; AsyncWebServerRequest *req = (AsyncWebServerRequest*)r; req->_onTimeout(time); }, this); + c->onData([](void *r, AsyncClient* c, void *buf, size_t len){ (void)c; AsyncWebServerRequest *req = (AsyncWebServerRequest*)r; req->_onData(buf, len); }, this); + c->onPoll([](void *r, AsyncClient* c){ (void)c; AsyncWebServerRequest *req = ( AsyncWebServerRequest*)r; req->_onPoll(); }, this); +} + +AsyncWebServerRequest::~AsyncWebServerRequest(){ + _headers.free(); + + _params.free(); + _pathParams.free(); + + _interestingHeaders.free(); + + if(_response != NULL){ + delete _response; + } + + if(_tempObject != NULL){ + free(_tempObject); + } + + if(_tempFile){ + _tempFile.close(); + } +} + +void AsyncWebServerRequest::_onData(void *buf, size_t len){ + size_t i = 0; + while (true) { + + if(_parseState < PARSE_REQ_BODY){ + // Find new line in buf + char *str = (char*)buf; + for (i = 0; i < len; i++) { + if (str[i] == '\n') { + break; + } + } + if (i == len) { // No new line, just add the buffer in _temp + char ch = str[len-1]; + str[len-1] = 0; + _temp.reserve(_temp.length()+len); + _temp.concat(str); + _temp.concat(ch); + } else { // Found new line - extract it and parse + str[i] = 0; // Terminate the string at the end of the line. + _temp.concat(str); + _temp.trim(); + _parseLine(); + if (++i < len) { + // Still have more buffer to process + buf = str+i; + len-= i; + continue; + } + } + } else if(_parseState == PARSE_REQ_BODY){ + // A handler should be already attached at this point in _parseLine function. + // If handler does nothing (_onRequest is NULL), we don't need to really parse the body. + const bool needParse = _handler && !_handler->isRequestHandlerTrivial(); + if(_isMultipart){ + if(needParse){ + size_t i; + for(i=0; i end) equal = end; + String name = params.substring(start, equal); + String value = equal + 1 < end ? params.substring(equal + 1, end) : String(); + _addParam(new AsyncWebParameter(urlDecode(name), urlDecode(value))); + start = end + 1; + } +} + +bool AsyncWebServerRequest::_parseReqHead(){ + // Split the head into method, url and version + int index = _temp.indexOf(' '); + String m = _temp.substring(0, index); + index = _temp.indexOf(' ', index+1); + String u = _temp.substring(m.length()+1, index); + _temp = _temp.substring(index+1); + + if(m == "GET"){ + _method = HTTP_GET; + } else if(m == "POST"){ + _method = HTTP_POST; + } else if(m == "DELETE"){ + _method = HTTP_DELETE; + } else if(m == "PUT"){ + _method = HTTP_PUT; + } else if(m == "PATCH"){ + _method = HTTP_PATCH; + } else if(m == "HEAD"){ + _method = HTTP_HEAD; + } else if(m == "OPTIONS"){ + _method = HTTP_OPTIONS; + } + + String g = String(); + index = u.indexOf('?'); + if(index > 0){ + g = u.substring(index +1); + u = u.substring(0, index); + } + _url = urlDecode(u); + _addGetParams(g); + + if(!_temp.startsWith("HTTP/1.0")) + _version = 1; + + _temp = String(); + return true; +} + +bool strContains(String src, String find, bool mindcase = true) { + int pos=0, i=0; + const int slen = src.length(); + const int flen = find.length(); + + if (slen < flen) return false; + while (pos <= (slen - flen)) { + for (i=0; i < flen; i++) { + if (mindcase) { + if (src[pos+i] != find[i]) i = flen + 1; // no match + } else if (tolower(src[pos+i]) != tolower(find[i])) i = flen + 1; // no match + } + if (i == flen) return true; + pos++; + } + return false; +} + +bool AsyncWebServerRequest::_parseReqHeader(){ + int index = _temp.indexOf(':'); + if(index){ + String name = _temp.substring(0, index); + String value = _temp.substring(index + 2); + if(name.equalsIgnoreCase("Host")){ + _host = value; + } else if(name.equalsIgnoreCase("Content-Type")){ + _contentType = value.substring(0, value.indexOf(';')); + if (value.startsWith("multipart/")){ + _boundary = value.substring(value.indexOf('=')+1); + _boundary.replace("\"",""); + _isMultipart = true; + } + } else if(name.equalsIgnoreCase("Content-Length")){ + _contentLength = atoi(value.c_str()); + } else if(name.equalsIgnoreCase("Expect") && value == "100-continue"){ + _expectingContinue = true; + } else if(name.equalsIgnoreCase("Authorization")){ + if(value.length() > 5 && value.substring(0,5).equalsIgnoreCase("Basic")){ + _authorization = value.substring(6); + } else if(value.length() > 6 && value.substring(0,6).equalsIgnoreCase("Digest")){ + _isDigest = true; + _authorization = value.substring(7); + } + } else { + if(name.equalsIgnoreCase("Upgrade") && value.equalsIgnoreCase("websocket")){ + // WebSocket request can be uniquely identified by header: [Upgrade: websocket] + _reqconntype = RCT_WS; + } else { + if(name.equalsIgnoreCase("Accept") && strContains(value, "text/event-stream", false)){ + // WebEvent request can be uniquely identified by header: [Accept: text/event-stream] + _reqconntype = RCT_EVENT; + } + } + } + _headers.add(new AsyncWebHeader(name, value)); + } + _temp = String(); + return true; +} + +void AsyncWebServerRequest::_parsePlainPostChar(uint8_t data){ + if(data && (char)data != '&') + _temp += (char)data; + if(!data || (char)data == '&' || _parsedLength == _contentLength){ + String name = "body"; + String value = _temp; + if(!_temp.startsWith("{") && !_temp.startsWith("[") && _temp.indexOf('=') > 0){ + name = _temp.substring(0, _temp.indexOf('=')); + value = _temp.substring(_temp.indexOf('=') + 1); + } + _addParam(new AsyncWebParameter(urlDecode(name), urlDecode(value), true)); + _temp = String(); + } +} + +void AsyncWebServerRequest::_handleUploadByte(uint8_t data, bool last){ + _itemBuffer[_itemBufferIndex++] = data; + + if(last || _itemBufferIndex == 1460){ + //check if authenticated before calling the upload + if(_handler) + _handler->handleUpload(this, _itemFilename, _itemSize - _itemBufferIndex, _itemBuffer, _itemBufferIndex, false); + _itemBufferIndex = 0; + } +} + +enum { + EXPECT_BOUNDARY, + PARSE_HEADERS, + WAIT_FOR_RETURN1, + EXPECT_FEED1, + EXPECT_DASH1, + EXPECT_DASH2, + BOUNDARY_OR_DATA, + DASH3_OR_RETURN2, + EXPECT_FEED2, + PARSING_FINISHED, + PARSE_ERROR +}; + +void AsyncWebServerRequest::_parseMultipartPostByte(uint8_t data, bool last){ +#define itemWriteByte(b) do { _itemSize++; if(_itemIsFile) _handleUploadByte(b, last); else _itemValue+=(char)(b); } while(0) + + if(!_parsedLength){ + _multiParseState = EXPECT_BOUNDARY; + _temp = String(); + _itemName = String(); + _itemFilename = String(); + _itemType = String(); + } + + if(_multiParseState == WAIT_FOR_RETURN1){ + if(data != '\r'){ + itemWriteByte(data); + } else { + _multiParseState = EXPECT_FEED1; + } + } else if(_multiParseState == EXPECT_BOUNDARY){ + if(_parsedLength < 2 && data != '-'){ + _multiParseState = PARSE_ERROR; + return; + } else if(_parsedLength - 2 < _boundary.length() && _boundary.c_str()[_parsedLength - 2] != data){ + _multiParseState = PARSE_ERROR; + return; + } else if(_parsedLength - 2 == _boundary.length() && data != '\r'){ + _multiParseState = PARSE_ERROR; + return; + } else if(_parsedLength - 3 == _boundary.length()){ + if(data != '\n'){ + _multiParseState = PARSE_ERROR; + return; + } + _multiParseState = PARSE_HEADERS; + _itemIsFile = false; + } + } else if(_multiParseState == PARSE_HEADERS){ + if((char)data != '\r' && (char)data != '\n') + _temp += (char)data; + if((char)data == '\n'){ + if(_temp.length()){ + if(_temp.length() > 12 && _temp.substring(0, 12).equalsIgnoreCase("Content-Type")){ + _itemType = _temp.substring(14); + _itemIsFile = true; + } else if(_temp.length() > 19 && _temp.substring(0, 19).equalsIgnoreCase("Content-Disposition")){ + _temp = _temp.substring(_temp.indexOf(';') + 2); + while(_temp.indexOf(';') > 0){ + String name = _temp.substring(0, _temp.indexOf('=')); + String nameVal = _temp.substring(_temp.indexOf('=') + 2, _temp.indexOf(';') - 1); + if(name == "name"){ + _itemName = nameVal; + } else if(name == "filename"){ + _itemFilename = nameVal; + _itemIsFile = true; + } + _temp = _temp.substring(_temp.indexOf(';') + 2); + } + String name = _temp.substring(0, _temp.indexOf('=')); + String nameVal = _temp.substring(_temp.indexOf('=') + 2, _temp.length() - 1); + if(name == "name"){ + _itemName = nameVal; + } else if(name == "filename"){ + _itemFilename = nameVal; + _itemIsFile = true; + } + } + _temp = String(); + } else { + _multiParseState = WAIT_FOR_RETURN1; + //value starts from here + _itemSize = 0; + _itemStartIndex = _parsedLength; + _itemValue = String(); + if(_itemIsFile){ + if(_itemBuffer) + free(_itemBuffer); + _itemBuffer = (uint8_t*)malloc(1460); + if(_itemBuffer == NULL){ + _multiParseState = PARSE_ERROR; + return; + } + _itemBufferIndex = 0; + } + } + } + } else if(_multiParseState == EXPECT_FEED1){ + if(data != '\n'){ + _multiParseState = WAIT_FOR_RETURN1; + itemWriteByte('\r'); _parseMultipartPostByte(data, last); + } else { + _multiParseState = EXPECT_DASH1; + } + } else if(_multiParseState == EXPECT_DASH1){ + if(data != '-'){ + _multiParseState = WAIT_FOR_RETURN1; + itemWriteByte('\r'); itemWriteByte('\n'); _parseMultipartPostByte(data, last); + } else { + _multiParseState = EXPECT_DASH2; + } + } else if(_multiParseState == EXPECT_DASH2){ + if(data != '-'){ + _multiParseState = WAIT_FOR_RETURN1; + itemWriteByte('\r'); itemWriteByte('\n'); itemWriteByte('-'); _parseMultipartPostByte(data, last); + } else { + _multiParseState = BOUNDARY_OR_DATA; + _boundaryPosition = 0; + } + } else if(_multiParseState == BOUNDARY_OR_DATA){ + if(_boundaryPosition < _boundary.length() && _boundary.c_str()[_boundaryPosition] != data){ + _multiParseState = WAIT_FOR_RETURN1; + itemWriteByte('\r'); itemWriteByte('\n'); itemWriteByte('-'); itemWriteByte('-'); + uint8_t i; + for(i=0; i<_boundaryPosition; i++) + itemWriteByte(_boundary.c_str()[i]); + _parseMultipartPostByte(data, last); + } else if(_boundaryPosition == _boundary.length() - 1){ + _multiParseState = DASH3_OR_RETURN2; + if(!_itemIsFile){ + _addParam(new AsyncWebParameter(_itemName, _itemValue, true)); + } else { + if(_itemSize){ + //check if authenticated before calling the upload + if(_handler) _handler->handleUpload(this, _itemFilename, _itemSize - _itemBufferIndex, _itemBuffer, _itemBufferIndex, true); + _itemBufferIndex = 0; + _addParam(new AsyncWebParameter(_itemName, _itemFilename, true, true, _itemSize)); + } + free(_itemBuffer); + _itemBuffer = NULL; + } + + } else { + _boundaryPosition++; + } + } else if(_multiParseState == DASH3_OR_RETURN2){ + if(data == '-' && (_contentLength - _parsedLength - 4) != 0){ + //os_printf("ERROR: The parser got to the end of the POST but is expecting %u bytes more!\nDrop an issue so we can have more info on the matter!\n", _contentLength - _parsedLength - 4); + _contentLength = _parsedLength + 4;//lets close the request gracefully + } + if(data == '\r'){ + _multiParseState = EXPECT_FEED2; + } else if(data == '-' && _contentLength == (_parsedLength + 4)){ + _multiParseState = PARSING_FINISHED; + } else { + _multiParseState = WAIT_FOR_RETURN1; + itemWriteByte('\r'); itemWriteByte('\n'); itemWriteByte('-'); itemWriteByte('-'); + uint8_t i; for(i=0; i<_boundary.length(); i++) itemWriteByte(_boundary.c_str()[i]); + _parseMultipartPostByte(data, last); + } + } else if(_multiParseState == EXPECT_FEED2){ + if(data == '\n'){ + _multiParseState = PARSE_HEADERS; + _itemIsFile = false; + } else { + _multiParseState = WAIT_FOR_RETURN1; + itemWriteByte('\r'); itemWriteByte('\n'); itemWriteByte('-'); itemWriteByte('-'); + uint8_t i; for(i=0; i<_boundary.length(); i++) itemWriteByte(_boundary.c_str()[i]); + itemWriteByte('\r'); _parseMultipartPostByte(data, last); + } + } +} + +void AsyncWebServerRequest::_parseLine(){ + if(_parseState == PARSE_REQ_START){ + if(!_temp.length()){ + _parseState = PARSE_REQ_FAIL; + _client->close(); + } else { + _parseReqHead(); + _parseState = PARSE_REQ_HEADERS; + } + return; + } + + if(_parseState == PARSE_REQ_HEADERS){ + if(!_temp.length()){ + //end of headers + _server->_rewriteRequest(this); + _server->_attachHandler(this); + _removeNotInterestingHeaders(); + if(_expectingContinue){ + const char * response = "HTTP/1.1 100 Continue\r\n\r\n"; + _client->write(response, os_strlen(response)); + } + //check handler for authentication + if(_contentLength){ + _parseState = PARSE_REQ_BODY; + } else { + _parseState = PARSE_REQ_END; + if(_handler) _handler->handleRequest(this); + else send(501); + } + } else _parseReqHeader(); + } +} + +size_t AsyncWebServerRequest::headers() const{ + return _headers.length(); +} + +bool AsyncWebServerRequest::hasHeader(const String& name) const { + for(const auto& h: _headers){ + if(h->name().equalsIgnoreCase(name)){ + return true; + } + } + return false; +} + +bool AsyncWebServerRequest::hasHeader(const __FlashStringHelper * data) const { + PGM_P p = reinterpret_cast(data); + size_t n = 0; + while (1) { + if (pgm_read_byte(p+n) == 0) break; + n += 1; + } + char * name = (char*) malloc(n+1); + name[n] = 0; + if (name) { + for(size_t b=0; bname().equalsIgnoreCase(name)){ + return h; + } + } + return nullptr; +} + +AsyncWebHeader* AsyncWebServerRequest::getHeader(const __FlashStringHelper * data) const { + PGM_P p = reinterpret_cast(data); + size_t n = strlen_P(p); + char * name = (char*) malloc(n+1); + if (name) { + strcpy_P(name, p); + AsyncWebHeader* result = getHeader( String(name)); + free(name); + return result; + } else { + return nullptr; + } +} + +AsyncWebHeader* AsyncWebServerRequest::getHeader(size_t num) const { + auto header = _headers.nth(num); + return header ? *header : nullptr; +} + +size_t AsyncWebServerRequest::params() const { + return _params.length(); +} + +bool AsyncWebServerRequest::hasParam(const String& name, bool post, bool file) const { + for(const auto& p: _params){ + if(p->name() == name && p->isPost() == post && p->isFile() == file){ + return true; + } + } + return false; +} + +bool AsyncWebServerRequest::hasParam(const __FlashStringHelper * data, bool post, bool file) const { + PGM_P p = reinterpret_cast(data); + size_t n = strlen_P(p); + + char * name = (char*) malloc(n+1); + name[n] = 0; + if (name) { + strcpy_P(name,p); + bool result = hasParam( name, post, file); + free(name); + return result; + } else { + return false; + } +} + +AsyncWebParameter* AsyncWebServerRequest::getParam(const String& name, bool post, bool file) const { + for(const auto& p: _params){ + if(p->name() == name && p->isPost() == post && p->isFile() == file){ + return p; + } + } + return nullptr; +} + +AsyncWebParameter* AsyncWebServerRequest::getParam(const __FlashStringHelper * data, bool post, bool file) const { + PGM_P p = reinterpret_cast(data); + size_t n = strlen_P(p); + char * name = (char*) malloc(n+1); + if (name) { + strcpy_P(name, p); + AsyncWebParameter* result = getParam(name, post, file); + free(name); + return result; + } else { + return nullptr; + } +} + +AsyncWebParameter* AsyncWebServerRequest::getParam(size_t num) const { + auto param = _params.nth(num); + return param ? *param : nullptr; +} + +void AsyncWebServerRequest::addInterestingHeader(const String& name){ + if(!_interestingHeaders.containsIgnoreCase(name)) + _interestingHeaders.add(name); +} + +void AsyncWebServerRequest::send(AsyncWebServerResponse *response){ + _response = response; + if(_response == NULL){ + _client->close(true); + _onDisconnect(); + return; + } + if(!_response->_sourceValid()){ + delete response; + _response = NULL; + send(500); + } + else { + _client->setRxTimeout(0); + _response->_respond(this); + } +} + +AsyncWebServerResponse * AsyncWebServerRequest::beginResponse(int code, const String& contentType, const String& content){ + return new AsyncBasicResponse(code, contentType, content); +} + +AsyncWebServerResponse * AsyncWebServerRequest::beginResponse(FS &fs, const String& path, const String& contentType, bool download, AwsTemplateProcessor callback){ + if(fs.exists(path) || (!download && fs.exists(path+".gz"))) + return new AsyncFileResponse(fs, path, contentType, download, callback); + return NULL; +} + +AsyncWebServerResponse * AsyncWebServerRequest::beginResponse(File content, const String& path, const String& contentType, bool download, AwsTemplateProcessor callback){ + if(content == true) + return new AsyncFileResponse(content, path, contentType, download, callback); + return NULL; +} + +AsyncWebServerResponse * AsyncWebServerRequest::beginResponse(Stream &stream, const String& contentType, size_t len, AwsTemplateProcessor callback){ + return new AsyncStreamResponse(stream, contentType, len, callback); +} + +AsyncWebServerResponse * AsyncWebServerRequest::beginResponse(const String& contentType, size_t len, AwsResponseFiller callback, AwsTemplateProcessor templateCallback){ + return new AsyncCallbackResponse(contentType, len, callback, templateCallback); +} + +AsyncWebServerResponse * AsyncWebServerRequest::beginChunkedResponse(const String& contentType, AwsResponseFiller callback, AwsTemplateProcessor templateCallback){ + if(_version) + return new AsyncChunkedResponse(contentType, callback, templateCallback); + return new AsyncCallbackResponse(contentType, 0, callback, templateCallback); +} + +AsyncResponseStream * AsyncWebServerRequest::beginResponseStream(const String& contentType, size_t bufferSize){ + return new AsyncResponseStream(contentType, bufferSize); +} + +AsyncWebServerResponse * AsyncWebServerRequest::beginResponse_P(int code, const String& contentType, const uint8_t * content, size_t len, AwsTemplateProcessor callback){ + return new AsyncProgmemResponse(code, contentType, content, len, callback); +} + +AsyncWebServerResponse * AsyncWebServerRequest::beginResponse_P(int code, const String& contentType, PGM_P content, AwsTemplateProcessor callback){ + return beginResponse_P(code, contentType, (const uint8_t *)content, strlen_P(content), callback); +} + +void AsyncWebServerRequest::send(int code, const String& contentType, const String& content){ + send(beginResponse(code, contentType, content)); +} + +void AsyncWebServerRequest::send(FS &fs, const String& path, const String& contentType, bool download, AwsTemplateProcessor callback){ + if(fs.exists(path) || (!download && fs.exists(path+".gz"))){ + send(beginResponse(fs, path, contentType, download, callback)); + } else send(404); +} + +void AsyncWebServerRequest::send(File content, const String& path, const String& contentType, bool download, AwsTemplateProcessor callback){ + if(content == true){ + send(beginResponse(content, path, contentType, download, callback)); + } else send(404); +} + +void AsyncWebServerRequest::send(Stream &stream, const String& contentType, size_t len, AwsTemplateProcessor callback){ + send(beginResponse(stream, contentType, len, callback)); +} + +void AsyncWebServerRequest::send(const String& contentType, size_t len, AwsResponseFiller callback, AwsTemplateProcessor templateCallback){ + send(beginResponse(contentType, len, callback, templateCallback)); +} + +void AsyncWebServerRequest::sendChunked(const String& contentType, AwsResponseFiller callback, AwsTemplateProcessor templateCallback){ + send(beginChunkedResponse(contentType, callback, templateCallback)); +} + +void AsyncWebServerRequest::send_P(int code, const String& contentType, const uint8_t * content, size_t len, AwsTemplateProcessor callback){ + send(beginResponse_P(code, contentType, content, len, callback)); +} + +void AsyncWebServerRequest::send_P(int code, const String& contentType, PGM_P content, AwsTemplateProcessor callback){ + send(beginResponse_P(code, contentType, content, callback)); +} + +void AsyncWebServerRequest::redirect(const String& url){ + AsyncWebServerResponse * response = beginResponse(302); + response->addHeader("Location",url); + send(response); +} + +bool AsyncWebServerRequest::authenticate(const char * username, const char * password, const char * realm, bool passwordIsHash){ + if(_authorization.length()){ + if(_isDigest) + return checkDigestAuthentication(_authorization.c_str(), methodToString(), username, password, realm, passwordIsHash, NULL, NULL, NULL); + else if(!passwordIsHash) + return checkBasicAuthentication(_authorization.c_str(), username, password); + else + return _authorization.equals(password); + } + return false; +} + +bool AsyncWebServerRequest::authenticate(const char * hash){ + if(!_authorization.length() || hash == NULL) + return false; + + if(_isDigest){ + String hStr = String(hash); + int separator = hStr.indexOf(":"); + if(separator <= 0) + return false; + String username = hStr.substring(0, separator); + hStr = hStr.substring(separator + 1); + separator = hStr.indexOf(":"); + if(separator <= 0) + return false; + String realm = hStr.substring(0, separator); + hStr = hStr.substring(separator + 1); + return checkDigestAuthentication(_authorization.c_str(), methodToString(), username.c_str(), hStr.c_str(), realm.c_str(), true, NULL, NULL, NULL); + } + + return (_authorization.equals(hash)); +} + +void AsyncWebServerRequest::requestAuthentication(const char * realm, bool isDigest){ + AsyncWebServerResponse * r = beginResponse(401); + if(!isDigest && realm == NULL){ + r->addHeader("WWW-Authenticate", "Basic realm=\"Login Required\""); + } else if(!isDigest){ + String header = "Basic realm=\""; + header.concat(realm); + header.concat("\""); + r->addHeader("WWW-Authenticate", header); + } else { + String header = "Digest "; + header.concat(requestDigestAuthentication(realm)); + r->addHeader("WWW-Authenticate", header); + } + send(r); +} + +bool AsyncWebServerRequest::hasArg(const char* name) const { + for(const auto& arg: _params){ + if(arg->name() == name){ + return true; + } + } + return false; +} + +bool AsyncWebServerRequest::hasArg(const __FlashStringHelper * data) const { + PGM_P p = reinterpret_cast(data); + size_t n = strlen_P(p); + char * name = (char*) malloc(n+1); + if (name) { + strcpy_P(name, p); + bool result = hasArg( name ); + free(name); + return result; + } else { + return false; + } +} + + +const String& AsyncWebServerRequest::arg(const String& name) const { + for(const auto& arg: _params){ + if(arg->name() == name){ + return arg->value(); + } + } + return SharedEmptyString; +} + +const String& AsyncWebServerRequest::arg(const __FlashStringHelper * data) const { + PGM_P p = reinterpret_cast(data); + size_t n = strlen_P(p); + char * name = (char*) malloc(n+1); + if (name) { + strcpy_P(name, p); + const String & result = arg( String(name) ); + free(name); + return result; + } else { + return SharedEmptyString; + } + +} + +const String& AsyncWebServerRequest::arg(size_t i) const { + return getParam(i)->value(); +} + +const String& AsyncWebServerRequest::argName(size_t i) const { + return getParam(i)->name(); +} + +const String& AsyncWebServerRequest::pathArg(size_t i) const { + auto param = _pathParams.nth(i); + return param ? **param : SharedEmptyString; +} + +const String& AsyncWebServerRequest::header(const char* name) const { + AsyncWebHeader* h = getHeader(String(name)); + return h ? h->value() : SharedEmptyString; +} + +const String& AsyncWebServerRequest::header(const __FlashStringHelper * data) const { + PGM_P p = reinterpret_cast(data); + size_t n = strlen_P(p); + char * name = (char*) malloc(n+1); + if (name) { + strcpy_P(name, p); + const String & result = header( (const char *)name ); + free(name); + return result; + } else { + return SharedEmptyString; + } +}; + + +const String& AsyncWebServerRequest::header(size_t i) const { + AsyncWebHeader* h = getHeader(i); + return h ? h->value() : SharedEmptyString; +} + +const String& AsyncWebServerRequest::headerName(size_t i) const { + AsyncWebHeader* h = getHeader(i); + return h ? h->name() : SharedEmptyString; +} + +String AsyncWebServerRequest::urlDecode(const String& text) const { + char temp[] = "0x00"; + unsigned int len = text.length(); + unsigned int i = 0; + String decoded = String(); + decoded.reserve(len); // Allocate the string internal buffer - never longer from source text + while (i < len){ + char decodedChar; + char encodedChar = text.charAt(i++); + if ((encodedChar == '%') && (i + 1 < len)){ + temp[2] = text.charAt(i++); + temp[3] = text.charAt(i++); + decodedChar = strtol(temp, NULL, 16); + } else if (encodedChar == '+') { + decodedChar = ' '; + } else { + decodedChar = encodedChar; // normal ascii char + } + decoded.concat(decodedChar); + } + return decoded; +} + + +const char * AsyncWebServerRequest::methodToString() const { + if(_method == HTTP_ANY) return "ANY"; + else if(_method & HTTP_GET) return "GET"; + else if(_method & HTTP_POST) return "POST"; + else if(_method & HTTP_DELETE) return "DELETE"; + else if(_method & HTTP_PUT) return "PUT"; + else if(_method & HTTP_PATCH) return "PATCH"; + else if(_method & HTTP_HEAD) return "HEAD"; + else if(_method & HTTP_OPTIONS) return "OPTIONS"; + return "UNKNOWN"; +} + +const char *AsyncWebServerRequest::requestedConnTypeToString() const { + switch (_reqconntype) { + case RCT_NOT_USED: return "RCT_NOT_USED"; + case RCT_DEFAULT: return "RCT_DEFAULT"; + case RCT_HTTP: return "RCT_HTTP"; + case RCT_WS: return "RCT_WS"; + case RCT_EVENT: return "RCT_EVENT"; + default: return "ERROR"; + } +} + +bool AsyncWebServerRequest::isExpectedRequestedConnType(RequestedConnectionType erct1, RequestedConnectionType erct2, RequestedConnectionType erct3) { + bool res = false; + if ((erct1 != RCT_NOT_USED) && (erct1 == _reqconntype)) res = true; + if ((erct2 != RCT_NOT_USED) && (erct2 == _reqconntype)) res = true; + if ((erct3 != RCT_NOT_USED) && (erct3 == _reqconntype)) res = true; + return res; +} diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/WebResponseImpl.h b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/WebResponseImpl.h new file mode 100644 index 0000000..9a64e3a --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/WebResponseImpl.h @@ -0,0 +1,136 @@ +/* + Asynchronous WebServer library for Espressif MCUs + + Copyright (c) 2016 Hristo Gochkov. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ +#ifndef ASYNCWEBSERVERRESPONSEIMPL_H_ +#define ASYNCWEBSERVERRESPONSEIMPL_H_ + +#ifdef Arduino_h +// arduino is not compatible with std::vector +#undef min +#undef max +#endif +#include +// It is possible to restore these defines, but one can use _min and _max instead. Or std::min, std::max. + +class AsyncBasicResponse: public AsyncWebServerResponse { + private: + String _content; + public: + AsyncBasicResponse(int code, const String& contentType=String(), const String& content=String()); + void _respond(AsyncWebServerRequest *request); + size_t _ack(AsyncWebServerRequest *request, size_t len, uint32_t time); + bool _sourceValid() const { return true; } +}; + +class AsyncAbstractResponse: public AsyncWebServerResponse { + private: + String _head; + // Data is inserted into cache at begin(). + // This is inefficient with vector, but if we use some other container, + // we won't be able to access it as contiguous array of bytes when reading from it, + // so by gaining performance in one place, we'll lose it in another. + std::vector _cache; + size_t _readDataFromCacheOrContent(uint8_t* data, const size_t len); + size_t _fillBufferAndProcessTemplates(uint8_t* buf, size_t maxLen); + protected: + AwsTemplateProcessor _callback; + public: + AsyncAbstractResponse(AwsTemplateProcessor callback=nullptr); + void _respond(AsyncWebServerRequest *request); + size_t _ack(AsyncWebServerRequest *request, size_t len, uint32_t time); + bool _sourceValid() const { return false; } + virtual size_t _fillBuffer(uint8_t *buf __attribute__((unused)), size_t maxLen __attribute__((unused))) { return 0; } +}; + +#ifndef TEMPLATE_PLACEHOLDER +#define TEMPLATE_PLACEHOLDER '%' +#endif + +#define TEMPLATE_PARAM_NAME_LENGTH 32 +class AsyncFileResponse: public AsyncAbstractResponse { + using File = fs::File; + using FS = fs::FS; + private: + File _content; + String _path; + void _setContentType(const String& path); + public: + AsyncFileResponse(FS &fs, const String& path, const String& contentType=String(), bool download=false, AwsTemplateProcessor callback=nullptr); + AsyncFileResponse(File content, const String& path, const String& contentType=String(), bool download=false, AwsTemplateProcessor callback=nullptr); + ~AsyncFileResponse(); + bool _sourceValid() const { return !!(_content); } + virtual size_t _fillBuffer(uint8_t *buf, size_t maxLen) override; +}; + +class AsyncStreamResponse: public AsyncAbstractResponse { + private: + Stream *_content; + public: + AsyncStreamResponse(Stream &stream, const String& contentType, size_t len, AwsTemplateProcessor callback=nullptr); + bool _sourceValid() const { return !!(_content); } + virtual size_t _fillBuffer(uint8_t *buf, size_t maxLen) override; +}; + +class AsyncCallbackResponse: public AsyncAbstractResponse { + private: + AwsResponseFiller _content; + size_t _filledLength; + public: + AsyncCallbackResponse(const String& contentType, size_t len, AwsResponseFiller callback, AwsTemplateProcessor templateCallback=nullptr); + bool _sourceValid() const { return !!(_content); } + virtual size_t _fillBuffer(uint8_t *buf, size_t maxLen) override; +}; + +class AsyncChunkedResponse: public AsyncAbstractResponse { + private: + AwsResponseFiller _content; + size_t _filledLength; + public: + AsyncChunkedResponse(const String& contentType, AwsResponseFiller callback, AwsTemplateProcessor templateCallback=nullptr); + bool _sourceValid() const { return !!(_content); } + virtual size_t _fillBuffer(uint8_t *buf, size_t maxLen) override; +}; + +class AsyncProgmemResponse: public AsyncAbstractResponse { + private: + const uint8_t * _content; + size_t _readLength; + public: + AsyncProgmemResponse(int code, const String& contentType, const uint8_t * content, size_t len, AwsTemplateProcessor callback=nullptr); + bool _sourceValid() const { return true; } + virtual size_t _fillBuffer(uint8_t *buf, size_t maxLen) override; +}; + +class cbuf; + +class AsyncResponseStream: public AsyncAbstractResponse, public Print { + private: + cbuf *_content; + public: + AsyncResponseStream(const String& contentType, size_t bufferSize); + ~AsyncResponseStream(); + bool _sourceValid() const { return (_state < RESPONSE_END); } + virtual size_t _fillBuffer(uint8_t *buf, size_t maxLen) override; + size_t write(const uint8_t *data, size_t len); + size_t write(uint8_t data); + using Print::write; +}; + +#endif /* ASYNCWEBSERVERRESPONSEIMPL_H_ */ diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/WebResponses.cpp b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/WebResponses.cpp new file mode 100644 index 0000000..a22e991 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/WebResponses.cpp @@ -0,0 +1,699 @@ +/* + Asynchronous WebServer library for Espressif MCUs + + Copyright (c) 2016 Hristo Gochkov. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ +#include "ESPAsyncWebServer.h" +#include "WebResponseImpl.h" +#include "cbuf.h" + +// Since ESP8266 does not link memchr by default, here's its implementation. +void* memchr(void* ptr, int ch, size_t count) +{ + unsigned char* p = static_cast(ptr); + while(count--) + if(*p++ == static_cast(ch)) + return --p; + return nullptr; +} + + +/* + * Abstract Response + * */ +const char* AsyncWebServerResponse::_responseCodeToString(int code) { + switch (code) { + case 100: return "Continue"; + case 101: return "Switching Protocols"; + case 200: return "OK"; + case 201: return "Created"; + case 202: return "Accepted"; + case 203: return "Non-Authoritative Information"; + case 204: return "No Content"; + case 205: return "Reset Content"; + case 206: return "Partial Content"; + case 300: return "Multiple Choices"; + case 301: return "Moved Permanently"; + case 302: return "Found"; + case 303: return "See Other"; + case 304: return "Not Modified"; + case 305: return "Use Proxy"; + case 307: return "Temporary Redirect"; + case 400: return "Bad Request"; + case 401: return "Unauthorized"; + case 402: return "Payment Required"; + case 403: return "Forbidden"; + case 404: return "Not Found"; + case 405: return "Method Not Allowed"; + case 406: return "Not Acceptable"; + case 407: return "Proxy Authentication Required"; + case 408: return "Request Time-out"; + case 409: return "Conflict"; + case 410: return "Gone"; + case 411: return "Length Required"; + case 412: return "Precondition Failed"; + case 413: return "Request Entity Too Large"; + case 414: return "Request-URI Too Large"; + case 415: return "Unsupported Media Type"; + case 416: return "Requested range not satisfiable"; + case 417: return "Expectation Failed"; + case 500: return "Internal Server Error"; + case 501: return "Not Implemented"; + case 502: return "Bad Gateway"; + case 503: return "Service Unavailable"; + case 504: return "Gateway Time-out"; + case 505: return "HTTP Version not supported"; + default: return ""; + } +} + +AsyncWebServerResponse::AsyncWebServerResponse() + : _code(0) + , _headers(LinkedList([](AsyncWebHeader *h){ delete h; })) + , _contentType() + , _contentLength(0) + , _sendContentLength(true) + , _chunked(false) + , _headLength(0) + , _sentLength(0) + , _ackedLength(0) + , _writtenLength(0) + , _state(RESPONSE_SETUP) +{ + for(auto header: DefaultHeaders::Instance()) { + _headers.add(new AsyncWebHeader(header->name(), header->value())); + } +} + +AsyncWebServerResponse::~AsyncWebServerResponse(){ + _headers.free(); +} + +void AsyncWebServerResponse::setCode(int code){ + if(_state == RESPONSE_SETUP) + _code = code; +} + +void AsyncWebServerResponse::setContentLength(size_t len){ + if(_state == RESPONSE_SETUP) + _contentLength = len; +} + +void AsyncWebServerResponse::setContentType(const String& type){ + if(_state == RESPONSE_SETUP) + _contentType = type; +} + +void AsyncWebServerResponse::addHeader(const String& name, const String& value){ + _headers.add(new AsyncWebHeader(name, value)); +} + +String AsyncWebServerResponse::_assembleHead(uint8_t version){ + if(version){ + addHeader("Accept-Ranges","none"); + if(_chunked) + addHeader("Transfer-Encoding","chunked"); + } + String out = String(); + int bufSize = 300; + char buf[bufSize]; + + snprintf(buf, bufSize, "HTTP/1.%d %d %s\r\n", version, _code, _responseCodeToString(_code)); + out.concat(buf); + + if(_sendContentLength) { + snprintf(buf, bufSize, "Content-Length: %d\r\n", _contentLength); + out.concat(buf); + } + if(_contentType.length()) { + snprintf(buf, bufSize, "Content-Type: %s\r\n", _contentType.c_str()); + out.concat(buf); + } + + for(const auto& header: _headers){ + snprintf(buf, bufSize, "%s: %s\r\n", header->name().c_str(), header->value().c_str()); + out.concat(buf); + } + _headers.free(); + + out.concat("\r\n"); + _headLength = out.length(); + return out; +} + +bool AsyncWebServerResponse::_started() const { return _state > RESPONSE_SETUP; } +bool AsyncWebServerResponse::_finished() const { return _state > RESPONSE_WAIT_ACK; } +bool AsyncWebServerResponse::_failed() const { return _state == RESPONSE_FAILED; } +bool AsyncWebServerResponse::_sourceValid() const { return false; } +void AsyncWebServerResponse::_respond(AsyncWebServerRequest *request){ _state = RESPONSE_END; request->client()->close(); } +size_t AsyncWebServerResponse::_ack(AsyncWebServerRequest *request, size_t len, uint32_t time){ (void)request; (void)len; (void)time; return 0; } + +/* + * String/Code Response + * */ +AsyncBasicResponse::AsyncBasicResponse(int code, const String& contentType, const String& content){ + _code = code; + _content = content; + _contentType = contentType; + if(_content.length()){ + _contentLength = _content.length(); + if(!_contentType.length()) + _contentType = "text/plain"; + } + addHeader("Connection","close"); +} + +void AsyncBasicResponse::_respond(AsyncWebServerRequest *request){ + _state = RESPONSE_HEADERS; + String out = _assembleHead(request->version()); + size_t outLen = out.length(); + size_t space = request->client()->space(); + if(!_contentLength && space >= outLen){ + _writtenLength += request->client()->write(out.c_str(), outLen); + _state = RESPONSE_WAIT_ACK; + } else if(_contentLength && space >= outLen + _contentLength){ + out += _content; + outLen += _contentLength; + _writtenLength += request->client()->write(out.c_str(), outLen); + _state = RESPONSE_WAIT_ACK; + } else if(space && space < outLen){ + String partial = out.substring(0, space); + _content = out.substring(space) + _content; + _contentLength += outLen - space; + _writtenLength += request->client()->write(partial.c_str(), partial.length()); + _state = RESPONSE_CONTENT; + } else if(space > outLen && space < (outLen + _contentLength)){ + size_t shift = space - outLen; + outLen += shift; + _sentLength += shift; + out += _content.substring(0, shift); + _content = _content.substring(shift); + _writtenLength += request->client()->write(out.c_str(), outLen); + _state = RESPONSE_CONTENT; + } else { + _content = out + _content; + _contentLength += outLen; + _state = RESPONSE_CONTENT; + } +} + +size_t AsyncBasicResponse::_ack(AsyncWebServerRequest *request, size_t len, uint32_t time){ + (void)time; + _ackedLength += len; + if(_state == RESPONSE_CONTENT){ + size_t available = _contentLength - _sentLength; + size_t space = request->client()->space(); + //we can fit in this packet + if(space > available){ + _writtenLength += request->client()->write(_content.c_str(), available); + _content = String(); + _state = RESPONSE_WAIT_ACK; + return available; + } + //send some data, the rest on ack + String out = _content.substring(0, space); + _content = _content.substring(space); + _sentLength += space; + _writtenLength += request->client()->write(out.c_str(), space); + return space; + } else if(_state == RESPONSE_WAIT_ACK){ + if(_ackedLength >= _writtenLength){ + _state = RESPONSE_END; + } + } + return 0; +} + + +/* + * Abstract Response + * */ + +AsyncAbstractResponse::AsyncAbstractResponse(AwsTemplateProcessor callback): _callback(callback) +{ + // In case of template processing, we're unable to determine real response size + if(callback) { + _contentLength = 0; + _sendContentLength = false; + _chunked = true; + } +} + +void AsyncAbstractResponse::_respond(AsyncWebServerRequest *request){ + addHeader("Connection","close"); + _head = _assembleHead(request->version()); + _state = RESPONSE_HEADERS; + _ack(request, 0, 0); +} + +size_t AsyncAbstractResponse::_ack(AsyncWebServerRequest *request, size_t len, uint32_t time){ + (void)time; + if(!_sourceValid()){ + _state = RESPONSE_FAILED; + request->client()->close(); + return 0; + } + _ackedLength += len; + size_t space = request->client()->space(); + + size_t headLen = _head.length(); + if(_state == RESPONSE_HEADERS){ + if(space >= headLen){ + _state = RESPONSE_CONTENT; + space -= headLen; + } else { + String out = _head.substring(0, space); + _head = _head.substring(space); + _writtenLength += request->client()->write(out.c_str(), out.length()); + return out.length(); + } + } + + if(_state == RESPONSE_CONTENT){ + size_t outLen; + if(_chunked){ + if(space <= 8){ + return 0; + } + outLen = space; + } else if(!_sendContentLength){ + outLen = space; + } else { + outLen = ((_contentLength - _sentLength) > space)?space:(_contentLength - _sentLength); + } + + uint8_t *buf = (uint8_t *)malloc(outLen+headLen); + if (!buf) { + // os_printf("_ack malloc %d failed\n", outLen+headLen); + return 0; + } + + if(headLen){ + memcpy(buf, _head.c_str(), _head.length()); + } + + size_t readLen = 0; + + if(_chunked){ + // HTTP 1.1 allows leading zeros in chunk length. Or spaces may be added. + // See RFC2616 sections 2, 3.6.1. + readLen = _fillBufferAndProcessTemplates(buf+headLen+6, outLen - 8); + if(readLen == RESPONSE_TRY_AGAIN){ + free(buf); + return 0; + } + outLen = sprintf((char*)buf+headLen, "%x", readLen) + headLen; + while(outLen < headLen + 4) buf[outLen++] = ' '; + buf[outLen++] = '\r'; + buf[outLen++] = '\n'; + outLen += readLen; + buf[outLen++] = '\r'; + buf[outLen++] = '\n'; + } else { + readLen = _fillBufferAndProcessTemplates(buf+headLen, outLen); + if(readLen == RESPONSE_TRY_AGAIN){ + free(buf); + return 0; + } + outLen = readLen + headLen; + } + + if(headLen){ + _head = String(); + } + + if(outLen){ + _writtenLength += request->client()->write((const char*)buf, outLen); + } + + if(_chunked){ + _sentLength += readLen; + } else { + _sentLength += outLen - headLen; + } + + free(buf); + + if((_chunked && readLen == 0) || (!_sendContentLength && outLen == 0) || (!_chunked && _sentLength == _contentLength)){ + _state = RESPONSE_WAIT_ACK; + } + return outLen; + + } else if(_state == RESPONSE_WAIT_ACK){ + if(!_sendContentLength || _ackedLength >= _writtenLength){ + _state = RESPONSE_END; + if(!_chunked && !_sendContentLength) + request->client()->close(true); + } + } + return 0; +} + +size_t AsyncAbstractResponse::_readDataFromCacheOrContent(uint8_t* data, const size_t len) +{ + // If we have something in cache, copy it to buffer + const size_t readFromCache = std::min(len, _cache.size()); + if(readFromCache) { + memcpy(data, _cache.data(), readFromCache); + _cache.erase(_cache.begin(), _cache.begin() + readFromCache); + } + // If we need to read more... + const size_t needFromFile = len - readFromCache; + const size_t readFromContent = _fillBuffer(data + readFromCache, needFromFile); + return readFromCache + readFromContent; +} + +size_t AsyncAbstractResponse::_fillBufferAndProcessTemplates(uint8_t* data, size_t len) +{ + if(!_callback) + return _fillBuffer(data, len); + + const size_t originalLen = len; + len = _readDataFromCacheOrContent(data, len); + // Now we've read 'len' bytes, either from cache or from file + // Search for template placeholders + uint8_t* pTemplateStart = data; + while((pTemplateStart < &data[len]) && (pTemplateStart = (uint8_t*)memchr(pTemplateStart, TEMPLATE_PLACEHOLDER, &data[len - 1] - pTemplateStart + 1))) { // data[0] ... data[len - 1] + uint8_t* pTemplateEnd = (pTemplateStart < &data[len - 1]) ? (uint8_t*)memchr(pTemplateStart + 1, TEMPLATE_PLACEHOLDER, &data[len - 1] - pTemplateStart) : nullptr; + // temporary buffer to hold parameter name + uint8_t buf[TEMPLATE_PARAM_NAME_LENGTH + 1]; + String paramName; + // If closing placeholder is found: + if(pTemplateEnd) { + // prepare argument to callback + const size_t paramNameLength = std::min(sizeof(buf) - 1, (unsigned int)(pTemplateEnd - pTemplateStart - 1)); + if(paramNameLength) { + memcpy(buf, pTemplateStart + 1, paramNameLength); + buf[paramNameLength] = 0; + paramName = String(reinterpret_cast(buf)); + } else { // double percent sign encountered, this is single percent sign escaped. + // remove the 2nd percent sign + memmove(pTemplateEnd, pTemplateEnd + 1, &data[len] - pTemplateEnd - 1); + len += _readDataFromCacheOrContent(&data[len - 1], 1) - 1; + ++pTemplateStart; + } + } else if(&data[len - 1] - pTemplateStart + 1 < TEMPLATE_PARAM_NAME_LENGTH + 2) { // closing placeholder not found, check if it's in the remaining file data + memcpy(buf, pTemplateStart + 1, &data[len - 1] - pTemplateStart); + const size_t readFromCacheOrContent = _readDataFromCacheOrContent(buf + (&data[len - 1] - pTemplateStart), TEMPLATE_PARAM_NAME_LENGTH + 2 - (&data[len - 1] - pTemplateStart + 1)); + if(readFromCacheOrContent) { + pTemplateEnd = (uint8_t*)memchr(buf + (&data[len - 1] - pTemplateStart), TEMPLATE_PLACEHOLDER, readFromCacheOrContent); + if(pTemplateEnd) { + // prepare argument to callback + *pTemplateEnd = 0; + paramName = String(reinterpret_cast(buf)); + // Copy remaining read-ahead data into cache + _cache.insert(_cache.begin(), pTemplateEnd + 1, buf + (&data[len - 1] - pTemplateStart) + readFromCacheOrContent); + pTemplateEnd = &data[len - 1]; + } + else // closing placeholder not found in file data, store found percent symbol as is and advance to the next position + { + // but first, store read file data in cache + _cache.insert(_cache.begin(), buf + (&data[len - 1] - pTemplateStart), buf + (&data[len - 1] - pTemplateStart) + readFromCacheOrContent); + ++pTemplateStart; + } + } + else // closing placeholder not found in content data, store found percent symbol as is and advance to the next position + ++pTemplateStart; + } + else // closing placeholder not found in content data, store found percent symbol as is and advance to the next position + ++pTemplateStart; + if(paramName.length()) { + // call callback and replace with result. + // Everything in range [pTemplateStart, pTemplateEnd] can be safely replaced with parameter value. + // Data after pTemplateEnd may need to be moved. + // The first byte of data after placeholder is located at pTemplateEnd + 1. + // It should be located at pTemplateStart + numBytesCopied (to begin right after inserted parameter value). + const String paramValue(_callback(paramName)); + const char* pvstr = paramValue.c_str(); + const unsigned int pvlen = paramValue.length(); + const size_t numBytesCopied = std::min(pvlen, static_cast(&data[originalLen - 1] - pTemplateStart + 1)); + // make room for param value + // 1. move extra data to cache if parameter value is longer than placeholder AND if there is no room to store + if((pTemplateEnd + 1 < pTemplateStart + numBytesCopied) && (originalLen - (pTemplateStart + numBytesCopied - pTemplateEnd - 1) < len)) { + _cache.insert(_cache.begin(), &data[originalLen - (pTemplateStart + numBytesCopied - pTemplateEnd - 1)], &data[len]); + //2. parameter value is longer than placeholder text, push the data after placeholder which not saved into cache further to the end + memmove(pTemplateStart + numBytesCopied, pTemplateEnd + 1, &data[originalLen] - pTemplateStart - numBytesCopied); + len = originalLen; // fix issue with truncated data, not sure if it has any side effects + } else if(pTemplateEnd + 1 != pTemplateStart + numBytesCopied) + //2. Either parameter value is shorter than placeholder text OR there is enough free space in buffer to fit. + // Move the entire data after the placeholder + memmove(pTemplateStart + numBytesCopied, pTemplateEnd + 1, &data[len] - pTemplateEnd - 1); + // 3. replace placeholder with actual value + memcpy(pTemplateStart, pvstr, numBytesCopied); + // If result is longer than buffer, copy the remainder into cache (this could happen only if placeholder text itself did not fit entirely in buffer) + if(numBytesCopied < pvlen) { + _cache.insert(_cache.begin(), pvstr + numBytesCopied, pvstr + pvlen); + } else if(pTemplateStart + numBytesCopied < pTemplateEnd + 1) { // result is copied fully; if result is shorter than placeholder text... + // there is some free room, fill it from cache + const size_t roomFreed = pTemplateEnd + 1 - pTemplateStart - numBytesCopied; + const size_t totalFreeRoom = originalLen - len + roomFreed; + len += _readDataFromCacheOrContent(&data[len - roomFreed], totalFreeRoom) - roomFreed; + } else { // result is copied fully; it is longer than placeholder text + const size_t roomTaken = pTemplateStart + numBytesCopied - pTemplateEnd - 1; + len = std::min(len + roomTaken, originalLen); + } + } + } // while(pTemplateStart) + return len; +} + + +/* + * File Response + * */ + +AsyncFileResponse::~AsyncFileResponse(){ + if(_content) + _content.close(); +} + +void AsyncFileResponse::_setContentType(const String& path){ + if (path.endsWith(".html")) _contentType = "text/html"; + else if (path.endsWith(".htm")) _contentType = "text/html"; + else if (path.endsWith(".css")) _contentType = "text/css"; + else if (path.endsWith(".json")) _contentType = "application/json"; + else if (path.endsWith(".js")) _contentType = "application/javascript"; + else if (path.endsWith(".png")) _contentType = "image/png"; + else if (path.endsWith(".gif")) _contentType = "image/gif"; + else if (path.endsWith(".jpg")) _contentType = "image/jpeg"; + else if (path.endsWith(".ico")) _contentType = "image/x-icon"; + else if (path.endsWith(".svg")) _contentType = "image/svg+xml"; + else if (path.endsWith(".eot")) _contentType = "font/eot"; + else if (path.endsWith(".woff")) _contentType = "font/woff"; + else if (path.endsWith(".woff2")) _contentType = "font/woff2"; + else if (path.endsWith(".ttf")) _contentType = "font/ttf"; + else if (path.endsWith(".xml")) _contentType = "text/xml"; + else if (path.endsWith(".pdf")) _contentType = "application/pdf"; + else if (path.endsWith(".zip")) _contentType = "application/zip"; + else if(path.endsWith(".gz")) _contentType = "application/x-gzip"; + else _contentType = "text/plain"; +} + +AsyncFileResponse::AsyncFileResponse(FS &fs, const String& path, const String& contentType, bool download, AwsTemplateProcessor callback): AsyncAbstractResponse(callback){ + _code = 200; + _path = path; + + if(!download && !fs.exists(_path) && fs.exists(_path+".gz")){ + _path = _path+".gz"; + addHeader("Content-Encoding", "gzip"); + _callback = nullptr; // Unable to process zipped templates + _sendContentLength = true; + _chunked = false; + } + + _content = fs.open(_path, "r"); + _contentLength = _content.size(); + + if(contentType == "") + _setContentType(path); + else + _contentType = contentType; + + int filenameStart = path.lastIndexOf('/') + 1; + char buf[26+path.length()-filenameStart]; + char* filename = (char*)path.c_str() + filenameStart; + + if(download) { + // set filename and force download + snprintf(buf, sizeof (buf), "attachment; filename=\"%s\"", filename); + } else { + // set filename and force rendering + snprintf(buf, sizeof (buf), "inline; filename=\"%s\"", filename); + } + addHeader("Content-Disposition", buf); +} + +AsyncFileResponse::AsyncFileResponse(File content, const String& path, const String& contentType, bool download, AwsTemplateProcessor callback): AsyncAbstractResponse(callback){ + _code = 200; + _path = path; + + if(!download && String(content.name()).endsWith(".gz") && !path.endsWith(".gz")){ + addHeader("Content-Encoding", "gzip"); + _callback = nullptr; // Unable to process gzipped templates + _sendContentLength = true; + _chunked = false; + } + + _content = content; + _contentLength = _content.size(); + + if(contentType == "") + _setContentType(path); + else + _contentType = contentType; + + int filenameStart = path.lastIndexOf('/') + 1; + char buf[26+path.length()-filenameStart]; + char* filename = (char*)path.c_str() + filenameStart; + + if(download) { + snprintf(buf, sizeof (buf), "attachment; filename=\"%s\"", filename); + } else { + snprintf(buf, sizeof (buf), "inline; filename=\"%s\"", filename); + } + addHeader("Content-Disposition", buf); +} + +size_t AsyncFileResponse::_fillBuffer(uint8_t *data, size_t len){ + return _content.read(data, len); +} + +/* + * Stream Response + * */ + +AsyncStreamResponse::AsyncStreamResponse(Stream &stream, const String& contentType, size_t len, AwsTemplateProcessor callback): AsyncAbstractResponse(callback) { + _code = 200; + _content = &stream; + _contentLength = len; + _contentType = contentType; +} + +size_t AsyncStreamResponse::_fillBuffer(uint8_t *data, size_t len){ + size_t available = _content->available(); + size_t outLen = (available > len)?len:available; + size_t i; + for(i=0;iread(); + return outLen; +} + +/* + * Callback Response + * */ + +AsyncCallbackResponse::AsyncCallbackResponse(const String& contentType, size_t len, AwsResponseFiller callback, AwsTemplateProcessor templateCallback): AsyncAbstractResponse(templateCallback) { + _code = 200; + _content = callback; + _contentLength = len; + if(!len) + _sendContentLength = false; + _contentType = contentType; + _filledLength = 0; +} + +size_t AsyncCallbackResponse::_fillBuffer(uint8_t *data, size_t len){ + size_t ret = _content(data, len, _filledLength); + if(ret != RESPONSE_TRY_AGAIN){ + _filledLength += ret; + } + return ret; +} + +/* + * Chunked Response + * */ + +AsyncChunkedResponse::AsyncChunkedResponse(const String& contentType, AwsResponseFiller callback, AwsTemplateProcessor processorCallback): AsyncAbstractResponse(processorCallback) { + _code = 200; + _content = callback; + _contentLength = 0; + _contentType = contentType; + _sendContentLength = false; + _chunked = true; + _filledLength = 0; +} + +size_t AsyncChunkedResponse::_fillBuffer(uint8_t *data, size_t len){ + size_t ret = _content(data, len, _filledLength); + if(ret != RESPONSE_TRY_AGAIN){ + _filledLength += ret; + } + return ret; +} + +/* + * Progmem Response + * */ + +AsyncProgmemResponse::AsyncProgmemResponse(int code, const String& contentType, const uint8_t * content, size_t len, AwsTemplateProcessor callback): AsyncAbstractResponse(callback) { + _code = code; + _content = content; + _contentType = contentType; + _contentLength = len; + _readLength = 0; +} + +size_t AsyncProgmemResponse::_fillBuffer(uint8_t *data, size_t len){ + size_t left = _contentLength - _readLength; + if (left > len) { + memcpy_P(data, _content + _readLength, len); + _readLength += len; + return len; + } + memcpy_P(data, _content + _readLength, left); + _readLength += left; + return left; +} + + +/* + * Response Stream (You can print/write/printf to it, up to the contentLen bytes) + * */ + +AsyncResponseStream::AsyncResponseStream(const String& contentType, size_t bufferSize){ + _code = 200; + _contentLength = 0; + _contentType = contentType; + _content = new cbuf(bufferSize); +} + +AsyncResponseStream::~AsyncResponseStream(){ + delete _content; +} + +size_t AsyncResponseStream::_fillBuffer(uint8_t *buf, size_t maxLen){ + return _content->read((char*)buf, maxLen); +} + +size_t AsyncResponseStream::write(const uint8_t *data, size_t len){ + if(_started()) + return 0; + + if(len > _content->room()){ + size_t needed = len - _content->room(); + _content->resizeAdd(needed); + } + size_t written = _content->write((const char*)data, len); + _contentLength += written; + return written; +} + +size_t AsyncResponseStream::write(uint8_t data){ + return write(&data, 1); +} diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/WebServer.cpp b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/WebServer.cpp new file mode 100644 index 0000000..95c2dd6 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/WebServer.cpp @@ -0,0 +1,193 @@ +/* + Asynchronous WebServer library for Espressif MCUs + + Copyright (c) 2016 Hristo Gochkov. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ +#include "ESPAsyncWebServer.h" +#include "WebHandlerImpl.h" + +bool ON_STA_FILTER(AsyncWebServerRequest *request) { + return WiFi.localIP() == request->client()->localIP(); +} + +bool ON_AP_FILTER(AsyncWebServerRequest *request) { + return WiFi.localIP() != request->client()->localIP(); +} + + +AsyncWebServer::AsyncWebServer(uint16_t port) + : _server(port) + , _rewrites(LinkedList([](AsyncWebRewrite* r){ delete r; })) + , _handlers(LinkedList([](AsyncWebHandler* h){ delete h; })) +{ + _catchAllHandler = new AsyncCallbackWebHandler(); + if(_catchAllHandler == NULL) + return; + _server.onClient([](void *s, AsyncClient* c){ + if(c == NULL) + return; + c->setRxTimeout(3); + AsyncWebServerRequest *r = new AsyncWebServerRequest((AsyncWebServer*)s, c); + if(r == NULL){ + c->close(true); + c->free(); + delete c; + } + }, this); +} + +AsyncWebServer::~AsyncWebServer(){ + reset(); + end(); + if(_catchAllHandler) delete _catchAllHandler; +} + +AsyncWebRewrite& AsyncWebServer::addRewrite(AsyncWebRewrite* rewrite){ + _rewrites.add(rewrite); + return *rewrite; +} + +bool AsyncWebServer::removeRewrite(AsyncWebRewrite *rewrite){ + return _rewrites.remove(rewrite); +} + +AsyncWebRewrite& AsyncWebServer::rewrite(const char* from, const char* to){ + return addRewrite(new AsyncWebRewrite(from, to)); +} + +AsyncWebHandler& AsyncWebServer::addHandler(AsyncWebHandler* handler){ + _handlers.add(handler); + return *handler; +} + +bool AsyncWebServer::removeHandler(AsyncWebHandler *handler){ + return _handlers.remove(handler); +} + +void AsyncWebServer::begin(){ + _server.setNoDelay(true); + _server.begin(); +} + +void AsyncWebServer::end(){ + _server.end(); +} + +#if ASYNC_TCP_SSL_ENABLED +void AsyncWebServer::onSslFileRequest(AcSSlFileHandler cb, void* arg){ + _server.onSslFileRequest(cb, arg); +} + +void AsyncWebServer::beginSecure(const char *cert, const char *key, const char *password){ + _server.beginSecure(cert, key, password); +} +#endif + +void AsyncWebServer::_handleDisconnect(AsyncWebServerRequest *request){ + delete request; +} + +void AsyncWebServer::_rewriteRequest(AsyncWebServerRequest *request){ + for(const auto& r: _rewrites){ + if (r->match(request)){ + request->_url = r->toUrl(); + request->_addGetParams(r->params()); + } + } +} + +void AsyncWebServer::_attachHandler(AsyncWebServerRequest *request){ + for(const auto& h: _handlers){ + if (h->filter(request) && h->canHandle(request)){ + request->setHandler(h); + return; + } + } + + request->addInterestingHeader("ANY"); + request->setHandler(_catchAllHandler); +} + + +AsyncCallbackWebHandler& AsyncWebServer::on(const char* uri, WebRequestMethodComposite method, ArRequestHandlerFunction onRequest, ArUploadHandlerFunction onUpload, ArBodyHandlerFunction onBody){ + AsyncCallbackWebHandler* handler = new AsyncCallbackWebHandler(); + handler->setUri(uri); + handler->setMethod(method); + handler->onRequest(onRequest); + handler->onUpload(onUpload); + handler->onBody(onBody); + addHandler(handler); + return *handler; +} + +AsyncCallbackWebHandler& AsyncWebServer::on(const char* uri, WebRequestMethodComposite method, ArRequestHandlerFunction onRequest, ArUploadHandlerFunction onUpload){ + AsyncCallbackWebHandler* handler = new AsyncCallbackWebHandler(); + handler->setUri(uri); + handler->setMethod(method); + handler->onRequest(onRequest); + handler->onUpload(onUpload); + addHandler(handler); + return *handler; +} + +AsyncCallbackWebHandler& AsyncWebServer::on(const char* uri, WebRequestMethodComposite method, ArRequestHandlerFunction onRequest){ + AsyncCallbackWebHandler* handler = new AsyncCallbackWebHandler(); + handler->setUri(uri); + handler->setMethod(method); + handler->onRequest(onRequest); + addHandler(handler); + return *handler; +} + +AsyncCallbackWebHandler& AsyncWebServer::on(const char* uri, ArRequestHandlerFunction onRequest){ + AsyncCallbackWebHandler* handler = new AsyncCallbackWebHandler(); + handler->setUri(uri); + handler->onRequest(onRequest); + addHandler(handler); + return *handler; +} + +AsyncStaticWebHandler& AsyncWebServer::serveStatic(const char* uri, fs::FS& fs, const char* path, const char* cache_control){ + AsyncStaticWebHandler* handler = new AsyncStaticWebHandler(uri, fs, path, cache_control); + addHandler(handler); + return *handler; +} + +void AsyncWebServer::onNotFound(ArRequestHandlerFunction fn){ + _catchAllHandler->onRequest(fn); +} + +void AsyncWebServer::onFileUpload(ArUploadHandlerFunction fn){ + _catchAllHandler->onUpload(fn); +} + +void AsyncWebServer::onRequestBody(ArBodyHandlerFunction fn){ + _catchAllHandler->onBody(fn); +} + +void AsyncWebServer::reset(){ + _rewrites.free(); + _handlers.free(); + + if (_catchAllHandler != NULL){ + _catchAllHandler->onRequest(NULL); + _catchAllHandler->onUpload(NULL); + _catchAllHandler->onBody(NULL); + } +} + diff --git a/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/edit.htm b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/edit.htm new file mode 100644 index 0000000..43d4984 --- /dev/null +++ b/libraries/DS18B20-ESP8266-Arduino/libraries/ESPAsyncWebServer/src/edit.htm @@ -0,0 +1,627 @@ + + + + +ESP Editor + + + + + + +
+
+
+
+ + + + diff --git a/libraries/DateTime/DateTime.cpp b/libraries/DateTime/DateTime.cpp new file mode 100644 index 0000000..71355fd --- /dev/null +++ b/libraries/DateTime/DateTime.cpp @@ -0,0 +1,157 @@ +/* + DateTime.cpp - Arduino Date and Time library + Copyright (c) Michael Margolis. All right reserved. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +*/ + +extern "C" { + // AVR LibC Includes +} +//#include // for memset +#include "DateTime.h" +#include + +//extern unsigned long _time; + +#define LEAP_YEAR(_year) ((_year%4)==0) +static byte monthDays[]={31,28,31,30,31,30,31,31,30,31,30,31}; + +// private methods + +void DateTimeClass::setTime(time_t time) +{ + // set the system time to the given time value (as seconds since Jan 1 1970) + this->sysTime = time; + this->prevMillis = millis(); +} + + +//****************************************************************************** +//* DateTime Public Methods +//****************************************************************************** + +DateTimeClass::DateTimeClass() +{ + this->status = dtStatusNotSet; +} + +time_t DateTimeClass::now() +{ + while( millis() - prevMillis >= 1000){ + this->sysTime++; + this->prevMillis += 1000; + } + return sysTime; +} + +void DateTimeClass::sync(time_t time) +{ + setTime(time); + //status.isSynced = true; // this will be set back to false if the clock resets + //status.isSet = true; // if this is true and isSynced is false then clock was reset using EEPROM -- TODO + this->status = dtStatusSync; +} + +boolean DateTimeClass::available() +{ +// refresh time components if clock is set (even if not synced), just return false if not set + if(this->status != dtStatusNotSet) { + this->now(); // refresh sysTime + this->localTime(&this->sysTime,&Second,&Minute,&Hour,&Day,&DayofWeek,&Month,&Year) ; + return true; + } + else + return false; +} +void DateTimeClass::localTime(time_t *timep,byte *psec,byte *pmin,byte *phour,byte *pday,byte *pwday,byte *pmonth,byte *pyear) { +// convert the given time_t to time components +// this is a more compact version of the C library localtime function + + time_t long epoch=*timep; + byte year; + byte month, monthLength; + unsigned long days; + + *psec=epoch%60; + epoch/=60; // now it is minutes + *pmin=epoch%60; + epoch/=60; // now it is hours + *phour=epoch%24; + epoch/=24; // now it is days + *pwday=(epoch+4)%7; + + year=70; + days=0; + while((unsigned)(days += (LEAP_YEAR(year) ? 366 : 365)) <= epoch) { + year++; + } + *pyear=year; // *pyear is returned as years from 1900 + + days -= LEAP_YEAR(year) ? 366 : 365; + epoch -= days; // now it is days in this year, starting at 0 + //*pdayofyear=epoch; // days since jan 1 this year + + days=0; + month=0; + monthLength=0; + for (month=0; month<12; month++) { + if (month==1) { // february + if (LEAP_YEAR(year)) { + monthLength=29; + } else { + monthLength=28; + } + } else { + monthLength = monthDays[month]; + } + + if (epoch>=monthLength) { + epoch-=monthLength; + } else { + break; + } + } + *pmonth=month; // jan is month 0 + *pday=epoch+1; // day of month +} + + +time_t DateTimeClass::makeTime(byte sec, byte min, byte hour, byte day, byte month, int year ){ +// converts time components to time_t +// note year argument is full four digit year (or digits since 2000), i.e.1975, (year 8 is 2008) + + int i; + time_t seconds; + + if(year < 69) + year+= 2000; + // seconds from 1970 till 1 jan 00:00:00 this year + seconds= (year-1970)*(60*60*24L*365); + + // add extra days for leap years + for (i=1970; i +//#include // next two typedefs replace here (fixed for rel 0012) +typedef uint8_t byte; +typedef uint8_t boolean; + +typedef unsigned long time_t; + +/*==============================================================================*/ +/* Useful Constants */ +#define SECS_PER_MIN (60UL) +#define SECS_PER_HOUR (3600UL) +#define SECS_PER_DAY (SECS_PER_HOUR * 24L) +#define DAYS_PER_WEEK (7L) +#define SECS_PER_WEEK (SECS_PER_DAY * DAYS_PER_WEEK) +#define SECS_PER_YEAR (SECS_PER_WEEK * 52L) +#define SECS_YR_2000 (946681200UL) + +/* Useful Macros for getting elapsed time */ +#define numberOfSeconds(_time_) (_time_ % SECS_PER_MIN) +#define numberOfMinutes(_time_) ((_time_ / SECS_PER_MIN) % SECS_PER_MIN) +#define numberOfHours(_time_) (( _time_% SECS_PER_DAY) / SECS_PER_HOUR) +#define dayOfWeek(_time_) (( _time_ / SECS_PER_DAY + 4) % DAYS_PER_WEEK) // 0 = Sunday +#define elapsedDays(_time_) ( _time_ / SECS_PER_DAY) // this is number of days since Jan 1 1970 +#define elapsedSecsToday(_time_) (_time_ % SECS_PER_DAY) // the number of seconds since last midnight +#define previousMidnight(_time_) (( _time_ / SECS_PER_DAY) * SECS_PER_DAY) // time at the start of the given day +#define nextMidnight(_time_) ( previousMidnight(_time_) + SECS_PER_DAY ) // time at the end of the given day +#define elapsedSecsThisWeek(_time_) (elapsedSecsToday(_time_) + (dayOfWeek(_time_) * SECS_PER_DAY) ) + +// todo add date math macros +/*============================================================================*/ + +typedef enum { + dtSunday, dtMonday, dtTuesday, dtWednesday, dtThursday, dtFriday, dtSaturday +} dtDays_t; + +typedef enum {dtStatusNotSet, dtStatusSet, dtStatusSync +} dtStatus_t ; + +class DateTimeClass +{ +private: + time_t sysTime; // this is the internal time counter as seconds since midnight Jan 1 1970 (aka unix time) + unsigned long prevMillis; + void setTime(time_t time); +public: + DateTimeClass(); + void sync(time_t time); // set internal time to the given value + time_t now(); // return the current time as seconds since Jan1 1970 + boolean available(); // returns false if not set, else refreshes the Date and Time properties and returns true + dtStatus_t status; + byte Hour; + byte Minute; + byte Second; + byte Day; + byte DayofWeek; // Sunday is day 0 + byte Month; // Jan is month 0 + byte Year; // the Year minus 1900 + + // functions to convert to and from time components (hrs, secs, days, years etc) to time_t + void localTime(time_t *timep,byte *psec,byte *pmin,byte *phour,byte *pday,byte *pwday,byte *pmonth,byte *pyear); // extracts time components from time_t + time_t makeTime(byte sec, byte min, byte hour, byte day, byte month, int year ); // returns time_t from components +}; + +extern DateTimeClass DateTime; // make an instance for the user + + +#endif /* DateTime_h */ + diff --git a/libraries/DateTime/Examples/DateTime/DateTime.pde b/libraries/DateTime/Examples/DateTime/DateTime.pde new file mode 100644 index 0000000..7ee5d9e --- /dev/null +++ b/libraries/DateTime/Examples/DateTime/DateTime.pde @@ -0,0 +1,74 @@ +// DateTime.pde +// example sketch for the DateTime library + +#include +#include + +#define TIME_MSG_LEN 11 // time sync to PC is HEADER followed by unix time_t as ten ascii digits +#define TIME_HEADER 255 // Header tag for serial time sync message + +void setup(){ + Serial.begin(19200); + pinMode(13,OUTPUT); // we flash the LED each second +} + +void loop(){ + unsigned long prevtime; + if( getPCtime()) { // try to get time sync from pc + Serial.print("Clock synced at: "); + Serial.println(DateTime.now(),DEC); + } + if(DateTime.available()) { // update clocks if time has been synced + digitalWrite(13,LOW); // first flash the LED + prevtime = DateTime.now(); + while( prevtime == DateTime.now() ) // wait for the second to rollover + ; + DateTime.available(); //refresh the Date and time properties + digitalClockDisplay( ); // update digital clock + + // send our time to any app at the other end of the serial port + Serial.print( TIME_HEADER,BYTE); // this is the header for the current time + Serial.println(DateTime.now()); + digitalWrite(13,HIGH); + } + delay(100); +} + +boolean getPCtime() { + // if time sync available from serial port, update time and return true + while(Serial.available() >= TIME_MSG_LEN ){ // time message consists of a header and ten ascii digits + if( Serial.read() == TIME_HEADER ) { + time_t pctime = 0; + for(int i=0; i < TIME_MSG_LEN -1; i++){ + char c= Serial.read(); + if( c >= '0' && c <= '9'){ + pctime = (10 * pctime) + (c - '0') ; // convert digits to a number + } + } + DateTime.sync(pctime); // Sync Arduino clock to the time received on the serial port + return true; // return true if time message received on the serial port + } + } + return false; //if no message return false +} + +void digitalClockDisplay(){ + // digital clock display of current date and time + Serial.print(DateTime.Hour,DEC); + printDigits(DateTime.Minute); + printDigits(DateTime.Second); + Serial.print(" "); + Serial.print(DateTimeStrings.dayStr(DateTime.DayofWeek)); + Serial.print(" "); + Serial.print(DateTimeStrings.monthStr(DateTime.Month)); + Serial.print(" "); + Serial.println(DateTime.Day,DEC); +} + +void printDigits(byte digits){ + // utility function for digital clock display: prints preceding colon and leading 0 + Serial.print(":"); + if(digits < 10) + Serial.print('0'); + Serial.print(digits,DEC); +} diff --git a/libraries/DateTime/Examples/Processing/SetArduinoClock.pde b/libraries/DateTime/Examples/Processing/SetArduinoClock.pde new file mode 100644 index 0000000..a2eda0e --- /dev/null +++ b/libraries/DateTime/Examples/Processing/SetArduinoClock.pde @@ -0,0 +1,106 @@ +/** + * SetArduinoClock. + * + * portIndex must be set to the port connected to the Arduino + * + * Clicking the window sends the current time to the arduino + * as the string value of the number of seconds since Jan 1 1970 + * + * The clock has two second hands, the white one is the Arduino time + * the red one is the local time, if the time is exactly the same + * you will only see the red hand, although due to round trip serial delays + * the Arduino clock may be one second behind. + */ + +import processing.serial.*; + +Serial myPort; // Create object from Serial class +public static final short LF = 10; // ASCII linefeed +public static final short TIME_HEADER = 255; //header byte for arduino serial time message +public static final short FONT_SIZE = 12; +public static final short portIndex = 0; // select the com port, 0 is the first port +color PCClock = color(153, 51, 0); +PFont fontA; +long prevTime=0; // holds value of the previous time update + +void setup() { + size(200, 200); + println(Serial.list()); + println(" Connecting to -> " + Serial.list()[portIndex]); + myPort = new Serial(this,Serial.list()[portIndex], 19200); + + fontA = createFont("Arial", FONT_SIZE); + textFont(fontA); + textSize( FONT_SIZE); + stroke(255); + smooth(); + background(0); + text("Mouse click to set Arduino time", 5,10); +} + +long getTimeNow(){ + // java time is in ms, we want secs + GregorianCalendar cal = new GregorianCalendar(); + cal.setTime(new Date()); + int tzo = cal.get(Calendar.ZONE_OFFSET); + int dst = cal.get(Calendar.DST_OFFSET); + long now = (cal.getTimeInMillis() / 1000) ; + now = now + (tzo/1000) + (dst/1000); + return now; +} + +void mousePressed() { + String time = String.valueOf(getTimeNow()); + char header = 0xff; + myPort.write(header); // send header and time to arduino + myPort.write(time); +} + +void serialEvent(Serial p) { + String inString = myPort.readStringUntil(LF); + if(inString != null && inString.length() >= 13 && inString.charAt(0) == TIME_HEADER) { + int val = 0; + long time = 0; + for(int i = 1; i <= 10; i++) + time = time * 10 + (inString.charAt(i) - '0'); + UpdateClock(time); + } + else if(inString != null && inString.length() > 0 ) + println(inString); // display serial input that is not a time messgage +} + +void UpdateClock( long t){ + int sec, min, hr; + Long time = new Long(t); + sec = time.intValue() % 60; + min = (time.intValue() / 60) % 60 ; + hr = (time.intValue() % 86400) / 3660; + UpdateClock(sec, min, hr ); +} + +void UpdateClock( int sec, int min, int hour){ + fill(80); + noStroke(); + // Angles for sin() and cos() start at 3 o'clock; + // subtract HALF_PI to make them start at the top + ellipse(100, 100, 160, 160); + float s = map(sec, 0, 60, 0, TWO_PI) - HALF_PI; + float m = map(min, 0, 60, 0, TWO_PI) - HALF_PI; + float h = map(hour % 12, 0, 12, 0, TWO_PI) - HALF_PI; + stroke(255); + strokeWeight(1); + line(100, 100, cos(s) * 72 + 100, sin(s) * 72 + 100); + strokeWeight(2); + line(100, 100, cos(m) * 60 + 100, sin(m) * 60 + 100); + strokeWeight(4); + line(100, 100, cos(h) * 50 + 100, sin(h) * 50 + 100); + // now show the seconds on the computer running this app + s = map(second(), 0, 60, 0, TWO_PI) - HALF_PI; + strokeWeight(1); + stroke(PCClock); // another second hand in red to indicate Java time + line(100, 100, cos(s) * 72 + 100, sin(s) * 72 + 100); +} + +void draw() { + +} diff --git a/libraries/DateTime/keywords.txt b/libraries/DateTime/keywords.txt new file mode 100644 index 0000000..2ca8b50 --- /dev/null +++ b/libraries/DateTime/keywords.txt @@ -0,0 +1,31 @@ +####################################### +# Syntax Coloring Map For DateTime +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +####################################### +# Methods and Functions (KEYWORD2) +####################################### +Hour KEYWORD2 +Minute KEYWORD2 +Second KEYWORD2 +Day KEYWORD2 +DayofWeek KEYWORD2 +Month KEYWORD2 +Year KEYWORD2 +localTime KEYWORD2 +makeTime KEYWORD2 +sync KEYWORD2 +now KEYWORD2 +available KEYWORD2 +status KEYWORD2 +####################################### +# Instances (KEYWORD2) +####################################### +DateTime KEYWORD2 +####################################### +# Constants (LITERAL1) +####################################### \ No newline at end of file diff --git a/libraries/HC-SR04/Readme.txt b/libraries/HC-SR04/Readme.txt new file mode 100755 index 0000000..649fb4b --- /dev/null +++ b/libraries/HC-SR04/Readme.txt @@ -0,0 +1,5 @@ +ʹÓ÷½·¨£º°Ñ½âѹ°üÖÐÎļþ£¬¸´ÖƵ½X:\arduino-1.0\libraries\xxx ϼ´¿É + +¼¼ÊõÖ§³Ö£ºwww.arduino.cn +Ó²¼þÔÞÖú£ºwww.openjumper.com +ÌÔ±¦£ºi3water.taobao.com \ No newline at end of file diff --git a/libraries/HC-SR04/SR04.cpp b/libraries/HC-SR04/SR04.cpp new file mode 100755 index 0000000..3e59ba5 --- /dev/null +++ b/libraries/HC-SR04/SR04.cpp @@ -0,0 +1,80 @@ + +#include "SR04.h" + +SR04::SR04(int echoPin, int triggerPin) { + _echoPin = echoPin; + _triggerPin = triggerPin; + pinMode(_echoPin, INPUT); + pinMode(_triggerPin, OUTPUT); + _autoMode = false; + _distance = 999; +} + + +long SR04::Distance() { + long d = 0; + _duration = 0; + digitalWrite(_triggerPin, LOW); + delayMicroseconds(2); + digitalWrite(_triggerPin, HIGH); + delayMicroseconds(10); + digitalWrite(_triggerPin, LOW); + delayMicroseconds(2); + _duration = pulseIn(_echoPin, HIGH, PULSE_TIMEOUT); + d = MicrosecondsToCentimeter(_duration); + delay(25); + return d; +} + +long SR04::DistanceAvg(int wait, int count) { + long min, max, avg, d; + min = 999; + max = 0; + avg = d = 0; + + if (wait < 25) { + wait = 25; + } + + if (count < 1) { + count = 1; + } + + for (int x = 0; x < count + 2; x++) { + d = Distance(); + + if (d < min) { + min = d; + } + + if (d > max) { + max = d; + } + + avg += d; + } + + // substract highest and lowest value + avg -= (max + min); + // calculate average + avg /= count; + return avg; +} + +void SR04::Ping() { + _distance = Distance(); +} + +long SR04::getDistance() { + return _distance; +} + +long SR04::MicrosecondsToCentimeter(long duration) { + long d = (duration * 100) / 5882; + //d = (d == 0)?999:d; + return d; +} + + + + diff --git a/libraries/HC-SR04/SR04.h b/libraries/HC-SR04/SR04.h new file mode 100755 index 0000000..4624a15 --- /dev/null +++ b/libraries/HC-SR04/SR04.h @@ -0,0 +1,91 @@ +#ifndef SR04_H +#define SR04_H + +#if defined(ARDUINO) && ARDUINO >= 100 + #include "Arduino.h" +#else + #include "WProgram.h" +#endif +//#include "pins_arduino.h" + +#include + +#define PULSE_TIMEOUT 150000L // 100ms +#define DEFAULT_DELAY 10 +#define DEFAULT_PINGS 5 +class SR04 { +public: + + /** + * Constructor + * Ultrasonic sensor SR04, four connections pins + * VCC, ECHO, TRIGGER, GND + *
+ * \param echoPin digital INPUT-Pin for measuring distance + * \param triggerPin if 10us high a trigger signal is generated from + * SR04 + * + * \return void + */ + SR04(int echoPin, int triggerPin); + + /** + * Do a measurment for this sensor. Return distance as long + * in centimenter + * \return long distance in centimeter + */ + long Distance(); + + /** + * Do count measurents and calculate the average. + * To avoid defilement from ow/high peaks, min/max values + * are substracted from the average + * + * \param wait delay between measurements, default = DEFAULT_DELAY/ms + * \param count number of measurements, default DEFAULT_PINGS + * \return long distance in centimeter + **/ + long DistanceAvg(int wait=DEFAULT_DELAY, int count=DEFAULT_PINGS); + + /** + * Do only a ping. Get result with methode getDistance() + * + * \param keine + */ + void Ping() ; + + /** + * return latest distance. Methode Ping() should be called before + * \param keine + * \return Distanz in Zentimeter + */ + long getDistance(); + + +private: + /** + * Do the measurement calculation and return result in centimeter + * SR04 measure echo time to obstacle and return way. + *
+ * Sound travels with 340m/sec + *
+ * Example: Obstace 100cm away from SR04. Measure time is 100cm to + * obstacle and 100cm return = 200cm + *
+ * 1sec = 1000ms = 1.000.000uS + * 1.000.000 / 340 = Distance in microseconds for 100cm + * 2941uS fuer 100cm = 5882 uS fuer 200cm + * + * duration / 5882 * 100 = distance in cm + */ + long MicrosecondsToCentimeter(long duration); + + long _currentDistance; + int _echoPin, _triggerPin; + long _duration, _distance; + bool _autoMode; +}; +#endif + + + diff --git a/libraries/HC-SR04/examples/SR04_Example/SR04_Example.ino b/libraries/HC-SR04/examples/SR04_Example/SR04_Example.ino new file mode 100755 index 0000000..75bf397 --- /dev/null +++ b/libraries/HC-SR04/examples/SR04_Example/SR04_Example.ino @@ -0,0 +1,17 @@ +#include "SR04.h" +#define TRIG_PIN 12 +#define ECHO_PIN 11 +SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN); +long a; + +void setup() { + Serial.begin(9600); + delay(1000); +} + +void loop() { + a=sr04.Distance(); + Serial.print(a); + Serial.println("cm"); + delay(1000); +} diff --git a/libraries/HC-SR04/keywords.txt b/libraries/HC-SR04/keywords.txt new file mode 100755 index 0000000..2f1463c --- /dev/null +++ b/libraries/HC-SR04/keywords.txt @@ -0,0 +1,27 @@ +####################################### +# Syntax Coloring Map SR04 lib +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +####################################### +# Methods and Functions (KEYWORD2) +####################################### +Distance KEYWORD2 Distance +DistanceAvg KEYWORD2 Distance average +Ping KEYWORD2 only a ping signal +getDistance KEYWORD2 get distance after ping + +####################################### +# Instances (KEYWORD2) +####################################### +SR04 KEYWORD2 ultrasonic library + +####################################### +# Constants (LITERAL1) +####################################### + + + diff --git a/libraries/IRremote/IRremote.cpp b/libraries/IRremote/IRremote.cpp new file mode 100755 index 0000000..888f5e2 --- /dev/null +++ b/libraries/IRremote/IRremote.cpp @@ -0,0 +1,1154 @@ +/* + * IRremote + * Version 0.11 August, 2009 + * Copyright 2009 Ken Shirriff + * For details, see http://arcfn.com/2009/08/multi-protocol-infrared-remote-library.html + * + * Modified by Paul Stoffregen to support other boards and timers + * Modified by Mitra Ardron + * Added Sanyo and Mitsubishi controllers + * Modified Sony to spot the repeat codes that some Sony's send + * + * Interrupt code based on NECIRrcv by Joe Knapp + * http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1210243556 + * Also influenced by http://zovirl.com/2008/11/12/building-a-universal-remote-with-an-arduino/ + * + * JVC and Panasonic protocol added by Kristian Lauszus (Thanks to zenwheel and other people at the original blog post) + * LG added by Darryl Smith (based on the JVC protocol) + */ + +#include "IRremote.h" +#include "IRremoteInt.h" + +// Provides ISR +#include + +volatile irparams_t irparams; + +// These versions of MATCH, MATCH_MARK, and MATCH_SPACE are only for debugging. +// To use them, set DEBUG in IRremoteInt.h +// Normally macros are used for efficiency +#ifdef DEBUG +int MATCH(int measured, int desired) { + Serial.print("Testing: "); + Serial.print(TICKS_LOW(desired), DEC); + Serial.print(" <= "); + Serial.print(measured, DEC); + Serial.print(" <= "); + Serial.println(TICKS_HIGH(desired), DEC); + return measured >= TICKS_LOW(desired) && measured <= TICKS_HIGH(desired); +} + +int MATCH_MARK(int measured_ticks, int desired_us) { + Serial.print("Testing mark "); + Serial.print(measured_ticks * USECPERTICK, DEC); + Serial.print(" vs "); + Serial.print(desired_us, DEC); + Serial.print(": "); + Serial.print(TICKS_LOW(desired_us + MARK_EXCESS), DEC); + Serial.print(" <= "); + Serial.print(measured_ticks, DEC); + Serial.print(" <= "); + Serial.println(TICKS_HIGH(desired_us + MARK_EXCESS), DEC); + return measured_ticks >= TICKS_LOW(desired_us + MARK_EXCESS) && measured_ticks <= TICKS_HIGH(desired_us + MARK_EXCESS); +} + +int MATCH_SPACE(int measured_ticks, int desired_us) { + Serial.print("Testing space "); + Serial.print(measured_ticks * USECPERTICK, DEC); + Serial.print(" vs "); + Serial.print(desired_us, DEC); + Serial.print(": "); + Serial.print(TICKS_LOW(desired_us - MARK_EXCESS), DEC); + Serial.print(" <= "); + Serial.print(measured_ticks, DEC); + Serial.print(" <= "); + Serial.println(TICKS_HIGH(desired_us - MARK_EXCESS), DEC); + return measured_ticks >= TICKS_LOW(desired_us - MARK_EXCESS) && measured_ticks <= TICKS_HIGH(desired_us - MARK_EXCESS); +} +#else +int MATCH(int measured, int desired) {return measured >= TICKS_LOW(desired) && measured <= TICKS_HIGH(desired);} +int MATCH_MARK(int measured_ticks, int desired_us) {return MATCH(measured_ticks, (desired_us + MARK_EXCESS));} +int MATCH_SPACE(int measured_ticks, int desired_us) {return MATCH(measured_ticks, (desired_us - MARK_EXCESS));} +// Debugging versions are in IRremote.cpp +#endif + +void IRsend::sendNEC(unsigned long data, int nbits) +{ + enableIROut(38); + mark(NEC_HDR_MARK); + space(NEC_HDR_SPACE); + for (int i = 0; i < nbits; i++) { + if (data & TOPBIT) { + mark(NEC_BIT_MARK); + space(NEC_ONE_SPACE); + } + else { + mark(NEC_BIT_MARK); + space(NEC_ZERO_SPACE); + } + data <<= 1; + } + mark(NEC_BIT_MARK); + space(0); +} + +void IRsend::sendSony(unsigned long data, int nbits) { + enableIROut(40); + mark(SONY_HDR_MARK); + space(SONY_HDR_SPACE); + data = data << (32 - nbits); + for (int i = 0; i < nbits; i++) { + if (data & TOPBIT) { + mark(SONY_ONE_MARK); + space(SONY_HDR_SPACE); + } + else { + mark(SONY_ZERO_MARK); + space(SONY_HDR_SPACE); + } + data <<= 1; + } +} + +void IRsend::sendRaw(unsigned int buf[], int len, int hz) +{ + enableIROut(hz); + for (int i = 0; i < len; i++) { + if (i & 1) { + space(buf[i]); + } + else { + mark(buf[i]); + } + } + space(0); // Just to be sure +} + +// Note: first bit must be a one (start bit) +void IRsend::sendRC5(unsigned long data, int nbits) +{ + enableIROut(36); + data = data << (32 - nbits); + mark(RC5_T1); // First start bit + space(RC5_T1); // Second start bit + mark(RC5_T1); // Second start bit + for (int i = 0; i < nbits; i++) { + if (data & TOPBIT) { + space(RC5_T1); // 1 is space, then mark + mark(RC5_T1); + } + else { + mark(RC5_T1); + space(RC5_T1); + } + data <<= 1; + } + space(0); // Turn off at end +} + +// Caller needs to take care of flipping the toggle bit +void IRsend::sendRC6(unsigned long data, int nbits) +{ + enableIROut(36); + data = data << (32 - nbits); + mark(RC6_HDR_MARK); + space(RC6_HDR_SPACE); + mark(RC6_T1); // start bit + space(RC6_T1); + int t; + for (int i = 0; i < nbits; i++) { + if (i == 3) { + // double-wide trailer bit + t = 2 * RC6_T1; + } + else { + t = RC6_T1; + } + if (data & TOPBIT) { + mark(t); + space(t); + } + else { + space(t); + mark(t); + } + + data <<= 1; + } + space(0); // Turn off at end +} +void IRsend::sendPanasonic(unsigned int address, unsigned long data) { + enableIROut(35); + mark(PANASONIC_HDR_MARK); + space(PANASONIC_HDR_SPACE); + + for(int i=0;i<16;i++) + { + mark(PANASONIC_BIT_MARK); + if (address & 0x8000) { + space(PANASONIC_ONE_SPACE); + } else { + space(PANASONIC_ZERO_SPACE); + } + address <<= 1; + } + for (int i=0; i < 32; i++) { + mark(PANASONIC_BIT_MARK); + if (data & TOPBIT) { + space(PANASONIC_ONE_SPACE); + } else { + space(PANASONIC_ZERO_SPACE); + } + data <<= 1; + } + mark(PANASONIC_BIT_MARK); + space(0); +} +void IRsend::sendJVC(unsigned long data, int nbits, int repeat) +{ + enableIROut(38); + data = data << (32 - nbits); + if (!repeat){ + mark(JVC_HDR_MARK); + space(JVC_HDR_SPACE); + } + for (int i = 0; i < nbits; i++) { + if (data & TOPBIT) { + mark(JVC_BIT_MARK); + space(JVC_ONE_SPACE); + } + else { + mark(JVC_BIT_MARK); + space(JVC_ZERO_SPACE); + } + data <<= 1; + } + mark(JVC_BIT_MARK); + space(0); +} + +void IRsend::sendSAMSUNG(unsigned long data, int nbits) +{ + enableIROut(38); + mark(SAMSUNG_HDR_MARK); + space(SAMSUNG_HDR_SPACE); + for (int i = 0; i < nbits; i++) { + if (data & TOPBIT) { + mark(SAMSUNG_BIT_MARK); + space(SAMSUNG_ONE_SPACE); + } + else { + mark(SAMSUNG_BIT_MARK); + space(SAMSUNG_ZERO_SPACE); + } + data <<= 1; + } + mark(SAMSUNG_BIT_MARK); + space(0); +} + +void IRsend::mark(int time) { + // Sends an IR mark for the specified number of microseconds. + // The mark output is modulated at the PWM frequency. + TIMER_ENABLE_PWM; // Enable pin 3 PWM output + if (time > 0) delayMicroseconds(time); +} + +/* Leave pin off for time (given in microseconds) */ +void IRsend::space(int time) { + // Sends an IR space for the specified number of microseconds. + // A space is no output, so the PWM output is disabled. + TIMER_DISABLE_PWM; // Disable pin 3 PWM output + if (time > 0) delayMicroseconds(time); +} + +void IRsend::enableIROut(int khz) { + // Enables IR output. The khz value controls the modulation frequency in kilohertz. + // The IR output will be on pin 3 (OC2B). + // This routine is designed for 36-40KHz; if you use it for other values, it's up to you + // to make sure it gives reasonable results. (Watch out for overflow / underflow / rounding.) + // TIMER2 is used in phase-correct PWM mode, with OCR2A controlling the frequency and OCR2B + // controlling the duty cycle. + // There is no prescaling, so the output frequency is 16MHz / (2 * OCR2A) + // To turn the output on and off, we leave the PWM running, but connect and disconnect the output pin. + // A few hours staring at the ATmega documentation and this will all make sense. + // See my Secrets of Arduino PWM at http://arcfn.com/2009/07/secrets-of-arduino-pwm.html for details. + + + // Disable the Timer2 Interrupt (which is used for receiving IR) + TIMER_DISABLE_INTR; //Timer2 Overflow Interrupt + + pinMode(TIMER_PWM_PIN, OUTPUT); + digitalWrite(TIMER_PWM_PIN, LOW); // When not sending PWM, we want it low + + // COM2A = 00: disconnect OC2A + // COM2B = 00: disconnect OC2B; to send signal set to 10: OC2B non-inverted + // WGM2 = 101: phase-correct PWM with OCRA as top + // CS2 = 000: no prescaling + // The top value for the timer. The modulation frequency will be SYSCLOCK / 2 / OCR2A. + TIMER_CONFIG_KHZ(khz); +} + +IRrecv::IRrecv(int recvpin) +{ + irparams.recvpin = recvpin; + irparams.blinkflag = 0; +} + +// initialization +void IRrecv::enableIRIn() { + cli(); + // setup pulse clock timer interrupt + //Prescale /8 (16M/8 = 0.5 microseconds per tick) + // Therefore, the timer interval can range from 0.5 to 128 microseconds + // depending on the reset value (255 to 0) + TIMER_CONFIG_NORMAL(); + + //Timer2 Overflow Interrupt Enable + TIMER_ENABLE_INTR; + + TIMER_RESET; + + sei(); // enable interrupts + + // initialize state machine variables + irparams.rcvstate = STATE_IDLE; + irparams.rawlen = 0; + + // set pin modes + pinMode(irparams.recvpin, INPUT); +} + +// enable/disable blinking of pin 13 on IR processing +void IRrecv::blink13(int blinkflag) +{ + irparams.blinkflag = blinkflag; + if (blinkflag) + pinMode(BLINKLED, OUTPUT); +} + +// TIMER2 interrupt code to collect raw data. +// Widths of alternating SPACE, MARK are recorded in rawbuf. +// Recorded in ticks of 50 microseconds. +// rawlen counts the number of entries recorded so far. +// First entry is the SPACE between transmissions. +// As soon as a SPACE gets long, ready is set, state switches to IDLE, timing of SPACE continues. +// As soon as first MARK arrives, gap width is recorded, ready is cleared, and new logging starts +ISR(TIMER_INTR_NAME) +{ + TIMER_RESET; + + uint8_t irdata = (uint8_t)digitalRead(irparams.recvpin); + + irparams.timer++; // One more 50us tick + if (irparams.rawlen >= RAWBUF) { + // Buffer overflow + irparams.rcvstate = STATE_STOP; + } + switch(irparams.rcvstate) { + case STATE_IDLE: // In the middle of a gap + if (irdata == MARK) { + if (irparams.timer < GAP_TICKS) { + // Not big enough to be a gap. + irparams.timer = 0; + } + else { + // gap just ended, record duration and start recording transmission + irparams.rawlen = 0; + irparams.rawbuf[irparams.rawlen++] = irparams.timer; + irparams.timer = 0; + irparams.rcvstate = STATE_MARK; + } + } + break; + case STATE_MARK: // timing MARK + if (irdata == SPACE) { // MARK ended, record time + irparams.rawbuf[irparams.rawlen++] = irparams.timer; + irparams.timer = 0; + irparams.rcvstate = STATE_SPACE; + } + break; + case STATE_SPACE: // timing SPACE + if (irdata == MARK) { // SPACE just ended, record it + irparams.rawbuf[irparams.rawlen++] = irparams.timer; + irparams.timer = 0; + irparams.rcvstate = STATE_MARK; + } + else { // SPACE + if (irparams.timer > GAP_TICKS) { + // big SPACE, indicates gap between codes + // Mark current code as ready for processing + // Switch to STOP + // Don't reset timer; keep counting space width + irparams.rcvstate = STATE_STOP; + } + } + break; + case STATE_STOP: // waiting, measuring gap + if (irdata == MARK) { // reset gap timer + irparams.timer = 0; + } + break; + } + + if (irparams.blinkflag) { + if (irdata == MARK) { + BLINKLED_ON(); // turn pin 13 LED on + } + else { + BLINKLED_OFF(); // turn pin 13 LED off + } + } +} + +void IRrecv::resume() { + irparams.rcvstate = STATE_IDLE; + irparams.rawlen = 0; +} + + + +// Decodes the received IR message +// Returns 0 if no data ready, 1 if data ready. +// Results of decoding are stored in results +int IRrecv::decode(decode_results *results) { + results->rawbuf = irparams.rawbuf; + results->rawlen = irparams.rawlen; + if (irparams.rcvstate != STATE_STOP) { + return ERR; + } +#ifdef DEBUG + Serial.println("Attempting NEC decode"); +#endif + if (decodeNEC(results)) { + return DECODED; + } +#ifdef DEBUG + Serial.println("Attempting Sony decode"); +#endif + if (decodeSony(results)) { + return DECODED; + } +#ifdef DEBUG + Serial.println("Attempting Sanyo decode"); +#endif + if (decodeSanyo(results)) { + return DECODED; + } +#ifdef DEBUG + Serial.println("Attempting Mitsubishi decode"); +#endif + if (decodeMitsubishi(results)) { + return DECODED; + } +#ifdef DEBUG + Serial.println("Attempting RC5 decode"); +#endif + if (decodeRC5(results)) { + return DECODED; + } +#ifdef DEBUG + Serial.println("Attempting RC6 decode"); +#endif + if (decodeRC6(results)) { + return DECODED; + } +#ifdef DEBUG + Serial.println("Attempting Panasonic decode"); +#endif + if (decodePanasonic(results)) { + return DECODED; + } +#ifdef DEBUG + Serial.println("Attempting LG decode"); +#endif + if (decodeLG(results)) { + return DECODED; + } +#ifdef DEBUG + Serial.println("Attempting JVC decode"); +#endif + if (decodeJVC(results)) { + return DECODED; + } +#ifdef DEBUG + Serial.println("Attempting SAMSUNG decode"); +#endif + if (decodeSAMSUNG(results)) { + return DECODED; + } + // decodeHash returns a hash on any input. + // Thus, it needs to be last in the list. + // If you add any decodes, add them before this. + if (decodeHash(results)) { + return DECODED; + } + // Throw away and start over + resume(); + return ERR; +} + +// NECs have a repeat only 4 items long +long IRrecv::decodeNEC(decode_results *results) { + long data = 0; + int offset = 1; // Skip first space + // Initial mark + if (!MATCH_MARK(results->rawbuf[offset], NEC_HDR_MARK)) { + return ERR; + } + offset++; + // Check for repeat + if (irparams.rawlen == 4 && + MATCH_SPACE(results->rawbuf[offset], NEC_RPT_SPACE) && + MATCH_MARK(results->rawbuf[offset+1], NEC_BIT_MARK)) { + results->bits = 0; + results->value = REPEAT; + results->decode_type = NEC; + return DECODED; + } + if (irparams.rawlen < 2 * NEC_BITS + 4) { + return ERR; + } + // Initial space + if (!MATCH_SPACE(results->rawbuf[offset], NEC_HDR_SPACE)) { + return ERR; + } + offset++; + for (int i = 0; i < NEC_BITS; i++) { + if (!MATCH_MARK(results->rawbuf[offset], NEC_BIT_MARK)) { + return ERR; + } + offset++; + if (MATCH_SPACE(results->rawbuf[offset], NEC_ONE_SPACE)) { + data = (data << 1) | 1; + } + else if (MATCH_SPACE(results->rawbuf[offset], NEC_ZERO_SPACE)) { + data <<= 1; + } + else { + return ERR; + } + offset++; + } + // Success + results->bits = NEC_BITS; + results->value = data; + results->decode_type = NEC; + return DECODED; +} + +long IRrecv::decodeSony(decode_results *results) { + long data = 0; + if (irparams.rawlen < 2 * SONY_BITS + 2) { + return ERR; + } + int offset = 0; // Dont skip first space, check its size + + // Some Sony's deliver repeats fast after first + // unfortunately can't spot difference from of repeat from two fast clicks + if (results->rawbuf[offset] < SONY_DOUBLE_SPACE_USECS) { + // Serial.print("IR Gap found: "); + results->bits = 0; + results->value = REPEAT; + results->decode_type = SANYO; + return DECODED; + } + offset++; + + // Initial mark + if (!MATCH_MARK(results->rawbuf[offset], SONY_HDR_MARK)) { + return ERR; + } + offset++; + + while (offset + 1 < irparams.rawlen) { + if (!MATCH_SPACE(results->rawbuf[offset], SONY_HDR_SPACE)) { + break; + } + offset++; + if (MATCH_MARK(results->rawbuf[offset], SONY_ONE_MARK)) { + data = (data << 1) | 1; + } + else if (MATCH_MARK(results->rawbuf[offset], SONY_ZERO_MARK)) { + data <<= 1; + } + else { + return ERR; + } + offset++; + } + + // Success + results->bits = (offset - 1) / 2; + if (results->bits < 12) { + results->bits = 0; + return ERR; + } + results->value = data; + results->decode_type = SONY; + return DECODED; +} + +// I think this is a Sanyo decoder - serial = SA 8650B +// Looks like Sony except for timings, 48 chars of data and time/space different +long IRrecv::decodeSanyo(decode_results *results) { + long data = 0; + if (irparams.rawlen < 2 * SANYO_BITS + 2) { + return ERR; + } + int offset = 0; // Skip first space + // Initial space + /* Put this back in for debugging - note can't use #DEBUG as if Debug on we don't see the repeat cos of the delay + Serial.print("IR Gap: "); + Serial.println( results->rawbuf[offset]); + Serial.println( "test against:"); + Serial.println(results->rawbuf[offset]); + */ + if (results->rawbuf[offset] < SANYO_DOUBLE_SPACE_USECS) { + // Serial.print("IR Gap found: "); + results->bits = 0; + results->value = REPEAT; + results->decode_type = SANYO; + return DECODED; + } + offset++; + + // Initial mark + if (!MATCH_MARK(results->rawbuf[offset], SANYO_HDR_MARK)) { + return ERR; + } + offset++; + + // Skip Second Mark + if (!MATCH_MARK(results->rawbuf[offset], SANYO_HDR_MARK)) { + return ERR; + } + offset++; + + while (offset + 1 < irparams.rawlen) { + if (!MATCH_SPACE(results->rawbuf[offset], SANYO_HDR_SPACE)) { + break; + } + offset++; + if (MATCH_MARK(results->rawbuf[offset], SANYO_ONE_MARK)) { + data = (data << 1) | 1; + } + else if (MATCH_MARK(results->rawbuf[offset], SANYO_ZERO_MARK)) { + data <<= 1; + } + else { + return ERR; + } + offset++; + } + + // Success + results->bits = (offset - 1) / 2; + if (results->bits < 12) { + results->bits = 0; + return ERR; + } + results->value = data; + results->decode_type = SANYO; + return DECODED; +} + +// Looks like Sony except for timings, 48 chars of data and time/space different +long IRrecv::decodeMitsubishi(decode_results *results) { + // Serial.print("?!? decoding Mitsubishi:");Serial.print(irparams.rawlen); Serial.print(" want "); Serial.println( 2 * MITSUBISHI_BITS + 2); + long data = 0; + if (irparams.rawlen < 2 * MITSUBISHI_BITS + 2) { + return ERR; + } + int offset = 0; // Skip first space + // Initial space + /* Put this back in for debugging - note can't use #DEBUG as if Debug on we don't see the repeat cos of the delay + Serial.print("IR Gap: "); + Serial.println( results->rawbuf[offset]); + Serial.println( "test against:"); + Serial.println(results->rawbuf[offset]); + */ + /* Not seeing double keys from Mitsubishi + if (results->rawbuf[offset] < MITSUBISHI_DOUBLE_SPACE_USECS) { + // Serial.print("IR Gap found: "); + results->bits = 0; + results->value = REPEAT; + results->decode_type = MITSUBISHI; + return DECODED; + } + */ + offset++; + + // Typical + // 14200 7 41 7 42 7 42 7 17 7 17 7 18 7 41 7 18 7 17 7 17 7 18 7 41 8 17 7 17 7 18 7 17 7 + + // Initial Space + if (!MATCH_MARK(results->rawbuf[offset], MITSUBISHI_HDR_SPACE)) { + return ERR; + } + offset++; + while (offset + 1 < irparams.rawlen) { + if (MATCH_MARK(results->rawbuf[offset], MITSUBISHI_ONE_MARK)) { + data = (data << 1) | 1; + } + else if (MATCH_MARK(results->rawbuf[offset], MITSUBISHI_ZERO_MARK)) { + data <<= 1; + } + else { + // Serial.println("A"); Serial.println(offset); Serial.println(results->rawbuf[offset]); + return ERR; + } + offset++; + if (!MATCH_SPACE(results->rawbuf[offset], MITSUBISHI_HDR_SPACE)) { + // Serial.println("B"); Serial.println(offset); Serial.println(results->rawbuf[offset]); + break; + } + offset++; + } + + // Success + results->bits = (offset - 1) / 2; + if (results->bits < MITSUBISHI_BITS) { + results->bits = 0; + return ERR; + } + results->value = data; + results->decode_type = MITSUBISHI; + return DECODED; +} + + +// Gets one undecoded level at a time from the raw buffer. +// The RC5/6 decoding is easier if the data is broken into time intervals. +// E.g. if the buffer has MARK for 2 time intervals and SPACE for 1, +// successive calls to getRClevel will return MARK, MARK, SPACE. +// offset and used are updated to keep track of the current position. +// t1 is the time interval for a single bit in microseconds. +// Returns -1 for error (measured time interval is not a multiple of t1). +int IRrecv::getRClevel(decode_results *results, int *offset, int *used, int t1) { + if (*offset >= results->rawlen) { + // After end of recorded buffer, assume SPACE. + return SPACE; + } + int width = results->rawbuf[*offset]; + int val = ((*offset) % 2) ? MARK : SPACE; + int correction = (val == MARK) ? MARK_EXCESS : - MARK_EXCESS; + + int avail; + if (MATCH(width, t1 + correction)) { + avail = 1; + } + else if (MATCH(width, 2*t1 + correction)) { + avail = 2; + } + else if (MATCH(width, 3*t1 + correction)) { + avail = 3; + } + else { + return -1; + } + + (*used)++; + if (*used >= avail) { + *used = 0; + (*offset)++; + } +#ifdef DEBUG + if (val == MARK) { + Serial.println("MARK"); + } + else { + Serial.println("SPACE"); + } +#endif + return val; +} + +long IRrecv::decodeRC5(decode_results *results) { + if (irparams.rawlen < MIN_RC5_SAMPLES + 2) { + return ERR; + } + int offset = 1; // Skip gap space + long data = 0; + int used = 0; + // Get start bits + if (getRClevel(results, &offset, &used, RC5_T1) != MARK) return ERR; + if (getRClevel(results, &offset, &used, RC5_T1) != SPACE) return ERR; + if (getRClevel(results, &offset, &used, RC5_T1) != MARK) return ERR; + int nbits; + for (nbits = 0; offset < irparams.rawlen; nbits++) { + int levelA = getRClevel(results, &offset, &used, RC5_T1); + int levelB = getRClevel(results, &offset, &used, RC5_T1); + if (levelA == SPACE && levelB == MARK) { + // 1 bit + data = (data << 1) | 1; + } + else if (levelA == MARK && levelB == SPACE) { + // zero bit + data <<= 1; + } + else { + return ERR; + } + } + + // Success + results->bits = nbits; + results->value = data; + results->decode_type = RC5; + return DECODED; +} + +long IRrecv::decodeRC6(decode_results *results) { + if (results->rawlen < MIN_RC6_SAMPLES) { + return ERR; + } + int offset = 1; // Skip first space + // Initial mark + if (!MATCH_MARK(results->rawbuf[offset], RC6_HDR_MARK)) { + return ERR; + } + offset++; + if (!MATCH_SPACE(results->rawbuf[offset], RC6_HDR_SPACE)) { + return ERR; + } + offset++; + long data = 0; + int used = 0; + // Get start bit (1) + if (getRClevel(results, &offset, &used, RC6_T1) != MARK) return ERR; + if (getRClevel(results, &offset, &used, RC6_T1) != SPACE) return ERR; + int nbits; + for (nbits = 0; offset < results->rawlen; nbits++) { + int levelA, levelB; // Next two levels + levelA = getRClevel(results, &offset, &used, RC6_T1); + if (nbits == 3) { + // T bit is double wide; make sure second half matches + if (levelA != getRClevel(results, &offset, &used, RC6_T1)) return ERR; + } + levelB = getRClevel(results, &offset, &used, RC6_T1); + if (nbits == 3) { + // T bit is double wide; make sure second half matches + if (levelB != getRClevel(results, &offset, &used, RC6_T1)) return ERR; + } + if (levelA == MARK && levelB == SPACE) { // reversed compared to RC5 + // 1 bit + data = (data << 1) | 1; + } + else if (levelA == SPACE && levelB == MARK) { + // zero bit + data <<= 1; + } + else { + return ERR; // Error + } + } + // Success + results->bits = nbits; + results->value = data; + results->decode_type = RC6; + return DECODED; +} +long IRrecv::decodePanasonic(decode_results *results) { + unsigned long long data = 0; + int offset = 1; + + if (!MATCH_MARK(results->rawbuf[offset], PANASONIC_HDR_MARK)) { + return ERR; + } + offset++; + if (!MATCH_MARK(results->rawbuf[offset], PANASONIC_HDR_SPACE)) { + return ERR; + } + offset++; + + // decode address + for (int i = 0; i < PANASONIC_BITS; i++) { + if (!MATCH_MARK(results->rawbuf[offset++], PANASONIC_BIT_MARK)) { + return ERR; + } + if (MATCH_SPACE(results->rawbuf[offset],PANASONIC_ONE_SPACE)) { + data = (data << 1) | 1; + } else if (MATCH_SPACE(results->rawbuf[offset],PANASONIC_ZERO_SPACE)) { + data <<= 1; + } else { + return ERR; + } + offset++; + } + results->value = (unsigned long)data; + results->panasonicAddress = (unsigned int)(data >> 32); + results->decode_type = PANASONIC; + results->bits = PANASONIC_BITS; + return DECODED; +} + +long IRrecv::decodeLG(decode_results *results) { + long data = 0; + int offset = 1; // Skip first space + + // Initial mark + if (!MATCH_MARK(results->rawbuf[offset], LG_HDR_MARK)) { + return ERR; + } + offset++; + if (irparams.rawlen < 2 * LG_BITS + 1 ) { + return ERR; + } + // Initial space + if (!MATCH_SPACE(results->rawbuf[offset], LG_HDR_SPACE)) { + return ERR; + } + offset++; + for (int i = 0; i < LG_BITS; i++) { + if (!MATCH_MARK(results->rawbuf[offset], LG_BIT_MARK)) { + return ERR; + } + offset++; + if (MATCH_SPACE(results->rawbuf[offset], LG_ONE_SPACE)) { + data = (data << 1) | 1; + } + else if (MATCH_SPACE(results->rawbuf[offset], LG_ZERO_SPACE)) { + data <<= 1; + } + else { + return ERR; + } + offset++; + } + //Stop bit + if (!MATCH_MARK(results->rawbuf[offset], LG_BIT_MARK)){ + return ERR; + } + // Success + results->bits = LG_BITS; + results->value = data; + results->decode_type = LG; + return DECODED; +} + + +long IRrecv::decodeJVC(decode_results *results) { + long data = 0; + int offset = 1; // Skip first space + // Check for repeat + if (irparams.rawlen - 1 == 33 && + MATCH_MARK(results->rawbuf[offset], JVC_BIT_MARK) && + MATCH_MARK(results->rawbuf[irparams.rawlen-1], JVC_BIT_MARK)) { + results->bits = 0; + results->value = REPEAT; + results->decode_type = JVC; + return DECODED; + } + // Initial mark + if (!MATCH_MARK(results->rawbuf[offset], JVC_HDR_MARK)) { + return ERR; + } + offset++; + if (irparams.rawlen < 2 * JVC_BITS + 1 ) { + return ERR; + } + // Initial space + if (!MATCH_SPACE(results->rawbuf[offset], JVC_HDR_SPACE)) { + return ERR; + } + offset++; + for (int i = 0; i < JVC_BITS; i++) { + if (!MATCH_MARK(results->rawbuf[offset], JVC_BIT_MARK)) { + return ERR; + } + offset++; + if (MATCH_SPACE(results->rawbuf[offset], JVC_ONE_SPACE)) { + data = (data << 1) | 1; + } + else if (MATCH_SPACE(results->rawbuf[offset], JVC_ZERO_SPACE)) { + data <<= 1; + } + else { + return ERR; + } + offset++; + } + //Stop bit + if (!MATCH_MARK(results->rawbuf[offset], JVC_BIT_MARK)){ + return ERR; + } + // Success + results->bits = JVC_BITS; + results->value = data; + results->decode_type = JVC; + return DECODED; +} + +// SAMSUNGs have a repeat only 4 items long +long IRrecv::decodeSAMSUNG(decode_results *results) { + long data = 0; + int offset = 1; // Skip first space + // Initial mark + if (!MATCH_MARK(results->rawbuf[offset], SAMSUNG_HDR_MARK)) { + return ERR; + } + offset++; + // Check for repeat + if (irparams.rawlen == 4 && + MATCH_SPACE(results->rawbuf[offset], SAMSUNG_RPT_SPACE) && + MATCH_MARK(results->rawbuf[offset+1], SAMSUNG_BIT_MARK)) { + results->bits = 0; + results->value = REPEAT; + results->decode_type = SAMSUNG; + return DECODED; + } + if (irparams.rawlen < 2 * SAMSUNG_BITS + 4) { + return ERR; + } + // Initial space + if (!MATCH_SPACE(results->rawbuf[offset], SAMSUNG_HDR_SPACE)) { + return ERR; + } + offset++; + for (int i = 0; i < SAMSUNG_BITS; i++) { + if (!MATCH_MARK(results->rawbuf[offset], SAMSUNG_BIT_MARK)) { + return ERR; + } + offset++; + if (MATCH_SPACE(results->rawbuf[offset], SAMSUNG_ONE_SPACE)) { + data = (data << 1) | 1; + } + else if (MATCH_SPACE(results->rawbuf[offset], SAMSUNG_ZERO_SPACE)) { + data <<= 1; + } + else { + return ERR; + } + offset++; + } + // Success + results->bits = SAMSUNG_BITS; + results->value = data; + results->decode_type = SAMSUNG; + return DECODED; +} + +/* ----------------------------------------------------------------------- + * hashdecode - decode an arbitrary IR code. + * Instead of decoding using a standard encoding scheme + * (e.g. Sony, NEC, RC5), the code is hashed to a 32-bit value. + * + * The algorithm: look at the sequence of MARK signals, and see if each one + * is shorter (0), the same length (1), or longer (2) than the previous. + * Do the same with the SPACE signals. Hszh the resulting sequence of 0's, + * 1's, and 2's to a 32-bit value. This will give a unique value for each + * different code (probably), for most code systems. + * + * http://arcfn.com/2010/01/using-arbitrary-remotes-with-arduino.html + */ + +// Compare two tick values, returning 0 if newval is shorter, +// 1 if newval is equal, and 2 if newval is longer +// Use a tolerance of 20% +int IRrecv::compare(unsigned int oldval, unsigned int newval) { + if (newval < oldval * .8) { + return 0; + } + else if (oldval < newval * .8) { + return 2; + } + else { + return 1; + } +} + +// Use FNV hash algorithm: http://isthe.com/chongo/tech/comp/fnv/#FNV-param +#define FNV_PRIME_32 16777619 +#define FNV_BASIS_32 2166136261 + +/* Converts the raw code values into a 32-bit hash code. + * Hopefully this code is unique for each button. + * This isn't a "real" decoding, just an arbitrary value. + */ +long IRrecv::decodeHash(decode_results *results) { + // Require at least 6 samples to prevent triggering on noise + if (results->rawlen < 6) { + return ERR; + } + long hash = FNV_BASIS_32; + for (int i = 1; i+2 < results->rawlen; i++) { + int value = compare(results->rawbuf[i], results->rawbuf[i+2]); + // Add value into the hash + hash = (hash * FNV_PRIME_32) ^ value; + } + results->value = hash; + results->bits = 32; + results->decode_type = UNKNOWN; + return DECODED; +} + +/* Sharp and DISH support by Todd Treece ( http://unionbridge.org/design/ircommand ) + +The Dish send function needs to be repeated 4 times, and the Sharp function +has the necessary repeat built in because of the need to invert the signal. + +Sharp protocol documentation: +http://www.sbprojects.com/knowledge/ir/sharp.htm + +Here are the LIRC files that I found that seem to match the remote codes +from the oscilloscope: + +Sharp LCD TV: +http://lirc.sourceforge.net/remotes/sharp/GA538WJSA + +DISH NETWORK (echostar 301): +http://lirc.sourceforge.net/remotes/echostar/301_501_3100_5100_58xx_59xx + +For the DISH codes, only send the last for characters of the hex. +i.e. use 0x1C10 instead of 0x0000000000001C10 which is listed in the +linked LIRC file. +*/ + +void IRsend::sendSharpRaw(unsigned long data, int nbits) { + enableIROut(38); + + // Sending codes in bursts of 3 (normal, inverted, normal) makes transmission + // much more reliable. That's the exact behaviour of CD-S6470 remote control. + for (int n = 0; n < 3; n++) { + for (int i = 1 << (nbits-1); i > 0; i>>=1) { + if (data & i) { + mark(SHARP_BIT_MARK); + space(SHARP_ONE_SPACE); + } + else { + mark(SHARP_BIT_MARK); + space(SHARP_ZERO_SPACE); + } + } + + mark(SHARP_BIT_MARK); + space(SHARP_ZERO_SPACE); + delay(40); + + data = data ^ SHARP_TOGGLE_MASK; + } +} + +// Sharp send compatible with data obtained through decodeSharp +void IRsend::sendSharp(unsigned int address, unsigned int command) { + sendSharpRaw((address << 10) | (command << 2) | 2, 15); +} + +void IRsend::sendDISH(unsigned long data, int nbits) { + enableIROut(56); + mark(DISH_HDR_MARK); + space(DISH_HDR_SPACE); + for (int i = 0; i < nbits; i++) { + if (data & DISH_TOP_BIT) { + mark(DISH_BIT_MARK); + space(DISH_ONE_SPACE); + } + else { + mark(DISH_BIT_MARK); + space(DISH_ZERO_SPACE); + } + data <<= 1; + } +} diff --git a/libraries/IRremote/IRremote.h b/libraries/IRremote/IRremote.h new file mode 100755 index 0000000..17d5e81 --- /dev/null +++ b/libraries/IRremote/IRremote.h @@ -0,0 +1,128 @@ +/* + * IRremote + * Version 0.1 July, 2009 + * Copyright 2009 Ken Shirriff + * For details, see http://arcfn.com/2009/08/multi-protocol-infrared-remote-library.htm http://arcfn.com + * Edited by Mitra to add new controller SANYO + * + * Interrupt code based on NECIRrcv by Joe Knapp + * http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1210243556 + * Also influenced by http://zovirl.com/2008/11/12/building-a-universal-remote-with-an-arduino/ + * + * JVC and Panasonic protocol added by Kristian Lauszus (Thanks to zenwheel and other people at the original blog post) +* LG added by Darryl Smith (based on the JVC protocol) + */ + +#ifndef IRremote_h +#define IRremote_h + +// The following are compile-time library options. +// If you change them, recompile the library. +// If DEBUG is defined, a lot of debugging output will be printed during decoding. +// TEST must be defined for the IRtest unittests to work. It will make some +// methods virtual, which will be slightly slower, which is why it is optional. +// #define DEBUG +// #define TEST + +// Results returned from the decoder +class decode_results { +public: + int decode_type; // NEC, SONY, RC5, UNKNOWN + union { // This is used for decoding Panasonic and Sharp data + unsigned int panasonicAddress; + unsigned int sharpAddress; + }; + unsigned long value; // Decoded value + int bits; // Number of bits in decoded value + volatile unsigned int *rawbuf; // Raw intervals in .5 us ticks + int rawlen; // Number of records in rawbuf. +}; + +// Values for decode_type +#define NEC 1 +#define SONY 2 +#define RC5 3 +#define RC6 4 +#define DISH 5 +#define SHARP 6 +#define PANASONIC 7 +#define JVC 8 +#define SANYO 9 +#define MITSUBISHI 10 +#define SAMSUNG 11 +#define LG 12 +#define UNKNOWN -1 + +// Decoded value for NEC when a repeat code is received +#define REPEAT 0xffffffff + +// main class for receiving IR +class IRrecv +{ +public: + IRrecv(int recvpin); + void blink13(int blinkflag); + int decode(decode_results *results); + void enableIRIn(); + void resume(); +private: + // These are called by decode + int getRClevel(decode_results *results, int *offset, int *used, int t1); + long decodeNEC(decode_results *results); + long decodeSony(decode_results *results); + long decodeSanyo(decode_results *results); + long decodeMitsubishi(decode_results *results); + long decodeRC5(decode_results *results); + long decodeRC6(decode_results *results); + long decodePanasonic(decode_results *results); + long decodeLG(decode_results *results); + long decodeJVC(decode_results *results); + long decodeSAMSUNG(decode_results *results); + long decodeHash(decode_results *results); + int compare(unsigned int oldval, unsigned int newval); + +} +; + +// Only used for testing; can remove virtual for shorter code +#ifdef TEST +#define VIRTUAL virtual +#else +#define VIRTUAL +#endif + +class IRsend +{ +public: + IRsend() {} + void sendNEC(unsigned long data, int nbits); + void sendSony(unsigned long data, int nbits); + // Neither Sanyo nor Mitsubishi send is implemented yet + // void sendSanyo(unsigned long data, int nbits); + // void sendMitsubishi(unsigned long data, int nbits); + void sendRaw(unsigned int buf[], int len, int hz); + void sendRC5(unsigned long data, int nbits); + void sendRC6(unsigned long data, int nbits); + void sendDISH(unsigned long data, int nbits); + void sendSharp(unsigned int address, unsigned int command); + void sendSharpRaw(unsigned long data, int nbits); + void sendPanasonic(unsigned int address, unsigned long data); + void sendJVC(unsigned long data, int nbits, int repeat); // *Note instead of sending the REPEAT constant if you want the JVC repeat signal sent, send the original code value and change the repeat argument from 0 to 1. JVC protocol repeats by skipping the header NOT by sending a separate code value like NEC does. + // private: + void sendSAMSUNG(unsigned long data, int nbits); + void enableIROut(int khz); + VIRTUAL void mark(int usec); + VIRTUAL void space(int usec); +} +; + +// Some useful constants + +#define USECPERTICK 50 // microseconds per clock interrupt tick +#define RAWBUF 100 // Length of raw duration buffer + +// Marks tend to be 100us too long, and spaces 100us too short +// when received due to sensor lag. +#define MARK_EXCESS 100 + +#endif diff --git a/libraries/IRremote/IRremoteInt.h b/libraries/IRremote/IRremoteInt.h new file mode 100755 index 0000000..e565327 --- /dev/null +++ b/libraries/IRremote/IRremoteInt.h @@ -0,0 +1,515 @@ +/* + * IRremote + * Version 0.1 July, 2009 + * Copyright 2009 Ken Shirriff + * For details, see http://arcfn.com/2009/08/multi-protocol-infrared-remote-library.html + * + * Modified by Paul Stoffregen to support other boards and timers + * + * Interrupt code based on NECIRrcv by Joe Knapp + * http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1210243556 + * Also influenced by http://zovirl.com/2008/11/12/building-a-universal-remote-with-an-arduino/ + * + * JVC and Panasonic protocol added by Kristian Lauszus (Thanks to zenwheel and other people at the original blog post) + */ + +#ifndef IRremoteint_h +#define IRremoteint_h + +#if defined(ARDUINO) && ARDUINO >= 100 +#include +#else +#include +#endif + +// define which timer to use +// +// Uncomment the timer you wish to use on your board. If you +// are using another library which uses timer2, you have options +// to switch IRremote to use a different timer. + +// Arduino Mega +#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) + //#define IR_USE_TIMER1 // tx = pin 11 + #define IR_USE_TIMER2 // tx = pin 9 + //#define IR_USE_TIMER3 // tx = pin 5 + //#define IR_USE_TIMER4 // tx = pin 6 + //#define IR_USE_TIMER5 // tx = pin 46 + +// Teensy 1.0 +#elif defined(__AVR_AT90USB162__) + #define IR_USE_TIMER1 // tx = pin 17 + +// Teensy 2.0 +#elif defined(__AVR_ATmega32U4__) + //#define IR_USE_TIMER1 // tx = pin 14 + //#define IR_USE_TIMER3 // tx = pin 9 + #define IR_USE_TIMER4_HS // tx = pin 10 + +// Teensy 3.0 +#elif defined(__MK20DX128__) + #define IR_USE_TIMER_CMT // tx = pin 5 + +// Teensy++ 1.0 & 2.0 +#elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__) + //#define IR_USE_TIMER1 // tx = pin 25 + #define IR_USE_TIMER2 // tx = pin 1 + //#define IR_USE_TIMER3 // tx = pin 16 + +// Sanguino +#elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__) + //#define IR_USE_TIMER1 // tx = pin 13 + #define IR_USE_TIMER2 // tx = pin 14 + +// Atmega8 +#elif defined(__AVR_ATmega8P__) || defined(__AVR_ATmega8__) + #define IR_USE_TIMER1 // tx = pin 9 + +// Arduino Duemilanove, Diecimila, LilyPad, Mini, Fio, etc +#else + //#define IR_USE_TIMER1 // tx = pin 9 + #define IR_USE_TIMER2 // tx = pin 3 +#endif + + + +#ifdef F_CPU +#define SYSCLOCK F_CPU // main Arduino clock +#else +#define SYSCLOCK 16000000 // main Arduino clock +#endif + +#define ERR 0 +#define DECODED 1 + + +// defines for setting and clearing register bits +#ifndef cbi +#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) +#endif +#ifndef sbi +#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) +#endif + +// Pulse parms are *50-100 for the Mark and *50+100 for the space +// First MARK is the one after the long gap +// pulse parameters in usec +#define NEC_HDR_MARK 9000 +#define NEC_HDR_SPACE 4500 +#define NEC_BIT_MARK 560 +#define NEC_ONE_SPACE 1600 +#define NEC_ZERO_SPACE 560 +#define NEC_RPT_SPACE 2250 + +#define SONY_HDR_MARK 2400 +#define SONY_HDR_SPACE 600 +#define SONY_ONE_MARK 1200 +#define SONY_ZERO_MARK 600 +#define SONY_RPT_LENGTH 45000 +#define SONY_DOUBLE_SPACE_USECS 500 // usually ssee 713 - not using ticks as get number wrapround + +// SA 8650B +#define SANYO_HDR_MARK 3500 // seen range 3500 +#define SANYO_HDR_SPACE 950 // seen 950 +#define SANYO_ONE_MARK 2400 // seen 2400 +#define SANYO_ZERO_MARK 700 // seen 700 +#define SANYO_DOUBLE_SPACE_USECS 800 // usually ssee 713 - not using ticks as get number wrapround +#define SANYO_RPT_LENGTH 45000 + +// Mitsubishi RM 75501 +// 14200 7 41 7 42 7 42 7 17 7 17 7 18 7 41 7 18 7 17 7 17 7 18 7 41 8 17 7 17 7 18 7 17 7 + +// #define MITSUBISHI_HDR_MARK 250 // seen range 3500 +#define MITSUBISHI_HDR_SPACE 350 // 7*50+100 +#define MITSUBISHI_ONE_MARK 1950 // 41*50-100 +#define MITSUBISHI_ZERO_MARK 750 // 17*50-100 +// #define MITSUBISHI_DOUBLE_SPACE_USECS 800 // usually ssee 713 - not using ticks as get number wrapround +// #define MITSUBISHI_RPT_LENGTH 45000 + + +#define RC5_T1 889 +#define RC5_RPT_LENGTH 46000 + +#define RC6_HDR_MARK 2666 +#define RC6_HDR_SPACE 889 +#define RC6_T1 444 +#define RC6_RPT_LENGTH 46000 + +#define SHARP_BIT_MARK 245 +#define SHARP_ONE_SPACE 1805 +#define SHARP_ZERO_SPACE 795 +#define SHARP_GAP 600000 +#define SHARP_TOGGLE_MASK 0x3FF +#define SHARP_RPT_SPACE 3000 + +#define DISH_HDR_MARK 400 +#define DISH_HDR_SPACE 6100 +#define DISH_BIT_MARK 400 +#define DISH_ONE_SPACE 1700 +#define DISH_ZERO_SPACE 2800 +#define DISH_RPT_SPACE 6200 +#define DISH_TOP_BIT 0x8000 + +#define PANASONIC_HDR_MARK 3502 +#define PANASONIC_HDR_SPACE 1750 +#define PANASONIC_BIT_MARK 502 +#define PANASONIC_ONE_SPACE 1244 +#define PANASONIC_ZERO_SPACE 400 + +#define JVC_HDR_MARK 8000 +#define JVC_HDR_SPACE 4000 +#define JVC_BIT_MARK 600 +#define JVC_ONE_SPACE 1600 +#define JVC_ZERO_SPACE 550 +#define JVC_RPT_LENGTH 60000 + +#define LG_HDR_MARK 8000 +#define LG_HDR_SPACE 4000 +#define LG_BIT_MARK 600 +#define LG_ONE_SPACE 1600 +#define LG_ZERO_SPACE 550 +#define LG_RPT_LENGTH 60000 + +#define SAMSUNG_HDR_MARK 5000 +#define SAMSUNG_HDR_SPACE 5000 +#define SAMSUNG_BIT_MARK 560 +#define SAMSUNG_ONE_SPACE 1600 +#define SAMSUNG_ZERO_SPACE 560 +#define SAMSUNG_RPT_SPACE 2250 + + +#define SHARP_BITS 15 +#define DISH_BITS 16 + +#define TOLERANCE 25 // percent tolerance in measurements +#define LTOL (1.0 - TOLERANCE/100.) +#define UTOL (1.0 + TOLERANCE/100.) + +#define _GAP 5000 // Minimum map between transmissions +#define GAP_TICKS (_GAP/USECPERTICK) + +#define TICKS_LOW(us) (int) (((us)*LTOL/USECPERTICK)) +#define TICKS_HIGH(us) (int) (((us)*UTOL/USECPERTICK + 1)) + +// receiver states +#define STATE_IDLE 2 +#define STATE_MARK 3 +#define STATE_SPACE 4 +#define STATE_STOP 5 + +// information for the interrupt handler +typedef struct { + uint8_t recvpin; // pin for IR data from detector + uint8_t rcvstate; // state machine + uint8_t blinkflag; // TRUE to enable blinking of pin 13 on IR processing + unsigned int timer; // state timer, counts 50uS ticks. + unsigned int rawbuf[RAWBUF]; // raw data + uint8_t rawlen; // counter of entries in rawbuf +} +irparams_t; + +// Defined in IRremote.cpp +extern volatile irparams_t irparams; + +// IR detector output is active low +#define MARK 0 +#define SPACE 1 + +#define TOPBIT 0x80000000 + +#define NEC_BITS 32 +#define SONY_BITS 12 +#define SANYO_BITS 12 +#define MITSUBISHI_BITS 16 +#define MIN_RC5_SAMPLES 11 +#define MIN_RC6_SAMPLES 1 +#define PANASONIC_BITS 48 +#define JVC_BITS 16 +#define LG_BITS 28 +#define SAMSUNG_BITS 32 + + + + +// defines for timer2 (8 bits) +#if defined(IR_USE_TIMER2) +#define TIMER_RESET +#define TIMER_ENABLE_PWM (TCCR2A |= _BV(COM2B1)) +#define TIMER_DISABLE_PWM (TCCR2A &= ~(_BV(COM2B1))) +#define TIMER_ENABLE_INTR (TIMSK2 = _BV(OCIE2A)) +#define TIMER_DISABLE_INTR (TIMSK2 = 0) +#define TIMER_INTR_NAME TIMER2_COMPA_vect +#define TIMER_CONFIG_KHZ(val) ({ \ + const uint8_t pwmval = SYSCLOCK / 2000 / (val); \ + TCCR2A = _BV(WGM20); \ + TCCR2B = _BV(WGM22) | _BV(CS20); \ + OCR2A = pwmval; \ + OCR2B = pwmval / 3; \ +}) +#define TIMER_COUNT_TOP (SYSCLOCK * USECPERTICK / 1000000) +#if (TIMER_COUNT_TOP < 256) +#define TIMER_CONFIG_NORMAL() ({ \ + TCCR2A = _BV(WGM21); \ + TCCR2B = _BV(CS20); \ + OCR2A = TIMER_COUNT_TOP; \ + TCNT2 = 0; \ +}) +#else +#define TIMER_CONFIG_NORMAL() ({ \ + TCCR2A = _BV(WGM21); \ + TCCR2B = _BV(CS21); \ + OCR2A = TIMER_COUNT_TOP / 8; \ + TCNT2 = 0; \ +}) +#endif +#if defined(CORE_OC2B_PIN) +#define TIMER_PWM_PIN CORE_OC2B_PIN /* Teensy */ +#elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) +#define TIMER_PWM_PIN 9 /* Arduino Mega */ +#elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__) +#define TIMER_PWM_PIN 14 /* Sanguino */ +#else +#define TIMER_PWM_PIN 3 /* Arduino Duemilanove, Diecimila, LilyPad, etc */ +#endif + + +// defines for timer1 (16 bits) +#elif defined(IR_USE_TIMER1) +#define TIMER_RESET +#define TIMER_ENABLE_PWM (TCCR1A |= _BV(COM1A1)) +#define TIMER_DISABLE_PWM (TCCR1A &= ~(_BV(COM1A1))) +#if defined(__AVR_ATmega8P__) || defined(__AVR_ATmega8__) + #define TIMER_ENABLE_INTR (TIMSK = _BV(OCIE1A)) + #define TIMER_DISABLE_INTR (TIMSK = 0) +#else + #define TIMER_ENABLE_INTR (TIMSK1 = _BV(OCIE1A)) + #define TIMER_DISABLE_INTR (TIMSK1 = 0) +#endif +#define TIMER_INTR_NAME TIMER1_COMPA_vect +#define TIMER_CONFIG_KHZ(val) ({ \ + const uint16_t pwmval = SYSCLOCK / 2000 / (val); \ + TCCR1A = _BV(WGM11); \ + TCCR1B = _BV(WGM13) | _BV(CS10); \ + ICR1 = pwmval; \ + OCR1A = pwmval / 3; \ +}) +#define TIMER_CONFIG_NORMAL() ({ \ + TCCR1A = 0; \ + TCCR1B = _BV(WGM12) | _BV(CS10); \ + OCR1A = SYSCLOCK * USECPERTICK / 1000000; \ + TCNT1 = 0; \ +}) +#if defined(CORE_OC1A_PIN) +#define TIMER_PWM_PIN CORE_OC1A_PIN /* Teensy */ +#elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) +#define TIMER_PWM_PIN 11 /* Arduino Mega */ +#elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__) +#define TIMER_PWM_PIN 13 /* Sanguino */ +#else +#define TIMER_PWM_PIN 9 /* Arduino Duemilanove, Diecimila, LilyPad, etc */ +#endif + + +// defines for timer3 (16 bits) +#elif defined(IR_USE_TIMER3) +#define TIMER_RESET +#define TIMER_ENABLE_PWM (TCCR3A |= _BV(COM3A1)) +#define TIMER_DISABLE_PWM (TCCR3A &= ~(_BV(COM3A1))) +#define TIMER_ENABLE_INTR (TIMSK3 = _BV(OCIE3A)) +#define TIMER_DISABLE_INTR (TIMSK3 = 0) +#define TIMER_INTR_NAME TIMER3_COMPA_vect +#define TIMER_CONFIG_KHZ(val) ({ \ + const uint16_t pwmval = SYSCLOCK / 2000 / (val); \ + TCCR3A = _BV(WGM31); \ + TCCR3B = _BV(WGM33) | _BV(CS30); \ + ICR3 = pwmval; \ + OCR3A = pwmval / 3; \ +}) +#define TIMER_CONFIG_NORMAL() ({ \ + TCCR3A = 0; \ + TCCR3B = _BV(WGM32) | _BV(CS30); \ + OCR3A = SYSCLOCK * USECPERTICK / 1000000; \ + TCNT3 = 0; \ +}) +#if defined(CORE_OC3A_PIN) +#define TIMER_PWM_PIN CORE_OC3A_PIN /* Teensy */ +#elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) +#define TIMER_PWM_PIN 5 /* Arduino Mega */ +#else +#error "Please add OC3A pin number here\n" +#endif + + +// defines for timer4 (10 bits, high speed option) +#elif defined(IR_USE_TIMER4_HS) +#define TIMER_RESET +#define TIMER_ENABLE_PWM (TCCR4A |= _BV(COM4A1)) +#define TIMER_DISABLE_PWM (TCCR4A &= ~(_BV(COM4A1))) +#define TIMER_ENABLE_INTR (TIMSK4 = _BV(TOIE4)) +#define TIMER_DISABLE_INTR (TIMSK4 = 0) +#define TIMER_INTR_NAME TIMER4_OVF_vect +#define TIMER_CONFIG_KHZ(val) ({ \ + const uint16_t pwmval = SYSCLOCK / 2000 / (val); \ + TCCR4A = (1<> 8; \ + OCR4C = pwmval; \ + TC4H = (pwmval / 3) >> 8; \ + OCR4A = (pwmval / 3) & 255; \ +}) +#define TIMER_CONFIG_NORMAL() ({ \ + TCCR4A = 0; \ + TCCR4B = _BV(CS40); \ + TCCR4C = 0; \ + TCCR4D = 0; \ + TCCR4E = 0; \ + TC4H = (SYSCLOCK * USECPERTICK / 1000000) >> 8; \ + OCR4C = (SYSCLOCK * USECPERTICK / 1000000) & 255; \ + TC4H = 0; \ + TCNT4 = 0; \ +}) +#if defined(CORE_OC4A_PIN) +#define TIMER_PWM_PIN CORE_OC4A_PIN /* Teensy */ +#elif defined(__AVR_ATmega32U4__) +#define TIMER_PWM_PIN 13 /* Leonardo */ +#else +#error "Please add OC4A pin number here\n" +#endif + + +// defines for timer4 (16 bits) +#elif defined(IR_USE_TIMER4) +#define TIMER_RESET +#define TIMER_ENABLE_PWM (TCCR4A |= _BV(COM4A1)) +#define TIMER_DISABLE_PWM (TCCR4A &= ~(_BV(COM4A1))) +#define TIMER_ENABLE_INTR (TIMSK4 = _BV(OCIE4A)) +#define TIMER_DISABLE_INTR (TIMSK4 = 0) +#define TIMER_INTR_NAME TIMER4_COMPA_vect +#define TIMER_CONFIG_KHZ(val) ({ \ + const uint16_t pwmval = SYSCLOCK / 2000 / (val); \ + TCCR4A = _BV(WGM41); \ + TCCR4B = _BV(WGM43) | _BV(CS40); \ + ICR4 = pwmval; \ + OCR4A = pwmval / 3; \ +}) +#define TIMER_CONFIG_NORMAL() ({ \ + TCCR4A = 0; \ + TCCR4B = _BV(WGM42) | _BV(CS40); \ + OCR4A = SYSCLOCK * USECPERTICK / 1000000; \ + TCNT4 = 0; \ +}) +#if defined(CORE_OC4A_PIN) +#define TIMER_PWM_PIN CORE_OC4A_PIN +#elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) +#define TIMER_PWM_PIN 6 /* Arduino Mega */ +#else +#error "Please add OC4A pin number here\n" +#endif + + +// defines for timer5 (16 bits) +#elif defined(IR_USE_TIMER5) +#define TIMER_RESET +#define TIMER_ENABLE_PWM (TCCR5A |= _BV(COM5A1)) +#define TIMER_DISABLE_PWM (TCCR5A &= ~(_BV(COM5A1))) +#define TIMER_ENABLE_INTR (TIMSK5 = _BV(OCIE5A)) +#define TIMER_DISABLE_INTR (TIMSK5 = 0) +#define TIMER_INTR_NAME TIMER5_COMPA_vect +#define TIMER_CONFIG_KHZ(val) ({ \ + const uint16_t pwmval = SYSCLOCK / 2000 / (val); \ + TCCR5A = _BV(WGM51); \ + TCCR5B = _BV(WGM53) | _BV(CS50); \ + ICR5 = pwmval; \ + OCR5A = pwmval / 3; \ +}) +#define TIMER_CONFIG_NORMAL() ({ \ + TCCR5A = 0; \ + TCCR5B = _BV(WGM52) | _BV(CS50); \ + OCR5A = SYSCLOCK * USECPERTICK / 1000000; \ + TCNT5 = 0; \ +}) +#if defined(CORE_OC5A_PIN) +#define TIMER_PWM_PIN CORE_OC5A_PIN +#elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) +#define TIMER_PWM_PIN 46 /* Arduino Mega */ +#else +#error "Please add OC5A pin number here\n" +#endif + + +// defines for special carrier modulator timer +#elif defined(IR_USE_TIMER_CMT) +#define TIMER_RESET ({ \ + uint8_t tmp = CMT_MSC; \ + CMT_CMD2 = 30; \ +}) +#define TIMER_ENABLE_PWM CORE_PIN5_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_DSE|PORT_PCR_SRE +#define TIMER_DISABLE_PWM CORE_PIN5_CONFIG = PORT_PCR_MUX(1)|PORT_PCR_DSE|PORT_PCR_SRE +#define TIMER_ENABLE_INTR NVIC_ENABLE_IRQ(IRQ_CMT) +#define TIMER_DISABLE_INTR NVIC_DISABLE_IRQ(IRQ_CMT) +#define TIMER_INTR_NAME cmt_isr +#ifdef ISR +#undef ISR +#endif +#define ISR(f) void f(void) +#if F_BUS == 48000000 +#define CMT_PPS_VAL 5 +#else +#define CMT_PPS_VAL 2 +#endif +#define TIMER_CONFIG_KHZ(val) ({ \ + SIM_SCGC4 |= SIM_SCGC4_CMT; \ + SIM_SOPT2 |= SIM_SOPT2_PTD7PAD; \ + CMT_PPS = CMT_PPS_VAL; \ + CMT_CGH1 = 2667 / val; \ + CMT_CGL1 = 5333 / val; \ + CMT_CMD1 = 0; \ + CMT_CMD2 = 30; \ + CMT_CMD3 = 0; \ + CMT_CMD4 = 0; \ + CMT_OC = 0x60; \ + CMT_MSC = 0x01; \ +}) +#define TIMER_CONFIG_NORMAL() ({ \ + SIM_SCGC4 |= SIM_SCGC4_CMT; \ + CMT_PPS = CMT_PPS_VAL; \ + CMT_CGH1 = 1; \ + CMT_CGL1 = 1; \ + CMT_CMD1 = 0; \ + CMT_CMD2 = 30; \ + CMT_CMD3 = 0; \ + CMT_CMD4 = 19; \ + CMT_OC = 0; \ + CMT_MSC = 0x03; \ +}) +#define TIMER_PWM_PIN 5 + + +#else // unknown timer +#error "Internal code configuration error, no known IR_USE_TIMER# defined\n" +#endif + + +// defines for blinking the LED +#if defined(CORE_LED0_PIN) +#define BLINKLED CORE_LED0_PIN +#define BLINKLED_ON() (digitalWrite(CORE_LED0_PIN, HIGH)) +#define BLINKLED_OFF() (digitalWrite(CORE_LED0_PIN, LOW)) +#elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) +#define BLINKLED 13 +#define BLINKLED_ON() (PORTB |= B10000000) +#define BLINKLED_OFF() (PORTB &= B01111111) +#elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__) +#define BLINKLED 0 +#define BLINKLED_ON() (PORTD |= B00000001) +#define BLINKLED_OFF() (PORTD &= B11111110) +#else +#define BLINKLED 13 +#define BLINKLED_ON() (PORTB |= B00100000) +#define BLINKLED_OFF() (PORTB &= B11011111) +#endif + +#endif diff --git a/libraries/IRremote/keywords.txt b/libraries/IRremote/keywords.txt new file mode 100755 index 0000000..6e77362 --- /dev/null +++ b/libraries/IRremote/keywords.txt @@ -0,0 +1,51 @@ +####################################### +# Syntax Coloring Map For IRremote +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +decode_results KEYWORD1 +IRrecv KEYWORD1 +IRsend KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +blink13 KEYWORD2 +decode KEYWORD2 +enableIRIn KEYWORD2 +resume KEYWORD2 +enableIROut KEYWORD2 +sendNEC KEYWORD2 +sendSony KEYWORD2 +sendSanyo KEYWORD2 +sendMitsubishi KEYWORD2 +sendRaw KEYWORD2 +sendRC5 KEYWORD2 +sendRC6 KEYWORD2 +sendDISH KEYWORD2 +sendSharp KEYWORD2 +sendSharpRaw KEYWORD2 +sendPanasonic KEYWORD2 +sendJVC KEYWORD2 + +# +####################################### +# Constants (LITERAL1) +####################################### + +NEC LITERAL1 +SONY LITERAL1 +SANYO LITERAL1 +MITSUBISHI LITERAL1 +RC5 LITERAL1 +RC6 LITERAL1 +DISH LITERAL1 +SHARP LITERAL1 +PANASONIC LITERAL1 +JVC LITERAL1 +UNKNOWN LITERAL1 +REPEAT LITERAL1 diff --git a/libraries/LiquidCrystal_I2C/.DS_Store b/libraries/LiquidCrystal_I2C/.DS_Store new file mode 100644 index 0000000..e7a47fd Binary files /dev/null and b/libraries/LiquidCrystal_I2C/.DS_Store differ diff --git a/libraries/LiquidCrystal_I2C/LiquidCrystal_I2C.cpp b/libraries/LiquidCrystal_I2C/LiquidCrystal_I2C.cpp new file mode 100755 index 0000000..331e375 --- /dev/null +++ b/libraries/LiquidCrystal_I2C/LiquidCrystal_I2C.cpp @@ -0,0 +1,315 @@ +// Based on the work by DFRobot + +#include "LiquidCrystal_I2C.h" +#include +#if defined(ARDUINO) && ARDUINO >= 100 + +#include "Arduino.h" + +#define printIIC(args) Wire.write(args) +inline size_t LiquidCrystal_I2C::write(uint8_t value) { + send(value, Rs); + return 1; +} + +#else +#include "WProgram.h" + +#define printIIC(args) Wire.send(args) +inline void LiquidCrystal_I2C::write(uint8_t value) { + send(value, Rs); +} + +#endif +#include "Wire.h" + + + +// When the display powers up, it is configured as follows: +// +// 1. Display clear +// 2. Function set: +// DL = 1; 8-bit interface data +// N = 0; 1-line display +// F = 0; 5x8 dot character font +// 3. Display on/off control: +// D = 0; Display off +// C = 0; Cursor off +// B = 0; Blinking off +// 4. Entry mode set: +// I/D = 1; Increment by 1 +// S = 0; No shift +// +// Note, however, that resetting the Arduino doesn't reset the LCD, so we +// can't assume that its in that state when a sketch starts (and the +// LiquidCrystal constructor is called). + +LiquidCrystal_I2C::LiquidCrystal_I2C(uint8_t lcd_Addr,uint8_t lcd_cols,uint8_t lcd_rows) +{ + _Addr = lcd_Addr; + _cols = lcd_cols; + _rows = lcd_rows; + _backlightval = LCD_NOBACKLIGHT; +} + +void LiquidCrystal_I2C::init(){ + init_priv(); +} + +void LiquidCrystal_I2C::init_priv() +{ + Wire.begin(); + _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS; + begin(_cols, _rows); +} + +void LiquidCrystal_I2C::begin(uint8_t cols, uint8_t lines, uint8_t dotsize) { + if (lines > 1) { + _displayfunction |= LCD_2LINE; + } + _numlines = lines; + + // for some 1 line displays you can select a 10 pixel high font + if ((dotsize != 0) && (lines == 1)) { + _displayfunction |= LCD_5x10DOTS; + } + + // SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION! + // according to datasheet, we need at least 40ms after power rises above 2.7V + // before sending commands. Arduino can turn on way befer 4.5V so we'll wait 50 + delay(50); + + // Now we pull both RS and R/W low to begin commands + expanderWrite(_backlightval); // reset expanderand turn backlight off (Bit 8 =1) + delay(1000); + + //put the LCD into 4 bit mode + // this is according to the hitachi HD44780 datasheet + // figure 24, pg 46 + + // we start in 8bit mode, try to set 4 bit mode + write4bits(0x03 << 4); + delayMicroseconds(4500); // wait min 4.1ms + + // second try + write4bits(0x03 << 4); + delayMicroseconds(4500); // wait min 4.1ms + + // third go! + write4bits(0x03 << 4); + delayMicroseconds(150); + + // finally, set to 4-bit interface + write4bits(0x02 << 4); + + + // set # lines, font size, etc. + command(LCD_FUNCTIONSET | _displayfunction); + + // turn the display on with no cursor or blinking default + _displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF; + display(); + + // clear it off + clear(); + + // Initialize to default text direction (for roman languages) + _displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT; + + // set the entry mode + command(LCD_ENTRYMODESET | _displaymode); + + home(); + +} + +/********** high level commands, for the user! */ +void LiquidCrystal_I2C::clear(){ + command(LCD_CLEARDISPLAY);// clear display, set cursor position to zero + delayMicroseconds(2000); // this command takes a long time! +} + +void LiquidCrystal_I2C::home(){ + command(LCD_RETURNHOME); // set cursor position to zero + delayMicroseconds(2000); // this command takes a long time! +} + +void LiquidCrystal_I2C::setCursor(uint8_t col, uint8_t row){ + int row_offsets[] = { 0x00, 0x40, 0x14, 0x54 }; + if ( row > (_numlines-1) ) { + row = _numlines-1; // we count rows starting w/0 + } + command(LCD_SETDDRAMADDR | (col + row_offsets[row])); +} + +// Turn the display on/off (quickly) +void LiquidCrystal_I2C::noDisplay() { + _displaycontrol &= ~LCD_DISPLAYON; + command(LCD_DISPLAYCONTROL | _displaycontrol); +} +void LiquidCrystal_I2C::display() { + _displaycontrol |= LCD_DISPLAYON; + command(LCD_DISPLAYCONTROL | _displaycontrol); +} + +// Turns the underline cursor on/off +void LiquidCrystal_I2C::noCursor() { + _displaycontrol &= ~LCD_CURSORON; + command(LCD_DISPLAYCONTROL | _displaycontrol); +} +void LiquidCrystal_I2C::cursor() { + _displaycontrol |= LCD_CURSORON; + command(LCD_DISPLAYCONTROL | _displaycontrol); +} + +// Turn on and off the blinking cursor +void LiquidCrystal_I2C::noBlink() { + _displaycontrol &= ~LCD_BLINKON; + command(LCD_DISPLAYCONTROL | _displaycontrol); +} +void LiquidCrystal_I2C::blink() { + _displaycontrol |= LCD_BLINKON; + command(LCD_DISPLAYCONTROL | _displaycontrol); +} + +// These commands scroll the display without changing the RAM +void LiquidCrystal_I2C::scrollDisplayLeft(void) { + command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT); +} +void LiquidCrystal_I2C::scrollDisplayRight(void) { + command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT); +} + +// This is for text that flows Left to Right +void LiquidCrystal_I2C::leftToRight(void) { + _displaymode |= LCD_ENTRYLEFT; + command(LCD_ENTRYMODESET | _displaymode); +} + +// This is for text that flows Right to Left +void LiquidCrystal_I2C::rightToLeft(void) { + _displaymode &= ~LCD_ENTRYLEFT; + command(LCD_ENTRYMODESET | _displaymode); +} + +// This will 'right justify' text from the cursor +void LiquidCrystal_I2C::autoscroll(void) { + _displaymode |= LCD_ENTRYSHIFTINCREMENT; + command(LCD_ENTRYMODESET | _displaymode); +} + +// This will 'left justify' text from the cursor +void LiquidCrystal_I2C::noAutoscroll(void) { + _displaymode &= ~LCD_ENTRYSHIFTINCREMENT; + command(LCD_ENTRYMODESET | _displaymode); +} + +// Allows us to fill the first 8 CGRAM locations +// with custom characters +void LiquidCrystal_I2C::createChar(uint8_t location, uint8_t charmap[]) { + location &= 0x7; // we only have 8 locations 0-7 + command(LCD_SETCGRAMADDR | (location << 3)); + for (int i=0; i<8; i++) { + write(charmap[i]); + } +} + +// Turn the (optional) backlight off/on +void LiquidCrystal_I2C::noBacklight(void) { + _backlightval=LCD_NOBACKLIGHT; + expanderWrite(0); +} + +void LiquidCrystal_I2C::backlight(void) { + _backlightval=LCD_BACKLIGHT; + expanderWrite(0); +} + + + +/*********** mid level commands, for sending data/cmds */ + +inline void LiquidCrystal_I2C::command(uint8_t value) { + send(value, 0); +} + + +/************ low level data pushing commands **********/ + +// write either command or data +void LiquidCrystal_I2C::send(uint8_t value, uint8_t mode) { + uint8_t highnib=value&0xf0; + uint8_t lownib=(value<<4)&0xf0; + write4bits((highnib)|mode); + write4bits((lownib)|mode); +} + +void LiquidCrystal_I2C::write4bits(uint8_t value) { + expanderWrite(value); + pulseEnable(value); +} + +void LiquidCrystal_I2C::expanderWrite(uint8_t _data){ + Wire.beginTransmission(_Addr); + printIIC((int)(_data) | _backlightval); + Wire.endTransmission(); +} + +void LiquidCrystal_I2C::pulseEnable(uint8_t _data){ + expanderWrite(_data | En); // En high + delayMicroseconds(1); // enable pulse must be >450ns + + expanderWrite(_data & ~En); // En low + delayMicroseconds(50); // commands need > 37us to settle +} + + +// Alias functions + +void LiquidCrystal_I2C::cursor_on(){ + cursor(); +} + +void LiquidCrystal_I2C::cursor_off(){ + noCursor(); +} + +void LiquidCrystal_I2C::blink_on(){ + blink(); +} + +void LiquidCrystal_I2C::blink_off(){ + noBlink(); +} + +void LiquidCrystal_I2C::load_custom_character(uint8_t char_num, uint8_t *rows){ + createChar(char_num, rows); +} + +void LiquidCrystal_I2C::setBacklight(uint8_t new_val){ + if(new_val){ + backlight(); // turn backlight on + }else{ + noBacklight(); // turn backlight off + } +} + +void LiquidCrystal_I2C::printstr(const char c[]){ + //This function is not identical to the function used for "real" I2C displays + //it's here so the user sketch doesn't have to be changed + print(c); +} + + +// unsupported API functions +void LiquidCrystal_I2C::off(){} +void LiquidCrystal_I2C::on(){} +void LiquidCrystal_I2C::setDelay (int cmdDelay,int charDelay) {} +uint8_t LiquidCrystal_I2C::status(){return 0;} +uint8_t LiquidCrystal_I2C::keypad (){return 0;} +uint8_t LiquidCrystal_I2C::init_bargraph(uint8_t graphtype){return 0;} +void LiquidCrystal_I2C::draw_horizontal_graph(uint8_t row, uint8_t column, uint8_t len, uint8_t pixel_col_end){} +void LiquidCrystal_I2C::draw_vertical_graph(uint8_t row, uint8_t column, uint8_t len, uint8_t pixel_row_end){} +void LiquidCrystal_I2C::setContrast(uint8_t new_val){} + + diff --git a/libraries/LiquidCrystal_I2C/LiquidCrystal_I2C.h b/libraries/LiquidCrystal_I2C/LiquidCrystal_I2C.h new file mode 100755 index 0000000..201be33 --- /dev/null +++ b/libraries/LiquidCrystal_I2C/LiquidCrystal_I2C.h @@ -0,0 +1,126 @@ +//YWROBOT +#ifndef LiquidCrystal_I2C_h +#define LiquidCrystal_I2C_h + +#include +#include "Print.h" +#include + +// commands +#define LCD_CLEARDISPLAY 0x01 +#define LCD_RETURNHOME 0x02 +#define LCD_ENTRYMODESET 0x04 +#define LCD_DISPLAYCONTROL 0x08 +#define LCD_CURSORSHIFT 0x10 +#define LCD_FUNCTIONSET 0x20 +#define LCD_SETCGRAMADDR 0x40 +#define LCD_SETDDRAMADDR 0x80 + +// flags for display entry mode +#define LCD_ENTRYRIGHT 0x00 +#define LCD_ENTRYLEFT 0x02 +#define LCD_ENTRYSHIFTINCREMENT 0x01 +#define LCD_ENTRYSHIFTDECREMENT 0x00 + +// flags for display on/off control +#define LCD_DISPLAYON 0x04 +#define LCD_DISPLAYOFF 0x00 +#define LCD_CURSORON 0x02 +#define LCD_CURSOROFF 0x00 +#define LCD_BLINKON 0x01 +#define LCD_BLINKOFF 0x00 + +// flags for display/cursor shift +#define LCD_DISPLAYMOVE 0x08 +#define LCD_CURSORMOVE 0x00 +#define LCD_MOVERIGHT 0x04 +#define LCD_MOVELEFT 0x00 + +// flags for function set +#define LCD_8BITMODE 0x10 +#define LCD_4BITMODE 0x00 +#define LCD_2LINE 0x08 +#define LCD_1LINE 0x00 +#define LCD_5x10DOTS 0x04 +#define LCD_5x8DOTS 0x00 + +// flags for backlight control +#define LCD_BACKLIGHT 0x08 +#define LCD_NOBACKLIGHT 0x00 + +#define En B00000100 // Enable bit +#define Rw B00000010 // Read/Write bit +#define Rs B00000001 // Register select bit + +class LiquidCrystal_I2C : public Print { +public: + LiquidCrystal_I2C(uint8_t lcd_Addr,uint8_t lcd_cols,uint8_t lcd_rows); + void begin(uint8_t cols, uint8_t rows, uint8_t charsize = LCD_5x8DOTS ); + void clear(); + void home(); + void noDisplay(); + void display(); + void noBlink(); + void blink(); + void noCursor(); + void cursor(); + void scrollDisplayLeft(); + void scrollDisplayRight(); + void printLeft(); + void printRight(); + void leftToRight(); + void rightToLeft(); + void shiftIncrement(); + void shiftDecrement(); + void noBacklight(); + void backlight(); + void autoscroll(); + void noAutoscroll(); + void createChar(uint8_t, uint8_t[]); + void setCursor(uint8_t, uint8_t); +#if defined(ARDUINO) && ARDUINO >= 100 + virtual size_t write(uint8_t); +#else + virtual void write(uint8_t); +#endif + void command(uint8_t); + void init(); + +////compatibility API function aliases +void blink_on(); // alias for blink() +void blink_off(); // alias for noBlink() +void cursor_on(); // alias for cursor() +void cursor_off(); // alias for noCursor() +void setBacklight(uint8_t new_val); // alias for backlight() and nobacklight() +void load_custom_character(uint8_t char_num, uint8_t *rows); // alias for createChar() +void printstr(const char[]); + +////Unsupported API functions (not implemented in this library) +uint8_t status(); +void setContrast(uint8_t new_val); +uint8_t keypad(); +void setDelay(int,int); +void on(); +void off(); +uint8_t init_bargraph(uint8_t graphtype); +void draw_horizontal_graph(uint8_t row, uint8_t column, uint8_t len, uint8_t pixel_col_end); +void draw_vertical_graph(uint8_t row, uint8_t column, uint8_t len, uint8_t pixel_col_end); + + +private: + void init_priv(); + void send(uint8_t, uint8_t); + void write4bits(uint8_t); + void expanderWrite(uint8_t); + void pulseEnable(uint8_t); + uint8_t _Addr; + uint8_t _displayfunction; + uint8_t _displaycontrol; + uint8_t _displaymode; + uint8_t _numlines; + uint8_t _cols; + uint8_t _rows; + uint8_t _backlightval; +}; + +#endif diff --git a/libraries/LiquidCrystal_I2C/LiquidCrystal_I2C.o b/libraries/LiquidCrystal_I2C/LiquidCrystal_I2C.o new file mode 100755 index 0000000..bca78e0 Binary files /dev/null and b/libraries/LiquidCrystal_I2C/LiquidCrystal_I2C.o differ diff --git a/libraries/LiquidCrystal_I2C/README.md b/libraries/LiquidCrystal_I2C/README.md new file mode 100755 index 0000000..aee58d3 --- /dev/null +++ b/libraries/LiquidCrystal_I2C/README.md @@ -0,0 +1,2 @@ +# LiquidCrystal_I2C +LiquidCrystal Arduino library for the DFRobot I2C LCD displays diff --git a/libraries/LiquidCrystal_I2C/examples/.DS_Store b/libraries/LiquidCrystal_I2C/examples/.DS_Store new file mode 100644 index 0000000..e7122cf Binary files /dev/null and b/libraries/LiquidCrystal_I2C/examples/.DS_Store differ diff --git a/libraries/LiquidCrystal_I2C/examples/CustomChars/CustomChars.pde b/libraries/LiquidCrystal_I2C/examples/CustomChars/CustomChars.pde new file mode 100755 index 0000000..beea6d6 --- /dev/null +++ b/libraries/LiquidCrystal_I2C/examples/CustomChars/CustomChars.pde @@ -0,0 +1,70 @@ +//YWROBOT +//Compatible with the Arduino IDE 1.0 +//Library version:1.1 +#include +#include + +#if defined(ARDUINO) && ARDUINO >= 100 +#define printByte(args) write(args); +#else +#define printByte(args) print(args,BYTE); +#endif + +uint8_t bell[8] = {0x4,0xe,0xe,0xe,0x1f,0x0,0x4}; +uint8_t note[8] = {0x2,0x3,0x2,0xe,0x1e,0xc,0x0}; +uint8_t clock[8] = {0x0,0xe,0x15,0x17,0x11,0xe,0x0}; +uint8_t heart[8] = {0x0,0xa,0x1f,0x1f,0xe,0x4,0x0}; +uint8_t duck[8] = {0x0,0xc,0x1d,0xf,0xf,0x6,0x0}; +uint8_t check[8] = {0x0,0x1,0x3,0x16,0x1c,0x8,0x0}; +uint8_t cross[8] = {0x0,0x1b,0xe,0x4,0xe,0x1b,0x0}; +uint8_t retarrow[8] = { 0x1,0x1,0x5,0x9,0x1f,0x8,0x4}; + +LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display + +void setup() +{ + lcd.init(); // initialize the lcd + lcd.backlight(); + + lcd.createChar(0, bell); + lcd.createChar(1, note); + lcd.createChar(2, clock); + lcd.createChar(3, heart); + lcd.createChar(4, duck); + lcd.createChar(5, check); + lcd.createChar(6, cross); + lcd.createChar(7, retarrow); + lcd.home(); + + lcd.print("Hello world..."); + lcd.setCursor(0, 1); + lcd.print(" i "); + lcd.printByte(3); + lcd.print(" arduinos!"); + delay(5000); + displayKeyCodes(); + +} + +// display all keycodes +void displayKeyCodes(void) { + uint8_t i = 0; + while (1) { + lcd.clear(); + lcd.print("Codes 0x"); lcd.print(i, HEX); + lcd.print("-0x"); lcd.print(i+16, HEX); + lcd.setCursor(0, 1); + for (int j=0; j<16; j++) { + lcd.printByte(i+j); + } + i+=16; + + delay(4000); + } +} + +void loop() +{ + +} + diff --git a/libraries/LiquidCrystal_I2C/examples/HelloWorld/HelloWorld.pde b/libraries/LiquidCrystal_I2C/examples/HelloWorld/HelloWorld.pde new file mode 100755 index 0000000..458c6d8 --- /dev/null +++ b/libraries/LiquidCrystal_I2C/examples/HelloWorld/HelloWorld.pde @@ -0,0 +1,28 @@ +//YWROBOT +//Compatible with the Arduino IDE 1.0 +//Library version:1.1 +#include +#include + +LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display + +void setup() +{ + lcd.init(); // initialize the lcd + lcd.init(); + // Print a message to the LCD. + lcd.backlight(); + lcd.setCursor(3,0); + lcd.print("Hello, world!"); + lcd.setCursor(2,1); + lcd.print("Ywrobot Arduino!"); + lcd.setCursor(0,2); + lcd.print("Arduino LCM IIC 2004"); + lcd.setCursor(2,3); + lcd.print("Power By Ec-yuan!"); +} + + +void loop() +{ +} diff --git a/libraries/LiquidCrystal_I2C/examples/SerialDisplay/SerialDisplay.pde b/libraries/LiquidCrystal_I2C/examples/SerialDisplay/SerialDisplay.pde new file mode 100755 index 0000000..1fca4e1 --- /dev/null +++ b/libraries/LiquidCrystal_I2C/examples/SerialDisplay/SerialDisplay.pde @@ -0,0 +1,34 @@ +/* + * Displays text sent over the serial port (e.g. from the Serial Monitor) on + * an attached LCD. + * YWROBOT + *Compatible with the Arduino IDE 1.0 + *Library version:1.1 + */ +#include +#include + +LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display + +void setup() +{ + lcd.init(); // initialize the lcd + lcd.backlight(); + Serial.begin(9600); +} + +void loop() +{ + // when characters arrive over the serial port... + if (Serial.available()) { + // wait a bit for the entire message to arrive + delay(100); + // clear the screen + lcd.clear(); + // read all the available characters + while (Serial.available() > 0) { + // display each character to the LCD + lcd.write(Serial.read()); + } + } +} diff --git a/libraries/LiquidCrystal_I2C/keywords.txt b/libraries/LiquidCrystal_I2C/keywords.txt new file mode 100755 index 0000000..8c450a9 --- /dev/null +++ b/libraries/LiquidCrystal_I2C/keywords.txt @@ -0,0 +1,46 @@ +########################################### +# Syntax Coloring Map For LiquidCrystal_I2C +########################################### + +########################################### +# Datatypes (KEYWORD1) +########################################### + +LiquidCrystal_I2C KEYWORD1 + +########################################### +# Methods and Functions (KEYWORD2) +########################################### +init KEYWORD2 +begin KEYWORD2 +clear KEYWORD2 +home KEYWORD2 +noDisplay KEYWORD2 +display KEYWORD2 +noBlink KEYWORD2 +blink KEYWORD2 +noCursor KEYWORD2 +cursor KEYWORD2 +scrollDisplayLeft KEYWORD2 +scrollDisplayRight KEYWORD2 +leftToRight KEYWORD2 +rightToLeft KEYWORD2 +shiftIncrement KEYWORD2 +shiftDecrement KEYWORD2 +noBacklight KEYWORD2 +backlight KEYWORD2 +autoscroll KEYWORD2 +noAutoscroll KEYWORD2 +createChar KEYWORD2 +setCursor KEYWORD2 +print KEYWORD2 +blink_on KEYWORD2 +blink_off KEYWORD2 +cursor_on KEYWORD2 +cursor_off KEYWORD2 +setBacklight KEYWORD2 +load_custom_character KEYWORD2 +printstr KEYWORD2 +########################################### +# Constants (LITERAL1) +########################################### diff --git a/libraries/LiquidCrystal_I2C/library.json b/libraries/LiquidCrystal_I2C/library.json new file mode 100755 index 0000000..5a23a39 --- /dev/null +++ b/libraries/LiquidCrystal_I2C/library.json @@ -0,0 +1,15 @@ +{ + "name": "LiquidCrystal_I2C", + "keywords": "LCD, liquidcrystal, I2C", + "description": "A library for DFRobot I2C LCD displays", + "repository": + { + "type": "git", + "url": "https://github.com/marcoschwartz/LiquidCrystal_I2C.git" + }, + "frameworks": "arduino", + "platforms": + [ + "atmelavr" + ] +} \ No newline at end of file diff --git a/libraries/LiquidCrystal_I2C/library.properties b/libraries/LiquidCrystal_I2C/library.properties new file mode 100755 index 0000000..e64a7f5 --- /dev/null +++ b/libraries/LiquidCrystal_I2C/library.properties @@ -0,0 +1,9 @@ +name=LiquidCrystal I2C +version=1.1.2 +author=Frank de Brabander +maintainer=Marco Schwartz +sentence=A library for I2C LCD displays. +paragraph= The library allows to control I2C displays with functions extremely similar to LiquidCrystal library. THIS LIBRARY MIGHT NOT BE COMPATIBLE WITH EXISTING SKETCHES. +category=Display +url=https://github.com/marcoschwartz/LiquidCrystal_I2C +architectures=avr diff --git a/libraries/MFRC522/.DS_Store b/libraries/MFRC522/.DS_Store new file mode 100644 index 0000000..5008ddf Binary files /dev/null and b/libraries/MFRC522/.DS_Store differ diff --git a/libraries/MFRC522/MFRC522.cpp b/libraries/MFRC522/MFRC522.cpp new file mode 100644 index 0000000..4287a2b --- /dev/null +++ b/libraries/MFRC522/MFRC522.cpp @@ -0,0 +1,1672 @@ +/* +* MFRC522.cpp - Library to use ARDUINO RFID MODULE KIT 13.56 MHZ WITH TAGS SPI W AND R BY COOQROBOT. +* NOTE: Please also check the comments in MFRC522.h - they provide useful hints and background information. +* Released into the public domain. +*/ + +#include +#include "MFRC522.h" +#include "MFRC522Debug.h" + +///////////////////////////////////////////////////////////////////////////////////// +// Functions for setting up the Arduino +///////////////////////////////////////////////////////////////////////////////////// + + +///////////////////////////////////////////////////////////////////////////////////// +// Basic interface functions for communicating with the MFRC522 +///////////////////////////////////////////////////////////////////////////////////// + +/** + * Writes a byte to the specified register in the MFRC522 chip. + * The interface is described in the datasheet section 8.1.2. + */ +void MFRC522::PCD_WriteRegister( PCD_Register reg, ///< The register to write to. One of the PCD_Register enums. + byte value ///< The value to write. + ) { + _spiClass->beginTransaction(_spiSettings); // Set the settings to work with SPI bus + digitalWrite(_chipSelectPin, LOW); // Select slave + _spiClass->transfer(reg); // MSB == 0 is for writing. LSB is not used in address. Datasheet section 8.1.2.3. + _spiClass->transfer(value); + digitalWrite(_chipSelectPin, HIGH); // Release slave again + _spiClass->endTransaction(); // Stop using the SPI bus +} // End PCD_WriteRegister() + +/** + * Writes a number of bytes to the specified register in the MFRC522 chip. + * The interface is described in the datasheet section 8.1.2. + */ +void MFRC522::PCD_WriteRegister( PCD_Register reg, ///< The register to write to. One of the PCD_Register enums. + byte count, ///< The number of bytes to write to the register + byte *values ///< The values to write. Byte array. + ) { + _spiClass->beginTransaction(_spiSettings); // Set the settings to work with SPI bus + digitalWrite(_chipSelectPin, LOW); // Select slave + _spiClass->transfer(reg); // MSB == 0 is for writing. LSB is not used in address. Datasheet section 8.1.2.3. + for (byte index = 0; index < count; index++) { + _spiClass->transfer(values[index]); + } + digitalWrite(_chipSelectPin, HIGH); // Release slave again + _spiClass->endTransaction(); // Stop using the SPI bus +} // End PCD_WriteRegister() + +/** + * Reads a byte from the specified register in the MFRC522 chip. + * The interface is described in the datasheet section 8.1.2. + */ +byte MFRC522::PCD_ReadRegister( PCD_Register reg ///< The register to read from. One of the PCD_Register enums. + ) { + byte value; + _spiClass->beginTransaction(_spiSettings); // Set the settings to work with SPI bus + digitalWrite(_chipSelectPin, LOW); // Select slave + _spiClass->transfer(0x80 | reg); // MSB == 1 is for reading. LSB is not used in address. Datasheet section 8.1.2.3. + value = _spiClass->transfer(0); // Read the value back. Send 0 to stop reading. + digitalWrite(_chipSelectPin, HIGH); // Release slave again + _spiClass->endTransaction(); // Stop using the SPI bus + return value; +} // End PCD_ReadRegister() + +/** + * Reads a number of bytes from the specified register in the MFRC522 chip. + * The interface is described in the datasheet section 8.1.2. + */ +void MFRC522::PCD_ReadRegister( PCD_Register reg, ///< The register to read from. One of the PCD_Register enums. + byte count, ///< The number of bytes to read + byte *values, ///< Byte array to store the values in. + byte rxAlign ///< Only bit positions rxAlign..7 in values[0] are updated. + ) { + if (count == 0) { + return; + } + //Serial.print(F("Reading ")); Serial.print(count); Serial.println(F(" bytes from register.")); + byte address = 0x80 | reg; // MSB == 1 is for reading. LSB is not used in address. Datasheet section 8.1.2.3. + byte index = 0; // Index in values array. + _spiClass->beginTransaction(_spiSettings); // Set the settings to work with SPI bus + digitalWrite(_chipSelectPin, LOW); // Select slave + count--; // One read is performed outside of the loop + _spiClass->transfer(address); // Tell MFRC522 which address we want to read + if (rxAlign) { // Only update bit positions rxAlign..7 in values[0] + // Create bit mask for bit positions rxAlign..7 + byte mask = (0xFF << rxAlign) & 0xFF; + // Read value and tell that we want to read the same address again. + byte value = _spiClass->transfer(address); + // Apply mask to both current value of values[0] and the new data in value. + values[0] = (values[0] & ~mask) | (value & mask); + index++; + } + while (index < count) { + values[index] = _spiClass->transfer(address); // Read value and tell that we want to read the same address again. + index++; + } + values[index] = _spiClass->transfer(0); // Read the final byte. Send 0 to stop reading. + digitalWrite(_chipSelectPin, HIGH); // Release slave again + _spiClass->endTransaction(); // Stop using the SPI bus +} // End PCD_ReadRegister() + +/** + * Sets the bits given in mask in register reg. + */ +void MFRC522::PCD_SetRegisterBitMask( PCD_Register reg, ///< The register to update. One of the PCD_Register enums. + byte mask ///< The bits to set. + ) { + byte tmp; + tmp = PCD_ReadRegister(reg); + PCD_WriteRegister(reg, tmp | mask); // set bit mask +} // End PCD_SetRegisterBitMask() + +/** + * Clears the bits given in mask from register reg. + */ +void MFRC522::PCD_ClearRegisterBitMask( PCD_Register reg, ///< The register to update. One of the PCD_Register enums. + byte mask ///< The bits to clear. + ) { + byte tmp; + tmp = PCD_ReadRegister(reg); + PCD_WriteRegister(reg, tmp & (~mask)); // clear bit mask +} // End PCD_ClearRegisterBitMask() + + +/** + * Use the CRC coprocessor in the MFRC522 to calculate a CRC_A. + * + * @return STATUS_OK on success, STATUS_??? otherwise. + */ +MFRC522::StatusCode MFRC522::PCD_CalculateCRC( byte *data, ///< In: Pointer to the data to transfer to the FIFO for CRC calculation. + byte length, ///< In: The number of bytes to transfer. + byte *result ///< Out: Pointer to result buffer. Result is written to result[0..1], low byte first. + ) { + PCD_WriteRegister(CommandReg, PCD_Idle); // Stop any active command. + PCD_WriteRegister(DivIrqReg, 0x04); // Clear the CRCIRq interrupt request bit + PCD_WriteRegister(FIFOLevelReg, 0x80); // FlushBuffer = 1, FIFO initialization + PCD_WriteRegister(FIFODataReg, length, data); // Write data to the FIFO + PCD_WriteRegister(CommandReg, PCD_CalcCRC); // Start the calculation + + // Wait for the CRC calculation to complete. Each iteration of the while-loop takes 17.73μs. + // TODO check/modify for other architectures than Arduino Uno 16bit + + // Wait for the CRC calculation to complete. Each iteration of the while-loop takes 17.73us. + for (uint16_t i = 5000; i > 0; i--) { + // DivIrqReg[7..0] bits are: Set2 reserved reserved MfinActIRq reserved CRCIRq reserved reserved + byte n = PCD_ReadRegister(DivIrqReg); + if (n & 0x04) { // CRCIRq bit set - calculation done + PCD_WriteRegister(CommandReg, PCD_Idle); // Stop calculating CRC for new content in the FIFO. + // Transfer the result from the registers to the result buffer + result[0] = PCD_ReadRegister(CRCResultRegL); + result[1] = PCD_ReadRegister(CRCResultRegH); + return STATUS_OK; + } + } + // 89ms passed and nothing happend. Communication with the MFRC522 might be down. + return STATUS_TIMEOUT; +} // End PCD_CalculateCRC() + + +///////////////////////////////////////////////////////////////////////////////////// +// Functions for manipulating the MFRC522 +///////////////////////////////////////////////////////////////////////////////////// + +/** + * Initializes the MFRC522 chip. + */ +void MFRC522::PCD_Init() { + bool hardReset = false; + + // Set the chipSelectPin as digital output, do not select the slave yet + pinMode(_chipSelectPin, OUTPUT); + digitalWrite(_chipSelectPin, HIGH); + + // If a valid pin number has been set, pull device out of power down / reset state. + if (_resetPowerDownPin != UNUSED_PIN) { + // Set the resetPowerDownPin as digital output, do not reset or power down. + pinMode(_resetPowerDownPin, OUTPUT); + + if (digitalRead(_resetPowerDownPin) == LOW) { // The MFRC522 chip is in power down mode. + digitalWrite(_resetPowerDownPin, HIGH); // Exit power down mode. This triggers a hard reset. + // Section 8.8.2 in the datasheet says the oscillator start-up time is the start up time of the crystal + 37,74μs. Let us be generous: 50ms. + delay(50); + hardReset = true; + } + } + + if (!hardReset) { // Perform a soft reset if we haven't triggered a hard reset above. + PCD_Reset(); + } + + // Reset baud rates + PCD_WriteRegister(TxModeReg, 0x00); + PCD_WriteRegister(RxModeReg, 0x00); + // Reset ModWidthReg + PCD_WriteRegister(ModWidthReg, 0x26); + + // When communicating with a PICC we need a timeout if something goes wrong. + // f_timer = 13.56 MHz / (2*TPreScaler+1) where TPreScaler = [TPrescaler_Hi:TPrescaler_Lo]. + // TPrescaler_Hi are the four low bits in TModeReg. TPrescaler_Lo is TPrescalerReg. + PCD_WriteRegister(TModeReg, 0x80); // TAuto=1; timer starts automatically at the end of the transmission in all communication modes at all speeds + PCD_WriteRegister(TPrescalerReg, 0xA9); // TPreScaler = TModeReg[3..0]:TPrescalerReg, ie 0x0A9 = 169 => f_timer=40kHz, ie a timer period of 25μs. + PCD_WriteRegister(TReloadRegH, 0x03); // Reload timer with 0x3E8 = 1000, ie 25ms before timeout. + PCD_WriteRegister(TReloadRegL, 0xE8); + + PCD_WriteRegister(TxASKReg, 0x40); // Default 0x00. Force a 100 % ASK modulation independent of the ModGsPReg register setting + PCD_WriteRegister(ModeReg, 0x3D); // Default 0x3F. Set the preset value for the CRC coprocessor for the CalcCRC command to 0x6363 (ISO 14443-3 part 6.2.4) + PCD_AntennaOn(); // Enable the antenna driver pins TX1 and TX2 (they were disabled by the reset) +} // End PCD_Init() + +/** + * Initializes the MFRC522 chip. + */ +void MFRC522::PCD_Init( byte chipSelectPin, ///< Arduino pin connected to MFRC522's SPI slave select input (Pin 24, NSS, active low) + byte resetPowerDownPin ///< Arduino pin connected to MFRC522's reset and power down input (Pin 6, NRSTPD, active low) + ) { + _chipSelectPin = chipSelectPin; + _resetPowerDownPin = resetPowerDownPin; + // Set the chipSelectPin as digital output, do not select the slave yet + PCD_Init(); +} // End PCD_Init() + +/** + * Performs a soft reset on the MFRC522 chip and waits for it to be ready again. + */ +void MFRC522::PCD_Reset() { + PCD_WriteRegister(CommandReg, PCD_SoftReset); // Issue the SoftReset command. + // The datasheet does not mention how long the SoftRest command takes to complete. + // But the MFRC522 might have been in soft power-down mode (triggered by bit 4 of CommandReg) + // Section 8.8.2 in the datasheet says the oscillator start-up time is the start up time of the crystal + 37,74μs. Let us be generous: 50ms. + delay(50); + // Wait for the PowerDown bit in CommandReg to be cleared + while (PCD_ReadRegister(CommandReg) & (1<<4)) { + // PCD still restarting - unlikely after waiting 50ms, but better safe than sorry. + } +} // End PCD_Reset() + +/** + * Turns the antenna on by enabling pins TX1 and TX2. + * After a reset these pins are disabled. + */ +void MFRC522::PCD_AntennaOn() { + byte value = PCD_ReadRegister(TxControlReg); + if ((value & 0x03) != 0x03) { + PCD_WriteRegister(TxControlReg, value | 0x03); + } +} // End PCD_AntennaOn() + +/** + * Turns the antenna off by disabling pins TX1 and TX2. + */ +void MFRC522::PCD_AntennaOff() { + PCD_ClearRegisterBitMask(TxControlReg, 0x03); +} // End PCD_AntennaOff() + +/** + * Get the current MFRC522 Receiver Gain (RxGain[2:0]) value. + * See 9.3.3.6 / table 98 in http://www.nxp.com/documents/data_sheet/MFRC522.pdf + * NOTE: Return value scrubbed with (0x07<<4)=01110000b as RCFfgReg may use reserved bits. + * + * @return Value of the RxGain, scrubbed to the 3 bits used. + */ +byte MFRC522::PCD_GetAntennaGain() { + return PCD_ReadRegister(RFCfgReg) & (0x07<<4); +} // End PCD_GetAntennaGain() + +/** + * Set the MFRC522 Receiver Gain (RxGain) to value specified by given mask. + * See 9.3.3.6 / table 98 in http://www.nxp.com/documents/data_sheet/MFRC522.pdf + * NOTE: Given mask is scrubbed with (0x07<<4)=01110000b as RCFfgReg may use reserved bits. + */ +void MFRC522::PCD_SetAntennaGain(byte mask) { + if (PCD_GetAntennaGain() != mask) { // only bother if there is a change + PCD_ClearRegisterBitMask(RFCfgReg, (0x07<<4)); // clear needed to allow 000 pattern + PCD_SetRegisterBitMask(RFCfgReg, mask & (0x07<<4)); // only set RxGain[2:0] bits + } +} // End PCD_SetAntennaGain() + +/** + * Performs a self-test of the MFRC522 + * See 16.1.1 in http://www.nxp.com/documents/data_sheet/MFRC522.pdf + * + * @return Whether or not the test passed. Or false if no firmware reference is available. + */ +bool MFRC522::PCD_PerformSelfTest() { + // This follows directly the steps outlined in 16.1.1 + // 1. Perform a soft reset. + PCD_Reset(); + + // 2. Clear the internal buffer by writing 25 bytes of 00h + byte ZEROES[25] = {0x00}; + PCD_WriteRegister(FIFOLevelReg, 0x80); // flush the FIFO buffer + PCD_WriteRegister(FIFODataReg, 25, ZEROES); // write 25 bytes of 00h to FIFO + PCD_WriteRegister(CommandReg, PCD_Mem); // transfer to internal buffer + + // 3. Enable self-test + PCD_WriteRegister(AutoTestReg, 0x09); + + // 4. Write 00h to FIFO buffer + PCD_WriteRegister(FIFODataReg, 0x00); + + // 5. Start self-test by issuing the CalcCRC command + PCD_WriteRegister(CommandReg, PCD_CalcCRC); + + // 6. Wait for self-test to complete + byte n; + for (uint8_t i = 0; i < 0xFF; i++) { + // The datasheet does not specify exact completion condition except + // that FIFO buffer should contain 64 bytes. + // While selftest is initiated by CalcCRC command + // it behaves differently from normal CRC computation, + // so one can't reliably use DivIrqReg to check for completion. + // It is reported that some devices does not trigger CRCIRq flag + // during selftest. + n = PCD_ReadRegister(FIFOLevelReg); + if (n >= 64) { + break; + } + } + PCD_WriteRegister(CommandReg, PCD_Idle); // Stop calculating CRC for new content in the FIFO. + + // 7. Read out resulting 64 bytes from the FIFO buffer. + byte result[64]; + PCD_ReadRegister(FIFODataReg, 64, result, 0); + + // Auto self-test done + // Reset AutoTestReg register to be 0 again. Required for normal operation. + PCD_WriteRegister(AutoTestReg, 0x00); + + // Determine firmware version (see section 9.3.4.8 in spec) + byte version = PCD_ReadRegister(VersionReg); + + // Pick the appropriate reference values + const byte *reference; + switch (version) { + case 0x88: // Fudan Semiconductor FM17522 clone + reference = FM17522_firmware_reference; + break; + case 0x90: // Version 0.0 + reference = MFRC522_firmware_referenceV0_0; + break; + case 0x91: // Version 1.0 + reference = MFRC522_firmware_referenceV1_0; + break; + case 0x92: // Version 2.0 + reference = MFRC522_firmware_referenceV2_0; + break; + default: // Unknown version + return false; // abort test + } + + // Verify that the results match up to our expectations + for (uint8_t i = 0; i < 64; i++) { + if (result[i] != pgm_read_byte(&(reference[i]))) { + return false; + } + } + + // Test passed; all is good. + return true; +} // End PCD_PerformSelfTest() + +///////////////////////////////////////////////////////////////////////////////////// +// Power control +///////////////////////////////////////////////////////////////////////////////////// + +//IMPORTANT NOTE!!!! +//Calling any other function that uses CommandReg will disable soft power down mode !!! +//For more details about power control, refer to the datasheet - page 33 (8.6) + +void MFRC522::PCD_SoftPowerDown(){//Note : Only soft power down mode is available throught software + byte val = PCD_ReadRegister(CommandReg); // Read state of the command register + val |= (1<<4);// set PowerDown bit ( bit 4 ) to 1 + PCD_WriteRegister(CommandReg, val);//write new value to the command register +} + +void MFRC522::PCD_SoftPowerUp(){ + byte val = PCD_ReadRegister(CommandReg); // Read state of the command register + val &= ~(1<<4);// set PowerDown bit ( bit 4 ) to 0 + PCD_WriteRegister(CommandReg, val);//write new value to the command register + // wait until PowerDown bit is cleared (this indicates end of wake up procedure) + const uint32_t timeout = (uint32_t)millis() + 500;// create timer for timeout (just in case) + + while(millis()<=timeout){ // set timeout to 500 ms + val = PCD_ReadRegister(CommandReg);// Read state of the command register + if(!(val & (1<<4))){ // if powerdown bit is 0 + break;// wake up procedure is finished + } + } +} + +///////////////////////////////////////////////////////////////////////////////////// +// Functions for communicating with PICCs +///////////////////////////////////////////////////////////////////////////////////// + +/** + * Executes the Transceive command. + * CRC validation can only be done if backData and backLen are specified. + * + * @return STATUS_OK on success, STATUS_??? otherwise. + */ +MFRC522::StatusCode MFRC522::PCD_TransceiveData( byte *sendData, ///< Pointer to the data to transfer to the FIFO. + byte sendLen, ///< Number of bytes to transfer to the FIFO. + byte *backData, ///< nullptr or pointer to buffer if data should be read back after executing the command. + byte *backLen, ///< In: Max number of bytes to write to *backData. Out: The number of bytes returned. + byte *validBits, ///< In/Out: The number of valid bits in the last byte. 0 for 8 valid bits. Default nullptr. + byte rxAlign, ///< In: Defines the bit position in backData[0] for the first bit received. Default 0. + bool checkCRC ///< In: True => The last two bytes of the response is assumed to be a CRC_A that must be validated. + ) { + byte waitIRq = 0x30; // RxIRq and IdleIRq + return PCD_CommunicateWithPICC(PCD_Transceive, waitIRq, sendData, sendLen, backData, backLen, validBits, rxAlign, checkCRC); +} // End PCD_TransceiveData() + +/** + * Transfers data to the MFRC522 FIFO, executes a command, waits for completion and transfers data back from the FIFO. + * CRC validation can only be done if backData and backLen are specified. + * + * @return STATUS_OK on success, STATUS_??? otherwise. + */ +MFRC522::StatusCode MFRC522::PCD_CommunicateWithPICC( byte command, ///< The command to execute. One of the PCD_Command enums. + byte waitIRq, ///< The bits in the ComIrqReg register that signals successful completion of the command. + byte *sendData, ///< Pointer to the data to transfer to the FIFO. + byte sendLen, ///< Number of bytes to transfer to the FIFO. + byte *backData, ///< nullptr or pointer to buffer if data should be read back after executing the command. + byte *backLen, ///< In: Max number of bytes to write to *backData. Out: The number of bytes returned. + byte *validBits, ///< In/Out: The number of valid bits in the last byte. 0 for 8 valid bits. + byte rxAlign, ///< In: Defines the bit position in backData[0] for the first bit received. Default 0. + bool checkCRC ///< In: True => The last two bytes of the response is assumed to be a CRC_A that must be validated. + ) { + // Prepare values for BitFramingReg + byte txLastBits = validBits ? *validBits : 0; + byte bitFraming = (rxAlign << 4) + txLastBits; // RxAlign = BitFramingReg[6..4]. TxLastBits = BitFramingReg[2..0] + + PCD_WriteRegister(CommandReg, PCD_Idle); // Stop any active command. + PCD_WriteRegister(ComIrqReg, 0x7F); // Clear all seven interrupt request bits + PCD_WriteRegister(FIFOLevelReg, 0x80); // FlushBuffer = 1, FIFO initialization + PCD_WriteRegister(FIFODataReg, sendLen, sendData); // Write sendData to the FIFO + PCD_WriteRegister(BitFramingReg, bitFraming); // Bit adjustments + PCD_WriteRegister(CommandReg, command); // Execute the command + if (command == PCD_Transceive) { + PCD_SetRegisterBitMask(BitFramingReg, 0x80); // StartSend=1, transmission of data starts + } + + // Wait for the command to complete. + // In PCD_Init() we set the TAuto flag in TModeReg. This means the timer automatically starts when the PCD stops transmitting. + // Each iteration of the do-while-loop takes 17.86μs. + // TODO check/modify for other architectures than Arduino Uno 16bit + uint16_t i; + for (i = 2000; i > 0; i--) { + byte n = PCD_ReadRegister(ComIrqReg); // ComIrqReg[7..0] bits are: Set1 TxIRq RxIRq IdleIRq HiAlertIRq LoAlertIRq ErrIRq TimerIRq + if (n & waitIRq) { // One of the interrupts that signal success has been set. + break; + } + if (n & 0x01) { // Timer interrupt - nothing received in 25ms + return STATUS_TIMEOUT; + } + } + // 35.7ms and nothing happend. Communication with the MFRC522 might be down. + if (i == 0) { + return STATUS_TIMEOUT; + } + + // Stop now if any errors except collisions were detected. + byte errorRegValue = PCD_ReadRegister(ErrorReg); // ErrorReg[7..0] bits are: WrErr TempErr reserved BufferOvfl CollErr CRCErr ParityErr ProtocolErr + if (errorRegValue & 0x13) { // BufferOvfl ParityErr ProtocolErr + return STATUS_ERROR; + } + + byte _validBits = 0; + + // If the caller wants data back, get it from the MFRC522. + if (backData && backLen) { + byte n = PCD_ReadRegister(FIFOLevelReg); // Number of bytes in the FIFO + if (n > *backLen) { + return STATUS_NO_ROOM; + } + *backLen = n; // Number of bytes returned + PCD_ReadRegister(FIFODataReg, n, backData, rxAlign); // Get received data from FIFO + _validBits = PCD_ReadRegister(ControlReg) & 0x07; // RxLastBits[2:0] indicates the number of valid bits in the last received byte. If this value is 000b, the whole byte is valid. + if (validBits) { + *validBits = _validBits; + } + } + + // Tell about collisions + if (errorRegValue & 0x08) { // CollErr + return STATUS_COLLISION; + } + + // Perform CRC_A validation if requested. + if (backData && backLen && checkCRC) { + // In this case a MIFARE Classic NAK is not OK. + if (*backLen == 1 && _validBits == 4) { + return STATUS_MIFARE_NACK; + } + // We need at least the CRC_A value and all 8 bits of the last byte must be received. + if (*backLen < 2 || _validBits != 0) { + return STATUS_CRC_WRONG; + } + // Verify CRC_A - do our own calculation and store the control in controlBuffer. + byte controlBuffer[2]; + MFRC522::StatusCode status = PCD_CalculateCRC(&backData[0], *backLen - 2, &controlBuffer[0]); + if (status != STATUS_OK) { + return status; + } + if ((backData[*backLen - 2] != controlBuffer[0]) || (backData[*backLen - 1] != controlBuffer[1])) { + return STATUS_CRC_WRONG; + } + } + + return STATUS_OK; +} // End PCD_CommunicateWithPICC() + +/** + * Transmits a REQuest command, Type A. Invites PICCs in state IDLE to go to READY and prepare for anticollision or selection. 7 bit frame. + * Beware: When two PICCs are in the field at the same time I often get STATUS_TIMEOUT - probably due do bad antenna design. + * + * @return STATUS_OK on success, STATUS_??? otherwise. + */ +MFRC522::StatusCode MFRC522::PICC_RequestA( byte *bufferATQA, ///< The buffer to store the ATQA (Answer to request) in + byte *bufferSize ///< Buffer size, at least two bytes. Also number of bytes returned if STATUS_OK. + ) { + return PICC_REQA_or_WUPA(PICC_CMD_REQA, bufferATQA, bufferSize); +} // End PICC_RequestA() + +/** + * Transmits a Wake-UP command, Type A. Invites PICCs in state IDLE and HALT to go to READY(*) and prepare for anticollision or selection. 7 bit frame. + * Beware: When two PICCs are in the field at the same time I often get STATUS_TIMEOUT - probably due do bad antenna design. + * + * @return STATUS_OK on success, STATUS_??? otherwise. + */ +MFRC522::StatusCode MFRC522::PICC_WakeupA( byte *bufferATQA, ///< The buffer to store the ATQA (Answer to request) in + byte *bufferSize ///< Buffer size, at least two bytes. Also number of bytes returned if STATUS_OK. + ) { + return PICC_REQA_or_WUPA(PICC_CMD_WUPA, bufferATQA, bufferSize); +} // End PICC_WakeupA() + +/** + * Transmits REQA or WUPA commands. + * Beware: When two PICCs are in the field at the same time I often get STATUS_TIMEOUT - probably due do bad antenna design. + * + * @return STATUS_OK on success, STATUS_??? otherwise. + */ +MFRC522::StatusCode MFRC522::PICC_REQA_or_WUPA( byte command, ///< The command to send - PICC_CMD_REQA or PICC_CMD_WUPA + byte *bufferATQA, ///< The buffer to store the ATQA (Answer to request) in + byte *bufferSize ///< Buffer size, at least two bytes. Also number of bytes returned if STATUS_OK. + ) { + byte validBits; + MFRC522::StatusCode status; + + if (bufferATQA == nullptr || *bufferSize < 2) { // The ATQA response is 2 bytes long. + return STATUS_NO_ROOM; + } + PCD_ClearRegisterBitMask(CollReg, 0x80); // ValuesAfterColl=1 => Bits received after collision are cleared. + validBits = 7; // For REQA and WUPA we need the short frame format - transmit only 7 bits of the last (and only) byte. TxLastBits = BitFramingReg[2..0] + status = PCD_TransceiveData(&command, 1, bufferATQA, bufferSize, &validBits); + if (status != STATUS_OK) { + return status; + } + if (*bufferSize != 2 || validBits != 0) { // ATQA must be exactly 16 bits. + return STATUS_ERROR; + } + return STATUS_OK; +} // End PICC_REQA_or_WUPA() + +/** + * Transmits SELECT/ANTICOLLISION commands to select a single PICC. + * Before calling this function the PICCs must be placed in the READY(*) state by calling PICC_RequestA() or PICC_WakeupA(). + * On success: + * - The chosen PICC is in state ACTIVE(*) and all other PICCs have returned to state IDLE/HALT. (Figure 7 of the ISO/IEC 14443-3 draft.) + * - The UID size and value of the chosen PICC is returned in *uid along with the SAK. + * + * A PICC UID consists of 4, 7 or 10 bytes. + * Only 4 bytes can be specified in a SELECT command, so for the longer UIDs two or three iterations are used: + * UID size Number of UID bytes Cascade levels Example of PICC + * ======== =================== ============== =============== + * single 4 1 MIFARE Classic + * double 7 2 MIFARE Ultralight + * triple 10 3 Not currently in use? + * + * @return STATUS_OK on success, STATUS_??? otherwise. + */ +MFRC522::StatusCode MFRC522::PICC_Select( Uid *uid, ///< Pointer to Uid struct. Normally output, but can also be used to supply a known UID. + byte validBits ///< The number of known UID bits supplied in *uid. Normally 0. If set you must also supply uid->size. + ) { + bool uidComplete; + bool selectDone; + bool useCascadeTag; + byte cascadeLevel = 1; + MFRC522::StatusCode result; + byte count; + byte index; + byte uidIndex; // The first index in uid->uidByte[] that is used in the current Cascade Level. + int8_t currentLevelKnownBits; // The number of known UID bits in the current Cascade Level. + byte buffer[9]; // The SELECT/ANTICOLLISION commands uses a 7 byte standard frame + 2 bytes CRC_A + byte bufferUsed; // The number of bytes used in the buffer, ie the number of bytes to transfer to the FIFO. + byte rxAlign; // Used in BitFramingReg. Defines the bit position for the first bit received. + byte txLastBits; // Used in BitFramingReg. The number of valid bits in the last transmitted byte. + byte *responseBuffer; + byte responseLength; + + // Description of buffer structure: + // Byte 0: SEL Indicates the Cascade Level: PICC_CMD_SEL_CL1, PICC_CMD_SEL_CL2 or PICC_CMD_SEL_CL3 + // Byte 1: NVB Number of Valid Bits (in complete command, not just the UID): High nibble: complete bytes, Low nibble: Extra bits. + // Byte 2: UID-data or CT See explanation below. CT means Cascade Tag. + // Byte 3: UID-data + // Byte 4: UID-data + // Byte 5: UID-data + // Byte 6: BCC Block Check Character - XOR of bytes 2-5 + // Byte 7: CRC_A + // Byte 8: CRC_A + // The BCC and CRC_A are only transmitted if we know all the UID bits of the current Cascade Level. + // + // Description of bytes 2-5: (Section 6.5.4 of the ISO/IEC 14443-3 draft: UID contents and cascade levels) + // UID size Cascade level Byte2 Byte3 Byte4 Byte5 + // ======== ============= ===== ===== ===== ===== + // 4 bytes 1 uid0 uid1 uid2 uid3 + // 7 bytes 1 CT uid0 uid1 uid2 + // 2 uid3 uid4 uid5 uid6 + // 10 bytes 1 CT uid0 uid1 uid2 + // 2 CT uid3 uid4 uid5 + // 3 uid6 uid7 uid8 uid9 + + // Sanity checks + if (validBits > 80) { + return STATUS_INVALID; + } + + // Prepare MFRC522 + PCD_ClearRegisterBitMask(CollReg, 0x80); // ValuesAfterColl=1 => Bits received after collision are cleared. + + // Repeat Cascade Level loop until we have a complete UID. + uidComplete = false; + while (!uidComplete) { + // Set the Cascade Level in the SEL byte, find out if we need to use the Cascade Tag in byte 2. + switch (cascadeLevel) { + case 1: + buffer[0] = PICC_CMD_SEL_CL1; + uidIndex = 0; + useCascadeTag = validBits && uid->size > 4; // When we know that the UID has more than 4 bytes + break; + + case 2: + buffer[0] = PICC_CMD_SEL_CL2; + uidIndex = 3; + useCascadeTag = validBits && uid->size > 7; // When we know that the UID has more than 7 bytes + break; + + case 3: + buffer[0] = PICC_CMD_SEL_CL3; + uidIndex = 6; + useCascadeTag = false; // Never used in CL3. + break; + + default: + return STATUS_INTERNAL_ERROR; + break; + } + + // How many UID bits are known in this Cascade Level? + currentLevelKnownBits = validBits - (8 * uidIndex); + if (currentLevelKnownBits < 0) { + currentLevelKnownBits = 0; + } + // Copy the known bits from uid->uidByte[] to buffer[] + index = 2; // destination index in buffer[] + if (useCascadeTag) { + buffer[index++] = PICC_CMD_CT; + } + byte bytesToCopy = currentLevelKnownBits / 8 + (currentLevelKnownBits % 8 ? 1 : 0); // The number of bytes needed to represent the known bits for this level. + if (bytesToCopy) { + byte maxBytes = useCascadeTag ? 3 : 4; // Max 4 bytes in each Cascade Level. Only 3 left if we use the Cascade Tag + if (bytesToCopy > maxBytes) { + bytesToCopy = maxBytes; + } + for (count = 0; count < bytesToCopy; count++) { + buffer[index++] = uid->uidByte[uidIndex + count]; + } + } + // Now that the data has been copied we need to include the 8 bits in CT in currentLevelKnownBits + if (useCascadeTag) { + currentLevelKnownBits += 8; + } + + // Repeat anti collision loop until we can transmit all UID bits + BCC and receive a SAK - max 32 iterations. + selectDone = false; + while (!selectDone) { + // Find out how many bits and bytes to send and receive. + if (currentLevelKnownBits >= 32) { // All UID bits in this Cascade Level are known. This is a SELECT. + //Serial.print(F("SELECT: currentLevelKnownBits=")); Serial.println(currentLevelKnownBits, DEC); + buffer[1] = 0x70; // NVB - Number of Valid Bits: Seven whole bytes + // Calculate BCC - Block Check Character + buffer[6] = buffer[2] ^ buffer[3] ^ buffer[4] ^ buffer[5]; + // Calculate CRC_A + result = PCD_CalculateCRC(buffer, 7, &buffer[7]); + if (result != STATUS_OK) { + return result; + } + txLastBits = 0; // 0 => All 8 bits are valid. + bufferUsed = 9; + // Store response in the last 3 bytes of buffer (BCC and CRC_A - not needed after tx) + responseBuffer = &buffer[6]; + responseLength = 3; + } + else { // This is an ANTICOLLISION. + //Serial.print(F("ANTICOLLISION: currentLevelKnownBits=")); Serial.println(currentLevelKnownBits, DEC); + txLastBits = currentLevelKnownBits % 8; + count = currentLevelKnownBits / 8; // Number of whole bytes in the UID part. + index = 2 + count; // Number of whole bytes: SEL + NVB + UIDs + buffer[1] = (index << 4) + txLastBits; // NVB - Number of Valid Bits + bufferUsed = index + (txLastBits ? 1 : 0); + // Store response in the unused part of buffer + responseBuffer = &buffer[index]; + responseLength = sizeof(buffer) - index; + } + + // Set bit adjustments + rxAlign = txLastBits; // Having a separate variable is overkill. But it makes the next line easier to read. + PCD_WriteRegister(BitFramingReg, (rxAlign << 4) + txLastBits); // RxAlign = BitFramingReg[6..4]. TxLastBits = BitFramingReg[2..0] + + // Transmit the buffer and receive the response. + result = PCD_TransceiveData(buffer, bufferUsed, responseBuffer, &responseLength, &txLastBits, rxAlign); + if (result == STATUS_COLLISION) { // More than one PICC in the field => collision. + byte valueOfCollReg = PCD_ReadRegister(CollReg); // CollReg[7..0] bits are: ValuesAfterColl reserved CollPosNotValid CollPos[4:0] + if (valueOfCollReg & 0x20) { // CollPosNotValid + return STATUS_COLLISION; // Without a valid collision position we cannot continue + } + byte collisionPos = valueOfCollReg & 0x1F; // Values 0-31, 0 means bit 32. + if (collisionPos == 0) { + collisionPos = 32; + } + if (collisionPos <= currentLevelKnownBits) { // No progress - should not happen + return STATUS_INTERNAL_ERROR; + } + // Choose the PICC with the bit set. + currentLevelKnownBits = collisionPos; + count = (currentLevelKnownBits - 1) % 8; // The bit to modify + index = 1 + (currentLevelKnownBits / 8) + (count ? 1 : 0); // First byte is index 0. + buffer[index] |= (1 << count); + } + else if (result != STATUS_OK) { + return result; + } + else { // STATUS_OK + if (currentLevelKnownBits >= 32) { // This was a SELECT. + selectDone = true; // No more anticollision + // We continue below outside the while. + } + else { // This was an ANTICOLLISION. + // We now have all 32 bits of the UID in this Cascade Level + currentLevelKnownBits = 32; + // Run loop again to do the SELECT. + } + } + } // End of while (!selectDone) + + // We do not check the CBB - it was constructed by us above. + + // Copy the found UID bytes from buffer[] to uid->uidByte[] + index = (buffer[2] == PICC_CMD_CT) ? 3 : 2; // source index in buffer[] + bytesToCopy = (buffer[2] == PICC_CMD_CT) ? 3 : 4; + for (count = 0; count < bytesToCopy; count++) { + uid->uidByte[uidIndex + count] = buffer[index++]; + } + + // Check response SAK (Select Acknowledge) + if (responseLength != 3 || txLastBits != 0) { // SAK must be exactly 24 bits (1 byte + CRC_A). + return STATUS_ERROR; + } + // Verify CRC_A - do our own calculation and store the control in buffer[2..3] - those bytes are not needed anymore. + result = PCD_CalculateCRC(responseBuffer, 1, &buffer[2]); + if (result != STATUS_OK) { + return result; + } + if ((buffer[2] != responseBuffer[1]) || (buffer[3] != responseBuffer[2])) { + return STATUS_CRC_WRONG; + } + if (responseBuffer[0] & 0x04) { // Cascade bit set - UID not complete yes + cascadeLevel++; + } + else { + uidComplete = true; + uid->sak = responseBuffer[0]; + } + } // End of while (!uidComplete) + + // Set correct uid->size + uid->size = 3 * cascadeLevel + 1; + + return STATUS_OK; +} // End PICC_Select() + +/** + * Instructs a PICC in state ACTIVE(*) to go to state HALT. + * + * @return STATUS_OK on success, STATUS_??? otherwise. + */ +MFRC522::StatusCode MFRC522::PICC_HaltA() { + MFRC522::StatusCode result; + byte buffer[4]; + + // Build command buffer + buffer[0] = PICC_CMD_HLTA; + buffer[1] = 0; + // Calculate CRC_A + result = PCD_CalculateCRC(buffer, 2, &buffer[2]); + if (result != STATUS_OK) { + return result; + } + + // Send the command. + // The standard says: + // If the PICC responds with any modulation during a period of 1 ms after the end of the frame containing the + // HLTA command, this response shall be interpreted as 'not acknowledge'. + // We interpret that this way: Only STATUS_TIMEOUT is a success. + result = PCD_TransceiveData(buffer, sizeof(buffer), nullptr, 0); + if (result == STATUS_TIMEOUT) { + return STATUS_OK; + } + if (result == STATUS_OK) { // That is ironically NOT ok in this case ;-) + return STATUS_ERROR; + } + return result; +} // End PICC_HaltA() + +///////////////////////////////////////////////////////////////////////////////////// +// Functions for communicating with MIFARE PICCs +///////////////////////////////////////////////////////////////////////////////////// + +/** + * Executes the MFRC522 MFAuthent command. + * This command manages MIFARE authentication to enable a secure communication to any MIFARE Mini, MIFARE 1K and MIFARE 4K card. + * The authentication is described in the MFRC522 datasheet section 10.3.1.9 and http://www.nxp.com/documents/data_sheet/MF1S503x.pdf section 10.1. + * For use with MIFARE Classic PICCs. + * The PICC must be selected - ie in state ACTIVE(*) - before calling this function. + * Remember to call PCD_StopCrypto1() after communicating with the authenticated PICC - otherwise no new communications can start. + * + * All keys are set to FFFFFFFFFFFFh at chip delivery. + * + * @return STATUS_OK on success, STATUS_??? otherwise. Probably STATUS_TIMEOUT if you supply the wrong key. + */ +MFRC522::StatusCode MFRC522::PCD_Authenticate(byte command, ///< PICC_CMD_MF_AUTH_KEY_A or PICC_CMD_MF_AUTH_KEY_B + byte blockAddr, ///< The block number. See numbering in the comments in the .h file. + MIFARE_Key *key, ///< Pointer to the Crypteo1 key to use (6 bytes) + Uid *uid ///< Pointer to Uid struct. The first 4 bytes of the UID is used. + ) { + byte waitIRq = 0x10; // IdleIRq + + // Build command buffer + byte sendData[12]; + sendData[0] = command; + sendData[1] = blockAddr; + for (byte i = 0; i < MF_KEY_SIZE; i++) { // 6 key bytes + sendData[2+i] = key->keyByte[i]; + } + // Use the last uid bytes as specified in http://cache.nxp.com/documents/application_note/AN10927.pdf + // section 3.2.5 "MIFARE Classic Authentication". + // The only missed case is the MF1Sxxxx shortcut activation, + // but it requires cascade tag (CT) byte, that is not part of uid. + for (byte i = 0; i < 4; i++) { // The last 4 bytes of the UID + sendData[8+i] = uid->uidByte[i+uid->size-4]; + } + + // Start the authentication. + return PCD_CommunicateWithPICC(PCD_MFAuthent, waitIRq, &sendData[0], sizeof(sendData)); +} // End PCD_Authenticate() + +/** + * Used to exit the PCD from its authenticated state. + * Remember to call this function after communicating with an authenticated PICC - otherwise no new communications can start. + */ +void MFRC522::PCD_StopCrypto1() { + // Clear MFCrypto1On bit + PCD_ClearRegisterBitMask(Status2Reg, 0x08); // Status2Reg[7..0] bits are: TempSensClear I2CForceHS reserved reserved MFCrypto1On ModemState[2:0] +} // End PCD_StopCrypto1() + +/** + * Reads 16 bytes (+ 2 bytes CRC_A) from the active PICC. + * + * For MIFARE Classic the sector containing the block must be authenticated before calling this function. + * + * For MIFARE Ultralight only addresses 00h to 0Fh are decoded. + * The MF0ICU1 returns a NAK for higher addresses. + * The MF0ICU1 responds to the READ command by sending 16 bytes starting from the page address defined by the command argument. + * For example; if blockAddr is 03h then pages 03h, 04h, 05h, 06h are returned. + * A roll-back is implemented: If blockAddr is 0Eh, then the contents of pages 0Eh, 0Fh, 00h and 01h are returned. + * + * The buffer must be at least 18 bytes because a CRC_A is also returned. + * Checks the CRC_A before returning STATUS_OK. + * + * @return STATUS_OK on success, STATUS_??? otherwise. + */ +MFRC522::StatusCode MFRC522::MIFARE_Read( byte blockAddr, ///< MIFARE Classic: The block (0-0xff) number. MIFARE Ultralight: The first page to return data from. + byte *buffer, ///< The buffer to store the data in + byte *bufferSize ///< Buffer size, at least 18 bytes. Also number of bytes returned if STATUS_OK. + ) { + MFRC522::StatusCode result; + + // Sanity check + if (buffer == nullptr || *bufferSize < 18) { + return STATUS_NO_ROOM; + } + + // Build command buffer + buffer[0] = PICC_CMD_MF_READ; + buffer[1] = blockAddr; + // Calculate CRC_A + result = PCD_CalculateCRC(buffer, 2, &buffer[2]); + if (result != STATUS_OK) { + return result; + } + + // Transmit the buffer and receive the response, validate CRC_A. + return PCD_TransceiveData(buffer, 4, buffer, bufferSize, nullptr, 0, true); +} // End MIFARE_Read() + +/** + * Writes 16 bytes to the active PICC. + * + * For MIFARE Classic the sector containing the block must be authenticated before calling this function. + * + * For MIFARE Ultralight the operation is called "COMPATIBILITY WRITE". + * Even though 16 bytes are transferred to the Ultralight PICC, only the least significant 4 bytes (bytes 0 to 3) + * are written to the specified address. It is recommended to set the remaining bytes 04h to 0Fh to all logic 0. + * * + * @return STATUS_OK on success, STATUS_??? otherwise. + */ +MFRC522::StatusCode MFRC522::MIFARE_Write( byte blockAddr, ///< MIFARE Classic: The block (0-0xff) number. MIFARE Ultralight: The page (2-15) to write to. + byte *buffer, ///< The 16 bytes to write to the PICC + byte bufferSize ///< Buffer size, must be at least 16 bytes. Exactly 16 bytes are written. + ) { + MFRC522::StatusCode result; + + // Sanity check + if (buffer == nullptr || bufferSize < 16) { + return STATUS_INVALID; + } + + // Mifare Classic protocol requires two communications to perform a write. + // Step 1: Tell the PICC we want to write to block blockAddr. + byte cmdBuffer[2]; + cmdBuffer[0] = PICC_CMD_MF_WRITE; + cmdBuffer[1] = blockAddr; + result = PCD_MIFARE_Transceive(cmdBuffer, 2); // Adds CRC_A and checks that the response is MF_ACK. + if (result != STATUS_OK) { + return result; + } + + // Step 2: Transfer the data + result = PCD_MIFARE_Transceive(buffer, bufferSize); // Adds CRC_A and checks that the response is MF_ACK. + if (result != STATUS_OK) { + return result; + } + + return STATUS_OK; +} // End MIFARE_Write() + +/** + * Writes a 4 byte page to the active MIFARE Ultralight PICC. + * + * @return STATUS_OK on success, STATUS_??? otherwise. + */ +MFRC522::StatusCode MFRC522::MIFARE_Ultralight_Write( byte page, ///< The page (2-15) to write to. + byte *buffer, ///< The 4 bytes to write to the PICC + byte bufferSize ///< Buffer size, must be at least 4 bytes. Exactly 4 bytes are written. + ) { + MFRC522::StatusCode result; + + // Sanity check + if (buffer == nullptr || bufferSize < 4) { + return STATUS_INVALID; + } + + // Build commmand buffer + byte cmdBuffer[6]; + cmdBuffer[0] = PICC_CMD_UL_WRITE; + cmdBuffer[1] = page; + memcpy(&cmdBuffer[2], buffer, 4); + + // Perform the write + result = PCD_MIFARE_Transceive(cmdBuffer, 6); // Adds CRC_A and checks that the response is MF_ACK. + if (result != STATUS_OK) { + return result; + } + return STATUS_OK; +} // End MIFARE_Ultralight_Write() + +/** + * MIFARE Decrement subtracts the delta from the value of the addressed block, and stores the result in a volatile memory. + * For MIFARE Classic only. The sector containing the block must be authenticated before calling this function. + * Only for blocks in "value block" mode, ie with access bits [C1 C2 C3] = [110] or [001]. + * Use MIFARE_Transfer() to store the result in a block. + * + * @return STATUS_OK on success, STATUS_??? otherwise. + */ +MFRC522::StatusCode MFRC522::MIFARE_Decrement( byte blockAddr, ///< The block (0-0xff) number. + int32_t delta ///< This number is subtracted from the value of block blockAddr. + ) { + return MIFARE_TwoStepHelper(PICC_CMD_MF_DECREMENT, blockAddr, delta); +} // End MIFARE_Decrement() + +/** + * MIFARE Increment adds the delta to the value of the addressed block, and stores the result in a volatile memory. + * For MIFARE Classic only. The sector containing the block must be authenticated before calling this function. + * Only for blocks in "value block" mode, ie with access bits [C1 C2 C3] = [110] or [001]. + * Use MIFARE_Transfer() to store the result in a block. + * + * @return STATUS_OK on success, STATUS_??? otherwise. + */ +MFRC522::StatusCode MFRC522::MIFARE_Increment( byte blockAddr, ///< The block (0-0xff) number. + int32_t delta ///< This number is added to the value of block blockAddr. + ) { + return MIFARE_TwoStepHelper(PICC_CMD_MF_INCREMENT, blockAddr, delta); +} // End MIFARE_Increment() + +/** + * MIFARE Restore copies the value of the addressed block into a volatile memory. + * For MIFARE Classic only. The sector containing the block must be authenticated before calling this function. + * Only for blocks in "value block" mode, ie with access bits [C1 C2 C3] = [110] or [001]. + * Use MIFARE_Transfer() to store the result in a block. + * + * @return STATUS_OK on success, STATUS_??? otherwise. + */ +MFRC522::StatusCode MFRC522::MIFARE_Restore( byte blockAddr ///< The block (0-0xff) number. + ) { + // The datasheet describes Restore as a two step operation, but does not explain what data to transfer in step 2. + // Doing only a single step does not work, so I chose to transfer 0L in step two. + return MIFARE_TwoStepHelper(PICC_CMD_MF_RESTORE, blockAddr, 0L); +} // End MIFARE_Restore() + +/** + * Helper function for the two-step MIFARE Classic protocol operations Decrement, Increment and Restore. + * + * @return STATUS_OK on success, STATUS_??? otherwise. + */ +MFRC522::StatusCode MFRC522::MIFARE_TwoStepHelper( byte command, ///< The command to use + byte blockAddr, ///< The block (0-0xff) number. + int32_t data ///< The data to transfer in step 2 + ) { + MFRC522::StatusCode result; + byte cmdBuffer[2]; // We only need room for 2 bytes. + + // Step 1: Tell the PICC the command and block address + cmdBuffer[0] = command; + cmdBuffer[1] = blockAddr; + result = PCD_MIFARE_Transceive( cmdBuffer, 2); // Adds CRC_A and checks that the response is MF_ACK. + if (result != STATUS_OK) { + return result; + } + + // Step 2: Transfer the data + result = PCD_MIFARE_Transceive( (byte *)&data, 4, true); // Adds CRC_A and accept timeout as success. + if (result != STATUS_OK) { + return result; + } + + return STATUS_OK; +} // End MIFARE_TwoStepHelper() + +/** + * MIFARE Transfer writes the value stored in the volatile memory into one MIFARE Classic block. + * For MIFARE Classic only. The sector containing the block must be authenticated before calling this function. + * Only for blocks in "value block" mode, ie with access bits [C1 C2 C3] = [110] or [001]. + * + * @return STATUS_OK on success, STATUS_??? otherwise. + */ +MFRC522::StatusCode MFRC522::MIFARE_Transfer( byte blockAddr ///< The block (0-0xff) number. + ) { + MFRC522::StatusCode result; + byte cmdBuffer[2]; // We only need room for 2 bytes. + + // Tell the PICC we want to transfer the result into block blockAddr. + cmdBuffer[0] = PICC_CMD_MF_TRANSFER; + cmdBuffer[1] = blockAddr; + result = PCD_MIFARE_Transceive( cmdBuffer, 2); // Adds CRC_A and checks that the response is MF_ACK. + if (result != STATUS_OK) { + return result; + } + return STATUS_OK; +} // End MIFARE_Transfer() + +/** + * Helper routine to read the current value from a Value Block. + * + * Only for MIFARE Classic and only for blocks in "value block" mode, that + * is: with access bits [C1 C2 C3] = [110] or [001]. The sector containing + * the block must be authenticated before calling this function. + * + * @param[in] blockAddr The block (0x00-0xff) number. + * @param[out] value Current value of the Value Block. + * @return STATUS_OK on success, STATUS_??? otherwise. + */ +MFRC522::StatusCode MFRC522::MIFARE_GetValue(byte blockAddr, int32_t *value) { + MFRC522::StatusCode status; + byte buffer[18]; + byte size = sizeof(buffer); + + // Read the block + status = MIFARE_Read(blockAddr, buffer, &size); + if (status == STATUS_OK) { + // Extract the value + *value = (int32_t(buffer[3])<<24) | (int32_t(buffer[2])<<16) | (int32_t(buffer[1])<<8) | int32_t(buffer[0]); + } + return status; +} // End MIFARE_GetValue() + +/** + * Helper routine to write a specific value into a Value Block. + * + * Only for MIFARE Classic and only for blocks in "value block" mode, that + * is: with access bits [C1 C2 C3] = [110] or [001]. The sector containing + * the block must be authenticated before calling this function. + * + * @param[in] blockAddr The block (0x00-0xff) number. + * @param[in] value New value of the Value Block. + * @return STATUS_OK on success, STATUS_??? otherwise. + */ +MFRC522::StatusCode MFRC522::MIFARE_SetValue(byte blockAddr, int32_t value) { + byte buffer[18]; + + // Translate the int32_t into 4 bytes; repeated 2x in value block + buffer[0] = buffer[ 8] = (value & 0xFF); + buffer[1] = buffer[ 9] = (value & 0xFF00) >> 8; + buffer[2] = buffer[10] = (value & 0xFF0000) >> 16; + buffer[3] = buffer[11] = (value & 0xFF000000) >> 24; + // Inverse 4 bytes also found in value block + buffer[4] = ~buffer[0]; + buffer[5] = ~buffer[1]; + buffer[6] = ~buffer[2]; + buffer[7] = ~buffer[3]; + // Address 2x with inverse address 2x + buffer[12] = buffer[14] = blockAddr; + buffer[13] = buffer[15] = ~blockAddr; + + // Write the whole data block + return MIFARE_Write(blockAddr, buffer, 16); +} // End MIFARE_SetValue() + +/** + * Authenticate with a NTAG216. + * + * Only for NTAG216. First implemented by Gargantuanman. + * + * @param[in] passWord password. + * @param[in] pACK result success???. + * @return STATUS_OK on success, STATUS_??? otherwise. + */ +MFRC522::StatusCode MFRC522::PCD_NTAG216_AUTH(byte* passWord, byte pACK[]) //Authenticate with 32bit password +{ + // TODO: Fix cmdBuffer length and rxlength. They really should match. + // (Better still, rxlength should not even be necessary.) + + MFRC522::StatusCode result; + byte cmdBuffer[18]; // We need room for 16 bytes data and 2 bytes CRC_A. + + cmdBuffer[0] = 0x1B; //Comando de autentificacion + + for (byte i = 0; i<4; i++) + cmdBuffer[i+1] = passWord[i]; + + result = PCD_CalculateCRC(cmdBuffer, 5, &cmdBuffer[5]); + + if (result!=STATUS_OK) { + return result; + } + + // Transceive the data, store the reply in cmdBuffer[] + byte waitIRq = 0x30; // RxIRq and IdleIRq +// byte cmdBufferSize = sizeof(cmdBuffer); + byte validBits = 0; + byte rxlength = 5; + result = PCD_CommunicateWithPICC(PCD_Transceive, waitIRq, cmdBuffer, 7, cmdBuffer, &rxlength, &validBits); + + pACK[0] = cmdBuffer[0]; + pACK[1] = cmdBuffer[1]; + + if (result!=STATUS_OK) { + return result; + } + + return STATUS_OK; +} // End PCD_NTAG216_AUTH() + + +///////////////////////////////////////////////////////////////////////////////////// +// Support functions +///////////////////////////////////////////////////////////////////////////////////// + +/** + * Wrapper for MIFARE protocol communication. + * Adds CRC_A, executes the Transceive command and checks that the response is MF_ACK or a timeout. + * + * @return STATUS_OK on success, STATUS_??? otherwise. + */ +MFRC522::StatusCode MFRC522::PCD_MIFARE_Transceive( byte *sendData, ///< Pointer to the data to transfer to the FIFO. Do NOT include the CRC_A. + byte sendLen, ///< Number of bytes in sendData. + bool acceptTimeout ///< True => A timeout is also success + ) { + MFRC522::StatusCode result; + byte cmdBuffer[18]; // We need room for 16 bytes data and 2 bytes CRC_A. + + // Sanity check + if (sendData == nullptr || sendLen > 16) { + return STATUS_INVALID; + } + + // Copy sendData[] to cmdBuffer[] and add CRC_A + memcpy(cmdBuffer, sendData, sendLen); + result = PCD_CalculateCRC(cmdBuffer, sendLen, &cmdBuffer[sendLen]); + if (result != STATUS_OK) { + return result; + } + sendLen += 2; + + // Transceive the data, store the reply in cmdBuffer[] + byte waitIRq = 0x30; // RxIRq and IdleIRq + byte cmdBufferSize = sizeof(cmdBuffer); + byte validBits = 0; + result = PCD_CommunicateWithPICC(PCD_Transceive, waitIRq, cmdBuffer, sendLen, cmdBuffer, &cmdBufferSize, &validBits); + if (acceptTimeout && result == STATUS_TIMEOUT) { + return STATUS_OK; + } + if (result != STATUS_OK) { + return result; + } + // The PICC must reply with a 4 bit ACK + if (cmdBufferSize != 1 || validBits != 4) { + return STATUS_ERROR; + } + if (cmdBuffer[0] != MF_ACK) { + return STATUS_MIFARE_NACK; + } + return STATUS_OK; +} // End PCD_MIFARE_Transceive() + +/** + * Returns a __FlashStringHelper pointer to a status code name. + * + * @return const __FlashStringHelper * + */ +const __FlashStringHelper *MFRC522::GetStatusCodeName(MFRC522::StatusCode code ///< One of the StatusCode enums. + ) { + return MFRC522Debug::GetStatusCodeName(code); +} // End GetStatusCodeName() + +/** + * Translates the SAK (Select Acknowledge) to a PICC type. + * + * @return PICC_Type + */ +MFRC522::PICC_Type MFRC522::PICC_GetType(byte sak ///< The SAK byte returned from PICC_Select(). + ) { + // http://www.nxp.com/documents/application_note/AN10833.pdf + // 3.2 Coding of Select Acknowledge (SAK) + // ignore 8-bit (iso14443 starts with LSBit = bit 1) + // fixes wrong type for manufacturer Infineon (http://nfc-tools.org/index.php?title=ISO14443A) + sak &= 0x7F; + switch (sak) { + case 0x04: return PICC_TYPE_NOT_COMPLETE; // UID not complete + case 0x09: return PICC_TYPE_MIFARE_MINI; + case 0x08: return PICC_TYPE_MIFARE_1K; + case 0x18: return PICC_TYPE_MIFARE_4K; + case 0x00: return PICC_TYPE_MIFARE_UL; + case 0x10: + case 0x11: return PICC_TYPE_MIFARE_PLUS; + case 0x01: return PICC_TYPE_TNP3XXX; + case 0x20: return PICC_TYPE_ISO_14443_4; + case 0x40: return PICC_TYPE_ISO_18092; + default: return PICC_TYPE_UNKNOWN; + } +} // End PICC_GetType() + +/** + * Returns a __FlashStringHelper pointer to the PICC type name. + * + * @return const __FlashStringHelper * + */ +const __FlashStringHelper *MFRC522::PICC_GetTypeName(PICC_Type piccType ///< One of the PICC_Type enums. + ) { + return MFRC522Debug::PICC_GetTypeName(piccType); +} // End PICC_GetTypeName() + +/** + * Dumps debug info about the connected PCD to Serial. + * Shows all known firmware versions + */ +void MFRC522::PCD_DumpVersionToSerial() { + // Get the MFRC522 firmware version + byte v = PCD_ReadRegister(VersionReg); + Serial.print(F("Firmware Version: 0x")); + Serial.print(v, HEX); + // Lookup which version + switch(v) { + case 0x88: Serial.println(F(" = (clone)")); break; + case 0x90: Serial.println(F(" = v0.0")); break; + case 0x91: Serial.println(F(" = v1.0")); break; + case 0x92: Serial.println(F(" = v2.0")); break; + default: Serial.println(F(" = (unknown)")); + } + // When 0x00 or 0xFF is returned, communication probably failed + if ((v == 0x00) || (v == 0xFF)) + Serial.println(F("WARNING: Communication failure, is the MFRC522 properly connected?")); +} // End PCD_DumpVersionToSerial() + +/** + * Dumps debug info about the selected PICC to Serial. + * On success the PICC is halted after dumping the data. + * For MIFARE Classic the factory default key of 0xFFFFFFFFFFFF is tried. + * + * @DEPRECATED Kept for bakward compatibility + */ +void MFRC522::PICC_DumpToSerial(Uid *uid ///< Pointer to Uid struct returned from a successful PICC_Select(). + ) { + MIFARE_Key key; + + // Dump UID, SAK and Type + PICC_DumpDetailsToSerial(uid); + + // Dump contents + PICC_Type piccType = PICC_GetType(uid->sak); + switch (piccType) { + case PICC_TYPE_MIFARE_MINI: + case PICC_TYPE_MIFARE_1K: + case PICC_TYPE_MIFARE_4K: + // All keys are set to FFFFFFFFFFFFh at chip delivery from the factory. + for (byte i = 0; i < 6; i++) { + key.keyByte[i] = 0xFF; + } + PICC_DumpMifareClassicToSerial(uid, piccType, &key); + break; + + case PICC_TYPE_MIFARE_UL: + PICC_DumpMifareUltralightToSerial(); + break; + + case PICC_TYPE_ISO_14443_4: + case PICC_TYPE_MIFARE_DESFIRE: + case PICC_TYPE_ISO_18092: + case PICC_TYPE_MIFARE_PLUS: + case PICC_TYPE_TNP3XXX: + Serial.println(F("Dumping memory contents not implemented for that PICC type.")); + break; + + case PICC_TYPE_UNKNOWN: + case PICC_TYPE_NOT_COMPLETE: + default: + break; // No memory dump here + } + + Serial.println(); + PICC_HaltA(); // Already done if it was a MIFARE Classic PICC. +} // End PICC_DumpToSerial() + +/** + * Dumps card info (UID,SAK,Type) about the selected PICC to Serial. + * + * @DEPRECATED kept for backward compatibility + */ +void MFRC522::PICC_DumpDetailsToSerial(Uid *uid ///< Pointer to Uid struct returned from a successful PICC_Select(). + ) { + // UID + Serial.print(F("Card UID:")); + for (byte i = 0; i < uid->size; i++) { + if(uid->uidByte[i] < 0x10) + Serial.print(F(" 0")); + else + Serial.print(F(" ")); + Serial.print(uid->uidByte[i], HEX); + } + Serial.println(); + + // SAK + Serial.print(F("Card SAK: ")); + if(uid->sak < 0x10) + Serial.print(F("0")); + Serial.println(uid->sak, HEX); + + // (suggested) PICC type + PICC_Type piccType = PICC_GetType(uid->sak); + Serial.print(F("PICC type: ")); + Serial.println(PICC_GetTypeName(piccType)); +} // End PICC_DumpDetailsToSerial() + +/** + * Dumps memory contents of a MIFARE Classic PICC. + * On success the PICC is halted after dumping the data. + */ +void MFRC522::PICC_DumpMifareClassicToSerial( Uid *uid, ///< Pointer to Uid struct returned from a successful PICC_Select(). + PICC_Type piccType, ///< One of the PICC_Type enums. + MIFARE_Key *key ///< Key A used for all sectors. + ) { + byte no_of_sectors = 0; + switch (piccType) { + case PICC_TYPE_MIFARE_MINI: + // Has 5 sectors * 4 blocks/sector * 16 bytes/block = 320 bytes. + no_of_sectors = 5; + break; + + case PICC_TYPE_MIFARE_1K: + // Has 16 sectors * 4 blocks/sector * 16 bytes/block = 1024 bytes. + no_of_sectors = 16; + break; + + case PICC_TYPE_MIFARE_4K: + // Has (32 sectors * 4 blocks/sector + 8 sectors * 16 blocks/sector) * 16 bytes/block = 4096 bytes. + no_of_sectors = 40; + break; + + default: // Should not happen. Ignore. + break; + } + + // Dump sectors, highest address first. + if (no_of_sectors) { + Serial.println(F("Sector Block 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 AccessBits")); + for (int8_t i = no_of_sectors - 1; i >= 0; i--) { + PICC_DumpMifareClassicSectorToSerial(uid, key, i); + } + } + PICC_HaltA(); // Halt the PICC before stopping the encrypted session. + PCD_StopCrypto1(); +} // End PICC_DumpMifareClassicToSerial() + +/** + * Dumps memory contents of a sector of a MIFARE Classic PICC. + * Uses PCD_Authenticate(), MIFARE_Read() and PCD_StopCrypto1. + * Always uses PICC_CMD_MF_AUTH_KEY_A because only Key A can always read the sector trailer access bits. + */ +void MFRC522::PICC_DumpMifareClassicSectorToSerial(Uid *uid, ///< Pointer to Uid struct returned from a successful PICC_Select(). + MIFARE_Key *key, ///< Key A for the sector. + byte sector ///< The sector to dump, 0..39. + ) { + MFRC522::StatusCode status; + byte firstBlock; // Address of lowest address to dump actually last block dumped) + byte no_of_blocks; // Number of blocks in sector + bool isSectorTrailer; // Set to true while handling the "last" (ie highest address) in the sector. + + // The access bits are stored in a peculiar fashion. + // There are four groups: + // g[3] Access bits for the sector trailer, block 3 (for sectors 0-31) or block 15 (for sectors 32-39) + // g[2] Access bits for block 2 (for sectors 0-31) or blocks 10-14 (for sectors 32-39) + // g[1] Access bits for block 1 (for sectors 0-31) or blocks 5-9 (for sectors 32-39) + // g[0] Access bits for block 0 (for sectors 0-31) or blocks 0-4 (for sectors 32-39) + // Each group has access bits [C1 C2 C3]. In this code C1 is MSB and C3 is LSB. + // The four CX bits are stored together in a nible cx and an inverted nible cx_. + byte c1, c2, c3; // Nibbles + byte c1_, c2_, c3_; // Inverted nibbles + bool invertedError; // True if one of the inverted nibbles did not match + byte g[4]; // Access bits for each of the four groups. + byte group; // 0-3 - active group for access bits + bool firstInGroup; // True for the first block dumped in the group + + // Determine position and size of sector. + if (sector < 32) { // Sectors 0..31 has 4 blocks each + no_of_blocks = 4; + firstBlock = sector * no_of_blocks; + } + else if (sector < 40) { // Sectors 32-39 has 16 blocks each + no_of_blocks = 16; + firstBlock = 128 + (sector - 32) * no_of_blocks; + } + else { // Illegal input, no MIFARE Classic PICC has more than 40 sectors. + return; + } + + // Dump blocks, highest address first. + byte byteCount; + byte buffer[18]; + byte blockAddr; + isSectorTrailer = true; + invertedError = false; // Avoid "unused variable" warning. + for (int8_t blockOffset = no_of_blocks - 1; blockOffset >= 0; blockOffset--) { + blockAddr = firstBlock + blockOffset; + // Sector number - only on first line + if (isSectorTrailer) { + if(sector < 10) + Serial.print(F(" ")); // Pad with spaces + else + Serial.print(F(" ")); // Pad with spaces + Serial.print(sector); + Serial.print(F(" ")); + } + else { + Serial.print(F(" ")); + } + // Block number + if(blockAddr < 10) + Serial.print(F(" ")); // Pad with spaces + else { + if(blockAddr < 100) + Serial.print(F(" ")); // Pad with spaces + else + Serial.print(F(" ")); // Pad with spaces + } + Serial.print(blockAddr); + Serial.print(F(" ")); + // Establish encrypted communications before reading the first block + if (isSectorTrailer) { + status = PCD_Authenticate(PICC_CMD_MF_AUTH_KEY_A, firstBlock, key, uid); + if (status != STATUS_OK) { + Serial.print(F("PCD_Authenticate() failed: ")); + Serial.println(GetStatusCodeName(status)); + return; + } + } + // Read block + byteCount = sizeof(buffer); + status = MIFARE_Read(blockAddr, buffer, &byteCount); + if (status != STATUS_OK) { + Serial.print(F("MIFARE_Read() failed: ")); + Serial.println(GetStatusCodeName(status)); + continue; + } + // Dump data + for (byte index = 0; index < 16; index++) { + if(buffer[index] < 0x10) + Serial.print(F(" 0")); + else + Serial.print(F(" ")); + Serial.print(buffer[index], HEX); + if ((index % 4) == 3) { + Serial.print(F(" ")); + } + } + // Parse sector trailer data + if (isSectorTrailer) { + c1 = buffer[7] >> 4; + c2 = buffer[8] & 0xF; + c3 = buffer[8] >> 4; + c1_ = buffer[6] & 0xF; + c2_ = buffer[6] >> 4; + c3_ = buffer[7] & 0xF; + invertedError = (c1 != (~c1_ & 0xF)) || (c2 != (~c2_ & 0xF)) || (c3 != (~c3_ & 0xF)); + g[0] = ((c1 & 1) << 2) | ((c2 & 1) << 1) | ((c3 & 1) << 0); + g[1] = ((c1 & 2) << 1) | ((c2 & 2) << 0) | ((c3 & 2) >> 1); + g[2] = ((c1 & 4) << 0) | ((c2 & 4) >> 1) | ((c3 & 4) >> 2); + g[3] = ((c1 & 8) >> 1) | ((c2 & 8) >> 2) | ((c3 & 8) >> 3); + isSectorTrailer = false; + } + + // Which access group is this block in? + if (no_of_blocks == 4) { + group = blockOffset; + firstInGroup = true; + } + else { + group = blockOffset / 5; + firstInGroup = (group == 3) || (group != (blockOffset + 1) / 5); + } + + if (firstInGroup) { + // Print access bits + Serial.print(F(" [ ")); + Serial.print((g[group] >> 2) & 1, DEC); Serial.print(F(" ")); + Serial.print((g[group] >> 1) & 1, DEC); Serial.print(F(" ")); + Serial.print((g[group] >> 0) & 1, DEC); + Serial.print(F(" ] ")); + if (invertedError) { + Serial.print(F(" Inverted access bits did not match! ")); + } + } + + if (group != 3 && (g[group] == 1 || g[group] == 6)) { // Not a sector trailer, a value block + int32_t value = (int32_t(buffer[3])<<24) | (int32_t(buffer[2])<<16) | (int32_t(buffer[1])<<8) | int32_t(buffer[0]); + Serial.print(F(" Value=0x")); Serial.print(value, HEX); + Serial.print(F(" Adr=0x")); Serial.print(buffer[12], HEX); + } + Serial.println(); + } + + return; +} // End PICC_DumpMifareClassicSectorToSerial() + +/** + * Dumps memory contents of a MIFARE Ultralight PICC. + */ +void MFRC522::PICC_DumpMifareUltralightToSerial() { + MFRC522::StatusCode status; + byte byteCount; + byte buffer[18]; + byte i; + + Serial.println(F("Page 0 1 2 3")); + // Try the mpages of the original Ultralight. Ultralight C has more pages. + for (byte page = 0; page < 16; page +=4) { // Read returns data for 4 pages at a time. + // Read pages + byteCount = sizeof(buffer); + status = MIFARE_Read(page, buffer, &byteCount); + if (status != STATUS_OK) { + Serial.print(F("MIFARE_Read() failed: ")); + Serial.println(GetStatusCodeName(status)); + break; + } + // Dump data + for (byte offset = 0; offset < 4; offset++) { + i = page + offset; + if(i < 10) + Serial.print(F(" ")); // Pad with spaces + else + Serial.print(F(" ")); // Pad with spaces + Serial.print(i); + Serial.print(F(" ")); + for (byte index = 0; index < 4; index++) { + i = 4 * offset + index; + if(buffer[i] < 0x10) + Serial.print(F(" 0")); + else + Serial.print(F(" ")); + Serial.print(buffer[i], HEX); + } + Serial.println(); + } + } +} // End PICC_DumpMifareUltralightToSerial() + +/** + * Calculates the bit pattern needed for the specified access bits. In the [C1 C2 C3] tuples C1 is MSB (=4) and C3 is LSB (=1). + */ +void MFRC522::MIFARE_SetAccessBits( byte *accessBitBuffer, ///< Pointer to byte 6, 7 and 8 in the sector trailer. Bytes [0..2] will be set. + byte g0, ///< Access bits [C1 C2 C3] for block 0 (for sectors 0-31) or blocks 0-4 (for sectors 32-39) + byte g1, ///< Access bits C1 C2 C3] for block 1 (for sectors 0-31) or blocks 5-9 (for sectors 32-39) + byte g2, ///< Access bits C1 C2 C3] for block 2 (for sectors 0-31) or blocks 10-14 (for sectors 32-39) + byte g3 ///< Access bits C1 C2 C3] for the sector trailer, block 3 (for sectors 0-31) or block 15 (for sectors 32-39) + ) { + byte c1 = ((g3 & 4) << 1) | ((g2 & 4) << 0) | ((g1 & 4) >> 1) | ((g0 & 4) >> 2); + byte c2 = ((g3 & 2) << 2) | ((g2 & 2) << 1) | ((g1 & 2) << 0) | ((g0 & 2) >> 1); + byte c3 = ((g3 & 1) << 3) | ((g2 & 1) << 2) | ((g1 & 1) << 1) | ((g0 & 1) << 0); + + accessBitBuffer[0] = (~c2 & 0xF) << 4 | (~c1 & 0xF); + accessBitBuffer[1] = c1 << 4 | (~c3 & 0xF); + accessBitBuffer[2] = c3 << 4 | c2; +} // End MIFARE_SetAccessBits() + +///////////////////////////////////////////////////////////////////////////////////// +// Convenience functions - does not add extra functionality +///////////////////////////////////////////////////////////////////////////////////// + +/** + * Returns true if a PICC responds to PICC_CMD_REQA. + * Only "new" cards in state IDLE are invited. Sleeping cards in state HALT are ignored. + * + * @return bool + */ +bool MFRC522::PICC_IsNewCardPresent() { + byte bufferATQA[2]; + byte bufferSize = sizeof(bufferATQA); + + // Reset baud rates + PCD_WriteRegister(TxModeReg, 0x00); + PCD_WriteRegister(RxModeReg, 0x00); + // Reset ModWidthReg + PCD_WriteRegister(ModWidthReg, 0x26); + + MFRC522::StatusCode result = PICC_RequestA(bufferATQA, &bufferSize); + return (result == STATUS_OK || result == STATUS_COLLISION); +} // End PICC_IsNewCardPresent() + +/** + * Simple wrapper around PICC_Select. + * Returns true if a UID could be read. + * Remember to call PICC_IsNewCardPresent(), PICC_RequestA() or PICC_WakeupA() first. + * The read UID is available in the class variable uid. + * + * @return bool + */ +bool MFRC522::PICC_ReadCardSerial() { + MFRC522::StatusCode result = PICC_Select(&uid); + return (result == STATUS_OK); +} // End diff --git a/libraries/MFRC522/MFRC522.h b/libraries/MFRC522/MFRC522.h new file mode 100644 index 0000000..6a3a54f --- /dev/null +++ b/libraries/MFRC522/MFRC522.h @@ -0,0 +1,435 @@ +/** + * MFRC522.h - Library to use ARDUINO RFID MODULE KIT 13.56 MHZ WITH TAGS SPI W AND R BY COOQROBOT. + * Based on code Dr.Leong ( WWW.B2CQSHOP.COM ) + * Created by Miguel Balboa (circuitito.com), Jan, 2012. + * Rewritten by Søren Thing Andersen (access.thing.dk), fall of 2013 (Translation to English, refactored, comments, anti collision, cascade levels.) + * Extended by Tom Clement with functionality to write to sector 0 of UID changeable Mifare cards. + * Released into the public domain. + * + * Please read this file for an overview and then MFRC522.cpp for comments on the specific functions. + * Search for "mf-rc522" on ebay.com to purchase the MF-RC522 board. + * + * There are three hardware components involved: + * 1) The micro controller: An Arduino + * 2) The PCD (short for Proximity Coupling Device): NXP MFRC522 Contactless Reader IC + * 3) The PICC (short for Proximity Integrated Circuit Card): A card or tag using the ISO 14443A interface, eg Mifare or NTAG203. + * + * The microcontroller and card reader uses SPI for communication. + * The protocol is described in the MFRC522 datasheet: http://www.nxp.com/documents/data_sheet/MFRC522.pdf + * + * The card reader and the tags communicate using a 13.56MHz electromagnetic field. + * The protocol is defined in ISO/IEC 14443-3 Identification cards -- Contactless integrated circuit cards -- Proximity cards -- Part 3: Initialization and anticollision". + * A free version of the final draft can be found at http://wg8.de/wg8n1496_17n3613_Ballot_FCD14443-3.pdf + * Details are found in chapter 6, Type A – Initialization and anticollision. + * + * If only the PICC UID is wanted, the above documents has all the needed information. + * To read and write from MIFARE PICCs, the MIFARE protocol is used after the PICC has been selected. + * The MIFARE Classic chips and protocol is described in the datasheets: + * 1K: http://www.mouser.com/ds/2/302/MF1S503x-89574.pdf + * 4K: http://datasheet.octopart.com/MF1S7035DA4,118-NXP-Semiconductors-datasheet-11046188.pdf + * Mini: http://www.idcardmarket.com/download/mifare_S20_datasheet.pdf + * The MIFARE Ultralight chip and protocol is described in the datasheets: + * Ultralight: http://www.nxp.com/documents/data_sheet/MF0ICU1.pdf + * Ultralight C: http://www.nxp.com/documents/short_data_sheet/MF0ICU2_SDS.pdf + * + * MIFARE Classic 1K (MF1S503x): + * Has 16 sectors * 4 blocks/sector * 16 bytes/block = 1024 bytes. + * The blocks are numbered 0-63. + * Block 3 in each sector is the Sector Trailer. See http://www.mouser.com/ds/2/302/MF1S503x-89574.pdf sections 8.6 and 8.7: + * Bytes 0-5: Key A + * Bytes 6-8: Access Bits + * Bytes 9: User data + * Bytes 10-15: Key B (or user data) + * Block 0 is read-only manufacturer data. + * To access a block, an authentication using a key from the block's sector must be performed first. + * Example: To read from block 10, first authenticate using a key from sector 3 (blocks 8-11). + * All keys are set to FFFFFFFFFFFFh at chip delivery. + * Warning: Please read section 8.7 "Memory Access". It includes this text: if the PICC detects a format violation the whole sector is irreversibly blocked. + * To use a block in "value block" mode (for Increment/Decrement operations) you need to change the sector trailer. Use PICC_SetAccessBits() to calculate the bit patterns. + * MIFARE Classic 4K (MF1S703x): + * Has (32 sectors * 4 blocks/sector + 8 sectors * 16 blocks/sector) * 16 bytes/block = 4096 bytes. + * The blocks are numbered 0-255. + * The last block in each sector is the Sector Trailer like above. + * MIFARE Classic Mini (MF1 IC S20): + * Has 5 sectors * 4 blocks/sector * 16 bytes/block = 320 bytes. + * The blocks are numbered 0-19. + * The last block in each sector is the Sector Trailer like above. + * + * MIFARE Ultralight (MF0ICU1): + * Has 16 pages of 4 bytes = 64 bytes. + * Pages 0 + 1 is used for the 7-byte UID. + * Page 2 contains the last check digit for the UID, one byte manufacturer internal data, and the lock bytes (see http://www.nxp.com/documents/data_sheet/MF0ICU1.pdf section 8.5.2) + * Page 3 is OTP, One Time Programmable bits. Once set to 1 they cannot revert to 0. + * Pages 4-15 are read/write unless blocked by the lock bytes in page 2. + * MIFARE Ultralight C (MF0ICU2): + * Has 48 pages of 4 bytes = 192 bytes. + * Pages 0 + 1 is used for the 7-byte UID. + * Page 2 contains the last check digit for the UID, one byte manufacturer internal data, and the lock bytes (see http://www.nxp.com/documents/data_sheet/MF0ICU1.pdf section 8.5.2) + * Page 3 is OTP, One Time Programmable bits. Once set to 1 they cannot revert to 0. + * Pages 4-39 are read/write unless blocked by the lock bytes in page 2. + * Page 40 Lock bytes + * Page 41 16 bit one way counter + * Pages 42-43 Authentication configuration + * Pages 44-47 Authentication key + */ +#ifndef MFRC522_h +#define MFRC522_h + +#include "require_cpp11.h" +#include "deprecated.h" +// Enable integer limits +#define __STDC_LIMIT_MACROS +#include +#include +#include + +// Firmware data for self-test +// Reference values based on firmware version +// Hint: if needed, you can remove unused self-test data to save flash memory +// +// Version 0.0 (0x90) +// Philips Semiconductors; Preliminary Specification Revision 2.0 - 01 August 2005; 16.1 self-test +const byte MFRC522_firmware_referenceV0_0[] PROGMEM = { + 0x00, 0x87, 0x98, 0x0f, 0x49, 0xFF, 0x07, 0x19, + 0xBF, 0x22, 0x30, 0x49, 0x59, 0x63, 0xAD, 0xCA, + 0x7F, 0xE3, 0x4E, 0x03, 0x5C, 0x4E, 0x49, 0x50, + 0x47, 0x9A, 0x37, 0x61, 0xE7, 0xE2, 0xC6, 0x2E, + 0x75, 0x5A, 0xED, 0x04, 0x3D, 0x02, 0x4B, 0x78, + 0x32, 0xFF, 0x58, 0x3B, 0x7C, 0xE9, 0x00, 0x94, + 0xB4, 0x4A, 0x59, 0x5B, 0xFD, 0xC9, 0x29, 0xDF, + 0x35, 0x96, 0x98, 0x9E, 0x4F, 0x30, 0x32, 0x8D +}; +// Version 1.0 (0x91) +// NXP Semiconductors; Rev. 3.8 - 17 September 2014; 16.1.1 self-test +const byte MFRC522_firmware_referenceV1_0[] PROGMEM = { + 0x00, 0xC6, 0x37, 0xD5, 0x32, 0xB7, 0x57, 0x5C, + 0xC2, 0xD8, 0x7C, 0x4D, 0xD9, 0x70, 0xC7, 0x73, + 0x10, 0xE6, 0xD2, 0xAA, 0x5E, 0xA1, 0x3E, 0x5A, + 0x14, 0xAF, 0x30, 0x61, 0xC9, 0x70, 0xDB, 0x2E, + 0x64, 0x22, 0x72, 0xB5, 0xBD, 0x65, 0xF4, 0xEC, + 0x22, 0xBC, 0xD3, 0x72, 0x35, 0xCD, 0xAA, 0x41, + 0x1F, 0xA7, 0xF3, 0x53, 0x14, 0xDE, 0x7E, 0x02, + 0xD9, 0x0F, 0xB5, 0x5E, 0x25, 0x1D, 0x29, 0x79 +}; +// Version 2.0 (0x92) +// NXP Semiconductors; Rev. 3.8 - 17 September 2014; 16.1.1 self-test +const byte MFRC522_firmware_referenceV2_0[] PROGMEM = { + 0x00, 0xEB, 0x66, 0xBA, 0x57, 0xBF, 0x23, 0x95, + 0xD0, 0xE3, 0x0D, 0x3D, 0x27, 0x89, 0x5C, 0xDE, + 0x9D, 0x3B, 0xA7, 0x00, 0x21, 0x5B, 0x89, 0x82, + 0x51, 0x3A, 0xEB, 0x02, 0x0C, 0xA5, 0x00, 0x49, + 0x7C, 0x84, 0x4D, 0xB3, 0xCC, 0xD2, 0x1B, 0x81, + 0x5D, 0x48, 0x76, 0xD5, 0x71, 0x61, 0x21, 0xA9, + 0x86, 0x96, 0x83, 0x38, 0xCF, 0x9D, 0x5B, 0x6D, + 0xDC, 0x15, 0xBA, 0x3E, 0x7D, 0x95, 0x3B, 0x2F +}; +// Clone +// Fudan Semiconductor FM17522 (0x88) +const byte FM17522_firmware_reference[] PROGMEM = { + 0x00, 0xD6, 0x78, 0x8C, 0xE2, 0xAA, 0x0C, 0x18, + 0x2A, 0xB8, 0x7A, 0x7F, 0xD3, 0x6A, 0xCF, 0x0B, + 0xB1, 0x37, 0x63, 0x4B, 0x69, 0xAE, 0x91, 0xC7, + 0xC3, 0x97, 0xAE, 0x77, 0xF4, 0x37, 0xD7, 0x9B, + 0x7C, 0xF5, 0x3C, 0x11, 0x8F, 0x15, 0xC3, 0xD7, + 0xC1, 0x5B, 0x00, 0x2A, 0xD0, 0x75, 0xDE, 0x9E, + 0x51, 0x64, 0xAB, 0x3E, 0xE9, 0x15, 0xB5, 0xAB, + 0x56, 0x9A, 0x98, 0x82, 0x26, 0xEA, 0x2A, 0x62 +}; + +class MFRC522 { +public: + // Size of the MFRC522 FIFO + static constexpr byte FIFO_SIZE = 64; // The FIFO is 64 bytes. + // Default value for unused pin + static constexpr uint8_t UNUSED_PIN = UINT8_MAX; + + // MFRC522 registers. Described in chapter 9 of the datasheet. + // When using SPI all addresses are shifted one bit left in the "SPI address byte" (section 8.1.2.3) + enum PCD_Register : byte { + // Page 0: Command and status + // 0x00 // reserved for future use + CommandReg = 0x01 << 1, // starts and stops command execution + ComIEnReg = 0x02 << 1, // enable and disable interrupt request control bits + DivIEnReg = 0x03 << 1, // enable and disable interrupt request control bits + ComIrqReg = 0x04 << 1, // interrupt request bits + DivIrqReg = 0x05 << 1, // interrupt request bits + ErrorReg = 0x06 << 1, // error bits showing the error status of the last command executed + Status1Reg = 0x07 << 1, // communication status bits + Status2Reg = 0x08 << 1, // receiver and transmitter status bits + FIFODataReg = 0x09 << 1, // input and output of 64 byte FIFO buffer + FIFOLevelReg = 0x0A << 1, // number of bytes stored in the FIFO buffer + WaterLevelReg = 0x0B << 1, // level for FIFO underflow and overflow warning + ControlReg = 0x0C << 1, // miscellaneous control registers + BitFramingReg = 0x0D << 1, // adjustments for bit-oriented frames + CollReg = 0x0E << 1, // bit position of the first bit-collision detected on the RF interface + // 0x0F // reserved for future use + + // Page 1: Command + // 0x10 // reserved for future use + ModeReg = 0x11 << 1, // defines general modes for transmitting and receiving + TxModeReg = 0x12 << 1, // defines transmission data rate and framing + RxModeReg = 0x13 << 1, // defines reception data rate and framing + TxControlReg = 0x14 << 1, // controls the logical behavior of the antenna driver pins TX1 and TX2 + TxASKReg = 0x15 << 1, // controls the setting of the transmission modulation + TxSelReg = 0x16 << 1, // selects the internal sources for the antenna driver + RxSelReg = 0x17 << 1, // selects internal receiver settings + RxThresholdReg = 0x18 << 1, // selects thresholds for the bit decoder + DemodReg = 0x19 << 1, // defines demodulator settings + // 0x1A // reserved for future use + // 0x1B // reserved for future use + MfTxReg = 0x1C << 1, // controls some MIFARE communication transmit parameters + MfRxReg = 0x1D << 1, // controls some MIFARE communication receive parameters + // 0x1E // reserved for future use + SerialSpeedReg = 0x1F << 1, // selects the speed of the serial UART interface + + // Page 2: Configuration + // 0x20 // reserved for future use + CRCResultRegH = 0x21 << 1, // shows the MSB and LSB values of the CRC calculation + CRCResultRegL = 0x22 << 1, + // 0x23 // reserved for future use + ModWidthReg = 0x24 << 1, // controls the ModWidth setting? + // 0x25 // reserved for future use + RFCfgReg = 0x26 << 1, // configures the receiver gain + GsNReg = 0x27 << 1, // selects the conductance of the antenna driver pins TX1 and TX2 for modulation + CWGsPReg = 0x28 << 1, // defines the conductance of the p-driver output during periods of no modulation + ModGsPReg = 0x29 << 1, // defines the conductance of the p-driver output during periods of modulation + TModeReg = 0x2A << 1, // defines settings for the internal timer + TPrescalerReg = 0x2B << 1, // the lower 8 bits of the TPrescaler value. The 4 high bits are in TModeReg. + TReloadRegH = 0x2C << 1, // defines the 16-bit timer reload value + TReloadRegL = 0x2D << 1, + TCounterValueRegH = 0x2E << 1, // shows the 16-bit timer value + TCounterValueRegL = 0x2F << 1, + + // Page 3: Test Registers + // 0x30 // reserved for future use + TestSel1Reg = 0x31 << 1, // general test signal configuration + TestSel2Reg = 0x32 << 1, // general test signal configuration + TestPinEnReg = 0x33 << 1, // enables pin output driver on pins D1 to D7 + TestPinValueReg = 0x34 << 1, // defines the values for D1 to D7 when it is used as an I/O bus + TestBusReg = 0x35 << 1, // shows the status of the internal test bus + AutoTestReg = 0x36 << 1, // controls the digital self-test + VersionReg = 0x37 << 1, // shows the software version + AnalogTestReg = 0x38 << 1, // controls the pins AUX1 and AUX2 + TestDAC1Reg = 0x39 << 1, // defines the test value for TestDAC1 + TestDAC2Reg = 0x3A << 1, // defines the test value for TestDAC2 + TestADCReg = 0x3B << 1 // shows the value of ADC I and Q channels + // 0x3C // reserved for production tests + // 0x3D // reserved for production tests + // 0x3E // reserved for production tests + // 0x3F // reserved for production tests + }; + + // MFRC522 commands. Described in chapter 10 of the datasheet. + enum PCD_Command : byte { + PCD_Idle = 0x00, // no action, cancels current command execution + PCD_Mem = 0x01, // stores 25 bytes into the internal buffer + PCD_GenerateRandomID = 0x02, // generates a 10-byte random ID number + PCD_CalcCRC = 0x03, // activates the CRC coprocessor or performs a self-test + PCD_Transmit = 0x04, // transmits data from the FIFO buffer + PCD_NoCmdChange = 0x07, // no command change, can be used to modify the CommandReg register bits without affecting the command, for example, the PowerDown bit + PCD_Receive = 0x08, // activates the receiver circuits + PCD_Transceive = 0x0C, // transmits data from FIFO buffer to antenna and automatically activates the receiver after transmission + PCD_MFAuthent = 0x0E, // performs the MIFARE standard authentication as a reader + PCD_SoftReset = 0x0F // resets the MFRC522 + }; + + // MFRC522 RxGain[2:0] masks, defines the receiver's signal voltage gain factor (on the PCD). + // Described in 9.3.3.6 / table 98 of the datasheet at http://www.nxp.com/documents/data_sheet/MFRC522.pdf + enum PCD_RxGain : byte { + RxGain_18dB = 0x00 << 4, // 000b - 18 dB, minimum + RxGain_23dB = 0x01 << 4, // 001b - 23 dB + RxGain_18dB_2 = 0x02 << 4, // 010b - 18 dB, it seems 010b is a duplicate for 000b + RxGain_23dB_2 = 0x03 << 4, // 011b - 23 dB, it seems 011b is a duplicate for 001b + RxGain_33dB = 0x04 << 4, // 100b - 33 dB, average, and typical default + RxGain_38dB = 0x05 << 4, // 101b - 38 dB + RxGain_43dB = 0x06 << 4, // 110b - 43 dB + RxGain_48dB = 0x07 << 4, // 111b - 48 dB, maximum + RxGain_min = 0x00 << 4, // 000b - 18 dB, minimum, convenience for RxGain_18dB + RxGain_avg = 0x04 << 4, // 100b - 33 dB, average, convenience for RxGain_33dB + RxGain_max = 0x07 << 4 // 111b - 48 dB, maximum, convenience for RxGain_48dB + }; + + // Commands sent to the PICC. + enum PICC_Command : byte { + // The commands used by the PCD to manage communication with several PICCs (ISO 14443-3, Type A, section 6.4) + PICC_CMD_REQA = 0x26, // REQuest command, Type A. Invites PICCs in state IDLE to go to READY and prepare for anticollision or selection. 7 bit frame. + PICC_CMD_WUPA = 0x52, // Wake-UP command, Type A. Invites PICCs in state IDLE and HALT to go to READY(*) and prepare for anticollision or selection. 7 bit frame. + PICC_CMD_CT = 0x88, // Cascade Tag. Not really a command, but used during anti collision. + PICC_CMD_SEL_CL1 = 0x93, // Anti collision/Select, Cascade Level 1 + PICC_CMD_SEL_CL2 = 0x95, // Anti collision/Select, Cascade Level 2 + PICC_CMD_SEL_CL3 = 0x97, // Anti collision/Select, Cascade Level 3 + PICC_CMD_HLTA = 0x50, // HaLT command, Type A. Instructs an ACTIVE PICC to go to state HALT. + PICC_CMD_RATS = 0xE0, // Request command for Answer To Reset. + // The commands used for MIFARE Classic (from http://www.mouser.com/ds/2/302/MF1S503x-89574.pdf, Section 9) + // Use PCD_MFAuthent to authenticate access to a sector, then use these commands to read/write/modify the blocks on the sector. + // The read/write commands can also be used for MIFARE Ultralight. + PICC_CMD_MF_AUTH_KEY_A = 0x60, // Perform authentication with Key A + PICC_CMD_MF_AUTH_KEY_B = 0x61, // Perform authentication with Key B + PICC_CMD_MF_READ = 0x30, // Reads one 16 byte block from the authenticated sector of the PICC. Also used for MIFARE Ultralight. + PICC_CMD_MF_WRITE = 0xA0, // Writes one 16 byte block to the authenticated sector of the PICC. Called "COMPATIBILITY WRITE" for MIFARE Ultralight. + PICC_CMD_MF_DECREMENT = 0xC0, // Decrements the contents of a block and stores the result in the internal data register. + PICC_CMD_MF_INCREMENT = 0xC1, // Increments the contents of a block and stores the result in the internal data register. + PICC_CMD_MF_RESTORE = 0xC2, // Reads the contents of a block into the internal data register. + PICC_CMD_MF_TRANSFER = 0xB0, // Writes the contents of the internal data register to a block. + // The commands used for MIFARE Ultralight (from http://www.nxp.com/documents/data_sheet/MF0ICU1.pdf, Section 8.6) + // The PICC_CMD_MF_READ and PICC_CMD_MF_WRITE can also be used for MIFARE Ultralight. + PICC_CMD_UL_WRITE = 0xA2 // Writes one 4 byte page to the PICC. + }; + + // MIFARE constants that does not fit anywhere else + enum MIFARE_Misc { + MF_ACK = 0xA, // The MIFARE Classic uses a 4 bit ACK/NAK. Any other value than 0xA is NAK. + MF_KEY_SIZE = 6 // A Mifare Crypto1 key is 6 bytes. + }; + + // PICC types we can detect. Remember to update PICC_GetTypeName() if you add more. + // last value set to 0xff, then compiler uses less ram, it seems some optimisations are triggered + enum PICC_Type : byte { + PICC_TYPE_UNKNOWN , + PICC_TYPE_ISO_14443_4 , // PICC compliant with ISO/IEC 14443-4 + PICC_TYPE_ISO_18092 , // PICC compliant with ISO/IEC 18092 (NFC) + PICC_TYPE_MIFARE_MINI , // MIFARE Classic protocol, 320 bytes + PICC_TYPE_MIFARE_1K , // MIFARE Classic protocol, 1KB + PICC_TYPE_MIFARE_4K , // MIFARE Classic protocol, 4KB + PICC_TYPE_MIFARE_UL , // MIFARE Ultralight or Ultralight C + PICC_TYPE_MIFARE_PLUS , // MIFARE Plus + PICC_TYPE_MIFARE_DESFIRE, // MIFARE DESFire + PICC_TYPE_TNP3XXX , // Only mentioned in NXP AN 10833 MIFARE Type Identification Procedure + PICC_TYPE_NOT_COMPLETE = 0xff // SAK indicates UID is not complete. + }; + + // Return codes from the functions in this class. Remember to update GetStatusCodeName() if you add more. + // last value set to 0xff, then compiler uses less ram, it seems some optimisations are triggered + enum StatusCode : byte { + STATUS_OK , // Success + STATUS_ERROR , // Error in communication + STATUS_COLLISION , // Collission detected + STATUS_TIMEOUT , // Timeout in communication. + STATUS_NO_ROOM , // A buffer is not big enough. + STATUS_INTERNAL_ERROR , // Internal error in the code. Should not happen ;-) + STATUS_INVALID , // Invalid argument. + STATUS_CRC_WRONG , // The CRC_A does not match + STATUS_MIFARE_NACK = 0xff // A MIFARE PICC responded with NAK. + }; + + // A struct used for passing the UID of a PICC. + typedef struct { + byte size; // Number of bytes in the UID. 4, 7 or 10. + byte uidByte[10]; + byte sak; // The SAK (Select acknowledge) byte returned from the PICC after successful selection. + } Uid; + + // A struct used for passing a MIFARE Crypto1 key + typedef struct { + byte keyByte[MF_KEY_SIZE]; + } MIFARE_Key; + + // Member variables + Uid uid; // Used by PICC_ReadCardSerial(). + + ///////////////////////////////////////////////////////////////////////////////////// + // Functions for setting up the Arduino + ///////////////////////////////////////////////////////////////////////////////////// + MFRC522(const byte chipSelectPin, const byte resetPowerDownPin, + SPIClass *spiClass = &SPI, const SPISettings spiSettings = SPISettings(SPI_CLOCK_DIV4, MSBFIRST, SPI_MODE0)) + : _chipSelectPin(chipSelectPin), _resetPowerDownPin(resetPowerDownPin), + _spiClass(spiClass), _spiSettings(spiSettings) {}; + MFRC522() : MFRC522(UNUSED_PIN, UNUSED_PIN) {}; + + ///////////////////////////////////////////////////////////////////////////////////// + // Basic interface functions for communicating with the MFRC522 + ///////////////////////////////////////////////////////////////////////////////////// + void PCD_WriteRegister(PCD_Register reg, byte value); + void PCD_WriteRegister(PCD_Register reg, byte count, byte *values); + byte PCD_ReadRegister(PCD_Register reg); + void PCD_ReadRegister(PCD_Register reg, byte count, byte *values, byte rxAlign = 0); + void PCD_SetRegisterBitMask(PCD_Register reg, byte mask); + void PCD_ClearRegisterBitMask(PCD_Register reg, byte mask); + StatusCode PCD_CalculateCRC(byte *data, byte length, byte *result); + + ///////////////////////////////////////////////////////////////////////////////////// + // Functions for manipulating the MFRC522 + ///////////////////////////////////////////////////////////////////////////////////// + void PCD_Init(); + void PCD_Init(byte chipSelectPin, byte resetPowerDownPin); + void PCD_Reset(); + void PCD_AntennaOn(); + void PCD_AntennaOff(); + byte PCD_GetAntennaGain(); + void PCD_SetAntennaGain(byte mask); + bool PCD_PerformSelfTest(); + + ///////////////////////////////////////////////////////////////////////////////////// + // Power control functions + ///////////////////////////////////////////////////////////////////////////////////// + void PCD_SoftPowerDown(); + void PCD_SoftPowerUp(); + + ///////////////////////////////////////////////////////////////////////////////////// + // Functions for communicating with PICCs + ///////////////////////////////////////////////////////////////////////////////////// + StatusCode PCD_TransceiveData(byte *sendData, byte sendLen, byte *backData, byte *backLen, byte *validBits = nullptr, byte rxAlign = 0, bool checkCRC = false); + StatusCode PCD_CommunicateWithPICC(byte command, byte waitIRq, byte *sendData, byte sendLen, byte *backData = nullptr, byte *backLen = nullptr, byte *validBits = nullptr, byte rxAlign = 0, bool checkCRC = false); + StatusCode PICC_RequestA(byte *bufferATQA, byte *bufferSize); + StatusCode PICC_WakeupA(byte *bufferATQA, byte *bufferSize); + StatusCode PICC_REQA_or_WUPA(byte command, byte *bufferATQA, byte *bufferSize); + virtual StatusCode PICC_Select(Uid *uid, byte validBits = 0); + StatusCode PICC_HaltA(); + + ///////////////////////////////////////////////////////////////////////////////////// + // Functions for communicating with MIFARE PICCs + ///////////////////////////////////////////////////////////////////////////////////// + StatusCode PCD_Authenticate(byte command, byte blockAddr, MIFARE_Key *key, Uid *uid); + void PCD_StopCrypto1(); + StatusCode MIFARE_Read(byte blockAddr, byte *buffer, byte *bufferSize); + StatusCode MIFARE_Write(byte blockAddr, byte *buffer, byte bufferSize); + StatusCode MIFARE_Ultralight_Write(byte page, byte *buffer, byte bufferSize); + StatusCode MIFARE_Decrement(byte blockAddr, int32_t delta); + StatusCode MIFARE_Increment(byte blockAddr, int32_t delta); + StatusCode MIFARE_Restore(byte blockAddr); + StatusCode MIFARE_Transfer(byte blockAddr); + StatusCode MIFARE_GetValue(byte blockAddr, int32_t *value); + StatusCode MIFARE_SetValue(byte blockAddr, int32_t value); + StatusCode PCD_NTAG216_AUTH(byte *passWord, byte pACK[]); + + ///////////////////////////////////////////////////////////////////////////////////// + // Support functions + ///////////////////////////////////////////////////////////////////////////////////// + StatusCode PCD_MIFARE_Transceive(byte *sendData, byte sendLen, bool acceptTimeout = false); + static PICC_Type PICC_GetType(byte sak); + + // Support functions for debuging - proxy for MFRC522Debug to keep backwarts compatibility + static const __FlashStringHelper *GetStatusCodeName(StatusCode code); + static const __FlashStringHelper *PICC_GetTypeName(PICC_Type type); + + // Support functions for debuging + void PCD_DumpVersionToSerial(); + void PICC_DumpToSerial(Uid *uid); + void PICC_DumpDetailsToSerial(Uid *uid); + void PICC_DumpMifareClassicToSerial(Uid *uid, PICC_Type piccType, MIFARE_Key *key); + void PICC_DumpMifareClassicSectorToSerial(Uid *uid, MIFARE_Key *key, byte sector); + void PICC_DumpMifareUltralightToSerial(); + + // Advanced functions for MIFARE + void MIFARE_SetAccessBits(byte *accessBitBuffer, byte g0, byte g1, byte g2, byte g3); + + ///////////////////////////////////////////////////////////////////////////////////// + // Convenience functions - does not add extra functionality + ///////////////////////////////////////////////////////////////////////////////////// + virtual bool PICC_IsNewCardPresent(); + virtual bool PICC_ReadCardSerial(); + +protected: + // Pins + byte _chipSelectPin; // Arduino pin connected to MFRC522's SPI slave select input (Pin 24, NSS, active low) + byte _resetPowerDownPin; // Arduino pin connected to MFRC522's reset and power down input (Pin 6, NRSTPD, active low) + + // SPI communication + SPIClass *_spiClass; // SPI class which abstracts hardware. + const SPISettings _spiSettings; // SPI settings. + + // Functions for communicating with MIFARE PICCs + StatusCode MIFARE_TwoStepHelper(byte command, byte blockAddr, int32_t data); +}; + +#endif diff --git a/libraries/MFRC522/MFRC522Debug.cpp b/libraries/MFRC522/MFRC522Debug.cpp new file mode 100644 index 0000000..29f995f --- /dev/null +++ b/libraries/MFRC522/MFRC522Debug.cpp @@ -0,0 +1,46 @@ + +#include "MFRC522Debug.h" + +/** + * Returns a __FlashStringHelper pointer to the PICC type name. + * + * @return const __FlashStringHelper * + */ +const __FlashStringHelper *MFRC522Debug::PICC_GetTypeName(MFRC522::PICC_Type piccType ///< One of the PICC_Type enums. +) { + switch (piccType) { + case MFRC522::PICC_TYPE_ISO_14443_4: return F("PICC compliant with ISO/IEC 14443-4"); + case MFRC522::PICC_TYPE_ISO_18092: return F("PICC compliant with ISO/IEC 18092 (NFC)"); + case MFRC522::PICC_TYPE_MIFARE_MINI: return F("MIFARE Mini, 320 bytes"); + case MFRC522::PICC_TYPE_MIFARE_1K: return F("MIFARE 1KB"); + case MFRC522::PICC_TYPE_MIFARE_4K: return F("MIFARE 4KB"); + case MFRC522::PICC_TYPE_MIFARE_UL: return F("MIFARE Ultralight or Ultralight C"); + case MFRC522::PICC_TYPE_MIFARE_PLUS: return F("MIFARE Plus"); + case MFRC522::PICC_TYPE_MIFARE_DESFIRE: return F("MIFARE DESFire"); + case MFRC522::PICC_TYPE_TNP3XXX: return F("MIFARE TNP3XXX"); + case MFRC522::PICC_TYPE_NOT_COMPLETE: return F("SAK indicates UID is not complete."); + case MFRC522::PICC_TYPE_UNKNOWN: + default: return F("Unknown type"); + } +} // End PICC_GetTypeName() + +/** + * Returns a __FlashStringHelper pointer to a status code name. + * + * @return const __FlashStringHelper * + */ +const __FlashStringHelper *MFRC522Debug::GetStatusCodeName(MFRC522::StatusCode code ///< One of the StatusCode enums. +) { + switch (code) { + case MFRC522::STATUS_OK: return F("Success."); + case MFRC522::STATUS_ERROR: return F("Error in communication."); + case MFRC522::STATUS_COLLISION: return F("Collission detected."); + case MFRC522::STATUS_TIMEOUT: return F("Timeout in communication."); + case MFRC522::STATUS_NO_ROOM: return F("A buffer is not big enough."); + case MFRC522::STATUS_INTERNAL_ERROR: return F("Internal error in the code. Should not happen."); + case MFRC522::STATUS_INVALID: return F("Invalid argument."); + case MFRC522::STATUS_CRC_WRONG: return F("The CRC_A does not match."); + case MFRC522::STATUS_MIFARE_NACK: return F("A MIFARE PICC responded with NAK."); + default: return F("Unknown error"); + } +} // End GetStatusCodeName() diff --git a/libraries/MFRC522/MFRC522Debug.h b/libraries/MFRC522/MFRC522Debug.h new file mode 100644 index 0000000..5c01248 --- /dev/null +++ b/libraries/MFRC522/MFRC522Debug.h @@ -0,0 +1,14 @@ +#include "MFRC522.h" + +#ifndef MFRC522Debug_h +#define MFRC522Debug_h + +class MFRC522Debug { +private: + +public: + // Get human readable code and type + static const __FlashStringHelper *PICC_GetTypeName(MFRC522::PICC_Type type); + static const __FlashStringHelper *GetStatusCodeName(MFRC522::StatusCode code); +}; +#endif // MFRC522Debug_h diff --git a/libraries/MFRC522/MFRC522Extended.cpp b/libraries/MFRC522/MFRC522Extended.cpp new file mode 100644 index 0000000..da603ab --- /dev/null +++ b/libraries/MFRC522/MFRC522Extended.cpp @@ -0,0 +1,1160 @@ +/* + * Library extends MFRC522.h to support RATS for ISO-14443-4 PICC. + * RATS - Request for Answer To Select. + * NOTE: Please also check the comments in MFRC522Extended.h + * @author JPG-Consulting +*/ + +#include "MFRC522Extended.h" + +///////////////////////////////////////////////////////////////////////////////////// +// Functions for communicating with PICCs +///////////////////////////////////////////////////////////////////////////////////// + +/** + * Transmits SELECT/ANTICOLLISION commands to select a single PICC. + * Before calling this function the PICCs must be placed in the READY(*) state by calling PICC_RequestA() or PICC_WakeupA(). + * On success: + * - The chosen PICC is in state ACTIVE(*) and all other PICCs have returned to state IDLE/HALT. (Figure 7 of the ISO/IEC 14443-3 draft.) + * - The UID size and value of the chosen PICC is returned in *uid along with the SAK. + * + * A PICC UID consists of 4, 7 or 10 bytes. + * Only 4 bytes can be specified in a SELECT command, so for the longer UIDs two or three iterations are used: + * UID size Number of UID bytes Cascade levels Example of PICC + * ======== =================== ============== =============== + * single 4 1 MIFARE Classic + * double 7 2 MIFARE Ultralight + * triple 10 3 Not currently in use? + * + * @return STATUS_OK on success, STATUS_??? otherwise. + */ +MFRC522::StatusCode MFRC522Extended::PICC_Select( Uid *uid, ///< Pointer to Uid struct. Normally output, but can also be used to supply a known UID. + byte validBits ///< The number of known UID bits supplied in *uid. Normally 0. If set you must also supply uid->size. + ) { + bool uidComplete; + bool selectDone; + bool useCascadeTag; + byte cascadeLevel = 1; + MFRC522::StatusCode result; + byte count; + byte index; + byte uidIndex; // The first index in uid->uidByte[] that is used in the current Cascade Level. + int8_t currentLevelKnownBits; // The number of known UID bits in the current Cascade Level. + byte buffer[9]; // The SELECT/ANTICOLLISION commands uses a 7 byte standard frame + 2 bytes CRC_A + byte bufferUsed; // The number of bytes used in the buffer, ie the number of bytes to transfer to the FIFO. + byte rxAlign; // Used in BitFramingReg. Defines the bit position for the first bit received. + byte txLastBits; // Used in BitFramingReg. The number of valid bits in the last transmitted byte. + byte *responseBuffer; + byte responseLength; + + // Description of buffer structure: + // Byte 0: SEL Indicates the Cascade Level: PICC_CMD_SEL_CL1, PICC_CMD_SEL_CL2 or PICC_CMD_SEL_CL3 + // Byte 1: NVB Number of Valid Bits (in complete command, not just the UID): High nibble: complete bytes, Low nibble: Extra bits. + // Byte 2: UID-data or CT See explanation below. CT means Cascade Tag. + // Byte 3: UID-data + // Byte 4: UID-data + // Byte 5: UID-data + // Byte 6: BCC Block Check Character - XOR of bytes 2-5 + // Byte 7: CRC_A + // Byte 8: CRC_A + // The BCC and CRC_A are only transmitted if we know all the UID bits of the current Cascade Level. + // + // Description of bytes 2-5: (Section 6.5.4 of the ISO/IEC 14443-3 draft: UID contents and cascade levels) + // UID size Cascade level Byte2 Byte3 Byte4 Byte5 + // ======== ============= ===== ===== ===== ===== + // 4 bytes 1 uid0 uid1 uid2 uid3 + // 7 bytes 1 CT uid0 uid1 uid2 + // 2 uid3 uid4 uid5 uid6 + // 10 bytes 1 CT uid0 uid1 uid2 + // 2 CT uid3 uid4 uid5 + // 3 uid6 uid7 uid8 uid9 + + // Sanity checks + if (validBits > 80) { + return STATUS_INVALID; + } + + // Prepare MFRC522 + PCD_ClearRegisterBitMask(CollReg, 0x80); // ValuesAfterColl=1 => Bits received after collision are cleared. + + // Repeat Cascade Level loop until we have a complete UID. + uidComplete = false; + while (!uidComplete) { + // Set the Cascade Level in the SEL byte, find out if we need to use the Cascade Tag in byte 2. + switch (cascadeLevel) { + case 1: + buffer[0] = PICC_CMD_SEL_CL1; + uidIndex = 0; + useCascadeTag = validBits && uid->size > 4; // When we know that the UID has more than 4 bytes + break; + + case 2: + buffer[0] = PICC_CMD_SEL_CL2; + uidIndex = 3; + useCascadeTag = validBits && uid->size > 7; // When we know that the UID has more than 7 bytes + break; + + case 3: + buffer[0] = PICC_CMD_SEL_CL3; + uidIndex = 6; + useCascadeTag = false; // Never used in CL3. + break; + + default: + return STATUS_INTERNAL_ERROR; + break; + } + + // How many UID bits are known in this Cascade Level? + currentLevelKnownBits = validBits - (8 * uidIndex); + if (currentLevelKnownBits < 0) { + currentLevelKnownBits = 0; + } + // Copy the known bits from uid->uidByte[] to buffer[] + index = 2; // destination index in buffer[] + if (useCascadeTag) { + buffer[index++] = PICC_CMD_CT; + } + byte bytesToCopy = currentLevelKnownBits / 8 + (currentLevelKnownBits % 8 ? 1 : 0); // The number of bytes needed to represent the known bits for this level. + if (bytesToCopy) { + byte maxBytes = useCascadeTag ? 3 : 4; // Max 4 bytes in each Cascade Level. Only 3 left if we use the Cascade Tag + if (bytesToCopy > maxBytes) { + bytesToCopy = maxBytes; + } + for (count = 0; count < bytesToCopy; count++) { + buffer[index++] = uid->uidByte[uidIndex + count]; + } + } + // Now that the data has been copied we need to include the 8 bits in CT in currentLevelKnownBits + if (useCascadeTag) { + currentLevelKnownBits += 8; + } + + // Repeat anti collision loop until we can transmit all UID bits + BCC and receive a SAK - max 32 iterations. + selectDone = false; + while (!selectDone) { + // Find out how many bits and bytes to send and receive. + if (currentLevelKnownBits >= 32) { // All UID bits in this Cascade Level are known. This is a SELECT. + //Serial.print(F("SELECT: currentLevelKnownBits=")); Serial.println(currentLevelKnownBits, DEC); + buffer[1] = 0x70; // NVB - Number of Valid Bits: Seven whole bytes + // Calculate BCC - Block Check Character + buffer[6] = buffer[2] ^ buffer[3] ^ buffer[4] ^ buffer[5]; + // Calculate CRC_A + result = PCD_CalculateCRC(buffer, 7, &buffer[7]); + if (result != STATUS_OK) { + return result; + } + txLastBits = 0; // 0 => All 8 bits are valid. + bufferUsed = 9; + // Store response in the last 3 bytes of buffer (BCC and CRC_A - not needed after tx) + responseBuffer = &buffer[6]; + responseLength = 3; + } + else { // This is an ANTICOLLISION. + //Serial.print(F("ANTICOLLISION: currentLevelKnownBits=")); Serial.println(currentLevelKnownBits, DEC); + txLastBits = currentLevelKnownBits % 8; + count = currentLevelKnownBits / 8; // Number of whole bytes in the UID part. + index = 2 + count; // Number of whole bytes: SEL + NVB + UIDs + buffer[1] = (index << 4) + txLastBits; // NVB - Number of Valid Bits + bufferUsed = index + (txLastBits ? 1 : 0); + // Store response in the unused part of buffer + responseBuffer = &buffer[index]; + responseLength = sizeof(buffer) - index; + } + + // Set bit adjustments + rxAlign = txLastBits; // Having a separate variable is overkill. But it makes the next line easier to read. + PCD_WriteRegister(BitFramingReg, (rxAlign << 4) + txLastBits); // RxAlign = BitFramingReg[6..4]. TxLastBits = BitFramingReg[2..0] + + // Transmit the buffer and receive the response. + result = PCD_TransceiveData(buffer, bufferUsed, responseBuffer, &responseLength, &txLastBits, rxAlign); + if (result == STATUS_COLLISION) { // More than one PICC in the field => collision. + byte valueOfCollReg = PCD_ReadRegister(CollReg); // CollReg[7..0] bits are: ValuesAfterColl reserved CollPosNotValid CollPos[4:0] + if (valueOfCollReg & 0x20) { // CollPosNotValid + return STATUS_COLLISION; // Without a valid collision position we cannot continue + } + byte collisionPos = valueOfCollReg & 0x1F; // Values 0-31, 0 means bit 32. + if (collisionPos == 0) { + collisionPos = 32; + } + if (collisionPos <= currentLevelKnownBits) { // No progress - should not happen + return STATUS_INTERNAL_ERROR; + } + // Choose the PICC with the bit set. + currentLevelKnownBits = collisionPos; + count = (currentLevelKnownBits - 1) % 8; // The bit to modify + index = 1 + (currentLevelKnownBits / 8) + (count ? 1 : 0); // First byte is index 0. + buffer[index] |= (1 << count); + } + else if (result != STATUS_OK) { + return result; + } + else { // STATUS_OK + if (currentLevelKnownBits >= 32) { // This was a SELECT. + selectDone = true; // No more anticollision + // We continue below outside the while. + } + else { // This was an ANTICOLLISION. + // We now have all 32 bits of the UID in this Cascade Level + currentLevelKnownBits = 32; + // Run loop again to do the SELECT. + } + } + } // End of while (!selectDone) + + // We do not check the CBB - it was constructed by us above. + + // Copy the found UID bytes from buffer[] to uid->uidByte[] + index = (buffer[2] == PICC_CMD_CT) ? 3 : 2; // source index in buffer[] + bytesToCopy = (buffer[2] == PICC_CMD_CT) ? 3 : 4; + for (count = 0; count < bytesToCopy; count++) { + uid->uidByte[uidIndex + count] = buffer[index++]; + } + + // Check response SAK (Select Acknowledge) + if (responseLength != 3 || txLastBits != 0) { // SAK must be exactly 24 bits (1 byte + CRC_A). + return STATUS_ERROR; + } + // Verify CRC_A - do our own calculation and store the control in buffer[2..3] - those bytes are not needed anymore. + result = PCD_CalculateCRC(responseBuffer, 1, &buffer[2]); + if (result != STATUS_OK) { + return result; + } + if ((buffer[2] != responseBuffer[1]) || (buffer[3] != responseBuffer[2])) { + return STATUS_CRC_WRONG; + } + if (responseBuffer[0] & 0x04) { // Cascade bit set - UID not complete yes + cascadeLevel++; + } + else { + uidComplete = true; + uid->sak = responseBuffer[0]; + } + } // End of while (!uidComplete) + + // Set correct uid->size + uid->size = 3 * cascadeLevel + 1; + + // IF SAK bit 6 = 1 then it is ISO/IEC 14443-4 (T=CL) + // A Request ATS command should be sent + // We also check SAK bit 3 is cero, as it stands for UID complete (1 would tell us it is incomplete) + if ((uid->sak & 0x24) == 0x20) { + Ats ats; + result = PICC_RequestATS(&ats); + if (result == STATUS_OK) { + // Check the ATS + if (ats.size > 0) + { + // TA1 has been transmitted? + // PPS must be supported... + if (ats.ta1.transmitted) + { + // TA1 + // 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | Description + // ---+---+---+---+---+---+---+---+------------------------------------------ + // 0 | - | - | - | 0 | - | - | - | Different D for each direction supported + // 1 | - | - | - | 0 | - | - | - | Only same D for both direction supported + // - | x | x | x | 0 | - | - | - | DS (Send D) + // - | - | - | - | 0 | x | x | x | DR (Receive D) + // + // D to bitrate table + // 3 | 2 | 1 | Value + // ---+---+---+----------------------------- + // 1 | - | - | 848 kBaud is supported + // - | 1 | - | 424 kBaud is supported + // - | - | 1 | 212 kBaud is supported + // 0 | 0 | 0 | Only 106 kBaud is supported + // + // Note: 106 kBaud is always supported + // + // I have almost constant timeouts when changing speeds :( + // default never used, so only delarate + //TagBitRates ds = BITRATE_106KBITS; + //TagBitRates dr = BITRATE_106KBITS; + TagBitRates ds; + TagBitRates dr; + + //// TODO Not working at 848 or 424 + //if (ats.ta1.ds & 0x04) + //{ + // ds = BITRATE_848KBITS; + //} + //else if (ats.ta1.ds & 0x02) + //{ + // ds = BITRATE_424KBITS; + //} + //else if (ats.ta1.ds & 0x01) + //{ + // ds = BITRATE_212KBITS; + //} + //else + //{ + // ds = BITRATE_106KBITS; + //} + + if (ats.ta1.ds & 0x01) + { + ds = BITRATE_212KBITS; + } + else + { + ds = BITRATE_106KBITS; + } + + //// Not working at 848 or 424 + //if (ats.ta1.dr & 0x04) + //{ + // dr = BITRATE_848KBITS; + //} + //else if (ats.ta1.dr & 0x02) + //{ + // dr = BITRATE_424KBITS; + //} + //else if (ats.ta1.dr & 0x01) + //{ + // dr = BITRATE_212KBITS; + //} + //else + //{ + // dr = BITRATE_106KBITS; + //} + + if (ats.ta1.dr & 0x01) + { + dr = BITRATE_212KBITS; + } + else + { + dr = BITRATE_106KBITS; + } + + PICC_PPS(ds, dr); + } + } + } + } + + return STATUS_OK; +} // End PICC_Select() + +/** + * Transmits a Request command for Answer To Select (ATS). + * + * @return STATUS_OK on success, STATUS_??? otherwise. + */ +MFRC522::StatusCode MFRC522Extended::PICC_RequestATS(Ats *ats) +{ + // TODO unused variable + //byte count; + MFRC522::StatusCode result; + + byte bufferATS[FIFO_SIZE]; + byte bufferSize = FIFO_SIZE; + + memset(bufferATS, 0, FIFO_SIZE); + + // Build command buffer + bufferATS[0] = PICC_CMD_RATS; + + // The CID defines the logical number of the addressed card and has a range of 0 + // through 14; 15 is reserved for future use (RFU). + // + // FSDI codes the maximum frame size (FSD) that the terminal can receive. + // + // FSDI | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9-F + // ------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+----------- + // FSD (bytes) | 16 | 24 | 32 | 40 | 48 | 64 | 96 | 128 | 256 | RFU > 256 + // + bufferATS[1] = 0x50; // FSD=64, CID=0 + + // Calculate CRC_A + result = PCD_CalculateCRC(bufferATS, 2, &bufferATS[2]); + if (result != STATUS_OK) { + return result; + } + + // Transmit the buffer and receive the response, validate CRC_A. + result = PCD_TransceiveData(bufferATS, 4, bufferATS, &bufferSize, NULL, 0, true); + if (result != STATUS_OK) { + PICC_HaltA(); + } + + // Set the ats structure data + ats->size = bufferATS[0]; + + // T0 byte: + // + // b8 | b7 | b6 | b5 | b4 | b3 | b2 | b1 | Meaning + //----+----+----+----+----+----+----+----+--------------------------- + // 0 | ...| ...| ...| ...|... | ...| ...| Set to 0 (RFU) + // 0 | 1 | x | x | ...|... | ...| ...| TC1 transmitted + // 0 | x | 1 | x | ...|... | ...| ...| TB1 transmitted + // 0 | x | x | 1 | ...|... | ...| ...| TA1 transmitted + // 0 | ...| ...| ...| x | x | x | x | Maximum frame size (FSCI) + // + // FSCI | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9-F + // ------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+----------- + // FSC (bytes) | 16 | 24 | 32 | 40 | 48 | 64 | 96 | 128 | 256 | RFU > 256 + // + // Default FSCI is 2 (32 bytes) + if (ats->size > 0x01) + { + // TC1, TB1 and TA1 where NOT transmitted + ats->ta1.transmitted = (bool)(bufferATS[1] & 0x40); + ats->tb1.transmitted = (bool)(bufferATS[1] & 0x20); + ats->tc1.transmitted = (bool)(bufferATS[1] & 0x10); + + // Decode FSCI + switch (bufferATS[1] & 0x0F) + { + case 0x00: + ats->fsc = 16; + break; + case 0x01: + ats->fsc = 24; + break; + case 0x02: + ats->fsc = 32; + break; + case 0x03: + ats->fsc = 40; + break; + case 0x04: + ats->fsc = 48; + break; + case 0x05: + ats->fsc = 64; + break; + case 0x06: + ats->fsc = 96; + break; + case 0x07: + ats->fsc = 128; + break; + case 0x08: + // This value cannot be hold by a byte + // The reason I ignore it is that MFRC255 FIFO is 64 bytes so this is not a possible value (or atleast it shouldn't) + //ats->fsc = 256; + break; + // TODO: What to do with RFU (Reserved for future use)? + default: + break; + } + + // TA1 + if (ats->ta1.transmitted) + { + ats->ta1.sameD = (bool)(bufferATS[2] & 0x80); + ats->ta1.ds = (TagBitRates)((bufferATS[2] & 0x70) >> 4); + ats->ta1.dr = (TagBitRates)(bufferATS[2] & 0x07); + } + else + { + // Default TA1 + ats->ta1.ds = BITRATE_106KBITS; + ats->ta1.dr = BITRATE_106KBITS; + } + + // TB1 + if (ats->tb1.transmitted) + { + uint8_t tb1Index = 2; + + if (ats->ta1.transmitted) + tb1Index++; + + ats->tb1.fwi = (bufferATS[tb1Index] & 0xF0) >> 4; + ats->tb1.sfgi = bufferATS[tb1Index] & 0x0F; + } + else + { + // Defaults for TB1 + ats->tb1.fwi = 0; // TODO: Don't know the default for this! + ats->tb1.sfgi = 0; // The default value of SFGI is 0 (meaning that the card does not need any particular SFGT) + } + + // TC1 + if (ats->tc1.transmitted) + { + uint8_t tc1Index = 2; + + if (ats->ta1.transmitted) + tc1Index++; + if (ats->tb1.transmitted) + tc1Index++; + + ats->tc1.supportsCID = (bool)(bufferATS[tc1Index] & 0x02); + ats->tc1.supportsNAD = (bool)(bufferATS[tc1Index] & 0x01); + } + else + { + // Defaults for TC1 + ats->tc1.supportsCID = true; + ats->tc1.supportsNAD = false; + } + } + else + { + // TC1, TB1 and TA1 where NOT transmitted + ats->ta1.transmitted = false; + ats->tb1.transmitted = false; + ats->tc1.transmitted = false; + + // Default FSCI + ats->fsc = 32; // Defaults to FSCI 2 (32 bytes) + + // Default TA1 + ats->ta1.sameD = false; + ats->ta1.ds = BITRATE_106KBITS; + ats->ta1.dr = BITRATE_106KBITS; + + // Defaults for TB1 + ats->tb1.transmitted = false; + ats->tb1.fwi = 0; // TODO: Don't know the default for this! + ats->tb1.sfgi = 0; // The default value of SFGI is 0 (meaning that the card does not need any particular SFGT) + + // Defaults for TC1 + ats->tc1.transmitted = false; + ats->tc1.supportsCID = true; + ats->tc1.supportsNAD = false; + } + + memcpy(ats->data, bufferATS, bufferSize - 2); + + return result; +} // End PICC_RequestATS() + +/** + * Transmits Protocol and Parameter Selection Request (PPS) without parameter 1 + * + * @return STATUS_OK on success, STATUS_??? otherwise. + */ +MFRC522::StatusCode MFRC522Extended::PICC_PPS() +{ + StatusCode result; + + byte ppsBuffer[4]; + byte ppsBufferSize = 4; + // Start byte: The start byte (PPS) consists of two parts: + // –The upper nibble(b8–b5) is set to’D'to identify the PPS. All other values are RFU. + // -The lower nibble(b4–b1), which is called the ‘card identifier’ (CID), defines the logical number of the addressed card. + ppsBuffer[0] = 0xD0; // CID is hardcoded as 0 in RATS + ppsBuffer[1] = 0x00; // PPS0 indicates whether PPS1 is present + + // Calculate CRC_A + result = PCD_CalculateCRC(ppsBuffer, 2, &ppsBuffer[2]); + if (result != STATUS_OK) { + return result; + } + + // Transmit the buffer and receive the response, validate CRC_A. + result = PCD_TransceiveData(ppsBuffer, 4, ppsBuffer, &ppsBufferSize, NULL, 0, true); + if (result == STATUS_OK) + { + // Enable CRC for T=CL + byte txReg = PCD_ReadRegister(TxModeReg) | 0x80; + byte rxReg = PCD_ReadRegister(RxModeReg) | 0x80; + + PCD_WriteRegister(TxModeReg, txReg); + PCD_WriteRegister(RxModeReg, rxReg); + } + + return result; +} // End PICC_PPS() + +/** + * Transmits Protocol and Parameter Selection Request (PPS) + * + * @return STATUS_OK on success, STATUS_??? otherwise. + */ +MFRC522::StatusCode MFRC522Extended::PICC_PPS(TagBitRates sendBitRate, ///< DS + TagBitRates receiveBitRate ///< DR +) { + StatusCode result; + + // TODO not used + //byte txReg = PCD_ReadRegister(TxModeReg) & 0x8F; + //byte rxReg = PCD_ReadRegister(RxModeReg) & 0x8F; + + byte ppsBuffer[5]; + byte ppsBufferSize = 5; + // Start byte: The start byte (PPS) consists of two parts: + // –The upper nibble(b8–b5) is set to’D'to identify the PPS. All other values are RFU. + // -The lower nibble(b4–b1), which is called the ‘card identifier’ (CID), defines the logical number of the addressed card. + ppsBuffer[0] = 0xD0; // CID is hardcoded as 0 in RATS + ppsBuffer[1] = 0x11; // PPS0 indicates whether PPS1 is present + + // Bit 8 - Set to '0' as MFRC522 allows different bit rates for send and receive + // Bit 4 - Set to '0' as it is Reserved for future use. + //ppsBuffer[2] = (((sendBitRate & 0x03) << 4) | (receiveBitRate & 0x03)) & 0xE7; + ppsBuffer[2] = (((sendBitRate & 0x03) << 2) | (receiveBitRate & 0x03)) & 0xE7; + + // Calculate CRC_A + result = PCD_CalculateCRC(ppsBuffer, 3, &ppsBuffer[3]); + if (result != STATUS_OK) { + return result; + } + + // Transmit the buffer and receive the response, validate CRC_A. + result = PCD_TransceiveData(ppsBuffer, 5, ppsBuffer, &ppsBufferSize, NULL, 0, true); + if (result == STATUS_OK) + { + // Make sure it is an answer to our PPS + // We should receive our PPS byte and 2 CRC bytes + if ((ppsBufferSize == 3) && (ppsBuffer[0] == 0xD0)) { + byte txReg = PCD_ReadRegister(TxModeReg) & 0x8F; + byte rxReg = PCD_ReadRegister(RxModeReg) & 0x8F; + + // Set bit rate and enable CRC for T=CL + txReg = (txReg & 0x8F) | ((receiveBitRate & 0x03) << 4) | 0x80; + rxReg = (rxReg & 0x8F) | ((sendBitRate & 0x03) << 4) | 0x80; + rxReg &= 0xF0; //Enforce although this should be set already + + // From ConfigIsoType + //rxReg |= 0x06; + + PCD_WriteRegister(TxModeReg, txReg); + PCD_WriteRegister(RxModeReg, rxReg); + + // At 212kBps + switch (sendBitRate) { + case BITRATE_212KBITS: + { + //PCD_WriteRegister(ModWidthReg, 0x13); + PCD_WriteRegister(ModWidthReg, 0x15); + } + break; + case BITRATE_424KBITS: + { + PCD_WriteRegister(ModWidthReg, 0x0A); + } + break; + case BITRATE_848KBITS: + { + PCD_WriteRegister(ModWidthReg, 0x05); + } + break; + default: + { + PCD_WriteRegister(ModWidthReg, 0x26); // Default value + } + break; + } + + //PCD_WriteRegister(RxThresholdReg, 0x84); // ISO-14443.4 Type A (default) + //PCD_WriteRegister(ControlReg, 0x10); + + delayMicroseconds(10); + } + else + { + return STATUS_ERROR; + } + } + + return result; +} // End PICC_PPS() + + +///////////////////////////////////////////////////////////////////////////////////// +// Functions for communicating with ISO/IEC 14433-4 cards +///////////////////////////////////////////////////////////////////////////////////// + +MFRC522::StatusCode MFRC522Extended::TCL_Transceive(PcbBlock *send, PcbBlock *back) +{ + MFRC522::StatusCode result; + byte inBuffer[FIFO_SIZE]; + byte inBufferSize = FIFO_SIZE; + byte outBuffer[send->inf.size + 5]; // PCB + CID + NAD + INF + EPILOGUE (CRC) + byte outBufferOffset = 1; + byte inBufferOffset = 1; + + // Set the PCB byte + outBuffer[0] = send->prologue.pcb; + + // Set the CID byte if available + if (send->prologue.pcb & 0x08) { + outBuffer[outBufferOffset] = send->prologue.cid; + outBufferOffset++; + } + + // Set the NAD byte if available + if (send->prologue.pcb & 0x04) { + outBuffer[outBufferOffset] = send->prologue.nad; + outBufferOffset++; + } + + // Copy the INF field if available + if (send->inf.size > 0) { + memcpy(&outBuffer[outBufferOffset], send->inf.data, send->inf.size); + outBufferOffset += send->inf.size; + } + + // Is the CRC enabled for transmission? + byte txModeReg = PCD_ReadRegister(TxModeReg); + if ((txModeReg & 0x80) != 0x80) { + // Calculate CRC_A + result = PCD_CalculateCRC(outBuffer, outBufferOffset, &outBuffer[outBufferOffset]); + if (result != STATUS_OK) { + return result; + } + + outBufferOffset += 2; + } + + // Transceive the block + result = PCD_TransceiveData(outBuffer, outBufferOffset, inBuffer, &inBufferSize); + if (result != STATUS_OK) { + return result; + } + + // We want to turn the received array back to a PcbBlock + back->prologue.pcb = inBuffer[0]; + + // CID byte is present? + if (send->prologue.pcb & 0x08) { + back->prologue.cid = inBuffer[inBufferOffset]; + inBufferOffset++; + } + + // NAD byte is present? + if (send->prologue.pcb & 0x04) { + back->prologue.nad = inBuffer[inBufferOffset]; + inBufferOffset++; + } + + // Check if CRC is taken care of by MFRC522 + byte rxModeReg = PCD_ReadRegister(TxModeReg); + if ((rxModeReg & 0x80) != 0x80) { + Serial.print("CRC is not taken care of by MFRC522: "); + Serial.println(rxModeReg, HEX); + + // Check the CRC + // We need at least the CRC_A value. + if ((int)(inBufferSize - inBufferOffset) < 2) { + return STATUS_CRC_WRONG; + } + + // Verify CRC_A - do our own calculation and store the control in controlBuffer. + byte controlBuffer[2]; + MFRC522::StatusCode status = PCD_CalculateCRC(inBuffer, inBufferSize - 2, controlBuffer); + if (status != STATUS_OK) { + return status; + } + + if ((inBuffer[inBufferSize - 2] != controlBuffer[0]) || (inBuffer[inBufferSize - 1] != controlBuffer[1])) { + return STATUS_CRC_WRONG; + } + + // Take away the CRC bytes + inBufferSize -= 2; + } + + // Got more data? + if (inBufferSize > inBufferOffset) { + if ((inBufferSize - inBufferOffset) > back->inf.size) { + return STATUS_NO_ROOM; + } + + memcpy(back->inf.data, &inBuffer[inBufferOffset], inBufferSize - inBufferOffset); + back->inf.size = inBufferSize - inBufferOffset; + } else { + back->inf.size = 0; + } + + // If the response is a R-Block check NACK + if (((inBuffer[0] & 0xC0) == 0x80) && (inBuffer[0] & 0x20)) { + return STATUS_MIFARE_NACK; + } + + return result; +} +/** + * Send an I-Block (Application) + */ +MFRC522::StatusCode MFRC522Extended::TCL_Transceive(TagInfo *tag, byte *sendData, byte sendLen, byte *backData, byte *backLen) +{ + MFRC522::StatusCode result; + + PcbBlock out; + PcbBlock in; + byte outBuffer[FIFO_SIZE]; + byte outBufferSize = FIFO_SIZE; + byte totalBackLen = *backLen; + + // This command sends an I-Block + out.prologue.pcb = 0x02; + + if (tag->ats.tc1.supportsCID) { + out.prologue.pcb |= 0x08; + out.prologue.cid = 0x00; // CID is curentlly hardcoded as 0x00 + } + + // This command doe not support NAD + out.prologue.pcb &= 0xFB; + out.prologue.nad = 0x00; + + // Set the block number + if (tag->blockNumber) { + out.prologue.pcb |= 0x01; + } + + // Do we have data to send? + if (sendData && (sendLen > 0)) { + out.inf.size = sendLen; + out.inf.data = sendData; + } else { + out.inf.size = 0; + out.inf.data = NULL; + } + + // Initialize the receiving data + // TODO Warning: Value escapes the local scope + in.inf.data = outBuffer; + in.inf.size = outBufferSize; + + result = TCL_Transceive(&out, &in); + if (result != STATUS_OK) { + return result; + } + + // Swap block number on success + tag->blockNumber = !tag->blockNumber; + + if (backData && (backLen > 0)) { + if (*backLen < in.inf.size) + return STATUS_NO_ROOM; + + *backLen = in.inf.size; + memcpy(backData, in.inf.data, in.inf.size); + } + + // Check chaining + if ((in.prologue.pcb & 0x10) == 0x00) + return result; + + // Result is chained + // Send an ACK to receive more data + // TODO: Should be checked I've never needed to send an ACK + while (in.prologue.pcb & 0x10) { + byte ackData[FIFO_SIZE]; + byte ackDataSize = FIFO_SIZE; + + result = TCL_TransceiveRBlock(tag, true, ackData, &ackDataSize); + if (result != STATUS_OK) + return result; + + if (backData && (backLen > 0)) { + if ((*backLen + ackDataSize) > totalBackLen) + return STATUS_NO_ROOM; + + memcpy(&(backData[*backLen]), ackData, ackDataSize); + *backLen += ackDataSize; + } + } + + return result; +} // End TCL_Transceive() + +/** + * Send R-Block to the PICC. + */ +MFRC522::StatusCode MFRC522Extended::TCL_TransceiveRBlock(TagInfo *tag, bool ack, byte *backData, byte *backLen) +{ + MFRC522::StatusCode result; + + PcbBlock out; + PcbBlock in; + byte outBuffer[FIFO_SIZE]; + byte outBufferSize = FIFO_SIZE; + + // This command sends an R-Block + if (ack) + out.prologue.pcb = 0xA2; // ACK + else + out.prologue.pcb = 0xB2; // NAK + + + if (tag->ats.tc1.supportsCID) { + out.prologue.pcb |= 0x08; + out.prologue.cid = 0x00; // CID is curentlly hardcoded as 0x00 + } + + // This command doe not support NAD + out.prologue.pcb &= 0xFB; + out.prologue.nad = 0x00; + + // Set the block number + if (tag->blockNumber) { + out.prologue.pcb |= 0x01; + } + + // No INF data for R-Block + out.inf.size = 0; + out.inf.data = NULL; + + // Initialize the receiving data + // TODO Warning: Value escapes the local scope + in.inf.data = outBuffer; + in.inf.size = outBufferSize; + + result = TCL_Transceive(&out, &in); + if (result != STATUS_OK) { + return result; + } + + // Swap block number on success + tag->blockNumber = !tag->blockNumber; + + if (backData && backLen) { + if (*backLen < in.inf.size) + return STATUS_NO_ROOM; + + *backLen = in.inf.size; + memcpy(backData, in.inf.data, in.inf.size); + } + + return result; +} // End TCL_TransceiveRBlock() + +/** + * Send an S-Block to deselect the card. + */ +MFRC522::StatusCode MFRC522Extended::TCL_Deselect(TagInfo *tag) +{ + MFRC522::StatusCode result; + byte outBuffer[4]; + byte outBufferSize = 1; + byte inBuffer[FIFO_SIZE]; + byte inBufferSize = FIFO_SIZE; + + outBuffer[0] = 0xC2; + if (tag->ats.tc1.supportsCID) + { + outBuffer[0] |= 0x08; + outBuffer[1] = 0x00; // CID is hardcoded + outBufferSize = 2; + } + + result = PCD_TransceiveData(outBuffer, outBufferSize, inBuffer, &inBufferSize); + if (result != STATUS_OK) { + return result; + } + + // TODO:Maybe do some checks? In my test it returns: CA 00 (Same data as I sent to my card) + + return result; +} // End TCL_Deselect() + +///////////////////////////////////////////////////////////////////////////////////// +// Support functions +///////////////////////////////////////////////////////////////////////////////////// + +/** + * Get the PICC type. + * + * @return PICC_Type + */ +MFRC522::PICC_Type MFRC522Extended::PICC_GetType(TagInfo *tag ///< The TagInfo returned from PICC_Select(). +) { + // http://www.nxp.com/documents/application_note/AN10833.pdf + // 3.2 Coding of Select Acknowledge (SAK) + // ignore 8-bit (iso14443 starts with LSBit = bit 1) + // fixes wrong type for manufacturer Infineon (http://nfc-tools.org/index.php?title=ISO14443A) + byte sak = tag->uid.sak & 0x7F; + switch (sak) { + case 0x04: return PICC_TYPE_NOT_COMPLETE; // UID not complete + case 0x09: return PICC_TYPE_MIFARE_MINI; + case 0x08: return PICC_TYPE_MIFARE_1K; + case 0x18: return PICC_TYPE_MIFARE_4K; + case 0x00: return PICC_TYPE_MIFARE_UL; + case 0x10: + case 0x11: return PICC_TYPE_MIFARE_PLUS; + case 0x01: return PICC_TYPE_TNP3XXX; + case 0x20: + if (tag->atqa == 0x0344) + return PICC_TYPE_MIFARE_DESFIRE; + return PICC_TYPE_ISO_14443_4; + case 0x40: return PICC_TYPE_ISO_18092; + default: return PICC_TYPE_UNKNOWN; + } +} // End PICC_GetType() + +/** + * Dumps debug info about the selected PICC to Serial. + * On success the PICC is halted after dumping the data. + * For MIFARE Classic the factory default key of 0xFFFFFFFFFFFF is tried. + */ +void MFRC522Extended::PICC_DumpToSerial(TagInfo *tag) +{ + MIFARE_Key key; + + // Dump UID, SAK and Type + PICC_DumpDetailsToSerial(tag); + + // Dump contents + PICC_Type piccType = MFRC522::PICC_GetType(tag->uid.sak); + switch (piccType) { + case PICC_TYPE_MIFARE_MINI: + case PICC_TYPE_MIFARE_1K: + case PICC_TYPE_MIFARE_4K: + // All keys are set to FFFFFFFFFFFFh at chip delivery from the factory. + for (byte i = 0; i < 6; i++) { + key.keyByte[i] = 0xFF; + } + PICC_DumpMifareClassicToSerial(&tag->uid, piccType, &key); + break; + + case PICC_TYPE_MIFARE_UL: + PICC_DumpMifareUltralightToSerial(); + break; + + case PICC_TYPE_ISO_14443_4: + case PICC_TYPE_MIFARE_DESFIRE: + PICC_DumpISO14443_4(tag); + Serial.println(F("Dumping memory contents not implemented for that PICC type.")); + break; + case PICC_TYPE_ISO_18092: + case PICC_TYPE_MIFARE_PLUS: + case PICC_TYPE_TNP3XXX: + Serial.println(F("Dumping memory contents not implemented for that PICC type.")); + break; + + case PICC_TYPE_UNKNOWN: + case PICC_TYPE_NOT_COMPLETE: + default: + break; // No memory dump here + } + + Serial.println(); + PICC_HaltA(); // Already done if it was a MIFARE Classic PICC. +} + +/** + * Dumps card info (UID,SAK,Type) about the selected PICC to Serial. + */ +void MFRC522Extended::PICC_DumpDetailsToSerial(TagInfo *tag ///< Pointer to TagInfo struct returned from a successful PICC_Select(). +) { + // ATQA + Serial.print(F("Card ATQA:")); + if (((tag->atqa & 0xFF00u) >> 8) < 0x10) + Serial.print(F(" 0")); + Serial.print((tag->atqa & 0xFF00u) >> 8, HEX); + if ((tag->atqa & 0x00FFu) < 0x10) + Serial.print(F("0")); + else + Serial.print(F(" ")); + Serial.println(tag->atqa & 0x00FFu, HEX); + + // UID + Serial.print(F("Card UID:")); + for (byte i = 0; i < tag->uid.size; i++) { + if (tag->uid.uidByte[i] < 0x10) + Serial.print(F(" 0")); + else + Serial.print(F(" ")); + Serial.print(tag->uid.uidByte[i], HEX); + } + Serial.println(); + + // SAK + Serial.print(F("Card SAK: ")); + if (tag->uid.sak < 0x10) + Serial.print(F("0")); + Serial.println(tag->uid.sak, HEX); + + // (suggested) PICC type + PICC_Type piccType = PICC_GetType(tag); + Serial.print(F("PICC type: ")); + Serial.println(PICC_GetTypeName(piccType)); +} // End PICC_DumpDetailsToSerial() + +/** + * Dumps memory contents of a ISO-14443-4 PICC. + */ +void MFRC522Extended::PICC_DumpISO14443_4(TagInfo *tag) +{ + // ATS + if (tag->ats.size > 0x00) { // The first byte is the ATS length including the length byte + Serial.print(F("Card ATS:")); + for (byte offset = 0; offset < tag->ats.size; offset++) { + if (tag->ats.data[offset] < 0x10) + Serial.print(F(" 0")); + else + Serial.print(F(" ")); + Serial.print(tag->ats.data[offset], HEX); + } + Serial.println(); + } + +} // End PICC_DumpISO14443_4 + +///////////////////////////////////////////////////////////////////////////////////// +// Convenience functions - does not add extra functionality +///////////////////////////////////////////////////////////////////////////////////// + +/** + * Returns true if a PICC responds to PICC_CMD_REQA. + * Only "new" cards in state IDLE are invited. Sleeping cards in state HALT are ignored. + * + * @return bool + */ +bool MFRC522Extended::PICC_IsNewCardPresent() { + byte bufferATQA[2]; + byte bufferSize = sizeof(bufferATQA); + + // Reset baud rates + PCD_WriteRegister(TxModeReg, 0x00); + PCD_WriteRegister(RxModeReg, 0x00); + // Reset ModWidthReg + PCD_WriteRegister(ModWidthReg, 0x26); + + MFRC522::StatusCode result = PICC_RequestA(bufferATQA, &bufferSize); + + if (result == STATUS_OK || result == STATUS_COLLISION) { + tag.atqa = ((uint16_t)bufferATQA[1] << 8) | bufferATQA[0]; + tag.ats.size = 0; + tag.ats.fsc = 32; // default FSC value + + // Defaults for TA1 + tag.ats.ta1.transmitted = false; + tag.ats.ta1.sameD = false; + tag.ats.ta1.ds = MFRC522Extended::BITRATE_106KBITS; + tag.ats.ta1.dr = MFRC522Extended::BITRATE_106KBITS; + + // Defaults for TB1 + tag.ats.tb1.transmitted = false; + tag.ats.tb1.fwi = 0; // TODO: Don't know the default for this! + tag.ats.tb1.sfgi = 0; // The default value of SFGI is 0 (meaning that the card does not need any particular SFGT) + + // Defaults for TC1 + tag.ats.tc1.transmitted = false; + tag.ats.tc1.supportsCID = true; + tag.ats.tc1.supportsNAD = false; + + memset(tag.ats.data, 0, FIFO_SIZE - 2); + + tag.blockNumber = false; + return true; + } + return false; +} // End PICC_IsNewCardPresent() + +/** + * Simple wrapper around PICC_Select. + * Returns true if a UID could be read. + * Remember to call PICC_IsNewCardPresent(), PICC_RequestA() or PICC_WakeupA() first. + * The read UID is available in the class variable uid. + * + * @return bool + */ +bool MFRC522Extended::PICC_ReadCardSerial() { + MFRC522::StatusCode result = PICC_Select(&tag.uid); + + // Backward compatibility + uid.size = tag.uid.size; + uid.sak = tag.uid.sak; + memcpy(uid.uidByte, tag.uid.uidByte, sizeof(tag.uid.uidByte)); + + return (result == STATUS_OK); +} // End diff --git a/libraries/MFRC522/MFRC522Extended.h b/libraries/MFRC522/MFRC522Extended.h new file mode 100644 index 0000000..b0ce017 --- /dev/null +++ b/libraries/MFRC522/MFRC522Extended.h @@ -0,0 +1,119 @@ +/** + * Library extends MFRC522.h to support RATS for ISO-14443-4 PICC. + * RATS - Request for Answer To Select. + * @author JPG-Consulting + */ +#ifndef MFRC522Extended_h +#define MFRC522Extended_h + +#include +#include "MFRC522.h" + +class MFRC522Extended : public MFRC522 { + +public: + // ISO/IEC 14443-4 bit rates + enum TagBitRates : byte { + BITRATE_106KBITS = 0x00, + BITRATE_212KBITS = 0x01, + BITRATE_424KBITS = 0x02, + BITRATE_848KBITS = 0x03 + }; + + // Structure to store ISO/IEC 14443-4 ATS + typedef struct { + byte size; + byte fsc; // Frame size for proximity card + + struct { + bool transmitted; + bool sameD; // Only the same D for both directions supported + TagBitRates ds; // Send D + TagBitRates dr; // Receive D + } ta1; + + struct { + bool transmitted; + byte fwi; // Frame waiting time integer + byte sfgi; // Start-up frame guard time integer + } tb1; + + struct { + bool transmitted; + bool supportsCID; + bool supportsNAD; + } tc1; + + // Raw data from ATS + byte data[FIFO_SIZE - 2]; // ATS cannot be bigger than FSD - 2 bytes (CRC), according to ISO 14443-4 5.2.2 + } Ats; + + // A struct used for passing the PICC information + typedef struct { + uint16_t atqa; + Uid uid; + Ats ats; + + // For Block PCB + bool blockNumber; + } TagInfo; + + // A struct used for passing PCB Block + typedef struct { + struct { + byte pcb; + byte cid; + byte nad; + } prologue; + struct { + byte size; + byte *data; + } inf; + } PcbBlock; + + // Member variables + TagInfo tag; + + ///////////////////////////////////////////////////////////////////////////////////// + // Contructors + ///////////////////////////////////////////////////////////////////////////////////// + MFRC522Extended() : MFRC522() {}; + MFRC522Extended(uint8_t ss, uint8_t rst) : MFRC522(ss, rst) {}; + + ///////////////////////////////////////////////////////////////////////////////////// + // Functions for communicating with PICCs + ///////////////////////////////////////////////////////////////////////////////////// + StatusCode PICC_Select(Uid *uid, byte validBits = 0) override; // overrride + StatusCode PICC_RequestATS(Ats *ats); + StatusCode PICC_PPS(); // PPS command without bitrate parameter + StatusCode PICC_PPS(TagBitRates sendBitRate, TagBitRates receiveBitRate); // Different D values + + ///////////////////////////////////////////////////////////////////////////////////// + // Functions for communicating with ISO/IEC 14433-4 cards + ///////////////////////////////////////////////////////////////////////////////////// + StatusCode TCL_Transceive(PcbBlock *send, PcbBlock *back); + StatusCode TCL_Transceive(TagInfo * tag, byte *sendData, byte sendLen, byte *backData = NULL, byte *backLen = NULL); + StatusCode TCL_TransceiveRBlock(TagInfo *tag, bool ack, byte *backData = NULL, byte *backLen = NULL); + StatusCode TCL_Deselect(TagInfo *tag); + + ///////////////////////////////////////////////////////////////////////////////////// + // Support functions + ///////////////////////////////////////////////////////////////////////////////////// + static PICC_Type PICC_GetType(TagInfo *tag); + using MFRC522::PICC_GetType;// // make old PICC_GetType(byte sak) available, otherwise would be hidden by PICC_GetType(TagInfo *tag) + + // Support functions for debuging + void PICC_DumpToSerial(TagInfo *tag); + using MFRC522::PICC_DumpToSerial; // make old PICC_DumpToSerial(Uid *uid) available, otherwise would be hidden by PICC_DumpToSerial(TagInfo *tag) + void PICC_DumpDetailsToSerial(TagInfo *tag); + using MFRC522::PICC_DumpDetailsToSerial; // make old PICC_DumpDetailsToSerial(Uid *uid) available, otherwise would be hidden by PICC_DumpDetailsToSerial(TagInfo *tag) + void PICC_DumpISO14443_4(TagInfo *tag); + + ///////////////////////////////////////////////////////////////////////////////////// + // Convenience functions - does not add extra functionality + ///////////////////////////////////////////////////////////////////////////////////// + bool PICC_IsNewCardPresent() override; // overrride + bool PICC_ReadCardSerial() override; // overrride +}; + +#endif diff --git a/libraries/MFRC522/MFRC522Hack.cpp b/libraries/MFRC522/MFRC522Hack.cpp new file mode 100644 index 0000000..aca169f --- /dev/null +++ b/libraries/MFRC522/MFRC522Hack.cpp @@ -0,0 +1,203 @@ +#include "MFRC522Hack.h" + +/** + * Performs the "magic sequence" needed to get Chinese UID changeable + * Mifare cards to allow writing to sector 0, where the card UID is stored. + * + * Note that you do not need to have selected the card through REQA or WUPA, + * this sequence works immediately when the card is in the reader vicinity. + * This means you can use this method even on "bricked" cards that your reader does + * not recognise anymore (see MFRC522Hack::MIFARE_UnbrickUidSector). + * + * Of course with non-bricked devices, you're free to select them before calling this function. + */ +bool MFRC522Hack::MIFARE_OpenUidBackdoor(const bool logErrors) const { + // Magic sequence: + // > 50 00 57 CD (HALT + CRC) + // > 40 (7 bits only) + // < A (4 bits only) + // > 43 + // < A (4 bits only) + // Then you can write to sector 0 without authenticating + + _device->PICC_HaltA(); // 50 00 57 CD + + byte cmd = 0x40; + byte validBits = 7; /* Our command is only 7 bits. After receiving card response, + this will contain amount of valid response bits. */ + byte response[32]; // Card's response is written here + byte received; + MFRC522::StatusCode status = _device->PCD_TransceiveData(&cmd, (byte) 1, response, &received, &validBits, (byte) 0, + false); // 40 + if (status != MFRC522::STATUS_OK) { + if (logErrors) { + Serial.println( + F("Card did not respond to 0x40 after HALT command. Are you sure it is a UID changeable one?")); + Serial.print(F("Error name: ")); + Serial.println(MFRC522Debug::GetStatusCodeName(status)); + } + return false; + } + if (received != 1 || response[0] != 0x0A) { + if (logErrors) { + Serial.print(F("Got bad response on backdoor 0x40 command: ")); + Serial.print(response[0], HEX); + Serial.print(F(" (")); + Serial.print(validBits); + Serial.print(F(" valid bits)\r\n")); + } + return false; + } + + cmd = 0x43; + validBits = 8; + status = _device->PCD_TransceiveData(&cmd, (byte) 1, response, &received, &validBits, (byte) 0, false); // 43 + if (status != MFRC522::STATUS_OK) { + if (logErrors) { + Serial.println(F("Error in communication at command 0x43, after successfully executing 0x40")); + Serial.print(F("Error name: ")); + Serial.println(MFRC522Debug::GetStatusCodeName(status)); + } + return false; + } + if (received != 1 || response[0] != 0x0A) { + if (logErrors) { + Serial.print(F("Got bad response on backdoor 0x43 command: ")); + Serial.print(response[0], HEX); + Serial.print(F(" (")); + Serial.print(validBits); + Serial.print(F(" valid bits)\r\n")); + } + return false; + } + + // You can now write to sector 0 without authenticating! + return true; +} // End MIFARE_OpenUidBackdoor() + +/** + * Reads entire block 0, including all manufacturer data, and overwrites + * that block with the new UID, a freshly calculated BCC, and the original + * manufacturer data. + * + * It assumes a default KEY A of 0xFFFFFFFFFFFF. + * Make sure to have selected the card before this function is called. + */ +bool MFRC522Hack::MIFARE_SetUid(const byte *newUid, const byte uidSize, const bool logErrors) const { + + // UID + BCC byte can not be larger than 16 together + if (!newUid || !uidSize || uidSize > 15) { + if (logErrors) { + Serial.println(F("New UID buffer empty, size 0, or size > 15 given")); + } + return false; + } + + // Authenticate for reading + MFRC522::MIFARE_Key key = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; + MFRC522::StatusCode status = _device->PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, (byte) 1, &key, &(_device->uid)); + if (status != MFRC522::STATUS_OK) { + + if (status == MFRC522::STATUS_TIMEOUT) { + // We get a read timeout if no card is selected yet, so let's select one + + // Wake the card up again if sleeping +// byte atqa_answer[2]; +// byte atqa_size = 2; +// PICC_WakeupA(atqa_answer, &atqa_size); + + if (!_device->PICC_IsNewCardPresent() || !_device->PICC_ReadCardSerial()) { + Serial.println(F("No card was previously selected, and none are available. Failed to set UID.")); + return false; + } + + status = _device->PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, (byte) 1, &key, &(_device->uid)); + if (status != MFRC522::STATUS_OK) { + // We tried, time to give up + if (logErrors) { + Serial.println(F("Failed to authenticate to card for reading, could not set UID: ")); + Serial.println(MFRC522Debug::GetStatusCodeName(status)); + } + return false; + } + } else { + if (logErrors) { + Serial.print(F("PCD_Authenticate() failed: ")); + Serial.println(MFRC522Debug::GetStatusCodeName(status)); + } + return false; + } + } + + // Read block 0 + byte block0_buffer[18]; + byte byteCount = sizeof(block0_buffer); + status = _device->MIFARE_Read((byte) 0, block0_buffer, &byteCount); + if (status != MFRC522::STATUS_OK) { + if (logErrors) { + Serial.print(F("MIFARE_Read() failed: ")); + Serial.println(MFRC522Debug::GetStatusCodeName(status)); + Serial.println(F("Are you sure your KEY A for sector 0 is 0xFFFFFFFFFFFF?")); + } + return false; + } + + // Write new UID to the data we just read, and calculate BCC byte + byte bcc = 0; + for (uint8_t i = 0; i < uidSize; i++) { + block0_buffer[i] = newUid[i]; + bcc ^= newUid[i]; + } + + // Write BCC byte to buffer + block0_buffer[uidSize] = bcc; + + // Stop encrypted traffic so we can send raw bytes + _device->PCD_StopCrypto1(); + + // Activate UID backdoor + if (!MIFARE_OpenUidBackdoor(logErrors)) { + if (logErrors) { + Serial.println(F("Activating the UID backdoor failed.")); + } + return false; + } + + // Write modified block 0 back to card + status = _device->MIFARE_Write((byte) 0, block0_buffer, (byte) 16); + if (status != MFRC522::STATUS_OK) { + if (logErrors) { + Serial.print(F("MIFARE_Write() failed: ")); + Serial.println(MFRC522Debug::GetStatusCodeName(status)); + } + return false; + } + + // Wake the card up again + byte atqa_answer[2]; + byte atqa_size = 2; + _device->PICC_WakeupA(atqa_answer, &atqa_size); + + return true; +} + +/** + * Resets entire sector 0 to zeroes, so the card can be read again by readers. + */ +bool MFRC522Hack::MIFARE_UnbrickUidSector(const bool logErrors) const { + MIFARE_OpenUidBackdoor(logErrors); + + byte block0_buffer[] = {0x01, 0x02, 0x03, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00}; + + // Write modified block 0 back to card + MFRC522::StatusCode status = _device->MIFARE_Write((byte) 0, block0_buffer, (byte) 16); + if (status != MFRC522::STATUS_OK) { + if (logErrors) { + Serial.print(F("MIFARE_Write() failed: ")); + Serial.println(MFRC522Debug::GetStatusCodeName(status)); + } + return false; + } + return true; +} diff --git a/libraries/MFRC522/MFRC522Hack.h b/libraries/MFRC522/MFRC522Hack.h new file mode 100644 index 0000000..c3aba44 --- /dev/null +++ b/libraries/MFRC522/MFRC522Hack.h @@ -0,0 +1,22 @@ +#ifndef MFRC522HACK_H +#define MFRC522HACK_H + +#include +#include "MFRC522.h" +#include "MFRC522Debug.h" + +class MFRC522Hack { +private: + MFRC522 *const _device; +public: + MFRC522Hack(MFRC522 *const device) : _device(device) {}; + + bool MIFARE_OpenUidBackdoor(const bool logErrors) const; + + bool MIFARE_SetUid(const byte *newUid, const byte uidSize, const bool logErrors) const; + + bool MIFARE_UnbrickUidSector(const bool logErrors) const; +}; + + +#endif //MFRC522HACK_H diff --git a/libraries/MFRC522/deprecated.h b/libraries/MFRC522/deprecated.h new file mode 100644 index 0000000..cba2cf2 --- /dev/null +++ b/libraries/MFRC522/deprecated.h @@ -0,0 +1,20 @@ +/** + * Copyright (c) 2016 by Ludwig Grill (www.rotzbua.de) + * Simple deprecated workaround for Arduino IDE + * IDE 1.6.8 use gcc 4.8 which do not support c++14 [[deprecated]] + * Later versions should support c++14, then use c++14 syntax + */ +#ifndef DEPRECATED_H +#define DEPRECATED_H + +#ifdef __has_cpp_attribute +#if __has_cpp_attribute(deprecated) +#define DEPRECATED [[deprecated]] +#define DEPRECATED_MSG(msg) [[deprecated(msg)]] +#endif // __has_cpp_attribute(deprecated) +#else +#define DEPRECATED __attribute__((deprecated)) +#define DEPRECATED_MSG(msg) __attribute__((deprecated(msg))) +#endif // __has_cpp_attribute + +#endif // DEPRECATED_H diff --git a/libraries/MFRC522/require_cpp11.h b/libraries/MFRC522/require_cpp11.h new file mode 100644 index 0000000..6561cd8 --- /dev/null +++ b/libraries/MFRC522/require_cpp11.h @@ -0,0 +1,12 @@ +/** + * Copyright (c) 2016 by Ludwig Grill (www.rotzbua.de) + * Throws error if c++11 is not supported + */ +#ifndef REQUIRE_CPP11_H +#define REQUIRE_CPP11_H + +#if __cplusplus < 201103L +#error "This library needs at least a C++11 compliant compiler, maybe compiler argument for C++11 support is missing or if you use Arduino IDE upgrade to version >=1.6.6" +#endif + +#endif // REQUIRE_CPP11_H diff --git a/libraries/OneWire/OneWire.cpp b/libraries/OneWire/OneWire.cpp new file mode 100644 index 0000000..38bf4ee --- /dev/null +++ b/libraries/OneWire/OneWire.cpp @@ -0,0 +1,580 @@ +/* +Copyright (c) 2007, Jim Studt (original old version - many contributors since) + +The latest version of this library may be found at: + http://www.pjrc.com/teensy/td_libs_OneWire.html + +OneWire has been maintained by Paul Stoffregen (paul@pjrc.com) since +January 2010. + +DO NOT EMAIL for technical support, especially not for ESP chips! +All project support questions must be posted on public forums +relevant to the board or chips used. If using Arduino, post on +Arduino's forum. If using ESP, post on the ESP community forums. +There is ABSOLUTELY NO TECH SUPPORT BY PRIVATE EMAIL! + +Github's issue tracker for OneWire should be used only to report +specific bugs. DO NOT request project support via Github. All +project and tech support questions must be posted on forums, not +github issues. If you experience a problem and you are not +absolutely sure it's an issue with the library, ask on a forum +first. Only use github to report issues after experts have +confirmed the issue is with OneWire rather than your project. + +Back in 2010, OneWire was in need of many bug fixes, but had +been abandoned the original author (Jim Studt). None of the known +contributors were interested in maintaining OneWire. Paul typically +works on OneWire every 6 to 12 months. Patches usually wait that +long. If anyone is interested in more actively maintaining OneWire, +please contact Paul (this is pretty much the only reason to use +private email about OneWire). + +OneWire is now very mature code. No changes other than adding +definitions for newer hardware support are anticipated. + +Version 2.3: + Unknown chip fallback mode, Roger Clark + Teensy-LC compatibility, Paul Stoffregen + Search bug fix, Love Nystrom + +Version 2.2: + Teensy 3.0 compatibility, Paul Stoffregen, paul@pjrc.com + Arduino Due compatibility, http://arduino.cc/forum/index.php?topic=141030 + Fix DS18B20 example negative temperature + Fix DS18B20 example's low res modes, Ken Butcher + Improve reset timing, Mark Tillotson + Add const qualifiers, Bertrik Sikken + Add initial value input to crc16, Bertrik Sikken + Add target_search() function, Scott Roberts + +Version 2.1: + Arduino 1.0 compatibility, Paul Stoffregen + Improve temperature example, Paul Stoffregen + DS250x_PROM example, Guillermo Lovato + PIC32 (chipKit) compatibility, Jason Dangel, dangel.jason AT gmail.com + Improvements from Glenn Trewitt: + - crc16() now works + - check_crc16() does all of calculation/checking work. + - Added read_bytes() and write_bytes(), to reduce tedious loops. + - Added ds2408 example. + Delete very old, out-of-date readme file (info is here) + +Version 2.0: Modifications by Paul Stoffregen, January 2010: +http://www.pjrc.com/teensy/td_libs_OneWire.html + Search fix from Robin James + http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1238032295/27#27 + Use direct optimized I/O in all cases + Disable interrupts during timing critical sections + (this solves many random communication errors) + Disable interrupts during read-modify-write I/O + Reduce RAM consumption by eliminating unnecessary + variables and trimming many to 8 bits + Optimize both crc8 - table version moved to flash + +Modified to work with larger numbers of devices - avoids loop. +Tested in Arduino 11 alpha with 12 sensors. +26 Sept 2008 -- Robin James +http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1238032295/27#27 + +Updated to work with arduino-0008 and to include skip() as of +2007/07/06. --RJL20 + +Modified to calculate the 8-bit CRC directly, avoiding the need for +the 256-byte lookup table to be loaded in RAM. Tested in arduino-0010 +-- Tom Pollard, Jan 23, 2008 + +Jim Studt's original library was modified by Josh Larios. + +Tom Pollard, pollard@alum.mit.edu, contributed around May 20, 2008 + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +Much of the code was inspired by Derek Yerger's code, though I don't +think much of that remains. In any event that was.. + (copyleft) 2006 by Derek Yerger - Free to distribute freely. + +The CRC code was excerpted and inspired by the Dallas Semiconductor +sample code bearing this copyright. +//--------------------------------------------------------------------------- +// Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES +// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// +// Except as contained in this notice, the name of Dallas Semiconductor +// shall not be used except as stated in the Dallas Semiconductor +// Branding Policy. +//-------------------------------------------------------------------------- +*/ + +#include +#include "OneWire.h" +#include "util/OneWire_direct_gpio.h" + + +void OneWire::begin(uint8_t pin) +{ + pinMode(pin, INPUT); + bitmask = PIN_TO_BITMASK(pin); + baseReg = PIN_TO_BASEREG(pin); +#if ONEWIRE_SEARCH + reset_search(); +#endif +} + + +// Perform the onewire reset function. We will wait up to 250uS for +// the bus to come high, if it doesn't then it is broken or shorted +// and we return a 0; +// +// Returns 1 if a device asserted a presence pulse, 0 otherwise. +// +uint8_t OneWire::reset(void) +{ + IO_REG_TYPE mask IO_REG_MASK_ATTR = bitmask; + volatile IO_REG_TYPE *reg IO_REG_BASE_ATTR = baseReg; + uint8_t r; + uint8_t retries = 125; + + noInterrupts(); + DIRECT_MODE_INPUT(reg, mask); + interrupts(); + // wait until the wire is high... just in case + do { + if (--retries == 0) return 0; + delayMicroseconds(2); + } while ( !DIRECT_READ(reg, mask)); + + noInterrupts(); + DIRECT_WRITE_LOW(reg, mask); + DIRECT_MODE_OUTPUT(reg, mask); // drive output low + interrupts(); + delayMicroseconds(480); + noInterrupts(); + DIRECT_MODE_INPUT(reg, mask); // allow it to float + delayMicroseconds(70); + r = !DIRECT_READ(reg, mask); + interrupts(); + delayMicroseconds(410); + return r; +} + +// +// Write a bit. Port and bit is used to cut lookup time and provide +// more certain timing. +// +void OneWire::write_bit(uint8_t v) +{ + IO_REG_TYPE mask IO_REG_MASK_ATTR = bitmask; + volatile IO_REG_TYPE *reg IO_REG_BASE_ATTR = baseReg; + + if (v & 1) { + noInterrupts(); + DIRECT_WRITE_LOW(reg, mask); + DIRECT_MODE_OUTPUT(reg, mask); // drive output low + delayMicroseconds(10); + DIRECT_WRITE_HIGH(reg, mask); // drive output high + interrupts(); + delayMicroseconds(55); + } else { + noInterrupts(); + DIRECT_WRITE_LOW(reg, mask); + DIRECT_MODE_OUTPUT(reg, mask); // drive output low + delayMicroseconds(65); + DIRECT_WRITE_HIGH(reg, mask); // drive output high + interrupts(); + delayMicroseconds(5); + } +} + +// +// Read a bit. Port and bit is used to cut lookup time and provide +// more certain timing. +// +uint8_t OneWire::read_bit(void) +{ + IO_REG_TYPE mask IO_REG_MASK_ATTR = bitmask; + volatile IO_REG_TYPE *reg IO_REG_BASE_ATTR = baseReg; + uint8_t r; + + noInterrupts(); + DIRECT_MODE_OUTPUT(reg, mask); + DIRECT_WRITE_LOW(reg, mask); + delayMicroseconds(3); + DIRECT_MODE_INPUT(reg, mask); // let pin float, pull up will raise + delayMicroseconds(10); + r = DIRECT_READ(reg, mask); + interrupts(); + delayMicroseconds(53); + return r; +} + +// +// Write a byte. The writing code uses the active drivers to raise the +// pin high, if you need power after the write (e.g. DS18S20 in +// parasite power mode) then set 'power' to 1, otherwise the pin will +// go tri-state at the end of the write to avoid heating in a short or +// other mishap. +// +void OneWire::write(uint8_t v, uint8_t power /* = 0 */) { + uint8_t bitMask; + + for (bitMask = 0x01; bitMask; bitMask <<= 1) { + OneWire::write_bit( (bitMask & v)?1:0); + } + if ( !power) { + noInterrupts(); + DIRECT_MODE_INPUT(baseReg, bitmask); + DIRECT_WRITE_LOW(baseReg, bitmask); + interrupts(); + } +} + +void OneWire::write_bytes(const uint8_t *buf, uint16_t count, bool power /* = 0 */) { + for (uint16_t i = 0 ; i < count ; i++) + write(buf[i]); + if (!power) { + noInterrupts(); + DIRECT_MODE_INPUT(baseReg, bitmask); + DIRECT_WRITE_LOW(baseReg, bitmask); + interrupts(); + } +} + +// +// Read a byte +// +uint8_t OneWire::read() { + uint8_t bitMask; + uint8_t r = 0; + + for (bitMask = 0x01; bitMask; bitMask <<= 1) { + if ( OneWire::read_bit()) r |= bitMask; + } + return r; +} + +void OneWire::read_bytes(uint8_t *buf, uint16_t count) { + for (uint16_t i = 0 ; i < count ; i++) + buf[i] = read(); +} + +// +// Do a ROM select +// +void OneWire::select(const uint8_t rom[8]) +{ + uint8_t i; + + write(0x55); // Choose ROM + + for (i = 0; i < 8; i++) write(rom[i]); +} + +// +// Do a ROM skip +// +void OneWire::skip() +{ + write(0xCC); // Skip ROM +} + +void OneWire::depower() +{ + noInterrupts(); + DIRECT_MODE_INPUT(baseReg, bitmask); + interrupts(); +} + +#if ONEWIRE_SEARCH + +// +// You need to use this function to start a search again from the beginning. +// You do not need to do it for the first search, though you could. +// +void OneWire::reset_search() +{ + // reset the search state + LastDiscrepancy = 0; + LastDeviceFlag = false; + LastFamilyDiscrepancy = 0; + for(int i = 7; ; i--) { + ROM_NO[i] = 0; + if ( i == 0) break; + } +} + +// Setup the search to find the device type 'family_code' on the next call +// to search(*newAddr) if it is present. +// +void OneWire::target_search(uint8_t family_code) +{ + // set the search state to find SearchFamily type devices + ROM_NO[0] = family_code; + for (uint8_t i = 1; i < 8; i++) + ROM_NO[i] = 0; + LastDiscrepancy = 64; + LastFamilyDiscrepancy = 0; + LastDeviceFlag = false; +} + +// +// Perform a search. If this function returns a '1' then it has +// enumerated the next device and you may retrieve the ROM from the +// OneWire::address variable. If there are no devices, no further +// devices, or something horrible happens in the middle of the +// enumeration then a 0 is returned. If a new device is found then +// its address is copied to newAddr. Use OneWire::reset_search() to +// start over. +// +// --- Replaced by the one from the Dallas Semiconductor web site --- +//-------------------------------------------------------------------------- +// Perform the 1-Wire Search Algorithm on the 1-Wire bus using the existing +// search state. +// Return TRUE : device found, ROM number in ROM_NO buffer +// FALSE : device not found, end of search +// +bool OneWire::search(uint8_t *newAddr, bool search_mode /* = true */) +{ + uint8_t id_bit_number; + uint8_t last_zero, rom_byte_number; + bool search_result; + uint8_t id_bit, cmp_id_bit; + + unsigned char rom_byte_mask, search_direction; + + // initialize for search + id_bit_number = 1; + last_zero = 0; + rom_byte_number = 0; + rom_byte_mask = 1; + search_result = false; + + // if the last call was not the last one + if (!LastDeviceFlag) { + // 1-Wire reset + if (!reset()) { + // reset the search + LastDiscrepancy = 0; + LastDeviceFlag = false; + LastFamilyDiscrepancy = 0; + return false; + } + + // issue the search command + if (search_mode == true) { + write(0xF0); // NORMAL SEARCH + } else { + write(0xEC); // CONDITIONAL SEARCH + } + + // loop to do the search + do + { + // read a bit and its complement + id_bit = read_bit(); + cmp_id_bit = read_bit(); + + // check for no devices on 1-wire + if ((id_bit == 1) && (cmp_id_bit == 1)) { + break; + } else { + // all devices coupled have 0 or 1 + if (id_bit != cmp_id_bit) { + search_direction = id_bit; // bit write value for search + } else { + // if this discrepancy if before the Last Discrepancy + // on a previous next then pick the same as last time + if (id_bit_number < LastDiscrepancy) { + search_direction = ((ROM_NO[rom_byte_number] & rom_byte_mask) > 0); + } else { + // if equal to last pick 1, if not then pick 0 + search_direction = (id_bit_number == LastDiscrepancy); + } + // if 0 was picked then record its position in LastZero + if (search_direction == 0) { + last_zero = id_bit_number; + + // check for Last discrepancy in family + if (last_zero < 9) + LastFamilyDiscrepancy = last_zero; + } + } + + // set or clear the bit in the ROM byte rom_byte_number + // with mask rom_byte_mask + if (search_direction == 1) + ROM_NO[rom_byte_number] |= rom_byte_mask; + else + ROM_NO[rom_byte_number] &= ~rom_byte_mask; + + // serial number search direction write bit + write_bit(search_direction); + + // increment the byte counter id_bit_number + // and shift the mask rom_byte_mask + id_bit_number++; + rom_byte_mask <<= 1; + + // if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask + if (rom_byte_mask == 0) { + rom_byte_number++; + rom_byte_mask = 1; + } + } + } + while(rom_byte_number < 8); // loop until through all ROM bytes 0-7 + + // if the search was successful then + if (!(id_bit_number < 65)) { + // search successful so set LastDiscrepancy,LastDeviceFlag,search_result + LastDiscrepancy = last_zero; + + // check for last device + if (LastDiscrepancy == 0) { + LastDeviceFlag = true; + } + search_result = true; + } + } + + // if no device found then reset counters so next 'search' will be like a first + if (!search_result || !ROM_NO[0]) { + LastDiscrepancy = 0; + LastDeviceFlag = false; + LastFamilyDiscrepancy = 0; + search_result = false; + } else { + for (int i = 0; i < 8; i++) newAddr[i] = ROM_NO[i]; + } + return search_result; + } + +#endif + +#if ONEWIRE_CRC +// The 1-Wire CRC scheme is described in Maxim Application Note 27: +// "Understanding and Using Cyclic Redundancy Checks with Maxim iButton Products" +// + +#if ONEWIRE_CRC8_TABLE +// Dow-CRC using polynomial X^8 + X^5 + X^4 + X^0 +// Tiny 2x16 entry CRC table created by Arjen Lentz +// See http://lentz.com.au/blog/calculating-crc-with-a-tiny-32-entry-lookup-table +static const uint8_t PROGMEM dscrc2x16_table[] = { + 0x00, 0x5E, 0xBC, 0xE2, 0x61, 0x3F, 0xDD, 0x83, + 0xC2, 0x9C, 0x7E, 0x20, 0xA3, 0xFD, 0x1F, 0x41, + 0x00, 0x9D, 0x23, 0xBE, 0x46, 0xDB, 0x65, 0xF8, + 0x8C, 0x11, 0xAF, 0x32, 0xCA, 0x57, 0xE9, 0x74 +}; + +// Compute a Dallas Semiconductor 8 bit CRC. These show up in the ROM +// and the registers. (Use tiny 2x16 entry CRC table) +uint8_t OneWire::crc8(const uint8_t *addr, uint8_t len) +{ + uint8_t crc = 0; + + while (len--) { + crc = *addr++ ^ crc; // just re-using crc as intermediate + crc = pgm_read_byte(dscrc2x16_table + (crc & 0x0f)) ^ + pgm_read_byte(dscrc2x16_table + 16 + ((crc >> 4) & 0x0f)); + } + + return crc; +} +#else +// +// Compute a Dallas Semiconductor 8 bit CRC directly. +// this is much slower, but a little smaller, than the lookup table. +// +uint8_t OneWire::crc8(const uint8_t *addr, uint8_t len) +{ + uint8_t crc = 0; + + while (len--) { +#if defined(__AVR__) + crc = _crc_ibutton_update(crc, *addr++); +#else + uint8_t inbyte = *addr++; + for (uint8_t i = 8; i; i--) { + uint8_t mix = (crc ^ inbyte) & 0x01; + crc >>= 1; + if (mix) crc ^= 0x8C; + inbyte >>= 1; + } +#endif + } + return crc; +} +#endif + +#if ONEWIRE_CRC16 +bool OneWire::check_crc16(const uint8_t* input, uint16_t len, const uint8_t* inverted_crc, uint16_t crc) +{ + crc = ~crc16(input, len, crc); + return (crc & 0xFF) == inverted_crc[0] && (crc >> 8) == inverted_crc[1]; +} + +uint16_t OneWire::crc16(const uint8_t* input, uint16_t len, uint16_t crc) +{ +#if defined(__AVR__) + for (uint16_t i = 0 ; i < len ; i++) { + crc = _crc16_update(crc, input[i]); + } +#else + static const uint8_t oddparity[16] = + { 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0 }; + + for (uint16_t i = 0 ; i < len ; i++) { + // Even though we're just copying a byte from the input, + // we'll be doing 16-bit computation with it. + uint16_t cdata = input[i]; + cdata = (cdata ^ crc) & 0xff; + crc >>= 8; + + if (oddparity[cdata & 0x0F] ^ oddparity[cdata >> 4]) + crc ^= 0xC001; + + cdata <<= 6; + crc ^= cdata; + cdata <<= 1; + crc ^= cdata; + } +#endif + return crc; +} +#endif + +#endif diff --git a/libraries/OneWire/OneWire.h b/libraries/OneWire/OneWire.h new file mode 100644 index 0000000..a7bfab7 --- /dev/null +++ b/libraries/OneWire/OneWire.h @@ -0,0 +1,182 @@ +#ifndef OneWire_h +#define OneWire_h + +#ifdef __cplusplus + +#include + +#if defined(__AVR__) +#include +#endif + +#if ARDUINO >= 100 +#include // for delayMicroseconds, digitalPinToBitMask, etc +#else +#include "WProgram.h" // for delayMicroseconds +#include "pins_arduino.h" // for digitalPinToBitMask, etc +#endif + +// You can exclude certain features from OneWire. In theory, this +// might save some space. In practice, the compiler automatically +// removes unused code (technically, the linker, using -fdata-sections +// and -ffunction-sections when compiling, and Wl,--gc-sections +// when linking), so most of these will not result in any code size +// reduction. Well, unless you try to use the missing features +// and redesign your program to not need them! ONEWIRE_CRC8_TABLE +// is the exception, because it selects a fast but large algorithm +// or a small but slow algorithm. + +// you can exclude onewire_search by defining that to 0 +#ifndef ONEWIRE_SEARCH +#define ONEWIRE_SEARCH 1 +#endif + +// You can exclude CRC checks altogether by defining this to 0 +#ifndef ONEWIRE_CRC +#define ONEWIRE_CRC 1 +#endif + +// Select the table-lookup method of computing the 8-bit CRC +// by setting this to 1. The lookup table enlarges code size by +// about 250 bytes. It does NOT consume RAM (but did in very +// old versions of OneWire). If you disable this, a slower +// but very compact algorithm is used. +#ifndef ONEWIRE_CRC8_TABLE +#define ONEWIRE_CRC8_TABLE 1 +#endif + +// You can allow 16-bit CRC checks by defining this to 1 +// (Note that ONEWIRE_CRC must also be 1.) +#ifndef ONEWIRE_CRC16 +#define ONEWIRE_CRC16 1 +#endif + +// Board-specific macros for direct GPIO +#include "util/OneWire_direct_regtype.h" + +class OneWire +{ + private: + IO_REG_TYPE bitmask; + volatile IO_REG_TYPE *baseReg; + +#if ONEWIRE_SEARCH + // global search state + unsigned char ROM_NO[8]; + uint8_t LastDiscrepancy; + uint8_t LastFamilyDiscrepancy; + bool LastDeviceFlag; +#endif + + public: + OneWire() { } + OneWire(uint8_t pin) { begin(pin); } + void begin(uint8_t pin); + + // Perform a 1-Wire reset cycle. Returns 1 if a device responds + // with a presence pulse. Returns 0 if there is no device or the + // bus is shorted or otherwise held low for more than 250uS + uint8_t reset(void); + + // Issue a 1-Wire rom select command, you do the reset first. + void select(const uint8_t rom[8]); + + // Issue a 1-Wire rom skip command, to address all on bus. + void skip(void); + + // Write a byte. If 'power' is one then the wire is held high at + // the end for parasitically powered devices. You are responsible + // for eventually depowering it by calling depower() or doing + // another read or write. + void write(uint8_t v, uint8_t power = 0); + + void write_bytes(const uint8_t *buf, uint16_t count, bool power = 0); + + // Read a byte. + uint8_t read(void); + + void read_bytes(uint8_t *buf, uint16_t count); + + // Write a bit. The bus is always left powered at the end, see + // note in write() about that. + void write_bit(uint8_t v); + + // Read a bit. + uint8_t read_bit(void); + + // Stop forcing power onto the bus. You only need to do this if + // you used the 'power' flag to write() or used a write_bit() call + // and aren't about to do another read or write. You would rather + // not leave this powered if you don't have to, just in case + // someone shorts your bus. + void depower(void); + +#if ONEWIRE_SEARCH + // Clear the search state so that if will start from the beginning again. + void reset_search(); + + // Setup the search to find the device type 'family_code' on the next call + // to search(*newAddr) if it is present. + void target_search(uint8_t family_code); + + // Look for the next device. Returns 1 if a new address has been + // returned. A zero might mean that the bus is shorted, there are + // no devices, or you have already retrieved all of them. It + // might be a good idea to check the CRC to make sure you didn't + // get garbage. The order is deterministic. You will always get + // the same devices in the same order. + bool search(uint8_t *newAddr, bool search_mode = true); +#endif + +#if ONEWIRE_CRC + // Compute a Dallas Semiconductor 8 bit CRC, these are used in the + // ROM and scratchpad registers. + static uint8_t crc8(const uint8_t *addr, uint8_t len); + +#if ONEWIRE_CRC16 + // Compute the 1-Wire CRC16 and compare it against the received CRC. + // Example usage (reading a DS2408): + // // Put everything in a buffer so we can compute the CRC easily. + // uint8_t buf[13]; + // buf[0] = 0xF0; // Read PIO Registers + // buf[1] = 0x88; // LSB address + // buf[2] = 0x00; // MSB address + // WriteBytes(net, buf, 3); // Write 3 cmd bytes + // ReadBytes(net, buf+3, 10); // Read 6 data bytes, 2 0xFF, 2 CRC16 + // if (!CheckCRC16(buf, 11, &buf[11])) { + // // Handle error. + // } + // + // @param input - Array of bytes to checksum. + // @param len - How many bytes to use. + // @param inverted_crc - The two CRC16 bytes in the received data. + // This should just point into the received data, + // *not* at a 16-bit integer. + // @param crc - The crc starting value (optional) + // @return True, iff the CRC matches. + static bool check_crc16(const uint8_t* input, uint16_t len, const uint8_t* inverted_crc, uint16_t crc = 0); + + // Compute a Dallas Semiconductor 16 bit CRC. This is required to check + // the integrity of data received from many 1-Wire devices. Note that the + // CRC computed here is *not* what you'll get from the 1-Wire network, + // for two reasons: + // 1) The CRC is transmitted bitwise inverted. + // 2) Depending on the endian-ness of your processor, the binary + // representation of the two-byte return value may have a different + // byte order than the two bytes you get from 1-Wire. + // @param input - Array of bytes to checksum. + // @param len - How many bytes to use. + // @param crc - The crc starting value (optional) + // @return The CRC16, as defined by Dallas Semiconductor. + static uint16_t crc16(const uint8_t* input, uint16_t len, uint16_t crc = 0); +#endif +#endif +}; + +// Prevent this name from leaking into Arduino sketches +#ifdef IO_REG_TYPE +#undef IO_REG_TYPE +#endif + +#endif // __cplusplus +#endif // OneWire_h diff --git a/libraries/OneWire/docs/issue_template.md b/libraries/OneWire/docs/issue_template.md new file mode 100644 index 0000000..0610992 --- /dev/null +++ b/libraries/OneWire/docs/issue_template.md @@ -0,0 +1,64 @@ +Please use this form only to report code defects or bugs. + +For any question, even questions directly pertaining to this code, post your question on the forums related to the board you are using. + +Arduino: forum.arduino.cc +Teensy: forum.pjrc.com +ESP8266: www.esp8266.com +ESP32: www.esp32.com +Adafruit Feather/Metro/Trinket: forums.adafruit.com +Particle Photon: community.particle.io + +If you are experiencing trouble but not certain of the cause, or need help using this code, ask on the appropriate forum. This is not the place to ask for support or help, even directly related to this code. Only use this form you are certain you have discovered a defect in this code! + +Please verify the problem occurs when using the very latest version, using the newest version of Arduino and any other related software. + + +----------------------------- Remove above ----------------------------- + + + +### Description + +Describe your problem. + + + +### Steps To Reproduce Problem + +Please give detailed instructions needed for anyone to attempt to reproduce the problem. + + + +### Hardware & Software + +Board +Shields / modules used +Arduino IDE version +Teensyduino version (if using Teensy) +Version info & package name (from Tools > Boards > Board Manager) +Operating system & version +Any other software or hardware? + + +### Arduino Sketch + +```cpp +// Change the code below by your sketch (please try to give the smallest code which demonstrates the problem) +#include + +// libraries: give links/details so anyone can compile your code for the same result + +void setup() { +} + +void loop() { +} +``` + + +### Errors or Incorrect Output + +If you see any errors or incorrect output, please show it here. Please use copy & paste to give an exact copy of the message. Details matter, so please show (not merely describe) the actual message or error exactly as it appears. + + diff --git a/libraries/OneWire/examples/DS18x20_Temperature/DS18x20_Temperature.pde b/libraries/OneWire/examples/DS18x20_Temperature/DS18x20_Temperature.pde new file mode 100644 index 0000000..c96853a --- /dev/null +++ b/libraries/OneWire/examples/DS18x20_Temperature/DS18x20_Temperature.pde @@ -0,0 +1,112 @@ +#include + +// OneWire DS18S20, DS18B20, DS1822 Temperature Example +// +// http://www.pjrc.com/teensy/td_libs_OneWire.html +// +// The DallasTemperature library can do all this work for you! +// https://github.com/milesburton/Arduino-Temperature-Control-Library + +OneWire ds(10); // on pin 10 (a 4.7K resistor is necessary) + +void setup(void) { + Serial.begin(9600); +} + +void loop(void) { + byte i; + byte present = 0; + byte type_s; + byte data[12]; + byte addr[8]; + float celsius, fahrenheit; + + if ( !ds.search(addr)) { + Serial.println("No more addresses."); + Serial.println(); + ds.reset_search(); + delay(250); + return; + } + + Serial.print("ROM ="); + for( i = 0; i < 8; i++) { + Serial.write(' '); + Serial.print(addr[i], HEX); + } + + if (OneWire::crc8(addr, 7) != addr[7]) { + Serial.println("CRC is not valid!"); + return; + } + Serial.println(); + + // the first ROM byte indicates which chip + switch (addr[0]) { + case 0x10: + Serial.println(" Chip = DS18S20"); // or old DS1820 + type_s = 1; + break; + case 0x28: + Serial.println(" Chip = DS18B20"); + type_s = 0; + break; + case 0x22: + Serial.println(" Chip = DS1822"); + type_s = 0; + break; + default: + Serial.println("Device is not a DS18x20 family device."); + return; + } + + ds.reset(); + ds.select(addr); + ds.write(0x44, 1); // start conversion, with parasite power on at the end + + delay(1000); // maybe 750ms is enough, maybe not + // we might do a ds.depower() here, but the reset will take care of it. + + present = ds.reset(); + ds.select(addr); + ds.write(0xBE); // Read Scratchpad + + Serial.print(" Data = "); + Serial.print(present, HEX); + Serial.print(" "); + for ( i = 0; i < 9; i++) { // we need 9 bytes + data[i] = ds.read(); + Serial.print(data[i], HEX); + Serial.print(" "); + } + Serial.print(" CRC="); + Serial.print(OneWire::crc8(data, 8), HEX); + Serial.println(); + + // Convert the data to actual temperature + // because the result is a 16 bit signed integer, it should + // be stored to an "int16_t" type, which is always 16 bits + // even when compiled on a 32 bit processor. + int16_t raw = (data[1] << 8) | data[0]; + if (type_s) { + raw = raw << 3; // 9 bit resolution default + if (data[7] == 0x10) { + // "count remain" gives full 12 bit resolution + raw = (raw & 0xFFF0) + 12 - data[6]; + } + } else { + byte cfg = (data[4] & 0x60); + // at lower res, the low bits are undefined, so let's zero them + if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms + else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms + else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms + //// default is 12 bit resolution, 750 ms conversion time + } + celsius = (float)raw / 16.0; + fahrenheit = celsius * 1.8 + 32.0; + Serial.print(" Temperature = "); + Serial.print(celsius); + Serial.print(" Celsius, "); + Serial.print(fahrenheit); + Serial.println(" Fahrenheit"); +} diff --git a/libraries/OneWire/examples/DS2408_Switch/DS2408_Switch.pde b/libraries/OneWire/examples/DS2408_Switch/DS2408_Switch.pde new file mode 100644 index 0000000..b70eda9 --- /dev/null +++ b/libraries/OneWire/examples/DS2408_Switch/DS2408_Switch.pde @@ -0,0 +1,74 @@ +#include + +/* + * DS2408 8-Channel Addressable Switch + * + * Writte by Glenn Trewitt, glenn at trewitt dot org + * + * Some notes about the DS2408: + * - Unlike most input/output ports, the DS2408 doesn't have mode bits to + * set whether the pins are input or output. If you issue a read command, + * they're inputs. If you write to them, they're outputs. + * - For reading from a switch, you should use 10K pull-up resisters. + */ + +OneWire net(10); // on pin 10 + + +void PrintBytes(const uint8_t* addr, uint8_t count, bool newline=false) { + for (uint8_t i = 0; i < count; i++) { + Serial.print(addr[i]>>4, HEX); + Serial.print(addr[i]&0x0f, HEX); + } + if (newline) + Serial.println(); +} + + +void setup(void) { + Serial.begin(9600); +} + +void loop(void) { + byte addr[8]; + + if (!net.search(addr)) { + Serial.print("No more addresses.\n"); + net.reset_search(); + delay(1000); + return; + } + + if (OneWire::crc8(addr, 7) != addr[7]) { + Serial.print("CRC is not valid!\n"); + return; + } + + if (addr[0] != 0x29) { + PrintBytes(addr, 8); + Serial.print(" is not a DS2408.\n"); + return; + } + + Serial.print(" Reading DS2408 "); + PrintBytes(addr, 8); + Serial.println(); + + uint8_t buf[13]; // Put everything in the buffer so we can compute CRC easily. + buf[0] = 0xF0; // Read PIO Registers + buf[1] = 0x88; // LSB address + buf[2] = 0x00; // MSB address + net.write_bytes(buf, 3); + net.read_bytes(buf+3, 10); // 3 cmd bytes, 6 data bytes, 2 0xFF, 2 CRC16 + net.reset(); + + if (!OneWire::check_crc16(buf, 11, &buf[11])) { + Serial.print("CRC failure in DS2408 at "); + PrintBytes(addr, 8, true); + return; + } + Serial.print(" DS2408 data = "); + // First 3 bytes contain command, register address. + Serial.println(buf[3], BIN); +} + diff --git a/libraries/OneWire/examples/DS250x_PROM/DS250x_PROM.pde b/libraries/OneWire/examples/DS250x_PROM/DS250x_PROM.pde new file mode 100644 index 0000000..db95a5e --- /dev/null +++ b/libraries/OneWire/examples/DS250x_PROM/DS250x_PROM.pde @@ -0,0 +1,75 @@ +/* +DS250x add-only programmable memory reader w/SKIP ROM. + + The DS250x is a 512/1024bit add-only PROM(you can add data but cannot change the old one) that's used mainly for device identification purposes + like serial number, mfgr data, unique identifiers, etc. It uses the Maxim 1-wire bus. + + This sketch will use the SKIP ROM function that skips the 1-Wire search phase since we only have one device connected in the bus on digital pin 6. + If more than one device is connected to the bus, it will fail. + Sketch will not verify if device connected is from the DS250x family since the skip rom function effectively skips the family-id byte readout. + thus it is possible to run this sketch with any Maxim OneWire device in which case the command CRC will most likely fail. + Sketch will only read the first page of memory(32bits) starting from the lower address(0000h), if more than 1 device is present, then use the sketch with search functions. + Remember to put a 4.7K pullup resistor between pin 6 and +Vcc + + To change the range or ammount of data to read, simply change the data array size, LSB/MSB addresses and for loop iterations + + This example code is in the public domain and is provided AS-IS. + + Built with Arduino 0022 and PJRC OneWire 2.0 library http://www.pjrc.com/teensy/td_libs_OneWire.html + + created by Guillermo Lovato + march/2011 + + */ + +#include +OneWire ds(6); // OneWire bus on digital pin 6 +void setup() { + Serial.begin (9600); +} + +void loop() { + byte i; // This is for the for loops + boolean present; // device present var + byte data[32]; // container for the data from device + byte leemem[3] = { // array with the commands to initiate a read, DS250x devices expect 3 bytes to start a read: command,LSB&MSB adresses + 0xF0 , 0x00 , 0x00 }; // 0xF0 is the Read Data command, followed by 00h 00h as starting address(the beginning, 0000h) + byte ccrc; // Variable to store the command CRC + byte ccrc_calc; + + present = ds.reset(); // OneWire bus reset, always needed to start operation on the bus, returns a 1/TRUE if there's a device present. + ds.skip(); // Skip ROM search + + if (present == true) { // We only try to read the data if there's a device present + Serial.println("DS250x device present"); + ds.write(leemem[0],1); // Read data command, leave ghost power on + ds.write(leemem[1],1); // LSB starting address, leave ghost power on + ds.write(leemem[2],1); // MSB starting address, leave ghost power on + + ccrc = ds.read(); // DS250x generates a CRC for the command we sent, we assign a read slot and store it's value + ccrc_calc = OneWire::crc8(leemem, 3); // We calculate the CRC of the commands we sent using the library function and store it + + if ( ccrc_calc != ccrc) { // Then we compare it to the value the ds250x calculated, if it fails, we print debug messages and abort + Serial.println("Invalid command CRC!"); + Serial.print("Calculated CRC:"); + Serial.println(ccrc_calc,HEX); // HEX makes it easier to observe and compare + Serial.print("DS250x readback CRC:"); + Serial.println(ccrc,HEX); + return; // Since CRC failed, we abort the rest of the loop and start over + } + Serial.println("Data is: "); // For the printout of the data + for ( i = 0; i < 32; i++) { // Now it's time to read the PROM data itself, each page is 32 bytes so we need 32 read commands + data[i] = ds.read(); // we store each read byte to a different position in the data array + Serial.print(data[i]); // printout in ASCII + Serial.print(" "); // blank space + } + Serial.println(); + delay(5000); // Delay so we don't saturate the serial output + } + else { // Nothing is connected in the bus + Serial.println("Nothing connected"); + delay(3000); + } +} + + diff --git a/libraries/OneWire/keywords.txt b/libraries/OneWire/keywords.txt new file mode 100644 index 0000000..bee5d90 --- /dev/null +++ b/libraries/OneWire/keywords.txt @@ -0,0 +1,38 @@ +####################################### +# Syntax Coloring Map For OneWire +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +OneWire KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +reset KEYWORD2 +write_bit KEYWORD2 +read_bit KEYWORD2 +write KEYWORD2 +write_bytes KEYWORD2 +read KEYWORD2 +read_bytes KEYWORD2 +select KEYWORD2 +skip KEYWORD2 +depower KEYWORD2 +reset_search KEYWORD2 +search KEYWORD2 +crc8 KEYWORD2 +crc16 KEYWORD2 +check_crc16 KEYWORD2 + +####################################### +# Instances (KEYWORD2) +####################################### + + +####################################### +# Constants (LITERAL1) +####################################### diff --git a/libraries/OneWire/library.json b/libraries/OneWire/library.json new file mode 100644 index 0000000..c529849 --- /dev/null +++ b/libraries/OneWire/library.json @@ -0,0 +1,61 @@ +{ + "name": "OneWire", + "description": "Control 1-Wire protocol (DS18S20, DS18B20, DS2408 and etc)", + "keywords": "onewire, 1-wire, bus, sensor, temperature, ibutton", + "authors": [ + { + "name": "Paul Stoffregen", + "email": "paul@pjrc.com", + "url": "http://www.pjrc.com", + "maintainer": true + }, + { + "name": "Jim Studt" + }, + { + "name": "Tom Pollard", + "email": "pollard@alum.mit.edu" + }, + { + "name": "Derek Yerger" + }, + { + "name": "Josh Larios" + }, + { + "name": "Robin James" + }, + { + "name": "Glenn Trewitt" + }, + { + "name": "Jason Dangel", + "email": "dangel.jason AT gmail.com" + }, + { + "name": "Guillermo Lovato" + }, + { + "name": "Ken Butcher" + }, + { + "name": "Mark Tillotson" + }, + { + "name": "Bertrik Sikken" + }, + { + "name": "Scott Roberts" + } + ], + "repository": { + "type": "git", + "url": "https://github.com/PaulStoffregen/OneWire" + }, + "version": "2.3.5", + "homepage": "https://www.pjrc.com/teensy/td_libs_OneWire.html", + "frameworks": "Arduino", + "examples": [ + "examples/*/*.pde" + ] +} diff --git a/libraries/OneWire/library.properties b/libraries/OneWire/library.properties new file mode 100644 index 0000000..a7e4ad7 --- /dev/null +++ b/libraries/OneWire/library.properties @@ -0,0 +1,10 @@ +name=OneWire +version=2.3.5 +author=Jim Studt, Tom Pollard, Robin James, Glenn Trewitt, Jason Dangel, Guillermo Lovato, Paul Stoffregen, Scott Roberts, Bertrik Sikken, Mark Tillotson, Ken Butcher, Roger Clark, Love Nystrom +maintainer=Paul Stoffregen +sentence=Access 1-wire temperature sensors, memory and other chips. +paragraph= +category=Communication +url=http://www.pjrc.com/teensy/td_libs_OneWire.html +architectures=* + diff --git a/libraries/OneWire/util/OneWire_direct_gpio.h b/libraries/OneWire/util/OneWire_direct_gpio.h new file mode 100644 index 0000000..0771367 --- /dev/null +++ b/libraries/OneWire/util/OneWire_direct_gpio.h @@ -0,0 +1,420 @@ +#ifndef OneWire_Direct_GPIO_h +#define OneWire_Direct_GPIO_h + +// This header should ONLY be included by OneWire.cpp. These defines are +// meant to be private, used within OneWire.cpp, but not exposed to Arduino +// sketches or other libraries which may include OneWire.h. + +#include + +// Platform specific I/O definitions + +#if defined(__AVR__) +#define PIN_TO_BASEREG(pin) (portInputRegister(digitalPinToPort(pin))) +#define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) +#define IO_REG_TYPE uint8_t +#define IO_REG_BASE_ATTR asm("r30") +#define IO_REG_MASK_ATTR +#if defined(__AVR_ATmega4809__) +#define DIRECT_READ(base, mask) (((*(base)) & (mask)) ? 1 : 0) +#define DIRECT_MODE_INPUT(base, mask) ((*((base)-8)) &= ~(mask)) +#define DIRECT_MODE_OUTPUT(base, mask) ((*((base)-8)) |= (mask)) +#define DIRECT_WRITE_LOW(base, mask) ((*((base)-4)) &= ~(mask)) +#define DIRECT_WRITE_HIGH(base, mask) ((*((base)-4)) |= (mask)) +#else +#define DIRECT_READ(base, mask) (((*(base)) & (mask)) ? 1 : 0) +#define DIRECT_MODE_INPUT(base, mask) ((*((base)+1)) &= ~(mask)) +#define DIRECT_MODE_OUTPUT(base, mask) ((*((base)+1)) |= (mask)) +#define DIRECT_WRITE_LOW(base, mask) ((*((base)+2)) &= ~(mask)) +#define DIRECT_WRITE_HIGH(base, mask) ((*((base)+2)) |= (mask)) +#endif + +#elif defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK66FX1M0__) || defined(__MK64FX512__) +#define PIN_TO_BASEREG(pin) (portOutputRegister(pin)) +#define PIN_TO_BITMASK(pin) (1) +#define IO_REG_TYPE uint8_t +#define IO_REG_BASE_ATTR +#define IO_REG_MASK_ATTR __attribute__ ((unused)) +#define DIRECT_READ(base, mask) (*((base)+512)) +#define DIRECT_MODE_INPUT(base, mask) (*((base)+640) = 0) +#define DIRECT_MODE_OUTPUT(base, mask) (*((base)+640) = 1) +#define DIRECT_WRITE_LOW(base, mask) (*((base)+256) = 1) +#define DIRECT_WRITE_HIGH(base, mask) (*((base)+128) = 1) + +#elif defined(__MKL26Z64__) +#define PIN_TO_BASEREG(pin) (portOutputRegister(pin)) +#define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) +#define IO_REG_TYPE uint8_t +#define IO_REG_BASE_ATTR +#define IO_REG_MASK_ATTR +#define DIRECT_READ(base, mask) ((*((base)+16) & (mask)) ? 1 : 0) +#define DIRECT_MODE_INPUT(base, mask) (*((base)+20) &= ~(mask)) +#define DIRECT_MODE_OUTPUT(base, mask) (*((base)+20) |= (mask)) +#define DIRECT_WRITE_LOW(base, mask) (*((base)+8) = (mask)) +#define DIRECT_WRITE_HIGH(base, mask) (*((base)+4) = (mask)) + +#elif defined(__IMXRT1052__) || defined(__IMXRT1062__) +#define PIN_TO_BASEREG(pin) (portOutputRegister(pin)) +#define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) +#define IO_REG_TYPE uint32_t +#define IO_REG_BASE_ATTR +#define IO_REG_MASK_ATTR +#define DIRECT_READ(base, mask) ((*((base)+2) & (mask)) ? 1 : 0) +#define DIRECT_MODE_INPUT(base, mask) (*((base)+1) &= ~(mask)) +#define DIRECT_MODE_OUTPUT(base, mask) (*((base)+1) |= (mask)) +#define DIRECT_WRITE_LOW(base, mask) (*((base)+34) = (mask)) +#define DIRECT_WRITE_HIGH(base, mask) (*((base)+33) = (mask)) + +#elif defined(__SAM3X8E__) || defined(__SAM3A8C__) || defined(__SAM3A4C__) +// Arduino 1.5.1 may have a bug in delayMicroseconds() on Arduino Due. +// http://arduino.cc/forum/index.php/topic,141030.msg1076268.html#msg1076268 +// If you have trouble with OneWire on Arduino Due, please check the +// status of delayMicroseconds() before reporting a bug in OneWire! +#define PIN_TO_BASEREG(pin) (&(digitalPinToPort(pin)->PIO_PER)) +#define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) +#define IO_REG_TYPE uint32_t +#define IO_REG_BASE_ATTR +#define IO_REG_MASK_ATTR +#define DIRECT_READ(base, mask) (((*((base)+15)) & (mask)) ? 1 : 0) +#define DIRECT_MODE_INPUT(base, mask) ((*((base)+5)) = (mask)) +#define DIRECT_MODE_OUTPUT(base, mask) ((*((base)+4)) = (mask)) +#define DIRECT_WRITE_LOW(base, mask) ((*((base)+13)) = (mask)) +#define DIRECT_WRITE_HIGH(base, mask) ((*((base)+12)) = (mask)) +#ifndef PROGMEM +#define PROGMEM +#endif +#ifndef pgm_read_byte +#define pgm_read_byte(addr) (*(const uint8_t *)(addr)) +#endif + +#elif defined(__PIC32MX__) +#define PIN_TO_BASEREG(pin) (portModeRegister(digitalPinToPort(pin))) +#define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) +#define IO_REG_TYPE uint32_t +#define IO_REG_BASE_ATTR +#define IO_REG_MASK_ATTR +#define DIRECT_READ(base, mask) (((*(base+4)) & (mask)) ? 1 : 0) //PORTX + 0x10 +#define DIRECT_MODE_INPUT(base, mask) ((*(base+2)) = (mask)) //TRISXSET + 0x08 +#define DIRECT_MODE_OUTPUT(base, mask) ((*(base+1)) = (mask)) //TRISXCLR + 0x04 +#define DIRECT_WRITE_LOW(base, mask) ((*(base+8+1)) = (mask)) //LATXCLR + 0x24 +#define DIRECT_WRITE_HIGH(base, mask) ((*(base+8+2)) = (mask)) //LATXSET + 0x28 + +#elif defined(ARDUINO_ARCH_ESP8266) +// Special note: I depend on the ESP community to maintain these definitions and +// submit good pull requests. I can not answer any ESP questions or help you +// resolve any problems related to ESP chips. Please do not contact me and please +// DO NOT CREATE GITHUB ISSUES for ESP support. All ESP questions must be asked +// on ESP community forums. +#define PIN_TO_BASEREG(pin) ((volatile uint32_t*) GPO) +#define PIN_TO_BITMASK(pin) (1 << pin) +#define IO_REG_TYPE uint32_t +#define IO_REG_BASE_ATTR +#define IO_REG_MASK_ATTR +#define DIRECT_READ(base, mask) ((GPI & (mask)) ? 1 : 0) //GPIO_IN_ADDRESS +#define DIRECT_MODE_INPUT(base, mask) (GPE &= ~(mask)) //GPIO_ENABLE_W1TC_ADDRESS +#define DIRECT_MODE_OUTPUT(base, mask) (GPE |= (mask)) //GPIO_ENABLE_W1TS_ADDRESS +#define DIRECT_WRITE_LOW(base, mask) (GPOC = (mask)) //GPIO_OUT_W1TC_ADDRESS +#define DIRECT_WRITE_HIGH(base, mask) (GPOS = (mask)) //GPIO_OUT_W1TS_ADDRESS + +#elif defined(ARDUINO_ARCH_ESP32) +#include +#define PIN_TO_BASEREG(pin) (0) +#define PIN_TO_BITMASK(pin) (pin) +#define IO_REG_TYPE uint32_t +#define IO_REG_BASE_ATTR +#define IO_REG_MASK_ATTR + +static inline __attribute__((always_inline)) +IO_REG_TYPE directRead(IO_REG_TYPE pin) +{ + if ( pin < 32 ) + return (GPIO.in >> pin) & 0x1; + else if ( pin < 40 ) + return (GPIO.in1.val >> (pin - 32)) & 0x1; + + return 0; +} + +static inline __attribute__((always_inline)) +void directWriteLow(IO_REG_TYPE pin) +{ + if ( pin < 32 ) + GPIO.out_w1tc = ((uint32_t)1 << pin); + else if ( pin < 34 ) + GPIO.out1_w1tc.val = ((uint32_t)1 << (pin - 32)); +} + +static inline __attribute__((always_inline)) +void directWriteHigh(IO_REG_TYPE pin) +{ + if ( pin < 32 ) + GPIO.out_w1ts = ((uint32_t)1 << pin); + else if ( pin < 34 ) + GPIO.out1_w1ts.val = ((uint32_t)1 << (pin - 32)); +} + +static inline __attribute__((always_inline)) +void directModeInput(IO_REG_TYPE pin) +{ + if ( digitalPinIsValid(pin) ) + { + uint32_t rtc_reg(rtc_gpio_desc[pin].reg); + + if ( rtc_reg ) // RTC pins PULL settings + { + ESP_REG(rtc_reg) = ESP_REG(rtc_reg) & ~(rtc_gpio_desc[pin].mux); + ESP_REG(rtc_reg) = ESP_REG(rtc_reg) & ~(rtc_gpio_desc[pin].pullup | rtc_gpio_desc[pin].pulldown); + } + + if ( pin < 32 ) + GPIO.enable_w1tc = ((uint32_t)1 << pin); + else + GPIO.enable1_w1tc.val = ((uint32_t)1 << (pin - 32)); + + uint32_t pinFunction((uint32_t)2 << FUN_DRV_S); // what are the drivers? + pinFunction |= FUN_IE; // input enable but required for output as well? + pinFunction |= ((uint32_t)2 << MCU_SEL_S); + + ESP_REG(DR_REG_IO_MUX_BASE + esp32_gpioMux[pin].reg) = pinFunction; + + GPIO.pin[pin].val = 0; + } +} + +static inline __attribute__((always_inline)) +void directModeOutput(IO_REG_TYPE pin) +{ + if ( digitalPinIsValid(pin) && pin <= 33 ) // pins above 33 can be only inputs + { + uint32_t rtc_reg(rtc_gpio_desc[pin].reg); + + if ( rtc_reg ) // RTC pins PULL settings + { + ESP_REG(rtc_reg) = ESP_REG(rtc_reg) & ~(rtc_gpio_desc[pin].mux); + ESP_REG(rtc_reg) = ESP_REG(rtc_reg) & ~(rtc_gpio_desc[pin].pullup | rtc_gpio_desc[pin].pulldown); + } + + if ( pin < 32 ) + GPIO.enable_w1ts = ((uint32_t)1 << pin); + else // already validated to pins <= 33 + GPIO.enable1_w1ts.val = ((uint32_t)1 << (pin - 32)); + + uint32_t pinFunction((uint32_t)2 << FUN_DRV_S); // what are the drivers? + pinFunction |= FUN_IE; // input enable but required for output as well? + pinFunction |= ((uint32_t)2 << MCU_SEL_S); + + ESP_REG(DR_REG_IO_MUX_BASE + esp32_gpioMux[pin].reg) = pinFunction; + + GPIO.pin[pin].val = 0; + } +} + +#define DIRECT_READ(base, pin) directRead(pin) +#define DIRECT_WRITE_LOW(base, pin) directWriteLow(pin) +#define DIRECT_WRITE_HIGH(base, pin) directWriteHigh(pin) +#define DIRECT_MODE_INPUT(base, pin) directModeInput(pin) +#define DIRECT_MODE_OUTPUT(base, pin) directModeOutput(pin) +// https://github.com/PaulStoffregen/OneWire/pull/47 +// https://github.com/stickbreaker/OneWire/commit/6eb7fc1c11a15b6ac8c60e5671cf36eb6829f82c +#ifdef interrupts +#undef interrupts +#endif +#ifdef noInterrupts +#undef noInterrupts +#endif +#define noInterrupts() {portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;portENTER_CRITICAL(&mux) +#define interrupts() portEXIT_CRITICAL(&mux);} +//#warning "ESP32 OneWire testing" + +#elif defined(ARDUINO_ARCH_STM32) +#define PIN_TO_BASEREG(pin) (0) +#define PIN_TO_BITMASK(pin) ((uint32_t)digitalPinToPinName(pin)) +#define IO_REG_TYPE uint32_t +#define IO_REG_BASE_ATTR +#define IO_REG_MASK_ATTR +#define DIRECT_READ(base, pin) digitalReadFast((PinName)pin) +#define DIRECT_WRITE_LOW(base, pin) digitalWriteFast((PinName)pin, LOW) +#define DIRECT_WRITE_HIGH(base, pin) digitalWriteFast((PinName)pin, HIGH) +#define DIRECT_MODE_INPUT(base, pin) pin_function((PinName)pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)) +#define DIRECT_MODE_OUTPUT(base, pin) pin_function((PinName)pin, STM_PIN_DATA(STM_MODE_OUTPUT_PP, GPIO_NOPULL, 0)) + +#elif defined(__SAMD21G18A__) +#define PIN_TO_BASEREG(pin) portModeRegister(digitalPinToPort(pin)) +#define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) +#define IO_REG_TYPE uint32_t +#define IO_REG_BASE_ATTR +#define IO_REG_MASK_ATTR +#define DIRECT_READ(base, mask) (((*((base)+8)) & (mask)) ? 1 : 0) +#define DIRECT_MODE_INPUT(base, mask) ((*((base)+1)) = (mask)) +#define DIRECT_MODE_OUTPUT(base, mask) ((*((base)+2)) = (mask)) +#define DIRECT_WRITE_LOW(base, mask) ((*((base)+5)) = (mask)) +#define DIRECT_WRITE_HIGH(base, mask) ((*((base)+6)) = (mask)) + +#elif defined(RBL_NRF51822) +#define PIN_TO_BASEREG(pin) (0) +#define PIN_TO_BITMASK(pin) (pin) +#define IO_REG_TYPE uint32_t +#define IO_REG_BASE_ATTR +#define IO_REG_MASK_ATTR +#define DIRECT_READ(base, pin) nrf_gpio_pin_read(pin) +#define DIRECT_WRITE_LOW(base, pin) nrf_gpio_pin_clear(pin) +#define DIRECT_WRITE_HIGH(base, pin) nrf_gpio_pin_set(pin) +#define DIRECT_MODE_INPUT(base, pin) nrf_gpio_cfg_input(pin, NRF_GPIO_PIN_NOPULL) +#define DIRECT_MODE_OUTPUT(base, pin) nrf_gpio_cfg_output(pin) + +#elif defined(__arc__) /* Arduino101/Genuino101 specifics */ + +#include "scss_registers.h" +#include "portable.h" +#include "avr/pgmspace.h" + +#define GPIO_ID(pin) (g_APinDescription[pin].ulGPIOId) +#define GPIO_TYPE(pin) (g_APinDescription[pin].ulGPIOType) +#define GPIO_BASE(pin) (g_APinDescription[pin].ulGPIOBase) +#define DIR_OFFSET_SS 0x01 +#define DIR_OFFSET_SOC 0x04 +#define EXT_PORT_OFFSET_SS 0x0A +#define EXT_PORT_OFFSET_SOC 0x50 + +/* GPIO registers base address */ +#define PIN_TO_BASEREG(pin) ((volatile uint32_t *)g_APinDescription[pin].ulGPIOBase) +#define PIN_TO_BITMASK(pin) pin +#define IO_REG_TYPE uint32_t +#define IO_REG_BASE_ATTR +#define IO_REG_MASK_ATTR + +static inline __attribute__((always_inline)) +IO_REG_TYPE directRead(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) +{ + IO_REG_TYPE ret; + if (SS_GPIO == GPIO_TYPE(pin)) { + ret = READ_ARC_REG(((IO_REG_TYPE)base + EXT_PORT_OFFSET_SS)); + } else { + ret = MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, EXT_PORT_OFFSET_SOC); + } + return ((ret >> GPIO_ID(pin)) & 0x01); +} + +static inline __attribute__((always_inline)) +void directModeInput(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) +{ + if (SS_GPIO == GPIO_TYPE(pin)) { + WRITE_ARC_REG(READ_ARC_REG((((IO_REG_TYPE)base) + DIR_OFFSET_SS)) & ~(0x01 << GPIO_ID(pin)), + ((IO_REG_TYPE)(base) + DIR_OFFSET_SS)); + } else { + MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, DIR_OFFSET_SOC) &= ~(0x01 << GPIO_ID(pin)); + } +} + +static inline __attribute__((always_inline)) +void directModeOutput(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) +{ + if (SS_GPIO == GPIO_TYPE(pin)) { + WRITE_ARC_REG(READ_ARC_REG(((IO_REG_TYPE)(base) + DIR_OFFSET_SS)) | (0x01 << GPIO_ID(pin)), + ((IO_REG_TYPE)(base) + DIR_OFFSET_SS)); + } else { + MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, DIR_OFFSET_SOC) |= (0x01 << GPIO_ID(pin)); + } +} + +static inline __attribute__((always_inline)) +void directWriteLow(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) +{ + if (SS_GPIO == GPIO_TYPE(pin)) { + WRITE_ARC_REG(READ_ARC_REG(base) & ~(0x01 << GPIO_ID(pin)), base); + } else { + MMIO_REG_VAL(base) &= ~(0x01 << GPIO_ID(pin)); + } +} + +static inline __attribute__((always_inline)) +void directWriteHigh(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) +{ + if (SS_GPIO == GPIO_TYPE(pin)) { + WRITE_ARC_REG(READ_ARC_REG(base) | (0x01 << GPIO_ID(pin)), base); + } else { + MMIO_REG_VAL(base) |= (0x01 << GPIO_ID(pin)); + } +} + +#define DIRECT_READ(base, pin) directRead(base, pin) +#define DIRECT_MODE_INPUT(base, pin) directModeInput(base, pin) +#define DIRECT_MODE_OUTPUT(base, pin) directModeOutput(base, pin) +#define DIRECT_WRITE_LOW(base, pin) directWriteLow(base, pin) +#define DIRECT_WRITE_HIGH(base, pin) directWriteHigh(base, pin) + +#elif defined(__riscv) + +/* + * Tested on highfive1 + * + * Stable results are achieved operating in the + * two high speed modes of the highfive1. It + * seems to be less reliable in slow mode. + */ +#define PIN_TO_BASEREG(pin) (0) +#define PIN_TO_BITMASK(pin) digitalPinToBitMask(pin) +#define IO_REG_TYPE uint32_t +#define IO_REG_BASE_ATTR +#define IO_REG_MASK_ATTR + +static inline __attribute__((always_inline)) +IO_REG_TYPE directRead(IO_REG_TYPE mask) +{ + return ((GPIO_REG(GPIO_INPUT_VAL) & mask) != 0) ? 1 : 0; +} + +static inline __attribute__((always_inline)) +void directModeInput(IO_REG_TYPE mask) +{ + GPIO_REG(GPIO_OUTPUT_XOR) &= ~mask; + GPIO_REG(GPIO_IOF_EN) &= ~mask; + + GPIO_REG(GPIO_INPUT_EN) |= mask; + GPIO_REG(GPIO_OUTPUT_EN) &= ~mask; +} + +static inline __attribute__((always_inline)) +void directModeOutput(IO_REG_TYPE mask) +{ + GPIO_REG(GPIO_OUTPUT_XOR) &= ~mask; + GPIO_REG(GPIO_IOF_EN) &= ~mask; + + GPIO_REG(GPIO_INPUT_EN) &= ~mask; + GPIO_REG(GPIO_OUTPUT_EN) |= mask; +} + +static inline __attribute__((always_inline)) +void directWriteLow(IO_REG_TYPE mask) +{ + GPIO_REG(GPIO_OUTPUT_VAL) &= ~mask; +} + +static inline __attribute__((always_inline)) +void directWriteHigh(IO_REG_TYPE mask) +{ + GPIO_REG(GPIO_OUTPUT_VAL) |= mask; +} + +#define DIRECT_READ(base, mask) directRead(mask) +#define DIRECT_WRITE_LOW(base, mask) directWriteLow(mask) +#define DIRECT_WRITE_HIGH(base, mask) directWriteHigh(mask) +#define DIRECT_MODE_INPUT(base, mask) directModeInput(mask) +#define DIRECT_MODE_OUTPUT(base, mask) directModeOutput(mask) + +#else +#define PIN_TO_BASEREG(pin) (0) +#define PIN_TO_BITMASK(pin) (pin) +#define IO_REG_TYPE unsigned int +#define IO_REG_BASE_ATTR +#define IO_REG_MASK_ATTR +#define DIRECT_READ(base, pin) digitalRead(pin) +#define DIRECT_WRITE_LOW(base, pin) digitalWrite(pin, LOW) +#define DIRECT_WRITE_HIGH(base, pin) digitalWrite(pin, HIGH) +#define DIRECT_MODE_INPUT(base, pin) pinMode(pin,INPUT) +#define DIRECT_MODE_OUTPUT(base, pin) pinMode(pin,OUTPUT) +#warning "OneWire. Fallback mode. Using API calls for pinMode,digitalRead and digitalWrite. Operation of this library is not guaranteed on this architecture." + +#endif + +#endif diff --git a/libraries/OneWire/util/OneWire_direct_regtype.h b/libraries/OneWire/util/OneWire_direct_regtype.h new file mode 100644 index 0000000..21c4634 --- /dev/null +++ b/libraries/OneWire/util/OneWire_direct_regtype.h @@ -0,0 +1,52 @@ +#ifndef OneWire_Direct_RegType_h +#define OneWire_Direct_RegType_h + +#include + +// Platform specific I/O register type + +#if defined(__AVR__) +#define IO_REG_TYPE uint8_t + +#elif defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK66FX1M0__) || defined(__MK64FX512__) +#define IO_REG_TYPE uint8_t + +#elif defined(__IMXRT1052__) || defined(__IMXRT1062__) +#define IO_REG_TYPE uint32_t + +#elif defined(__MKL26Z64__) +#define IO_REG_TYPE uint8_t + +#elif defined(__SAM3X8E__) || defined(__SAM3A8C__) || defined(__SAM3A4C__) +#define IO_REG_TYPE uint32_t + +#elif defined(__PIC32MX__) +#define IO_REG_TYPE uint32_t + +#elif defined(ARDUINO_ARCH_ESP8266) +#define IO_REG_TYPE uint32_t + +#elif defined(ARDUINO_ARCH_ESP32) +#define IO_REG_TYPE uint32_t +#define IO_REG_MASK_ATTR + +#elif defined(ARDUINO_ARCH_STM32) +#define IO_REG_TYPE uint32_t + +#elif defined(__SAMD21G18A__) +#define IO_REG_TYPE uint32_t + +#elif defined(RBL_NRF51822) +#define IO_REG_TYPE uint32_t + +#elif defined(__arc__) /* Arduino101/Genuino101 specifics */ +#define IO_REG_TYPE uint32_t + +#elif defined(__riscv) +#define IO_REG_TYPE uint32_t + +#else +#define IO_REG_TYPE unsigned int + +#endif +#endif diff --git a/libraries/Servo/.DS_Store b/libraries/Servo/.DS_Store new file mode 100644 index 0000000..78886fe Binary files /dev/null and b/libraries/Servo/.DS_Store differ diff --git a/libraries/Servo/Servo.cpp b/libraries/Servo/Servo.cpp new file mode 100755 index 0000000..7fdd9d5 --- /dev/null +++ b/libraries/Servo/Servo.cpp @@ -0,0 +1,317 @@ +/* + Servo.cpp - Interrupt driven Servo library for Arduino using 16 bit timers- Version 2 + Copyright (c) 2009 Michael Margolis. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#if defined(ARDUINO_ARCH_AVR) + +#include +#include + +#include "Servo.h" + +#define usToTicks(_us) (( clockCyclesPerMicrosecond()* _us) / 8) // converts microseconds to tick (assumes prescale of 8) // 12 Aug 2009 +#define ticksToUs(_ticks) (( (unsigned)_ticks * 8)/ clockCyclesPerMicrosecond() ) // converts from ticks back to microseconds + + +#define TRIM_DURATION 2 // compensation ticks to trim adjust for digitalWrite delays // 12 August 2009 + +//#define NBR_TIMERS (MAX_SERVOS / SERVOS_PER_TIMER) + +static servo_t servos[MAX_SERVOS]; // static array of servo structures +static volatile int8_t Channel[_Nbr_16timers ]; // counter for the servo being pulsed for each timer (or -1 if refresh interval) + +uint8_t ServoCount = 0; // the total number of attached servos + + +// convenience macros +#define SERVO_INDEX_TO_TIMER(_servo_nbr) ((timer16_Sequence_t)(_servo_nbr / SERVOS_PER_TIMER)) // returns the timer controlling this servo +#define SERVO_INDEX_TO_CHANNEL(_servo_nbr) (_servo_nbr % SERVOS_PER_TIMER) // returns the index of the servo on this timer +#define SERVO_INDEX(_timer,_channel) ((_timer*SERVOS_PER_TIMER) + _channel) // macro to access servo index by timer and channel +#define SERVO(_timer,_channel) (servos[SERVO_INDEX(_timer,_channel)]) // macro to access servo class by timer and channel + +#define SERVO_MIN() (MIN_PULSE_WIDTH - this->min * 4) // minimum value in uS for this servo +#define SERVO_MAX() (MAX_PULSE_WIDTH - this->max * 4) // maximum value in uS for this servo + +/************ static functions common to all instances ***********************/ + +static inline void handle_interrupts(timer16_Sequence_t timer, volatile uint16_t *TCNTn, volatile uint16_t* OCRnA) +{ + if( Channel[timer] < 0 ) + *TCNTn = 0; // channel set to -1 indicated that refresh interval completed so reset the timer + else{ + if( SERVO_INDEX(timer,Channel[timer]) < ServoCount && SERVO(timer,Channel[timer]).Pin.isActive == true ) + digitalWrite( SERVO(timer,Channel[timer]).Pin.nbr,LOW); // pulse this channel low if activated + } + + Channel[timer]++; // increment to the next channel + if( SERVO_INDEX(timer,Channel[timer]) < ServoCount && Channel[timer] < SERVOS_PER_TIMER) { + *OCRnA = *TCNTn + SERVO(timer,Channel[timer]).ticks; + if(SERVO(timer,Channel[timer]).Pin.isActive == true) // check if activated + digitalWrite( SERVO(timer,Channel[timer]).Pin.nbr,HIGH); // its an active channel so pulse it high + } + else { + // finished all channels so wait for the refresh period to expire before starting over + if( ((unsigned)*TCNTn) + 4 < usToTicks(REFRESH_INTERVAL) ) // allow a few ticks to ensure the next OCR1A not missed + *OCRnA = (unsigned int)usToTicks(REFRESH_INTERVAL); + else + *OCRnA = *TCNTn + 4; // at least REFRESH_INTERVAL has elapsed + Channel[timer] = -1; // this will get incremented at the end of the refresh period to start again at the first channel + } +} + +#ifndef WIRING // Wiring pre-defines signal handlers so don't define any if compiling for the Wiring platform +// Interrupt handlers for Arduino +#if defined(_useTimer1) +SIGNAL (TIMER1_COMPA_vect) +{ + handle_interrupts(_timer1, &TCNT1, &OCR1A); +} +#endif + +#if defined(_useTimer3) +SIGNAL (TIMER3_COMPA_vect) +{ + handle_interrupts(_timer3, &TCNT3, &OCR3A); +} +#endif + +#if defined(_useTimer4) +SIGNAL (TIMER4_COMPA_vect) +{ + handle_interrupts(_timer4, &TCNT4, &OCR4A); +} +#endif + +#if defined(_useTimer5) +SIGNAL (TIMER5_COMPA_vect) +{ + handle_interrupts(_timer5, &TCNT5, &OCR5A); +} +#endif + +#elif defined WIRING +// Interrupt handlers for Wiring +#if defined(_useTimer1) +void Timer1Service() +{ + handle_interrupts(_timer1, &TCNT1, &OCR1A); +} +#endif +#if defined(_useTimer3) +void Timer3Service() +{ + handle_interrupts(_timer3, &TCNT3, &OCR3A); +} +#endif +#endif + + +static void initISR(timer16_Sequence_t timer) +{ +#if defined (_useTimer1) + if(timer == _timer1) { + TCCR1A = 0; // normal counting mode + TCCR1B = _BV(CS11); // set prescaler of 8 + TCNT1 = 0; // clear the timer count +#if defined(__AVR_ATmega8__)|| defined(__AVR_ATmega128__) + TIFR |= _BV(OCF1A); // clear any pending interrupts; + TIMSK |= _BV(OCIE1A) ; // enable the output compare interrupt +#else + // here if not ATmega8 or ATmega128 + TIFR1 |= _BV(OCF1A); // clear any pending interrupts; + TIMSK1 |= _BV(OCIE1A) ; // enable the output compare interrupt +#endif +#if defined(WIRING) + timerAttach(TIMER1OUTCOMPAREA_INT, Timer1Service); +#endif + } +#endif + +#if defined (_useTimer3) + if(timer == _timer3) { + TCCR3A = 0; // normal counting mode + TCCR3B = _BV(CS31); // set prescaler of 8 + TCNT3 = 0; // clear the timer count +#if defined(__AVR_ATmega128__) + TIFR |= _BV(OCF3A); // clear any pending interrupts; + ETIMSK |= _BV(OCIE3A); // enable the output compare interrupt +#else + TIFR3 = _BV(OCF3A); // clear any pending interrupts; + TIMSK3 = _BV(OCIE3A) ; // enable the output compare interrupt +#endif +#if defined(WIRING) + timerAttach(TIMER3OUTCOMPAREA_INT, Timer3Service); // for Wiring platform only +#endif + } +#endif + +#if defined (_useTimer4) + if(timer == _timer4) { + TCCR4A = 0; // normal counting mode + TCCR4B = _BV(CS41); // set prescaler of 8 + TCNT4 = 0; // clear the timer count + TIFR4 = _BV(OCF4A); // clear any pending interrupts; + TIMSK4 = _BV(OCIE4A) ; // enable the output compare interrupt + } +#endif + +#if defined (_useTimer5) + if(timer == _timer5) { + TCCR5A = 0; // normal counting mode + TCCR5B = _BV(CS51); // set prescaler of 8 + TCNT5 = 0; // clear the timer count + TIFR5 = _BV(OCF5A); // clear any pending interrupts; + TIMSK5 = _BV(OCIE5A) ; // enable the output compare interrupt + } +#endif +} + +static void finISR(timer16_Sequence_t timer) +{ + //disable use of the given timer +#if defined WIRING // Wiring + if(timer == _timer1) { + #if defined(__AVR_ATmega1281__)||defined(__AVR_ATmega2561__) + TIMSK1 &= ~_BV(OCIE1A) ; // disable timer 1 output compare interrupt + #else + TIMSK &= ~_BV(OCIE1A) ; // disable timer 1 output compare interrupt + #endif + timerDetach(TIMER1OUTCOMPAREA_INT); + } + else if(timer == _timer3) { + #if defined(__AVR_ATmega1281__)||defined(__AVR_ATmega2561__) + TIMSK3 &= ~_BV(OCIE3A); // disable the timer3 output compare A interrupt + #else + ETIMSK &= ~_BV(OCIE3A); // disable the timer3 output compare A interrupt + #endif + timerDetach(TIMER3OUTCOMPAREA_INT); + } +#else + //For arduino - in future: call here to a currently undefined function to reset the timer +#endif +} + +static boolean isTimerActive(timer16_Sequence_t timer) +{ + // returns true if any servo is active on this timer + for(uint8_t channel=0; channel < SERVOS_PER_TIMER; channel++) { + if(SERVO(timer,channel).Pin.isActive == true) + return true; + } + return false; +} + + +/****************** end of static functions ******************************/ + +Servo::Servo() +{ + if( ServoCount < MAX_SERVOS) { + this->servoIndex = ServoCount++; // assign a servo index to this instance + servos[this->servoIndex].ticks = usToTicks(DEFAULT_PULSE_WIDTH); // store default values - 12 Aug 2009 + } + else + this->servoIndex = INVALID_SERVO ; // too many servos +} + +uint8_t Servo::attach(int pin) +{ + return this->attach(pin, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH); +} + +uint8_t Servo::attach(int pin, int min, int max) +{ + if(this->servoIndex < MAX_SERVOS ) { + pinMode( pin, OUTPUT) ; // set servo pin to output + servos[this->servoIndex].Pin.nbr = pin; + // todo min/max check: abs(min - MIN_PULSE_WIDTH) /4 < 128 + this->min = (MIN_PULSE_WIDTH - min)/4; //resolution of min/max is 4 uS + this->max = (MAX_PULSE_WIDTH - max)/4; + // initialize the timer if it has not already been initialized + timer16_Sequence_t timer = SERVO_INDEX_TO_TIMER(servoIndex); + if(isTimerActive(timer) == false) + initISR(timer); + servos[this->servoIndex].Pin.isActive = true; // this must be set after the check for isTimerActive + } + return this->servoIndex ; +} + +void Servo::detach() +{ + servos[this->servoIndex].Pin.isActive = false; + timer16_Sequence_t timer = SERVO_INDEX_TO_TIMER(servoIndex); + if(isTimerActive(timer) == false) { + finISR(timer); + } +} + +void Servo::write(int value) +{ + if(value < MIN_PULSE_WIDTH) + { // treat values less than 544 as angles in degrees (valid values in microseconds are handled as microseconds) + if(value < 0) value = 0; + if(value > 180) value = 180; + value = map(value, 0, 180, SERVO_MIN(), SERVO_MAX()); + } + this->writeMicroseconds(value); +} + +void Servo::writeMicroseconds(int value) +{ + // calculate and store the values for the given channel + byte channel = this->servoIndex; + if( (channel < MAX_SERVOS) ) // ensure channel is valid + { + if( value < SERVO_MIN() ) // ensure pulse width is valid + value = SERVO_MIN(); + else if( value > SERVO_MAX() ) + value = SERVO_MAX(); + + value = value - TRIM_DURATION; + value = usToTicks(value); // convert to ticks after compensating for interrupt overhead - 12 Aug 2009 + + uint8_t oldSREG = SREG; + cli(); + servos[channel].ticks = value; + SREG = oldSREG; + } +} + +int Servo::read() // return the value as degrees +{ + return map( this->readMicroseconds()+1, SERVO_MIN(), SERVO_MAX(), 0, 180); +} + +int Servo::readMicroseconds() +{ + unsigned int pulsewidth; + if( this->servoIndex != INVALID_SERVO ) + pulsewidth = ticksToUs(servos[this->servoIndex].ticks) + TRIM_DURATION ; // 12 aug 2009 + else + pulsewidth = 0; + + return pulsewidth; +} + +bool Servo::attached() +{ + return servos[this->servoIndex].Pin.isActive ; +} + +#endif // ARDUINO_ARCH_AVR + diff --git a/libraries/Servo/Servo.h b/libraries/Servo/Servo.h new file mode 100755 index 0000000..894d267 --- /dev/null +++ b/libraries/Servo/Servo.h @@ -0,0 +1,112 @@ +/* + Servo.h - Interrupt driven Servo library for Arduino using 16 bit timers- Version 2 + Copyright (c) 2009 Michael Margolis. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +/* + A servo is activated by creating an instance of the Servo class passing + the desired pin to the attach() method. + The servos are pulsed in the background using the value most recently + written using the write() method. + + Note that analogWrite of PWM on pins associated with the timer are + disabled when the first servo is attached. + Timers are seized as needed in groups of 12 servos - 24 servos use two + timers, 48 servos will use four. + The sequence used to sieze timers is defined in timers.h + + The methods are: + + Servo - Class for manipulating servo motors connected to Arduino pins. + + attach(pin ) - Attaches a servo motor to an i/o pin. + attach(pin, min, max ) - Attaches to a pin setting min and max values in microseconds + default min is 544, max is 2400 + + write() - Sets the servo angle in degrees. (invalid angle that is valid as pulse in microseconds is treated as microseconds) + writeMicroseconds() - Sets the servo pulse width in microseconds + read() - Gets the last written servo pulse width as an angle between 0 and 180. + readMicroseconds() - Gets the last written servo pulse width in microseconds. (was read_us() in first release) + attached() - Returns true if there is a servo attached. + detach() - Stops an attached servos from pulsing its i/o pin. + */ + +#ifndef Servo_h +#define Servo_h + +#include + +/* + * Defines for 16 bit timers used with Servo library + * + * If _useTimerX is defined then TimerX is a 16 bit timer on the current board + * timer16_Sequence_t enumerates the sequence that the timers should be allocated + * _Nbr_16timers indicates how many 16 bit timers are available. + */ + +// Architecture specific include +#if defined(ARDUINO_ARCH_AVR) +#include "avr/ServoTimers.h" +#elif defined(ARDUINO_ARCH_SAM) +#include "sam/ServoTimers.h" +#elif defined(ARDUINO_ARCH_SAMD) +#include "samd/ServoTimers.h" +#else +#error "This library only supports boards with an AVR, SAM or SAMD processor." +#endif + +#define Servo_VERSION 2 // software version of this library + +#define MIN_PULSE_WIDTH 544 // the shortest pulse sent to a servo +#define MAX_PULSE_WIDTH 2400 // the longest pulse sent to a servo +#define DEFAULT_PULSE_WIDTH 1500 // default pulse width when servo is attached +#define REFRESH_INTERVAL 20000 // minumim time to refresh servos in microseconds + +#define SERVOS_PER_TIMER 12 // the maximum number of servos controlled by one timer +#define MAX_SERVOS (_Nbr_16timers * SERVOS_PER_TIMER) + +#define INVALID_SERVO 255 // flag indicating an invalid servo index + +typedef struct { + uint8_t nbr :6 ; // a pin number from 0 to 63 + uint8_t isActive :1 ; // true if this channel is enabled, pin not pulsed if false +} ServoPin_t ; + +typedef struct { + ServoPin_t Pin; + volatile unsigned int ticks; +} servo_t; + +class Servo +{ +public: + Servo(); + uint8_t attach(int pin); // attach the given pin to the next free channel, sets pinMode, returns channel number or 0 if failure + uint8_t attach(int pin, int min, int max); // as above but also sets min and max values for writes. + void detach(); + void write(int value); // if value is < 200 its treated as an angle, otherwise as pulse width in microseconds + void writeMicroseconds(int value); // Write pulse width in microseconds + int read(); // returns current pulse width as an angle between 0 and 180 degrees + int readMicroseconds(); // returns current pulse width in microseconds for this servo (was read_us() in first release) + bool attached(); // return true if this servo is attached, otherwise false +private: + uint8_t servoIndex; // index into the channel data for this servo + int8_t min; // minimum is this value times 4 added to MIN_PULSE_WIDTH + int8_t max; // maximum is this value times 4 added to MAX_PULSE_WIDTH +}; + +#endif diff --git a/libraries/Servo/ServoTimers.h b/libraries/Servo/ServoTimers.h new file mode 100755 index 0000000..9794c8e --- /dev/null +++ b/libraries/Servo/ServoTimers.h @@ -0,0 +1,59 @@ +/* + Servo.h - Interrupt driven Servo library for Arduino using 16 bit timers- Version 2 + Copyright (c) 2009 Michael Margolis. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +/* + * Defines for 16 bit timers used with Servo library + * + * If _useTimerX is defined then TimerX is a 16 bit timer on the current board + * timer16_Sequence_t enumerates the sequence that the timers should be allocated + * _Nbr_16timers indicates how many 16 bit timers are available. + */ + +/** + * AVR Only definitions + * -------------------- + */ + +// Say which 16 bit timers can be used and in what order +#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) +#define _useTimer5 +#define _useTimer1 +#define _useTimer3 +#define _useTimer4 +typedef enum { _timer5, _timer1, _timer3, _timer4, _Nbr_16timers } timer16_Sequence_t; + +#elif defined(__AVR_ATmega32U4__) +#define _useTimer1 +typedef enum { _timer1, _Nbr_16timers } timer16_Sequence_t; + +#elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__) +#define _useTimer3 +#define _useTimer1 +typedef enum { _timer3, _timer1, _Nbr_16timers } timer16_Sequence_t; + +#elif defined(__AVR_ATmega128__) || defined(__AVR_ATmega1281__) || defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega2561__) +#define _useTimer3 +#define _useTimer1 +typedef enum { _timer3, _timer1, _Nbr_16timers } timer16_Sequence_t; + +#else // everything else +#define _useTimer1 +typedef enum { _timer1, _Nbr_16timers } timer16_Sequence_t; +#endif + diff --git a/libraries/Servo/avr/Servo.cpp b/libraries/Servo/avr/Servo.cpp new file mode 100755 index 0000000..7fdd9d5 --- /dev/null +++ b/libraries/Servo/avr/Servo.cpp @@ -0,0 +1,317 @@ +/* + Servo.cpp - Interrupt driven Servo library for Arduino using 16 bit timers- Version 2 + Copyright (c) 2009 Michael Margolis. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#if defined(ARDUINO_ARCH_AVR) + +#include +#include + +#include "Servo.h" + +#define usToTicks(_us) (( clockCyclesPerMicrosecond()* _us) / 8) // converts microseconds to tick (assumes prescale of 8) // 12 Aug 2009 +#define ticksToUs(_ticks) (( (unsigned)_ticks * 8)/ clockCyclesPerMicrosecond() ) // converts from ticks back to microseconds + + +#define TRIM_DURATION 2 // compensation ticks to trim adjust for digitalWrite delays // 12 August 2009 + +//#define NBR_TIMERS (MAX_SERVOS / SERVOS_PER_TIMER) + +static servo_t servos[MAX_SERVOS]; // static array of servo structures +static volatile int8_t Channel[_Nbr_16timers ]; // counter for the servo being pulsed for each timer (or -1 if refresh interval) + +uint8_t ServoCount = 0; // the total number of attached servos + + +// convenience macros +#define SERVO_INDEX_TO_TIMER(_servo_nbr) ((timer16_Sequence_t)(_servo_nbr / SERVOS_PER_TIMER)) // returns the timer controlling this servo +#define SERVO_INDEX_TO_CHANNEL(_servo_nbr) (_servo_nbr % SERVOS_PER_TIMER) // returns the index of the servo on this timer +#define SERVO_INDEX(_timer,_channel) ((_timer*SERVOS_PER_TIMER) + _channel) // macro to access servo index by timer and channel +#define SERVO(_timer,_channel) (servos[SERVO_INDEX(_timer,_channel)]) // macro to access servo class by timer and channel + +#define SERVO_MIN() (MIN_PULSE_WIDTH - this->min * 4) // minimum value in uS for this servo +#define SERVO_MAX() (MAX_PULSE_WIDTH - this->max * 4) // maximum value in uS for this servo + +/************ static functions common to all instances ***********************/ + +static inline void handle_interrupts(timer16_Sequence_t timer, volatile uint16_t *TCNTn, volatile uint16_t* OCRnA) +{ + if( Channel[timer] < 0 ) + *TCNTn = 0; // channel set to -1 indicated that refresh interval completed so reset the timer + else{ + if( SERVO_INDEX(timer,Channel[timer]) < ServoCount && SERVO(timer,Channel[timer]).Pin.isActive == true ) + digitalWrite( SERVO(timer,Channel[timer]).Pin.nbr,LOW); // pulse this channel low if activated + } + + Channel[timer]++; // increment to the next channel + if( SERVO_INDEX(timer,Channel[timer]) < ServoCount && Channel[timer] < SERVOS_PER_TIMER) { + *OCRnA = *TCNTn + SERVO(timer,Channel[timer]).ticks; + if(SERVO(timer,Channel[timer]).Pin.isActive == true) // check if activated + digitalWrite( SERVO(timer,Channel[timer]).Pin.nbr,HIGH); // its an active channel so pulse it high + } + else { + // finished all channels so wait for the refresh period to expire before starting over + if( ((unsigned)*TCNTn) + 4 < usToTicks(REFRESH_INTERVAL) ) // allow a few ticks to ensure the next OCR1A not missed + *OCRnA = (unsigned int)usToTicks(REFRESH_INTERVAL); + else + *OCRnA = *TCNTn + 4; // at least REFRESH_INTERVAL has elapsed + Channel[timer] = -1; // this will get incremented at the end of the refresh period to start again at the first channel + } +} + +#ifndef WIRING // Wiring pre-defines signal handlers so don't define any if compiling for the Wiring platform +// Interrupt handlers for Arduino +#if defined(_useTimer1) +SIGNAL (TIMER1_COMPA_vect) +{ + handle_interrupts(_timer1, &TCNT1, &OCR1A); +} +#endif + +#if defined(_useTimer3) +SIGNAL (TIMER3_COMPA_vect) +{ + handle_interrupts(_timer3, &TCNT3, &OCR3A); +} +#endif + +#if defined(_useTimer4) +SIGNAL (TIMER4_COMPA_vect) +{ + handle_interrupts(_timer4, &TCNT4, &OCR4A); +} +#endif + +#if defined(_useTimer5) +SIGNAL (TIMER5_COMPA_vect) +{ + handle_interrupts(_timer5, &TCNT5, &OCR5A); +} +#endif + +#elif defined WIRING +// Interrupt handlers for Wiring +#if defined(_useTimer1) +void Timer1Service() +{ + handle_interrupts(_timer1, &TCNT1, &OCR1A); +} +#endif +#if defined(_useTimer3) +void Timer3Service() +{ + handle_interrupts(_timer3, &TCNT3, &OCR3A); +} +#endif +#endif + + +static void initISR(timer16_Sequence_t timer) +{ +#if defined (_useTimer1) + if(timer == _timer1) { + TCCR1A = 0; // normal counting mode + TCCR1B = _BV(CS11); // set prescaler of 8 + TCNT1 = 0; // clear the timer count +#if defined(__AVR_ATmega8__)|| defined(__AVR_ATmega128__) + TIFR |= _BV(OCF1A); // clear any pending interrupts; + TIMSK |= _BV(OCIE1A) ; // enable the output compare interrupt +#else + // here if not ATmega8 or ATmega128 + TIFR1 |= _BV(OCF1A); // clear any pending interrupts; + TIMSK1 |= _BV(OCIE1A) ; // enable the output compare interrupt +#endif +#if defined(WIRING) + timerAttach(TIMER1OUTCOMPAREA_INT, Timer1Service); +#endif + } +#endif + +#if defined (_useTimer3) + if(timer == _timer3) { + TCCR3A = 0; // normal counting mode + TCCR3B = _BV(CS31); // set prescaler of 8 + TCNT3 = 0; // clear the timer count +#if defined(__AVR_ATmega128__) + TIFR |= _BV(OCF3A); // clear any pending interrupts; + ETIMSK |= _BV(OCIE3A); // enable the output compare interrupt +#else + TIFR3 = _BV(OCF3A); // clear any pending interrupts; + TIMSK3 = _BV(OCIE3A) ; // enable the output compare interrupt +#endif +#if defined(WIRING) + timerAttach(TIMER3OUTCOMPAREA_INT, Timer3Service); // for Wiring platform only +#endif + } +#endif + +#if defined (_useTimer4) + if(timer == _timer4) { + TCCR4A = 0; // normal counting mode + TCCR4B = _BV(CS41); // set prescaler of 8 + TCNT4 = 0; // clear the timer count + TIFR4 = _BV(OCF4A); // clear any pending interrupts; + TIMSK4 = _BV(OCIE4A) ; // enable the output compare interrupt + } +#endif + +#if defined (_useTimer5) + if(timer == _timer5) { + TCCR5A = 0; // normal counting mode + TCCR5B = _BV(CS51); // set prescaler of 8 + TCNT5 = 0; // clear the timer count + TIFR5 = _BV(OCF5A); // clear any pending interrupts; + TIMSK5 = _BV(OCIE5A) ; // enable the output compare interrupt + } +#endif +} + +static void finISR(timer16_Sequence_t timer) +{ + //disable use of the given timer +#if defined WIRING // Wiring + if(timer == _timer1) { + #if defined(__AVR_ATmega1281__)||defined(__AVR_ATmega2561__) + TIMSK1 &= ~_BV(OCIE1A) ; // disable timer 1 output compare interrupt + #else + TIMSK &= ~_BV(OCIE1A) ; // disable timer 1 output compare interrupt + #endif + timerDetach(TIMER1OUTCOMPAREA_INT); + } + else if(timer == _timer3) { + #if defined(__AVR_ATmega1281__)||defined(__AVR_ATmega2561__) + TIMSK3 &= ~_BV(OCIE3A); // disable the timer3 output compare A interrupt + #else + ETIMSK &= ~_BV(OCIE3A); // disable the timer3 output compare A interrupt + #endif + timerDetach(TIMER3OUTCOMPAREA_INT); + } +#else + //For arduino - in future: call here to a currently undefined function to reset the timer +#endif +} + +static boolean isTimerActive(timer16_Sequence_t timer) +{ + // returns true if any servo is active on this timer + for(uint8_t channel=0; channel < SERVOS_PER_TIMER; channel++) { + if(SERVO(timer,channel).Pin.isActive == true) + return true; + } + return false; +} + + +/****************** end of static functions ******************************/ + +Servo::Servo() +{ + if( ServoCount < MAX_SERVOS) { + this->servoIndex = ServoCount++; // assign a servo index to this instance + servos[this->servoIndex].ticks = usToTicks(DEFAULT_PULSE_WIDTH); // store default values - 12 Aug 2009 + } + else + this->servoIndex = INVALID_SERVO ; // too many servos +} + +uint8_t Servo::attach(int pin) +{ + return this->attach(pin, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH); +} + +uint8_t Servo::attach(int pin, int min, int max) +{ + if(this->servoIndex < MAX_SERVOS ) { + pinMode( pin, OUTPUT) ; // set servo pin to output + servos[this->servoIndex].Pin.nbr = pin; + // todo min/max check: abs(min - MIN_PULSE_WIDTH) /4 < 128 + this->min = (MIN_PULSE_WIDTH - min)/4; //resolution of min/max is 4 uS + this->max = (MAX_PULSE_WIDTH - max)/4; + // initialize the timer if it has not already been initialized + timer16_Sequence_t timer = SERVO_INDEX_TO_TIMER(servoIndex); + if(isTimerActive(timer) == false) + initISR(timer); + servos[this->servoIndex].Pin.isActive = true; // this must be set after the check for isTimerActive + } + return this->servoIndex ; +} + +void Servo::detach() +{ + servos[this->servoIndex].Pin.isActive = false; + timer16_Sequence_t timer = SERVO_INDEX_TO_TIMER(servoIndex); + if(isTimerActive(timer) == false) { + finISR(timer); + } +} + +void Servo::write(int value) +{ + if(value < MIN_PULSE_WIDTH) + { // treat values less than 544 as angles in degrees (valid values in microseconds are handled as microseconds) + if(value < 0) value = 0; + if(value > 180) value = 180; + value = map(value, 0, 180, SERVO_MIN(), SERVO_MAX()); + } + this->writeMicroseconds(value); +} + +void Servo::writeMicroseconds(int value) +{ + // calculate and store the values for the given channel + byte channel = this->servoIndex; + if( (channel < MAX_SERVOS) ) // ensure channel is valid + { + if( value < SERVO_MIN() ) // ensure pulse width is valid + value = SERVO_MIN(); + else if( value > SERVO_MAX() ) + value = SERVO_MAX(); + + value = value - TRIM_DURATION; + value = usToTicks(value); // convert to ticks after compensating for interrupt overhead - 12 Aug 2009 + + uint8_t oldSREG = SREG; + cli(); + servos[channel].ticks = value; + SREG = oldSREG; + } +} + +int Servo::read() // return the value as degrees +{ + return map( this->readMicroseconds()+1, SERVO_MIN(), SERVO_MAX(), 0, 180); +} + +int Servo::readMicroseconds() +{ + unsigned int pulsewidth; + if( this->servoIndex != INVALID_SERVO ) + pulsewidth = ticksToUs(servos[this->servoIndex].ticks) + TRIM_DURATION ; // 12 aug 2009 + else + pulsewidth = 0; + + return pulsewidth; +} + +bool Servo::attached() +{ + return servos[this->servoIndex].Pin.isActive ; +} + +#endif // ARDUINO_ARCH_AVR + diff --git a/libraries/Servo/avr/Servo.h b/libraries/Servo/avr/Servo.h new file mode 100755 index 0000000..894d267 --- /dev/null +++ b/libraries/Servo/avr/Servo.h @@ -0,0 +1,112 @@ +/* + Servo.h - Interrupt driven Servo library for Arduino using 16 bit timers- Version 2 + Copyright (c) 2009 Michael Margolis. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +/* + A servo is activated by creating an instance of the Servo class passing + the desired pin to the attach() method. + The servos are pulsed in the background using the value most recently + written using the write() method. + + Note that analogWrite of PWM on pins associated with the timer are + disabled when the first servo is attached. + Timers are seized as needed in groups of 12 servos - 24 servos use two + timers, 48 servos will use four. + The sequence used to sieze timers is defined in timers.h + + The methods are: + + Servo - Class for manipulating servo motors connected to Arduino pins. + + attach(pin ) - Attaches a servo motor to an i/o pin. + attach(pin, min, max ) - Attaches to a pin setting min and max values in microseconds + default min is 544, max is 2400 + + write() - Sets the servo angle in degrees. (invalid angle that is valid as pulse in microseconds is treated as microseconds) + writeMicroseconds() - Sets the servo pulse width in microseconds + read() - Gets the last written servo pulse width as an angle between 0 and 180. + readMicroseconds() - Gets the last written servo pulse width in microseconds. (was read_us() in first release) + attached() - Returns true if there is a servo attached. + detach() - Stops an attached servos from pulsing its i/o pin. + */ + +#ifndef Servo_h +#define Servo_h + +#include + +/* + * Defines for 16 bit timers used with Servo library + * + * If _useTimerX is defined then TimerX is a 16 bit timer on the current board + * timer16_Sequence_t enumerates the sequence that the timers should be allocated + * _Nbr_16timers indicates how many 16 bit timers are available. + */ + +// Architecture specific include +#if defined(ARDUINO_ARCH_AVR) +#include "avr/ServoTimers.h" +#elif defined(ARDUINO_ARCH_SAM) +#include "sam/ServoTimers.h" +#elif defined(ARDUINO_ARCH_SAMD) +#include "samd/ServoTimers.h" +#else +#error "This library only supports boards with an AVR, SAM or SAMD processor." +#endif + +#define Servo_VERSION 2 // software version of this library + +#define MIN_PULSE_WIDTH 544 // the shortest pulse sent to a servo +#define MAX_PULSE_WIDTH 2400 // the longest pulse sent to a servo +#define DEFAULT_PULSE_WIDTH 1500 // default pulse width when servo is attached +#define REFRESH_INTERVAL 20000 // minumim time to refresh servos in microseconds + +#define SERVOS_PER_TIMER 12 // the maximum number of servos controlled by one timer +#define MAX_SERVOS (_Nbr_16timers * SERVOS_PER_TIMER) + +#define INVALID_SERVO 255 // flag indicating an invalid servo index + +typedef struct { + uint8_t nbr :6 ; // a pin number from 0 to 63 + uint8_t isActive :1 ; // true if this channel is enabled, pin not pulsed if false +} ServoPin_t ; + +typedef struct { + ServoPin_t Pin; + volatile unsigned int ticks; +} servo_t; + +class Servo +{ +public: + Servo(); + uint8_t attach(int pin); // attach the given pin to the next free channel, sets pinMode, returns channel number or 0 if failure + uint8_t attach(int pin, int min, int max); // as above but also sets min and max values for writes. + void detach(); + void write(int value); // if value is < 200 its treated as an angle, otherwise as pulse width in microseconds + void writeMicroseconds(int value); // Write pulse width in microseconds + int read(); // returns current pulse width as an angle between 0 and 180 degrees + int readMicroseconds(); // returns current pulse width in microseconds for this servo (was read_us() in first release) + bool attached(); // return true if this servo is attached, otherwise false +private: + uint8_t servoIndex; // index into the channel data for this servo + int8_t min; // minimum is this value times 4 added to MIN_PULSE_WIDTH + int8_t max; // maximum is this value times 4 added to MAX_PULSE_WIDTH +}; + +#endif diff --git a/libraries/Servo/avr/ServoTimers.h b/libraries/Servo/avr/ServoTimers.h new file mode 100755 index 0000000..9794c8e --- /dev/null +++ b/libraries/Servo/avr/ServoTimers.h @@ -0,0 +1,59 @@ +/* + Servo.h - Interrupt driven Servo library for Arduino using 16 bit timers- Version 2 + Copyright (c) 2009 Michael Margolis. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +/* + * Defines for 16 bit timers used with Servo library + * + * If _useTimerX is defined then TimerX is a 16 bit timer on the current board + * timer16_Sequence_t enumerates the sequence that the timers should be allocated + * _Nbr_16timers indicates how many 16 bit timers are available. + */ + +/** + * AVR Only definitions + * -------------------- + */ + +// Say which 16 bit timers can be used and in what order +#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) +#define _useTimer5 +#define _useTimer1 +#define _useTimer3 +#define _useTimer4 +typedef enum { _timer5, _timer1, _timer3, _timer4, _Nbr_16timers } timer16_Sequence_t; + +#elif defined(__AVR_ATmega32U4__) +#define _useTimer1 +typedef enum { _timer1, _Nbr_16timers } timer16_Sequence_t; + +#elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__) +#define _useTimer3 +#define _useTimer1 +typedef enum { _timer3, _timer1, _Nbr_16timers } timer16_Sequence_t; + +#elif defined(__AVR_ATmega128__) || defined(__AVR_ATmega1281__) || defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega2561__) +#define _useTimer3 +#define _useTimer1 +typedef enum { _timer3, _timer1, _Nbr_16timers } timer16_Sequence_t; + +#else // everything else +#define _useTimer1 +typedef enum { _timer1, _Nbr_16timers } timer16_Sequence_t; +#endif + diff --git a/libraries/SevSeg/LICENSE.txt b/libraries/SevSeg/LICENSE.txt new file mode 100644 index 0000000..e06d208 --- /dev/null +++ b/libraries/SevSeg/LICENSE.txt @@ -0,0 +1,202 @@ +Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "{}" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright {yyyy} {name of copyright owner} + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + diff --git a/libraries/SevSeg/README.md b/libraries/SevSeg/README.md new file mode 100644 index 0000000..c06a9d1 --- /dev/null +++ b/libraries/SevSeg/README.md @@ -0,0 +1,177 @@ +SevSeg +====== + Copyright 2016 Dean Reading + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +* * * + +This library turns your Arduino into a seven segment display controller! Use it to easily display numbers on your seven segment display without any additional controllers. + +It supports common cathode and common anode displays, and the use of switching transistors. Numbers can be displayed in decimal or hexadecimal representation, with decimal places. Characters can be displayed (as accurately as possible). It also supports multiple displays, of varying dimensions. Shift registers and similar devices are NOT supported. + +[Download it from GitHub][1]. + +Direct any questions or suggestions to deanreading@hotmail.com. If I have the time, I'm happy to help you get things working. + +#### Update Version 3.3.0 (February 2017) + + Added the ability to keep leading zeros. This is now an extra + parameter in the begin() function. + +#### Update Version 3.2.0 (December 2016) + + Backwards compatible with version 3.1 + Updated to Arduino 1.5 Library Specification + New display function - no longer consumes processor time with delay() + Now supports hexadecimal number printing + The decimal point can now be omitted with a negative decPlaces + Alphanumeric strings can be displayed (inaccurately) with setChars + Removed #define RESISTORS_ON_SEGMENTS. Now a begin() input + Can now blank() the display + Now 'heavier' - uses more PROGMEM and RAM + + +* * * + +### HARDWARE + +#### Seven Segment Display Pins + +Your display should have: +**Digit Pins** \- One for each digit. These are the 'common pins'. They will be cathodes (negative pins) for common cathode displays, or anodes (positive pins) for common anode displays. +**8 Segment Pins** \- One for each of the seven segments plus the decimal point. + + +#### Arduino Connections + +All digit pins and segment pins can be connected to any of the Arduino's digital or analog pins; just make sure you take note of your connections! + + +#### Current-limiting Resistors + +Don't forget that the display uses LEDs, so you should use current-limiting resistors in series with the *digit pins*. 330 ohms is a safe value if you're unsure. If you use current-limiting resistors on the *segment pins* instead, then set resistorsOnSegments to true (see the example SevSeg_Counter.ino). + +#### Hardware Configuration + +You have to specify your hardware configuration to the library as the first argument in sevseg.begin. The options are detailed below. + +##### Simple, Low Power Displays +These displays are powered directly through the Arduino output pins. + * **COMMON_CATHODE** \- For common cathode displays without switches. These displays require a low voltage at the digit pin to illuminate the digit. + * **COMMON_ANODE** \- For common anode displays without switches. These displays require a high voltage at the digit pin to illuminate the digit. + +##### Displays with Switches +Some displays (mostly bigger ones) use switching transistors, but most people won't have to worry about the configurations below. + * **N_TRANSISTORS** \- If you use N-type transistors to sink current (or any other active-high, low-side switches). + * **P_TRANSISTORS** \- If you use P-type transistors to supply current (or any other active-low, high-side switches). + * **NP_COMMMON_CATHODE** \- If your setup uses N-type AND P-type transistors with a common cathode display. + * **NP_COMMMON_ANODE** \- If your setup uses N-type AND P-type transistors with a common anode display. +Note that use of active-high, high-side switches will have no impact on the configuration chosen. There are usually called high-side switches. + +#### Example Pinout + +In the below pinout, digits are numbered 1, 2, 3, 4. +Segments are numbered A through G plus Decimal Point (DP), according to [this picture][4]. +Pins are ordered as looking at the front of the display. + +[Cheap, 4-digit, 12-pin display from Ebay][5] (labelled HS410561k-32 on bottom edge): +4-digit common anode display, with 2 rows of 6 pins. +``` +Top Row: 1 A F 2 3 B +Bottom Row: E D DP C G 4 +``` + +* * * + +### SOFTWARE + +To install, copy the SevSeg folder into your arduino sketchbook\-libraries folder. More detailed instructions are [here][3]. +The Library Manager can be used from arduino version 1.6.2. + + +#### Setting Up + + + #include "SevSeg.h" + SevSeg sevseg; //Instantiate a seven segment object + + void setup() { + byte numDigits = 4; + byte digitPins[] = {2, 3, 4, 5}; + byte segmentPins[] = {6, 7, 8, 9, 10, 11, 12, 13}; + bool resistorsOnSegments = false; // 'false' means resistors are on digit pins + byte hardwareConfig = COMMON_ANODE; // See README.md for options + sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments); + ... + + +digitPins is an array that stores the arduino pin numbers that the digits are connected to. Order them from left to right. +segmentPins is an array that stores the arduino pin numbers that the segments are connected to. Order them from segment a to g , then the decimal place. +If you wish to use more than 8 digits, increase MAXNUMDIGITS in SevSeg.h. + + +#### Setting a Number + + + sevseg.setNumber(3141,3); // Displays '3.141' + + +The first argument is the number to display. The second argument indicates where the decimal place should be, counted from the least significant digit. E.g. to display an integer, the second argument is 0. +Floats are supported. In this case, the second argument indicates how many decimal places of precision you want to display. E.g: + + + sevseg.setNumber(3.14159f,3); //Displays '3.141' + + +Out of range numbers are shown as '----'. + +If the second argument is -1 or omitted, there will be no decimal place. + +Enter 'true' as the third agument to display the number in hexadecimal representation. + +#### Setting a Character String + + + sevseg.setChars("abcd"); + +Character arrays can be displayed - as accurately as possible on a seven segment display. See SevSeg.cpp digitCodeMap[] to notes on each character. Only alphanumeric characters, plus ' ' and '-' are supported. The character array should be NULL terminated. + + +#### Displaying + + + sevseg.refreshDisplay(); + + +Your program must run the refreshDisplay() function repeatedly to display the number. Note that any delays introduced by other functions will produce undesirable effects on the display. + +To blank the display, call: + + sevseg.blank(); + + + +#### Setting the Brightness + + + sevseg.setBrightness(90); + + +The brightness can be adjusted using a value between 0 and 100. +Note that a 0 does not correspond to no brightness. If your display has noticeable flickering, reducing the brightness level may correct it. + +[1]: https://github.com/DeanIsMe/SevSeg +[2]: https://docs.google.com/file/d/0Bwrp4uluZCpNdE9oWTY0M3BncTA/edit?usp=sharing +[3]: http://arduino.cc/en/Guide/Libraries +[4]: https://en.wikipedia.org/wiki/File:7_segment_display_labeled.svg +[5]: http://www.ebay.com/sch/i.html?LH_BIN=1&_from=R40&_sacat=0&_nkw=7+segment+display+4+digit+2+pcs&_sop=15 + diff --git a/libraries/SevSeg/SevSeg.cpp b/libraries/SevSeg/SevSeg.cpp new file mode 100644 index 0000000..4c56a6a --- /dev/null +++ b/libraries/SevSeg/SevSeg.cpp @@ -0,0 +1,545 @@ +/* SevSeg Library + + Copyright 2017 Dean Reading + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + + This library allows an Arduino to easily display numbers in decimal format on + a 7-segment display without a separate 7-segment display controller. + + Direct any questions or suggestions to deanreading@hotmail.com + See the included readme for instructions. + https://github.com/DeanIsMe/SevSeg + + CHANGELOG + Version 3.3.0 - February 2017 + Added the ability to keep leading zeros. This is now an extra + parameter in the begin() function. + + Version 3.2.0 - December 2016 + Updated to Arduino 1.5 Library Specification + New display function - no longer consumes processor time with delay() + Now supports hexadecimal number printing + The decimal point can now be omitted with a negative decPlaces + decPlaces is now optional in setNumber + Alphanumeric strings can be displayed (inaccurately) with setChars() + Removed #define RESISTORS_ON_SEGMENTS. Now a begin() input + Can now blank() the display + + Version 3.1 - September 2016 + Bug Fixes. No longer uses dynamic memory allocation. + Version 3.0 - November 2014 + Library re-design. A display with any number of digits can be used. + Floats are supported. Support for using transistors for switching. + Much more user friendly. No backwards compatibility. + Uploaded to GitHub to simplify any further development. + Version 2.3; Allows for brightness control. + Version 2.2; Allows 1, 2 or 3 digit displays to be used. + Version 2.1; Includes a bug fix. + Version 2.0; Now works for any digital pin arrangement. + Supports both common anode and common cathode displays. +*/ + +#include "SevSeg.h" + +#define BLANK_IDX 36 // Must match with 'digitCodeMap' +#define DASH_IDX 37 + +static const long powersOf10[] = { + 1, // 10^0 + 10, + 100, + 1000, + 10000, + 100000, + 1000000, + 10000000, + 100000000, + 1000000000 +}; // 10^9 + +static const long powersOf16[] = { + 0x1, // 16^0 + 0x10, + 0x100, + 0x1000, + 0x10000, + 0x100000, + 0x1000000, + 0x10000000 +}; // 16^7 + +// The codes below indicate which segments must be illuminated to display +// each number. +static const byte digitCodeMap[] = { + // GFEDCBA Segments 7-segment map: + B00111111, // 0 "0" AAA + B00000110, // 1 "1" F B + B01011011, // 2 "2" F B + B01001111, // 3 "3" GGG + B01100110, // 4 "4" E C + B01101101, // 5 "5" E C + B01111101, // 6 "6" DDD + B00000111, // 7 "7" + B01111111, // 8 "8" + B01101111, // 9 "9" + B01110111, // 65 'A' + B01111100, // 66 'b' + B00111001, // 67 'C' + B01011110, // 68 'd' + B01111001, // 69 'E' + B01110001, // 70 'F' + B00111101, // 71 'G' + B01110110, // 72 'H' + B00000110, // 73 'I' + B00001110, // 74 'J' + B01110110, // 75 'K' Same as 'H' + B00111000, // 76 'L' + B00000000, // 77 'M' NO DISPLAY + B01010100, // 78 'n' + B00111111, // 79 'O' + B01110011, // 80 'P' + B01100111, // 81 'q' + B01010000, // 82 'r' + B01101101, // 83 'S' + B01111000, // 84 't' + B00111110, // 85 'U' + B00111110, // 86 'V' Same as 'U' + B00000000, // 87 'W' NO DISPLAY + B01110110, // 88 'X' Same as 'H' + B01101110, // 89 'y' + B01011011, // 90 'Z' Same as '2' + B00000000, // 32 ' ' BLANK + B01000000, // 45 '-' DASH +}; + +// Constant pointers to constant data +const byte * const numeralCodes = digitCodeMap; +const byte * const alphaCodes = digitCodeMap + 10; + +// SevSeg Constructor +/******************************************************************************/ +SevSeg::SevSeg() +{ + // Initial value + ledOnTime = 2000; // Corresponds to a brightness of 100 + numDigits = 0; + prevUpdateIdx = 0; + prevUpdateTime = 0; + resOnSegments = 0; + updateWithDelays = 0; +} + + +// begin +/******************************************************************************/ +// Saves the input pin numbers to the class and sets up the pins to be used. +// If you use current-limiting resistors on your segment pins instead of the +// digit pins, then set resOnSegments as true. +// Set updateWithDelays to true if you want to use the 'pre-2017' update method +// That method occupies the processor with delay functions. +void SevSeg::begin(byte hardwareConfig, byte numDigitsIn, byte digitPinsIn[], + byte segmentPinsIn[], bool resOnSegmentsIn, + bool updateWithDelaysIn, bool leadingZerosIn) { + + resOnSegments = resOnSegmentsIn; + updateWithDelays = updateWithDelaysIn; + leadingZeros = leadingZerosIn; + + numDigits = numDigitsIn; + //Limit the max number of digits to prevent overflowing + if (numDigits > MAXNUMDIGITS) numDigits = MAXNUMDIGITS; + + switch (hardwareConfig) { + + case 0: // Common cathode + digitOn = LOW; + segmentOn = HIGH; + break; + + case 1: // Common anode + digitOn = HIGH; + segmentOn = LOW; + break; + + case 2: // With active-high, low-side switches (most commonly N-type FETs) + digitOn = HIGH; + segmentOn = HIGH; + break; + + case 3: // With active low, high side switches (most commonly P-type FETs) + digitOn = LOW; + segmentOn = LOW; + break; + } + + digitOff = !digitOn; + segmentOff = !segmentOn; + + // Save the input pin numbers to library variables + for (byte segmentNum = 0 ; segmentNum < 8 ; segmentNum++) { + segmentPins[segmentNum] = segmentPinsIn[segmentNum]; + } + + for (byte digitNum = 0 ; digitNum < numDigits ; digitNum++) { + digitPins[digitNum] = digitPinsIn[digitNum]; + } + + // Set the pins as outputs, and turn them off + for (byte digit = 0 ; digit < numDigits ; digit++) { + pinMode(digitPins[digit], OUTPUT); + digitalWrite(digitPins[digit], digitOff); + } + + for (byte segmentNum = 0 ; segmentNum < 8 ; segmentNum++) { + pinMode(segmentPins[segmentNum], OUTPUT); + digitalWrite(segmentPins[segmentNum], segmentOff); + } + + setNewNum(0, 0); // Initialise the number displayed to 0 +} + + +// refreshDisplay +/******************************************************************************/ +// Turns on the segments specified in 'digitCodes[]' +// There are 4 versions of this function, with the choice depending on the +// location of the current-limiting resistors, and whether or not you wish to +// use 'update delays' (the standard method until 2017). +// For resistors on *digits* we will cycle through all 8 segments (7 + period), +// turning on the *digits* as appropriate for a given segment, before moving on +// to the next segment. +// For resistors on *segments* we will cycle through all __ # of digits, +// turning on the *segments* as appropriate for a given digit, before moving on +// to the next digit. +// If using update delays, refreshDisplay has a delay between each digit/segment +// as it cycles through. It exits with all LEDs off. +// If not using updateDelays, refreshDisplay exits with a single digit/segment +// on. It will move to the next digit/segment after being called again (if +// enough time has passed). + +void SevSeg::refreshDisplay() { + + if (!updateWithDelays) { + + // Exit if it's not time for the next display change + if (micros() - prevUpdateTime < ledOnTime) return; + prevUpdateTime = micros(); + + if (!resOnSegments) { + /**********************************************/ + // RESISTORS ON DIGITS, UPDATE WITHOUT DELAYS + + + // Turn all lights off for the previous segment + for (byte digitNum = 0 ; digitNum < numDigits ; digitNum++) { + digitalWrite(digitPins[digitNum], digitOff); + } + digitalWrite(segmentPins[prevUpdateIdx], segmentOff); + + prevUpdateIdx++; + if (prevUpdateIdx >= 8) prevUpdateIdx = 0; + + byte segmentNum = prevUpdateIdx; + + // Illuminate the required digits for the new segment + digitalWrite(segmentPins[segmentNum], segmentOn); + for (byte digitNum = 0 ; digitNum < numDigits ; digitNum++) { + if (digitCodes[digitNum] & (1 << segmentNum)) { // Check a single bit + digitalWrite(digitPins[digitNum], digitOn); + } + } + } + else { + /**********************************************/ + // RESISTORS ON SEGMENTS, UPDATE WITHOUT DELAYS + + + // Turn all lights off for the previous digit + for (byte segmentNum = 0 ; segmentNum < 8 ; segmentNum++) { + digitalWrite(segmentPins[segmentNum], segmentOff); + } + digitalWrite(digitPins[prevUpdateIdx], digitOff); + + prevUpdateIdx++; + if (prevUpdateIdx >= numDigits) prevUpdateIdx = 0; + + byte digitNum = prevUpdateIdx; + + // Illuminate the required segments for the new digit + digitalWrite(digitPins[digitNum], digitOn); + for (byte segmentNum = 0 ; segmentNum < 8 ; segmentNum++) { + if (digitCodes[digitNum] & (1 << segmentNum)) { // Check a single bit + digitalWrite(segmentPins[segmentNum], segmentOn); + } + } + } + } + + else { + if (!resOnSegments) { + /**********************************************/ + // RESISTORS ON DIGITS, UPDATE WITH DELAYS + for (byte segmentNum = 0 ; segmentNum < 8 ; segmentNum++) { + + // Illuminate the required digits for this segment + digitalWrite(segmentPins[segmentNum], segmentOn); + for (byte digitNum = 0 ; digitNum < numDigits ; digitNum++) { + if (digitCodes[digitNum] & (1 << segmentNum)) { // Check a single bit + digitalWrite(digitPins[digitNum], digitOn); + } + } + + //Wait with lights on (to increase brightness) + delayMicroseconds(ledOnTime); + + //Turn all lights off + for (byte digitNum = 0 ; digitNum < numDigits ; digitNum++) { + digitalWrite(digitPins[digitNum], digitOff); + } + digitalWrite(segmentPins[segmentNum], segmentOff); + } + } + else { + /**********************************************/ + // RESISTORS ON SEGMENTS, UPDATE WITH DELAYS + for (byte digitNum = 0 ; digitNum < numDigits ; digitNum++) { + + // Illuminate the required segments for this digit + digitalWrite(digitPins[digitNum], digitOn); + for (byte segmentNum = 0 ; segmentNum < 8 ; segmentNum++) { + if (digitCodes[digitNum] & (1 << segmentNum)) { // Check a single bit + digitalWrite(segmentPins[segmentNum], segmentOn); + } + } + + //Wait with lights on (to increase brightness) + delayMicroseconds(ledOnTime); + + //Turn all lights off + for (byte segmentNum = 0 ; segmentNum < 8 ; segmentNum++) { + digitalWrite(segmentPins[segmentNum], segmentOff); + } + digitalWrite(digitPins[digitNum], digitOff); + } + } + } +} + +// setBrightness +/******************************************************************************/ + +void SevSeg::setBrightness(int brightness) { + brightness = constrain(brightness, 0, 100); + ledOnTime = map(brightness, 0, 100, 1, 2000); +} + + +// setNumber +/******************************************************************************/ +// This function only receives the input and passes it to 'setNewNum'. +// It is overloaded for all number data types, so that floats can be handled +// correctly. + +void SevSeg::setNumber(long numToShow, char decPlaces, bool hex) //long +{ + setNewNum(numToShow, decPlaces, hex); +} + +void SevSeg::setNumber(unsigned long numToShow, char decPlaces, bool hex) //unsigned long +{ + setNewNum(numToShow, decPlaces, hex); +} + +void SevSeg::setNumber(int numToShow, char decPlaces, bool hex) //int +{ + setNewNum(numToShow, decPlaces, hex); +} + +void SevSeg::setNumber(unsigned int numToShow, char decPlaces, bool hex) //unsigned int +{ + setNewNum(numToShow, decPlaces, hex); +} + +void SevSeg::setNumber(char numToShow, char decPlaces, bool hex) //char +{ + setNewNum(numToShow, decPlaces, hex); +} + +void SevSeg::setNumber(byte numToShow, char decPlaces, bool hex) //byte +{ + setNewNum(numToShow, decPlaces, hex); +} + +void SevSeg::setNumber(float numToShow, char decPlaces, bool hex) //float +{ + char decPlacesPos = constrain(decPlaces, 0, MAXNUMDIGITS); + if (hex) { + numToShow = numToShow * powersOf16[decPlacesPos]; + } + else { + numToShow = numToShow * powersOf10[decPlacesPos]; + } + // Modify the number so that it is rounded to an integer correctly + numToShow += (numToShow >= 0) ? 0.5f : -0.5f; + setNewNum(numToShow, decPlaces, hex); +} + + +// setNewNum +/******************************************************************************/ +// Changes the number that will be displayed. + +void SevSeg::setNewNum(long numToShow, char decPlaces, bool hex) { + byte digits[numDigits]; + findDigits(numToShow, decPlaces, hex, digits); + setDigitCodes(digits, decPlaces); +} + + +// setSegments +/******************************************************************************/ +// Sets the 'digitCodes' that are required to display the desired segments. +// Using this function, one can display any arbitrary set of segments (like +// letters, symbols or animated cursors). See setDigitCodes() for common +// numeric examples. +// +// Bit-segment mapping: 0bHGFEDCBA +// Visual mapping: +// AAAA 0000 +// F B 5 1 +// F B 5 1 +// GGGG 6666 +// E C 4 2 +// E C 4 2 (Segment H is often called +// DDDD H 3333 7 DP, for Decimal Point) + +void SevSeg::setSegments(byte segs[]) +{ + for (byte digit = 0; digit < numDigits; digit++) { + digitCodes[digit] = segs[digit]; + } +} + +// setChars +/******************************************************************************/ +// Displays the string on the display, as best as possible. +// Only alphanumeric characters plus '-' and ' ' are supported +void SevSeg::setChars(char str[]) +{ + for (byte digit = 0; digit < numDigits; digit++) { + digitCodes[digit] = 0; + } + + for (byte digitNum = 0; digitNum < numDigits; digitNum++) { + char ch = str[digitNum]; + if (ch == '\0') break; // NULL string terminator + if (ch >= '0' && ch <= '9') { // Numerical + digitCodes[digitNum] = numeralCodes[ch - '0']; + } + else if (ch >= 'A' && ch <= 'Z') { + digitCodes[digitNum] = alphaCodes[ch - 'A']; + } + else if (ch >= 'a' && ch <= 'z') { + digitCodes[digitNum] = alphaCodes[ch - 'a']; + } + else if (ch == ' ') { + digitCodes[digitNum] = digitCodeMap[BLANK_IDX]; + } + else { + // Every unknown character is shown as a dash + digitCodes[digitNum] = digitCodeMap[DASH_IDX]; + } + } +} + +// blank +/******************************************************************************/ +void SevSeg::blank(void) { + for (byte digitNum = 0 ; digitNum < numDigits ; digitNum++) { + digitCodes[digitNum] = digitCodeMap[BLANK_IDX]; + } + refreshDisplay(); +} + +// findDigits +/******************************************************************************/ +// Decides what each digit will display. +// Enforces the upper and lower limits on the number to be displayed. + +void SevSeg::findDigits(long numToShow, char decPlaces, bool hex, byte digits[]) { + const long * powersOfBase = hex ? powersOf16 : powersOf10; + const long maxNum = powersOfBase[numDigits] - 1; + const long minNum = -(powersOfBase[numDigits - 1] - 1); + + // If the number is out of range, just display dashes + if (numToShow > maxNum || numToShow < minNum) { + for (byte digitNum = 0 ; digitNum < numDigits ; digitNum++) { + digits[digitNum] = DASH_IDX; + } + } + else { + byte digitNum = 0; + + // Convert all number to positive values + if (numToShow < 0) { + digits[0] = DASH_IDX; + digitNum = 1; // Skip the first iteration + numToShow = -numToShow; + } + + // Find all digits for base's representation, starting with the most + // significant digit + for ( ; digitNum < numDigits ; digitNum++) { + long factor = powersOfBase[numDigits - 1 - digitNum]; + digits[digitNum] = numToShow / factor; + numToShow -= digits[digitNum] * factor; + } + + // Find unnnecessary leading zeros and set them to BLANK + if (decPlaces < 0) decPlaces = 0; + if (!leadingZeros) { + for (digitNum = 0 ; digitNum < (numDigits - 1 - decPlaces) ; digitNum++) { + if (digits[digitNum] == 0) { + digits[digitNum] = BLANK_IDX; + } + // Exit once the first non-zero number is encountered + else if (digits[digitNum] <= 9) { + break; + } + } + } + + } +} + + +// setDigitCodes +/******************************************************************************/ +// Sets the 'digitCodes' that are required to display the input numbers + +void SevSeg::setDigitCodes(byte digits[], char decPlaces) { + + // Set the digitCode for each digit in the display + for (byte digitNum = 0 ; digitNum < numDigits ; digitNum++) { + digitCodes[digitNum] = digitCodeMap[digits[digitNum]]; + // Set the decimal place segment + if (decPlaces >= 0) { + if (digitNum == numDigits - 1 - decPlaces) { + digitCodes[digitNum] |= B10000000; + } + } + } +} + +/// END /// diff --git a/libraries/SevSeg/SevSeg.h b/libraries/SevSeg/SevSeg.h new file mode 100644 index 0000000..3312093 --- /dev/null +++ b/libraries/SevSeg/SevSeg.h @@ -0,0 +1,86 @@ +/* SevSeg Library + + Copyright 2017 Dean Reading + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + + This library allows an Arduino to easily display numbers in decimal format on + a 7-segment display without a separate 7-segment display controller. + + Direct any questions or suggestions to deanreading@hotmail.com + See the included readme for instructions. + */ + +#ifndef MAXNUMDIGITS +#define MAXNUMDIGITS 8 // Can be increased, but the max number is 2^31 +#endif + +#ifndef SevSeg_h +#define SevSeg_h + +#if defined(ARDUINO) && ARDUINO >= 100 +#include "Arduino.h" +#else +#include "WProgram.h" +#endif + +// Use defines to link the hardware configurations to the correct numbers +#define COMMON_CATHODE 0 +#define COMMON_ANODE 1 +#define N_TRANSISTORS 2 +#define P_TRANSISTORS 3 +#define NP_COMMON_CATHODE 1 +#define NP_COMMON_ANODE 0 + + +class SevSeg +{ +public: + SevSeg(); + + void refreshDisplay(); + void begin(byte hardwareConfig, byte numDigitsIn, byte digitPinsIn[], + byte segmentPinsIn[], bool resOnSegmentsIn=0, + bool updateWithDelaysIn=0, bool leadingZerosIn=0); + void setBrightness(int brightnessIn); // A number from 0..100 + + void setNumber(long numToShow, char decPlaces=-1, bool hex=0); + void setNumber(unsigned long numToShow, char decPlaces=-1, bool hex=0); + void setNumber(int numToShow, char decPlaces=-1, bool hex=0); + void setNumber(unsigned int numToShow, char decPlaces=-1, bool hex=0); + void setNumber(char numToShow, char decPlaces=-1, bool hex=0); + void setNumber(byte numToShow, char decPlaces=-1, bool hex=0); + void setNumber(float numToShow, char decPlaces=-1, bool hex=0); + + void setSegments(byte segs[]); + void setChars(char str[]); + void blank(void); + +private: + void setNewNum(long numToShow, char decPlaces, bool hex=0); + void findDigits(long numToShow, char decPlaces, bool hex, byte digits[]); + void setDigitCodes(byte nums[], char decPlaces); + + bool digitOn,digitOff,segmentOn,segmentOff; + bool resOnSegments, updateWithDelays, leadingZeros; + byte digitPins[MAXNUMDIGITS]; + byte segmentPins[8]; + byte numDigits; + byte prevUpdateIdx; + byte digitCodes[MAXNUMDIGITS]; + int ledOnTime; + unsigned long prevUpdateTime; +}; + +#endif //SevSeg_h +/// END /// diff --git a/libraries/SevSeg/examples/SevSeg_Counter/SevSeg_Counter.ino b/libraries/SevSeg/examples/SevSeg_Counter/SevSeg_Counter.ino new file mode 100644 index 0000000..ac5acb5 --- /dev/null +++ b/libraries/SevSeg/examples/SevSeg_Counter/SevSeg_Counter.ino @@ -0,0 +1,54 @@ +/* SevSeg Counter Example + + Copyright 2017 Dean Reading + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + + This example demonstrates a very simple use of the SevSeg library with a 4 + digit display. It displays a counter that counts up, showing deci-seconds. + */ + +#include "SevSeg.h" +SevSeg sevseg; //Instantiate a seven segment controller object + +void setup() { + byte numDigits = 4; + byte digitPins[] = {2, 3, 4, 5}; + byte segmentPins[] = {6, 7, 8, 9, 10, 11, 12, 13}; + bool resistorsOnSegments = false; // 'false' means resistors are on digit pins + byte hardwareConfig = COMMON_ANODE; // See README.md for options + bool updateWithDelays = false; // Default. Recommended + bool leadingZeros = false; // Use 'true' if you'd like to keep the leading zeros + + sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments, updateWithDelays, leadingZeros); + sevseg.setBrightness(90); +} + +void loop() { + static unsigned long timer = millis(); + static int deciSeconds = 0; + + if (millis() - timer >= 100) { + timer += 100; + deciSeconds++; // 100 milliSeconds is equal to 1 deciSecond + + if (deciSeconds == 10000) { // Reset to 0 after counting for 1000 seconds. + deciSeconds=0; + } + sevseg.setNumber(deciSeconds, 1); + } + + sevseg.refreshDisplay(); // Must run repeatedly +} + +/// END /// diff --git a/libraries/SevSeg/examples/testWholeDisplay/testWholeDisplay.ino b/libraries/SevSeg/examples/testWholeDisplay/testWholeDisplay.ino new file mode 100644 index 0000000..ad24f28 --- /dev/null +++ b/libraries/SevSeg/examples/testWholeDisplay/testWholeDisplay.ino @@ -0,0 +1,69 @@ +/* +testWholeDisplay.ino +-test each segment in the display +-a simple example using Dean Reading's SevSeg library to light up all 4 digits plus the 4 decimal points on a 4 digit display +-the purpose of this example is to ensure you have the wires all hooked up right for every segment and digit, and to troubleshoot the display and wiring + to ensure *every* segment and period lights up + +By Gabriel Staples +Website: http://www.ElectricRCAircraftGuy.com +My contact info is available by clicking the "Contact Me" tab at the top of my website. +Written: 1 Oct. 2015 +Last Updated: 1 Oct. 2015 +*/ + +/* +LICENSING: +-this *example file* only is modified from Dean Reading's original example, and is in the public domain. + +Dean Reading's SevSeg library is as follows: +Copyright 2014 Dean Reading + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +#include + +SevSeg sevseg; //Instantiate a seven segment controller object + +void setup() +{ + byte numDigits = 4; + byte digitPins[] = {2, 3, 4, 5}; //Digits: 1,2,3,4 <--put one resistor (ex: 220 Ohms, or 330 Ohms, etc, on each digit pin) + byte segmentPins[] = {6, 7, 8, 9, 10, 11, 12, 13}; //Segments: A,B,C,D,E,F,G,Period + + sevseg.begin(COMMON_ANODE, numDigits, digitPins, segmentPins); + sevseg.setBrightness(10); //Note: 100 brightness simply corresponds to a delay of 2000us after lighting each segment. A brightness of 0 + //is a delay of 1us; it doesn't really affect brightness as much as it affects update rate (frequency). + //Therefore, for a 4-digit 7-segment + pd, COMMON_ANODE display, the max update rate for a "brightness" of 100 is 1/(2000us*8) = 62.5Hz. + //I am choosing a "brightness" of 10 because it increases the max update rate to approx. 1/(200us*8) = 625Hz. + //This is preferable, as it decreases aliasing when recording the display with a video camera....I think. +} + +void loop() +{ + //local vars + static byte decPlace = 0; + + sevseg.setNumber(8888,decPlace); + decPlace++; + decPlace %= 4; //rollover back to 0 once variable gets to 4; To anyone wondering: the % is called the "modulo" operator; see here for explanation & example: https://www.arduino.cc/en/Reference/Modulo + + sevseg.refreshDisplay(); // Must run repeatedly; don't use blocking code (ex: delay()) in the loop() function or this won't work right +} + + + + + + + diff --git a/libraries/SevSeg/keywords.txt b/libraries/SevSeg/keywords.txt new file mode 100644 index 0000000..5ac5249 --- /dev/null +++ b/libraries/SevSeg/keywords.txt @@ -0,0 +1,13 @@ +SevSeg KEYWORD1 +setNumber KEYWORD2 +refreshDisplay KEYWORD2 +setBrightness KEYWORD2 +setSegments KEYWORD2 +setChars KEYWORD2 +blank KEYWORD2 +COMMON_CATHODE LITERAL1 +COMMON_ANODE LITERAL1 +N_TRANSISTORS LITERAL1 +P_TRANSISTORS LITERAL1 +NP_COMMON_CATHODE LITERAL1 +NP_COMMON_ANODE LITERAL1 \ No newline at end of file diff --git a/libraries/SevSeg/library.properties b/libraries/SevSeg/library.properties new file mode 100644 index 0000000..8d28857 --- /dev/null +++ b/libraries/SevSeg/library.properties @@ -0,0 +1,10 @@ +name=SevSeg +version=3.3.0 +author=Dean Reading +maintainer=Dean Reading +sentence=Turns your Arduino into a seven segment display controller! +paragraph=Use it to easily display numbers on your seven segment display without any additional hardware. Supports common cathode and common anode displays, the use of switching transistors, decimal numbers, hexadecimal numbers, and alphanumeric characters. +category=Display +url=https://github.com/DeanIsMe/SevSeg +architectures=* +includes=SevSeg.h diff --git a/libraries/SimpleDHT/.DS_Store b/libraries/SimpleDHT/.DS_Store new file mode 100644 index 0000000..21bb021 Binary files /dev/null and b/libraries/SimpleDHT/.DS_Store differ diff --git a/libraries/SimpleDHT/.gitignore b/libraries/SimpleDHT/.gitignore new file mode 100755 index 0000000..bbf313b --- /dev/null +++ b/libraries/SimpleDHT/.gitignore @@ -0,0 +1,32 @@ +# Object files +*.o +*.ko +*.obj +*.elf + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +*.a +*.la +*.lo + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex + +# Debug files +*.dSYM/ diff --git a/libraries/SimpleDHT/LICENSE b/libraries/SimpleDHT/LICENSE new file mode 100755 index 0000000..f651abe --- /dev/null +++ b/libraries/SimpleDHT/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2016 winlin + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/libraries/SimpleDHT/README.md b/libraries/SimpleDHT/README.md new file mode 100755 index 0000000..f86da99 --- /dev/null +++ b/libraries/SimpleDHT/README.md @@ -0,0 +1,45 @@ +# SimpleDHT + +Simple, Stable and Fast Arduino Temp & Humidity Sensors for +[DHT11 etc](http://learn.adafruit.com/dht). + +1. Simple: Simple C++ code with lots of comments. +1. Stable: Strictly follow the standard DHT protocol. +1. Fast: Support 0.5HZ or 1HZ sampling rate. + +## Usage + +To use this library: + +1. Download the zip from specified version: https://github.com/winlinvip/SimpleDHT/releases +2. Import to Arduino: Arduino => Sketch => Include Library => Add .ZIP Library... +3. Open example: Arduino => File => Examples => Simple DHT sensor library => DHT11Default +4. Connect the DHT11 and Upload program to Arduino. +5. Open the Serial Window of Arduino IDE, we got the result as following. + +``` +================================= +Sample DHT11... +Sample OK: 19 *C, 31 % +================================= +Sample DHT11... +Sample OK: 19 *C, 31 % +================================= +``` + +> Remark: For DHT11, no more than 1 Hz sampling rate (once every second). + +## Examples + +This library including the following examples: + +1. DHT11Default: To sample the temperature and humidity. +1. DHT11WithRawBits: To sample the temperature and humidity, output the 40 raw bits. + + +## Links + +1. [adafruit/DHT-sensor-library](https://github.com/adafruit/DHT-sensor-library) +1. [Arduino #4469: Add SimpleDHT library.](https://github.com/arduino/Arduino/issues/4469) + +Winlin 2016.1 \ No newline at end of file diff --git a/libraries/SimpleDHT/SimpleDHT.cpp b/libraries/SimpleDHT/SimpleDHT.cpp new file mode 100755 index 0000000..1fe39aa --- /dev/null +++ b/libraries/SimpleDHT/SimpleDHT.cpp @@ -0,0 +1,155 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 winlin + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#include "SimpleDHT.h" + +int SimpleDHT11::confirm(int pin, int us, byte level) { + // wait one more count to ensure. + int cnt = us / 10 + 1; + + bool ok = false; + for (int i = 0; i < cnt; i++) { + if (digitalRead(pin) != level) { + ok = true; + break; + } + delayMicroseconds(10); + } + + if (!ok) { + return -1; + } + return 0; +} + +byte SimpleDHT11::bits2byte(byte data[8]) { + byte v = 0; + for (int i = 0; i < 8; i++) { + v += data[i] << (7 - i); + } + return v; +} + +int SimpleDHT11::sample(int pin, byte data[40]) { + // empty output data. + memset(data, 0, 40); + + // notify DHT11 to start: + // 1. PULL LOW 20ms. + // 2. PULL HIGH 20-40us. + // 3. SET TO INPUT. + pinMode(pin, OUTPUT); + digitalWrite(pin, LOW); + delay(20); + digitalWrite(pin, HIGH); + delayMicroseconds(30); + pinMode(pin, INPUT); + + // DHT11 starting: + // 1. PULL LOW 80us + // 2. PULL HIGH 80us + if (confirm(pin, 80, LOW)) { + return 100; + } + if (confirm(pin, 80, HIGH)) { + return 101; + } + + // DHT11 data transmite: + // 1. 1bit start, PULL LOW 50us + // 2. PULL HIGH 26-28us, bit(0) + // 3. PULL HIGH 70us, bit(1) + for (int j = 0; j < 40; j++) { + if (confirm(pin, 50, LOW)) { + return 102; + } + + // read a bit, should never call method, + // for the method call use more than 20us, + // so it maybe failed to detect the bit0. + bool ok = false; + int tick = 0; + for (int i = 0; i < 8; i++, tick++) { + if (digitalRead(pin) != HIGH) { + ok = true; + break; + } + delayMicroseconds(10); + } + if (!ok) { + return 103; + } + data[j] = (tick > 3? 1:0); + } + + // DHT11 EOF: + // 1. PULL LOW 50us. + if (confirm(pin, 50, LOW)) { + return 104; + } + + return 0; +} + +int SimpleDHT11::parse(byte data[40], byte* ptemperature, byte* phumidity) { + byte humidity = bits2byte(data); + byte humidity2 = bits2byte(data + 8); + byte temperature = bits2byte(data + 16); + byte temperature2 = bits2byte(data + 24); + byte check = bits2byte(data + 32); + byte expect = humidity + humidity2 + temperature + temperature2; + if (check != expect) { + return 105; + } + *ptemperature = temperature; + *phumidity = humidity; + return 0; +} + +int SimpleDHT11::read(int pin, byte* ptemperature, byte* phumidity, byte pdata[40]) { + int ret = 0; + + byte data[40] = {0}; + if ((ret = sample(pin, data)) != 0) { + return ret; + } + + byte temperature = 0; + byte humidity = 0; + if ((ret = parse(data, &temperature, &humidity)) != 0) { + return ret; + } + + if (pdata) { + memcpy(pdata, data, 40); + } + if (ptemperature) { + *ptemperature = temperature; + } + if (phumidity) { + *phumidity = humidity; + } + + return ret; +} diff --git a/libraries/SimpleDHT/SimpleDHT.h b/libraries/SimpleDHT/SimpleDHT.h new file mode 100755 index 0000000..19078e9 --- /dev/null +++ b/libraries/SimpleDHT/SimpleDHT.h @@ -0,0 +1,78 @@ +/* + The MIT License (MIT) + + Copyright (c) 2016 winlin + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. + */ + +#ifndef __SIMPLE_DHT_H +#define __SIMPLE_DHT_H + +#include + +/* + Simple DHT11 + + Simple, Stable and Fast DHT11 library. + + The circuit: + * VCC: 5V or 3V + * GND: GND + * DATA: Digital ping, for example, 2. + + 23 Jan 2016 By winlin + + https://github.com/winlinvip/SimpleDHT#usage + +*/ +class SimpleDHT11 { +public: + // to read from dht11. + // @param pin the DHT11 pin. + // @param ptemperature output, NULL to igore. + // @param phumidity output, NULL to ignore. + // @param pdata output 40bits sample, NULL to ignore. + // @remark the min delay for this method is 1s. + int read(int pin, byte* ptemperature, byte* phumidity, byte pdata[40]); +private: + // confirm the OUTPUT is level in us, + // for example, when DHT11 start sample, it will + // 1. PULL LOW 80us, call confirm(pin, 80, LOW) + // 2. PULL HIGH 80us, call confirm(pin, 80, HIGH) + // @return 0 success; oterwise, error. + // @remark should never used to read bits, + // for function call use more time, maybe never got bit0. + // @remark please use simple_dht11_read(). + int confirm(int pin, int us, byte level); + // @data the bits of a byte. + // @remark please use simple_dht11_read(). + byte bits2byte(byte data[8]); + // read temperature and humidity from dht11. + // @param pin the pin for DHT11, for example, 2. + // @param data a byte[40] to read bits to 5bytes. + // @return 0 success; otherwise, error. + // @remark please use simple_dht11_read(). + int sample(int pin, byte data[40]); + // parse the 40bits data to temperature and humidity. + // @remark please use simple_dht11_read(). + int parse(byte data[40], byte* ptemperature, byte* phumidity); +}; + +#endif diff --git a/libraries/SimpleDHT/examples/DHT11Default/DHT11Default.ino b/libraries/SimpleDHT/examples/DHT11Default/DHT11Default.ino new file mode 100755 index 0000000..cc241a1 --- /dev/null +++ b/libraries/SimpleDHT/examples/DHT11Default/DHT11Default.ino @@ -0,0 +1,33 @@ +#include + +// for DHT11, +// VCC: 5V or 3V +// GND: GND +// DATA: 2 +int pinDHT11 = 2; +SimpleDHT11 dht11; + +void setup() { + Serial.begin(9600); +} + +void loop() { + // start working... + Serial.println("================================="); + Serial.println("Sample DHT11..."); + + // read without samples. + byte temperature = 0; + byte humidity = 0; + if (dht11.read(pinDHT11, &temperature, &humidity, NULL)) { + Serial.print("Read DHT11 failed."); + return; + } + + Serial.print("Sample OK: "); + Serial.print((int)temperature); Serial.print(" *C, "); + Serial.print((int)humidity); Serial.println(" %"); + + // DHT11 sampling rate is 1HZ. + delay(1000); +} diff --git a/libraries/SimpleDHT/examples/DHT11WithRawBits/DHT11WithRawBits.ino b/libraries/SimpleDHT/examples/DHT11WithRawBits/DHT11WithRawBits.ino new file mode 100755 index 0000000..981cca5 --- /dev/null +++ b/libraries/SimpleDHT/examples/DHT11WithRawBits/DHT11WithRawBits.ino @@ -0,0 +1,43 @@ +#include + +// for DHT11, +// VCC: 5V or 3V +// GND: GND +// DATA: 2 +int pinDHT11 = 2; +SimpleDHT11 dht11; + +void setup() { + Serial.begin(9600); +} + +void loop() { + // start working... + Serial.println("================================="); + Serial.println("Sample DHT11..."); + + // read with raw sample data. + byte temperature = 0; + byte humidity = 0; + byte data[40] = {0}; + if (dht11.read(pinDHT11, &temperature, &humidity, data)) { + Serial.print("Read DHT11 failed"); + return; + } + + Serial.print("Sample RAW Bits: "); + for (int i = 0; i < 40; i++) { + Serial.print((int)data[i]); + if (i > 0 && ((i + 1) % 4) == 0) { + Serial.print(' '); + } + } + Serial.println(""); + + Serial.print("Sample OK: "); + Serial.print((int)temperature); Serial.print(" *C, "); + Serial.print((int)humidity); Serial.println(" %"); + + // DHT11 sampling rate is 1HZ. + delay(1000); +} diff --git a/libraries/SimpleDHT/keywords.txt b/libraries/SimpleDHT/keywords.txt new file mode 100755 index 0000000..a5f9585 --- /dev/null +++ b/libraries/SimpleDHT/keywords.txt @@ -0,0 +1,17 @@ +########################################### +# Syntax Coloring Map For SimpleDHT +########################################### + +########################################### +# Datatypes (KEYWORD1) +########################################### +SimpleDHT11 KEYWORD1 + +########################################### +# Methods and Functions (KEYWORD2) +########################################### +read KEYWORD2 + +########################################### +# Constants (LITERAL1) +########################################### diff --git a/libraries/SimpleDHT/library.properties b/libraries/SimpleDHT/library.properties new file mode 100755 index 0000000..1bc30fe --- /dev/null +++ b/libraries/SimpleDHT/library.properties @@ -0,0 +1,9 @@ +name=SimpleDHT +version=1.0.2 +author=Winlin +maintainer=Winlin +sentence=Arduino Temp & Humidity Sensors for DHT11 etc. +paragraph=Simple C++ code with lots of comments, strictly follow the standard DHT protocol, supports 0.5HZ or 1HZ sampling rate. +category=Sensors +url=https://github.com/winlinvip/simple-dht +architectures=* diff --git a/libraries/SparkFun_RedBot_Library/LICENSE.md b/libraries/SparkFun_RedBot_Library/LICENSE.md new file mode 100644 index 0000000..8a2f9ad --- /dev/null +++ b/libraries/SparkFun_RedBot_Library/LICENSE.md @@ -0,0 +1,57 @@ +SparkFun License Information +============================ + +SparkFun uses two different licenses for our files - one for hardware and one for code. + +Hardware +--------- + +**SparkFun hardware is released under [Creative Commons Share-alike 4.0 International](http://creativecommons.org/licenses/by-sa/4.0/).** + +Note: This is a human-readable summary of (and not a substitute for) the [license](http://creativecommons.org/licenses/by-sa/4.0/legalcode). + +You are free to: + +Share — copy and redistribute the material in any medium or format +Adapt — remix, transform, and build upon the material +for any purpose, even commercially. +The licensor cannot revoke these freedoms as long as you follow the license terms. +Under the following terms: + +Attribution — You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use. +ShareAlike — If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original. +No additional restrictions — You may not apply legal terms or technological measures that legally restrict others from doing anything the license permits. +Notices: + +You do not have to comply with the license for elements of the material in the public domain or where your use is permitted by an applicable exception or limitation. +No warranties are given. The license may not give you all of the permissions necessary for your intended use. For example, other rights such as publicity, privacy, or moral rights may limit how you use the material. + + +Code +-------- + +**SparkFun code, firmware, and software is released under the [MIT License](http://opensource.org/licenses/MIT).** + +The MIT License (MIT) + +Copyright (c) 2016 SparkFun Electronics, Inc. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + + diff --git a/libraries/SparkFun_RedBot_Library/README.md b/libraries/SparkFun_RedBot_Library/README.md new file mode 100644 index 0000000..6f5e0ed --- /dev/null +++ b/libraries/SparkFun_RedBot_Library/README.md @@ -0,0 +1,44 @@ +SparkFun RedBot Arduino Library +======================================== + +![SparkFun RedBot](https://cdn.sparkfun.com//assets/parts/1/0/2/8/8/13166-07a.jpg) + +[*SparkFun RedBot (ROB-12649)*](https://www.sparkfun.com/products/12649) + +Arduino library to run the SparkFun RedBot. + +Repository Contents +------------------- + +* **/examples** - Example sketches for the library (.ino). Run these from the Arduino IDE. +* **/src** - Source files for the library (.cpp, .h). +* **keywords.txt** - Keywords from this library that will be highlighted in the Arduino IDE. +* **library.properties** - General library properties for the Arduino package manager. + +Documentation +-------------- + +* **[Installing an Arduino Library Guide](https://learn.sparkfun.com/tutorials/installing-an-arduino-library)** - Basic information on how to install an Arduino library. +* **[Product Repository](https://github.com/sparkfun/RedBot)** - Main repository (including hardware files) for the RedBot. +* **[Assembly Guide](https://learn.sparkfun.com/tutorials/assembly-guide-for-redbot-with-shadow-chassis)** - Basic hookup guide for the RedBot. +* **[Experiment Guide](https://learn.sparkfun.com/tutorials/experiment-guide-for-redbot-with-shadow-chassis)** - Experiment guide for the RedBot. + +Products that use this Library +--------------------------------- + +* [ROB-13166](https://www.sparkfun.com/products/13166)- Basic RedBot Kit +* [ROB-12649](https://www.sparkfun.com/products/12649)- RedBot Experiment Kit + + +License Information +------------------- + +This product is _**open source**_! + +Please review the LICENSE.md file for license information. + +If you have any questions or concerns on licensing, please contact techsupport@sparkfun.com. + +Distributed as-is; no warranty is given. + +- Your friends at SparkFun. diff --git a/libraries/SparkFun_RedBot_Library/examples/Exp1_BasicTest/Exp1_BasicTest.ino b/libraries/SparkFun_RedBot_Library/examples/Exp1_BasicTest/Exp1_BasicTest.ino new file mode 100644 index 0000000..765df88 --- /dev/null +++ b/libraries/SparkFun_RedBot_Library/examples/Exp1_BasicTest/Exp1_BasicTest.ino @@ -0,0 +1,39 @@ +/*********************************************************************** + * Exp1_BasicTest -- RedBot Experiment 1 + * + * Time to make sure the electronics work! To test everything out, we're + * going to blink the LED on the board. + * + * This sketch was written by SparkFun Electronics, with lots of help from + * the Arduino community. + * + * 23 Sept 2013 N. Seidle/M. Hord + * 04 Oct 2014 B. Huang + ***********************************************************************/ + +// setup() function runs once at the very beginning. +void setup() +{ + pinMode(13, OUTPUT); // The RedBot has an LED connected to pin 13. + // Pins are all generic, so we have to first configure it + // as an OUTPUT using this command. +} + +// loop() function repeats over and over... forever! +void loop() +{ + // Blink sequence + digitalWrite(13, HIGH); // Turns LED ON -- HIGH puts 5V on pin 13. + delay(500); // delay(500) "pauses" the program for 500 milliseconds + digitalWrite(13, LOW); // Turns LED OFF -- LOW puts 0V on pin 13. + delay(500); // delay(500) "pauses" the program for 500 milliseconds + // The total delay period is 1000 ms, or 1 second. +} + +/*********************************************************************** + * In Arduino, an LED is often connected to pin 13 for "debug" purposes. + * This LED is used as an indicator to make sure that we're able to upload + * code to the board. It's also a good indicator that your program is running. + **********************************************************************/ + + diff --git a/libraries/SparkFun_RedBot_Library/examples/Exp2_DriveForward/Exp2_DriveForward.ino b/libraries/SparkFun_RedBot_Library/examples/Exp2_DriveForward/Exp2_DriveForward.ino new file mode 100644 index 0000000..9ab5298 --- /dev/null +++ b/libraries/SparkFun_RedBot_Library/examples/Exp2_DriveForward/Exp2_DriveForward.ino @@ -0,0 +1,32 @@ +/*********************************************************************** + * Exp2_DriveForward -- RedBot Experiment 2 + * + * Drive forward and stop. + * + * Hardware setup: + * The Power switch must be on, the motors must be connected, and the board must be receiving power + * from the battery. The motor switch must also be switched to RUN. + * + * 23 Sept 2013 N. Seidle/M. Hord + * 04 Oct 2014 B. Huang + ***********************************************************************/ + +#include // This line "includes" the RedBot library into your sketch. +// Provides special objects, methods, and functions for the RedBot. + +RedBotMotors motors; // Instantiate the motor control object. This only needs +// to be done once. + +void setup() +{ + motors.drive(255); // Turn on Left and right motors at full speed forward. + delay(2000); // Waits for 2 seconds + motors.stop(); // Stops both motors +} + +void loop() +{ + // Nothing here. We'll get to this in the next experiment. +} + + diff --git a/libraries/SparkFun_RedBot_Library/examples/Exp3_Turning/Exp3_Turning.ino b/libraries/SparkFun_RedBot_Library/examples/Exp3_Turning/Exp3_Turning.ino new file mode 100644 index 0000000..2e2f307 --- /dev/null +++ b/libraries/SparkFun_RedBot_Library/examples/Exp3_Turning/Exp3_Turning.ino @@ -0,0 +1,51 @@ +/*********************************************************************** + * Exp3_Turning -- RedBot Experiment 3 + * + * Explore turning with the RedBot by controlling the Right and Left motors + * separately. + * + * Hardware setup: + * This code requires only the most basic setup: the motors must be + * connected, and the board must be receiving power from the battery pack. + * + * 23 Sept 2013 N. Seidle/M. Hord + * 04 Oct 2014 B. Huang + ***********************************************************************/ +#include // This line "includes" the library into your sketch. + +RedBotMotors motors; // Instantiate the motor control object. + +void setup() +{ + // drive forward -- instead of using motors.drive(); Here is another way. + motors.rightMotor(150); // Turn on right motor clockwise medium power (motorPower = 150) + motors.leftMotor(-150); // Turn on left motor counter clockwise medium power (motorPower = 150) + delay(1000); // for 1000 ms. + motors.brake(); // brake() motors + + // pivot -- spinning both motors CCW causes the RedBot to turn to the right + motors.rightMotor(-100); // Turn CCW at motorPower of 100 + motors.leftMotor(-100); // Turn CCW at motorPower of 100 + delay(500); // for 500 ms. + motors.brake(); // brake() motors + delay(500); // for 500 ms. + + // drive forward -- instead of using motors.drive(); Here is another way. + motors.rightMotor(150); // Turn on right motor clockwise medium power (motorPower = 150) + motors.leftMotor(-150); // Turn on left motor counter clockwise medium power (motorPower = 150) + delay(1000); // for 1000 ms. + motors.brake(); // brake() motors +} + +void loop() +{ + // Figure 8 pattern -- Turn Right, Turn Left, Repeat + // motors.leftMotor(-200); // Left motor CCW at 200 + // motors.rightMotor(80); // Right motor CW at 80 + // delay(2000); + // motors.leftMotor(-80); // Left motor CCW at 80 + // motors.rightMotor(200); // Right motor CW at 200 + // delay(2000); +} + + diff --git a/libraries/SparkFun_RedBot_Library/examples/Exp4_1_MakingSounds/Exp4_1_MakingSounds.ino b/libraries/SparkFun_RedBot_Library/examples/Exp4_1_MakingSounds/Exp4_1_MakingSounds.ino new file mode 100644 index 0000000..5e6f135 --- /dev/null +++ b/libraries/SparkFun_RedBot_Library/examples/Exp4_1_MakingSounds/Exp4_1_MakingSounds.ino @@ -0,0 +1,52 @@ +/*********************************************************************** + * Exp4_1_MakingSounds -- RedBot Experiment 4.1 + * + * Push the button (D12) to make some noise and start running! + * + * Hardware setup: + * Plug the included RedBot Buzzer board into the Servo header labeled 9. + * + * This sketch was written by SparkFun Electronics,with lots of help from + * the Arduino community. This code is completely free for any use. + * + * 23 Sept 2013 N. Seidle/M. Hord + * 29 Oct 2014 B. Huang + ***********************************************************************/ + +#include +RedBotMotors motors; + +// Create a couple of constants for our pins. +const int buzzerPin = 9; +const int buttonPin = 12; + +void setup() +{ + pinMode(buttonPin, INPUT_PULLUP); // configures the button as an INPUT + // INPUT_PULLUP defaults it to HIGH. + pinMode(buzzerPin, OUTPUT); // configures the buzzerPin as an OUTPUT +} + +void loop() +{ + if ( digitalRead(buttonPin) == LOW ) // if the button is pushed (LOW) + { + tone(buzzerPin, 1000); // Play a 1kHz tone on the pin number held in + // the variable "buzzerPin". + delay(125); // Wait for 125ms. + noTone(buzzerPin); // Stop playing the tone. + + tone(buzzerPin, 2000); // Play a 2kHz tone on the buzzer pin + + motors.drive(255); // Start the motors. The whiskers will stop them. + delay(1000); // delay for 1000 ms (1 second) + + noTone(buzzerPin); // Stop playing the tone. + motors.brake(); // brake() or stop the motors. + } + else // otherwise, do this. + { + } +} + + diff --git a/libraries/SparkFun_RedBot_Library/examples/Exp4_2_Music/Exp4_2_Music.ino b/libraries/SparkFun_RedBot_Library/examples/Exp4_2_Music/Exp4_2_Music.ino new file mode 100644 index 0000000..522b731 --- /dev/null +++ b/libraries/SparkFun_RedBot_Library/examples/Exp4_2_Music/Exp4_2_Music.ino @@ -0,0 +1,133 @@ +/*********************************************************************** + * Exp04_2_Music -- RedBot Experiment 4.2 (Making Music) + * + * Rather than just making beeps and boops, what about playing an actual + * song? This example includes a "header" file called notes.h that has all + * the notes on any standard piano #defined to make composing sounds easier. + * + * Hardware setup: + * Plug the included RedBot Buzzer board into the Servo header labeled 9. + * + * This sketch was written by SparkFun Electronics,with lots of help from + * the Arduino community. This code is completely free for any use. + * + * 23 Sept 2013 N. Seidle/M. Hord + * 29 Oct 2014 B. Huang + * + * Music from: + * http://musicwithmstomomi.global2.vic.edu.au/2013/02/18/recorder-its-small-world-after-all/ + ***********************************************************************/ + +#include "notes.h" // Individual "notes" have been #defined in the notes.h tab to make + // playing sounds easier. noteC4, for example, is defined as 262, the + // frequency for middle C. See the tab above? + +#include +RedBotMotors motors; + +// Create a couple of constants for our pins. +const int buzzerPin = 9; +const int buttonPin = 12; + +void setup() +{ + pinMode(buttonPin, INPUT_PULLUP); // configures the button as an INPUT + // INPUT_PULLUP defaults it to HIGH. + pinMode(buzzerPin, OUTPUT); // configures the buzzerPin as an OUTPUT +} + +void loop() +{ + if(digitalRead(buttonPin) == LOW) + { + playTwinkleTwinkle(); + } +} + +void playTwinkleTwinkle() +{ + playNote(noteC4, QN); + playNote(noteC4, QN); + + playNote(noteG4, QN); + playNote(noteG4, QN); + + playNote(noteA4, QN); + playNote(noteA4, QN); + + playNote(noteG4, HN); + + playNote(noteF4, QN); + playNote(noteF4, QN); + +} + +void playSmallWorld() +{ + // we use a custom function below called playNote([note],[duration]) + // to play a note and delay a certain # of milliseconds. + // + // Both notes and durations are #defined in notes.h -- WN = whole note, + // HN = half note, QN = quarter note, EN = eighth note, SN = sixteenth note. + // + playNote(noteG5, HN+QN); + playNote(noteG5, QN); + playNote(noteB5, HN); + playNote(noteG5, HN); + playNote(noteA5, HN+QN); + playNote(noteA5, QN); + playNote(noteA5, HN+QN); + playNote(Rest, QN); + playNote(noteA5, HN+QN); + playNote(noteA5, QN); + playNote(noteC6, HN); + playNote(noteA5, HN); + playNote(noteB5, HN+QN); + playNote(noteB5, QN); + playNote(noteB5, HN+QN); + playNote(Rest, QN); + playNote(noteB5, HN+QN); + playNote(noteB5, QN); + playNote(noteD6, HN); + playNote(noteB5, HN); + playNote(noteC6, HN+QN); + playNote(noteC6, QN); + playNote(noteC6, HN); + playNote(noteB5, QN); + playNote(noteA5, QN); + playNote(noteD5, WN); + playNote(noteFs5, WN); + playNote(noteG5, WN); +} + +void playNote(int note, int duration) +// This custom function takes two parameters, note and duration to make playing songs easier. +// Each of the notes have been #defined in the notes.h file. The notes are broken down by +// octave and sharp (s) / flat (b). +{ + tone(buzzerPin, note, duration); + delay(duration); +} + +/*********************************************************************** + * Troubleshooting for experiment 4.1 + * My code won't upload! + * - Make sure that your USB cable is plugged into the robot and the + * computer you're using to write code. + * - Make sure that the "Power" switch is switched to "ON". + * - Double check that you have the right serial port selected under the + * "Tools" menu. The easiest way to check is to see which item + * disappears from the menu when you unplug the USB cable, and select + * that one when you plug the board back in. + * - Make sure the Serial Select switch at the top edge of the board is + * switched to "XBEE SW SERIAL", even if you have an Xbee attached. + * - Check that you have the right board selected under the "Tools" menu. + * The RedBot is Uno-compatible, so select "Arduino Uno" from the list. + * My motors aren't turning! + * - This code demonstrates only the tone() commands; there's no code to + * make the motors turn. + ***********************************************************************/ + + + + diff --git a/libraries/SparkFun_RedBot_Library/examples/Exp4_2_Music/notes.h b/libraries/SparkFun_RedBot_Library/examples/Exp4_2_Music/notes.h new file mode 100644 index 0000000..7ff2be0 --- /dev/null +++ b/libraries/SparkFun_RedBot_Library/examples/Exp4_2_Music/notes.h @@ -0,0 +1,177 @@ +/*************************************************************** + * Frequencies for an entire piano keyboard. We pulled this info + * from this website: + * http://www.phy.mtu.edu/~suits/notefreqs.html + * + * Note names are organized by octave and sharp (s) or flat (b). + * + * Note: #define is a pre-compiler directive, so on the first pass + * through your code, the compiler will replace anything that + * has a #define statement associated with it with the appropriate + * string or value. + * + * Notice that #define does not use an '=' or a ';' mark. + * + * It's convenient because it doesn't use up any processor resources + * and the math is done by the compiling computer rather than on + * the Arduino. + * + ***************************************************************/ +#define beatLength 200 // # of milliseconds per beat + +// Define the length of each note +#define WN beatLength*4 // ...a whole note... +#define HN beatLength*2 // ...a half note... +#define QN beatLength // ...a quarter note... +#define EN beatLength/2 // ...an eighth note... +#define SN beatLength/4 // ...and a sixteenth note. + +// These are pre-written #defines for every note on a piano +#define Rest 0 +#define noteC0 16 +#define noteCs0 17 +#define noteDb0 17 +#define noteD0 18 +#define noteDs0 19 +#define noteEb0 19 +#define noteE0 21 +#define noteF0 22 +#define noteFs0 23 +#define noteGb0 23 +#define noteG0 25 +#define noteGs0 26 +#define noteAb0 26 +#define noteA0 28 +#define noteAs0 29 +#define noteBb0 29 +#define noteB0 31 +#define noteC1 33 +#define noteCs1 35 +#define noteDb1 35 +#define noteD1 37 +#define noteDs1 39 +#define noteEb1 39 +#define noteE1 41 +#define noteF1 44 +#define noteFs1 46 +#define noteGb1 46 +#define noteG1 49 +#define noteGs1 52 +#define noteAb1 52 +#define noteA1 55 +#define noteAs1 58 +#define noteBb1 58 +#define noteB1 62 +#define noteC2 65 +#define noteCs2 69 +#define noteDb2 69 +#define noteD2 73 +#define noteDs2 78 +#define noteEb2 78 +#define noteE2 82 +#define noteF2 87 +#define noteFs2 93 +#define noteGb2 93 +#define noteG2 98 +#define noteGs2 104 +#define noteAb2 104 +#define noteA2 110 +#define noteAs2 117 +#define noteBb2 117 +#define noteB2 123 +#define noteC3 131 +#define noteCs3 139 +#define noteDb3 139 +#define noteD3 147 +#define noteDs3 156 +#define noteEb3 156 +#define noteE3 165 +#define noteF3 175 +#define noteFs3 185 +#define noteGb3 185 +#define noteG3 196 +#define noteGs3 208 +#define noteAb3 208 +#define noteA3 220 +#define noteAs3 233 +#define noteBb3 233 +#define noteB3 247 +#define noteC4 262 +#define noteCs4 277 +#define noteDb4 277 +#define noteD4 294 +#define noteDs4 311 +#define noteEb4 311 +#define noteE4 330 +#define noteF4 349 +#define noteFs4 370 +#define noteGb4 370 +#define noteG4 392 +#define noteGs4 415 +#define noteAb4 415 +#define noteA4 440 +#define noteAs4 466 +#define noteBb4 466 +#define noteB4 494 +#define noteC5 523 +#define noteCs5 554 +#define noteDb5 554 +#define noteD5 587 +#define noteDs5 622 +#define noteEb5 622 +#define noteE5 659 +#define noteF5 698 +#define noteFs5 740 +#define noteGb5 740 +#define noteG5 784 +#define noteGs5 831 +#define noteAb5 831 +#define noteA5 880 +#define noteAs5 932 +#define noteBb5 932 +#define noteB5 988 +#define noteC6 1047 +#define noteCs6 1109 +#define noteDb6 1109 +#define noteD6 1175 +#define noteDs6 1245 +#define noteEb6 1245 +#define noteE6 1319 +#define noteF6 1397 +#define noteFs6 1480 +#define noteGb6 1480 +#define noteG6 1568 +#define noteGs6 1661 +#define noteAb6 1661 +#define noteA6 1760 +#define noteAs6 1865 +#define noteBb6 1865 +#define noteB6 1976 +#define noteC7 2093 +#define noteCs7 2217 +#define noteDb7 2217 +#define noteD7 2349 +#define noteDs7 2489 +#define noteEb7 2489 +#define noteE7 2637 +#define noteF7 2794 +#define noteFs7 2960 +#define noteGb7 2960 +#define noteG7 3136 +#define noteGs7 3322 +#define noteAb7 3322 +#define noteA7 3520 +#define noteAs7 3729 +#define noteBb7 3729 +#define noteB7 3951 +#define noteC8 4186 +#define noteCs8 4435 +#define noteDb8 4435 +#define noteD8 4699 +#define noteDs8 4978 +#define noteEb8 4978 + + + + + diff --git a/libraries/SparkFun_RedBot_Library/examples/Exp5_Bumpers/Exp5_Bumpers.ino b/libraries/SparkFun_RedBot_Library/examples/Exp5_Bumpers/Exp5_Bumpers.ino new file mode 100644 index 0000000..ae4f1ed --- /dev/null +++ b/libraries/SparkFun_RedBot_Library/examples/Exp5_Bumpers/Exp5_Bumpers.ino @@ -0,0 +1,83 @@ +/*********************************************************************** + * Exp5_Bumpers -- RedBot Experiment 5 + * + * Now let's experiment with the whisker bumpers. These super-simple switches + * let you detect a collision before it really happens- the whisker will + * bump something before your robot crashes into it. + * + * This sketch was written by SparkFun Electronics, with lots of help from + * the Arduino community. + * This code is completely free for any use. + * Visit https://learn.sparkfun.com/tutorials/redbot-inventors-kit-guide + * for SIK information. + * + * 8 Oct 2013 M. Hord + * Revised 30 Oct 2014 B. Huang + ***********************************************************************/ + +#include +RedBotMotors motors; + +RedBotBumper lBumper = RedBotBumper(3); // initialzes bumper object on pin 3 +RedBotBumper rBumper = RedBotBumper(11); // initialzes bumper object on pin 11 + +int buttonPin = 12; // variable to store the button Pin + +int lBumperState; // state variable to store the bumper value +int rBumperState; // state variable to store the bumper value + +void setup() +{ + // nothing here. +} + +void loop() +{ + motors.drive(255); + lBumperState = lBumper.read(); // default INPUT state is HIGH, it is LOW when bumped + rBumperState = rBumper.read(); // default INPUT state is HIGH, it is LOW when bumped + + if (lBumperState == LOW) // left side is bumped/ + { + reverse(); // backs up + turnRight(); // turns + } + + if (rBumperState == LOW) // right side is bumped/ + { + reverse(); // backs up + turnLeft(); // turns + } + +} + +// reverse() function -- backs up at full power +void reverse() +{ + motors.drive(-255); + delay(500); + motors.brake(); + delay(100); // short delay to let robot fully stop +} + +// turnRight() function -- turns RedBot to the Right +void turnRight() +{ + motors.leftMotor(-150); // spin CCW + motors.rightMotor(-150); // spin CCW + delay(500); + motors.brake(); + delay(100); // short delay to let robot fully stop +} + +// turnRight() function -- turns RedBot to the Left +void turnLeft() +{ + motors.leftMotor(+150); // spin CW + motors.rightMotor(+150); // spin CW + delay(500); + motors.brake(); + delay(100); // short delay to let robot fully stop +} + + diff --git a/libraries/SparkFun_RedBot_Library/examples/Exp6_1_LineFollowing_IRSensors/Exp6_1_LineFollowing_IRSensors.ino b/libraries/SparkFun_RedBot_Library/examples/Exp6_1_LineFollowing_IRSensors/Exp6_1_LineFollowing_IRSensors.ino new file mode 100644 index 0000000..7c00450 --- /dev/null +++ b/libraries/SparkFun_RedBot_Library/examples/Exp6_1_LineFollowing_IRSensors/Exp6_1_LineFollowing_IRSensors.ino @@ -0,0 +1,39 @@ +/*********************************************************************** + * Exp6_LineFollowing_IRSensors -- RedBot Experiment 6_1 + * + * This code reads the three line following sensors on A3, A6, and A7 + * and prints them out to the Serial Monitor. Upload this example to your + * RedBot and open up the Serial Monitor by clicking the magnifying glass + * in the upper-right hand corner. + * + * This sketch was written by SparkFun Electronics,with lots of help from + * the Arduino community. This code is completely free for any use. + * + * 8 Oct 2013 M. Hord + * Revised, 31 Oct 2014 B. Huang + ***********************************************************************/ + +#include +RedBotSensor IRSensor1 = RedBotSensor(A3); // initialize a sensor object on A3 +RedBotSensor IRSensor2 = RedBotSensor(A6); // initialize a sensor object on A6 +RedBotSensor IRSensor3 = RedBotSensor(A7); // initialize a sensor object on A7 + +void setup() +{ + Serial.begin(9600); + Serial.println("Welcome to experiment 6!"); + Serial.println("------------------------"); +} + +void loop() +{ + Serial.print("IR Sensor Readings: "); + Serial.print(IRSensor1.read()); + Serial.print("\t"); // tab character + Serial.print(IRSensor2.read()); + Serial.print("\t"); // tab character + Serial.print(IRSensor3.read()); + Serial.println(); + delay(100); +} + diff --git a/libraries/SparkFun_RedBot_Library/examples/Exp6_2_LineFollowing_IRSensors/Exp6_2_LineFollowing_IRSensors.ino b/libraries/SparkFun_RedBot_Library/examples/Exp6_2_LineFollowing_IRSensors/Exp6_2_LineFollowing_IRSensors.ino new file mode 100644 index 0000000..03ec638 --- /dev/null +++ b/libraries/SparkFun_RedBot_Library/examples/Exp6_2_LineFollowing_IRSensors/Exp6_2_LineFollowing_IRSensors.ino @@ -0,0 +1,93 @@ +/*********************************************************************** + * Exp6_2_LineFollowing_IRSensors -- RedBot Experiment 6_2 + * + * This code reads the three line following sensors on A3, A6, and A7 + * and prints them out to the Serial Monitor. Upload this example to your + * RedBot and open up the Serial Monitor by clicking the magnifying glass + * in the upper-right hand corner. + * + * This is a real simple example of a line following algorithm. It has + * a lot of room for improvement, but works fairly well for a curved track. + * It does not handle right angles reliably -- maybe you can come up with a + * better solution? + * + * This sketch was written by SparkFun Electronics,with lots of help from + * the Arduino community. This code is completely free for any use. + * + * 18 Feb 2015 B. Huang + ***********************************************************************/ + +#include +RedBotSensor left = RedBotSensor(A3); // initialize a left sensor object on A3 +RedBotSensor center = RedBotSensor(A6); // initialize a center sensor object on A6 +RedBotSensor right = RedBotSensor(A7); // initialize a right sensor object on A7 + +// constants that are used in the code. LINETHRESHOLD is the level to detect +// if the sensor is on the line or not. If the sensor value is greater than this +// the sensor is above a DARK line. +// +// SPEED sets the nominal speed + +#define LINETHRESHOLD 800 +#define SPEED 60 // sets the nominal speed. Set to any number from 0 - 255. + +RedBotMotors motors; +int leftSpeed; // variable used to store the leftMotor speed +int rightSpeed; // variable used to store the rightMotor speed + +void setup() +{ + Serial.begin(9600); + Serial.println("Welcome to experiment 6.2 - Line Following"); + Serial.println("------------------------------------------"); + delay(2000); + Serial.println("IR Sensor Readings: "); + delay(500); +} + +void loop() +{ + Serial.print(left.read()); + Serial.print("\t"); // tab character + Serial.print(center.read()); + Serial.print("\t"); // tab character + Serial.print(right.read()); + Serial.println(); + + // if on the line drive left and right at the same speed (left is CCW / right is CW) + if(center.read() > LINETHRESHOLD) + { + leftSpeed = -SPEED; + rightSpeed = SPEED; + } + + // if the line is under the right sensor, adjust relative speeds to turn to the right + else if(right.read() > LINETHRESHOLD) + { + leftSpeed = -(SPEED + 50); + rightSpeed = SPEED - 50; + } + + // if the line is under the left sensor, adjust relative speeds to turn to the left + else if(left.read() > LINETHRESHOLD) + { + leftSpeed = -(SPEED - 50); + rightSpeed = SPEED + 50; + } + + // if all sensors are on black or up in the air, stop the motors. + // otherwise, run motors given the control speeds above. + if((left.read() > LINETHRESHOLD) && (center.read() > LINETHRESHOLD) && (right.read() > LINETHRESHOLD) ) + { + motors.stop(); + } + else + { + motors.leftMotor(leftSpeed); + motors.rightMotor(rightSpeed); + + } + delay(0); // add a delay to decrease sensitivity. +} + + diff --git a/libraries/SparkFun_RedBot_Library/examples/Exp7_1_RotaryEncoder/Exp7_1_RotaryEncoder.ino b/libraries/SparkFun_RedBot_Library/examples/Exp7_1_RotaryEncoder/Exp7_1_RotaryEncoder.ino new file mode 100644 index 0000000..c9811da --- /dev/null +++ b/libraries/SparkFun_RedBot_Library/examples/Exp7_1_RotaryEncoder/Exp7_1_RotaryEncoder.ino @@ -0,0 +1,59 @@ +/*********************************************************************** + * Exp7_1_RotaryEncoder -- RedBot Experiment 7_1 + * + * Knowing where your robot is can be very important. The RedBot supports + * the use of an encoder to track the number of revolutions each wheels has + * made, so you can tell not only how far each wheel has traveled but how + * fast the wheels are turning. + * + * This sketch was written by SparkFun Electronics, with lots of help from + * the Arduino community. This code is completely free for any use. + * + * 8 Oct 2013 M. Hord + * Revised, 31 Oct 2014 B. Huang + ***********************************************************************/ + +#include +RedBotMotors motors; + +RedBotEncoder encoder = RedBotEncoder(A2, 10); // initializes encoder on pins A2 and 10 +int buttonPin = 12; +int countsPerRev = 192; // 4 pairs of N-S x 48:1 gearbox = 192 ticks per wheel rev + +// variables used to store the left and right encoder counts. +int lCount; +int rCount; + +void setup() +{ + pinMode(buttonPin, INPUT_PULLUP); + Serial.begin(9600); + Serial.println("left right"); + Serial.println("================"); +} + +void loop(void) +{ + // wait for a button press to start driving. + if (digitalRead(buttonPin) == LOW) + { + encoder.clearEnc(BOTH); // Reset the counters. + motors.drive(150); // Start driving forward. + } + + // store the encoder counts to a variable. + lCount = encoder.getTicks(LEFT); // read the left motor encoder + rCount = encoder.getTicks(RIGHT); // read the right motor encoder + + // print out to Serial Monitor the left and right encoder counts. + Serial.print(lCount); + Serial.print("\t"); + Serial.println(rCount); + + // if either left or right motor are more than 5 revolutions, stop + if ((lCount >= 5*countsPerRev) || (rCount >= 5*countsPerRev) ) + { + motors.brake(); + } +} + diff --git a/libraries/SparkFun_RedBot_Library/examples/Exp7_2_DriveDistance/Exp7_2_DriveDistance.ino b/libraries/SparkFun_RedBot_Library/examples/Exp7_2_DriveDistance/Exp7_2_DriveDistance.ino new file mode 100644 index 0000000..a6b627c --- /dev/null +++ b/libraries/SparkFun_RedBot_Library/examples/Exp7_2_DriveDistance/Exp7_2_DriveDistance.ino @@ -0,0 +1,73 @@ +/*********************************************************************** + * Exp7_2_DriveDistance -- RedBot Experiment 7.2 + * + * In an earlier experiment, we used a combination of speed and time to + * drive a certain distance. Using the encoders, we can me much more accurate. + * In this example, we will show you how to setup your robot to drive a certain + * distance regardless of the motorPower. + * + * This sketch was written by SparkFun Electronics, with lots of help from + * the Arduino community. This code is completely free for any use. + * + * 8 Oct 2013 M. Hord + * Revised, 31 Oct 2014 B. Huang + ***********************************************************************/ +#include + +RedBotMotors motors; + +RedBotEncoder encoder = RedBotEncoder(A2, 10); +int buttonPin = 12; +int countsPerRev = 192; // 4 pairs of N-S x 48:1 gearbox = 192 ticks per wheel rev + +float wheelDiam = 2.56; // diam = 65mm / 25.4 mm/in +float wheelCirc = PI*wheelDiam; // Redbot wheel circumference = pi*D + +void setup() +{ + pinMode(buttonPin, INPUT_PULLUP); + Serial.begin(9600); +} + +void loop(void) +{ + // drive on button press. + if (digitalRead(buttonPin) == LOW) + { + driveDistance(12, 150); // drive 12 inches, at motorPower = 150. + } +} + +void driveDistance(float distance, int motorPower) +{ + long lCount = 0; + long rCount = 0; + float numRev; + // debug + Serial.print("driveDistance() "); + Serial.print(distance); + Serial.print(" inches at "); + Serial.print(motorPower); + Serial.println(" power."); + + numRev = (float) distance / wheelCirc; + Serial.println(numRev, 3); + encoder.clearEnc(BOTH); // clear the encoder count + motors.drive(motorPower); + + while (rCount < numRev*countsPerRev) + { + // while the left encoder is less than the target count -- debug print + // the encoder values and wait -- this is a holding loop. + lCount = encoder.getTicks(LEFT); + rCount = encoder.getTicks(RIGHT); + Serial.print(lCount); + Serial.print("\t"); + Serial.print(rCount); + Serial.print("\t"); + Serial.println(numRev*countsPerRev); + } + // now apply "brakes" to stop the motors. + motors.brake(); +} + diff --git a/libraries/SparkFun_RedBot_Library/examples/Exp7_3_DriveStraightDistance/Exp7_3_DriveStraightDistance.ino b/libraries/SparkFun_RedBot_Library/examples/Exp7_3_DriveStraightDistance/Exp7_3_DriveStraightDistance.ino new file mode 100644 index 0000000..6fc04d0 --- /dev/null +++ b/libraries/SparkFun_RedBot_Library/examples/Exp7_3_DriveStraightDistance/Exp7_3_DriveStraightDistance.ino @@ -0,0 +1,124 @@ + /*********************************************************************** + * Exp7_3_DriveStraight -- RedBot Experiment 7.3 + * + * Knowing where your robot is can be very important. The RedBot supports + * the use of an encoder to track the number of revolutions each wheels has + * made, so you can tell not only how far each wheel has traveled but how + * fast the wheels are turning. + * + * This sketch was written by SparkFun Electronics, with lots of help from + * the Arduino community. This code is completely free for any use. + * + * 8 Oct 2013 M. Hord + * Revised, 31 Oct 2014 B. Huang + ***********************************************************************/ + #include + RedBotMotors motors; + + RedBotEncoder encoder = RedBotEncoder(A2, 10); + int buttonPin = 12; + int countsPerRev = 192; // 4 pairs of N-S x 48:1 gearbox = 192 ticks per wheel rev + + float wheelDiam = 2.56; // diam = 65mm / 25.4 mm/in + float wheelCirc = PI*wheelDiam; // Redbot wheel circumference = pi*D + + void setup() + { + pinMode(buttonPin, INPUT_PULLUP); + Serial.begin(9600); + } + + void loop(void) + { + // set the power for left & right motors on button press + if (digitalRead(buttonPin) == LOW) + { + driveStraight(12, 150); + } + } + + void driveStraight(float distance, int motorPower) + { + long lCount = 0; + long rCount = 0; + long targetCount; + float numRev; + + // variables for tracking the left and right encoder counts + long prevlCount, prevrCount; + + long lDiff, rDiff; // diff between current encoder count and previous count + + // variables for setting left and right motor power + int leftPower = motorPower; + int rightPower = motorPower; + + // variable used to offset motor power on right vs left to keep straight. + int offset = 5; // offset amount to compensate Right vs. Left drive + + numRev = distance / wheelCirc; // calculate the target # of rotations + targetCount = numRev * countsPerRev; // calculate the target count + + // debug + Serial.print("driveStraight() "); + Serial.print(distance); + Serial.print(" inches at "); + Serial.print(motorPower); + Serial.println(" power."); + + Serial.print("Target: "); + Serial.print(numRev, 3); + Serial.println(" revolutions."); + Serial.println(); + + // print out header + Serial.print("Left\t"); // "Left" and tab + Serial.print("Right\t"); // "Right" and tab + Serial.println("Target count"); + Serial.println("============================"); + + encoder.clearEnc(BOTH); // clear the encoder count + delay(100); // short delay before starting the motors. + + motors.drive(motorPower); // start motors + + while (rCount < targetCount) + { + // while the right encoder is less than the target count -- debug print + // the encoder values and wait -- this is a holding loop. + lCount = encoder.getTicks(LEFT); + rCount = encoder.getTicks(RIGHT); + Serial.print(lCount); + Serial.print("\t"); + Serial.print(rCount); + Serial.print("\t"); + Serial.println(targetCount); + + motors.leftDrive(leftPower); + motors.rightDrive(rightPower); + + // calculate the rotation "speed" as a difference in the count from previous cycle. + lDiff = (lCount - prevlCount); + rDiff = (rCount - prevrCount); + + // store the current count as the "previous" count for the next cycle. + prevlCount = lCount; + prevrCount = rCount; + + // if left is faster than the right, slow down the left / speed up right + if (lDiff > rDiff) + { + leftPower = leftPower - offset; + rightPower = rightPower + offset; + } + // if right is faster than the left, speed up the left / slow down right + else if (lDiff < rDiff) + { + leftPower = leftPower + offset; + rightPower = rightPower - offset; + } + delay(50); // short delay to give motors a chance to respond. + } + // now apply "brakes" to stop the motors. + motors.brake(); + } \ No newline at end of file diff --git a/libraries/SparkFun_RedBot_Library/examples/Exp8_1_AccelerometerRead/Exp8_1_AccelerometerRead.ino b/libraries/SparkFun_RedBot_Library/examples/Exp8_1_AccelerometerRead/Exp8_1_AccelerometerRead.ino new file mode 100644 index 0000000..db718a8 --- /dev/null +++ b/libraries/SparkFun_RedBot_Library/examples/Exp8_1_AccelerometerRead/Exp8_1_AccelerometerRead.ino @@ -0,0 +1,71 @@ +/*********************************************************************** + * Exp8_1_AccelerometerRead -- RedBot Experiment 8.1 + * + * Measuring speed, velocity, and acceleration are all key + * components to robotics. This first experiment will introduce + * you to using the Accelerometer sensor on the RedBot. + * + * Hardware setup: + * You'll need to attach the RedBot Accelerometer board to hader on the upper + * right side of the mainboard. See the manual for details on how to do this. + * + * This sketch was written by SparkFun Electronics, with lots of help from + * the Arduino community. This code is completely free for any use. + * + * 8 Oct 2013 M. Hord + * Revised, 31 Oct 2014 B. Huang + * + * 8 Oct 2013 M. Hord + * + * This experiment was inspired by Paul Kassebaum at Mathworks, who made + * one of the very first non-SparkFun demo projects and brought it to the + * 2013 Open Hardware Summit in Boston. Thanks Paul! + ***********************************************************************/ + +#include +RedBotMotors motors; + +// The RedBot library includes support for the accelerometer. We've tried +// to make using the accelerometer as easy as to use as possible. + +RedBotAccel accelerometer; + +void setup(void) +{ + Serial.begin(9600); + Serial.println("Accelerometer Readings:"); + Serial.println(); + Serial.println("(X, Y, Z) -- [X-Z, Y-Z, X-Y]"); + Serial.println("============================"); +} + +void loop(void) +{ + accelerometer.read(); // updates the x, y, and z axis readings on the acceleromter + + // Display out the X, Y, and Z - axis "acceleration" measurements and also + // the relative angle between the X-Z, Y-Z, and X-Y vectors. (These give us + // the orientation of the RedBot in 3D space. + + Serial.print("("); + Serial.print(accelerometer.x); + Serial.print(", "); // tab + + Serial.print(accelerometer.y); + Serial.print(", "); // tab + + Serial.print(accelerometer.z); + Serial.print(") -- "); // tab + + Serial.print("["); + Serial.print(accelerometer.angleXZ); + Serial.print(", "); + Serial.print(accelerometer.angleYZ); + Serial.print(", "); + Serial.print(accelerometer.angleXY); + Serial.println("]"); + + // short delay in between readings/ + delay(100); +} + diff --git a/libraries/SparkFun_RedBot_Library/examples/Exp8_2_WindUp/Exp8_2_WindUp.ino b/libraries/SparkFun_RedBot_Library/examples/Exp8_2_WindUp/Exp8_2_WindUp.ino new file mode 100644 index 0000000..9cb39f4 --- /dev/null +++ b/libraries/SparkFun_RedBot_Library/examples/Exp8_2_WindUp/Exp8_2_WindUp.ino @@ -0,0 +1,92 @@ +/*********************************************************************** + * Exp8_2_WindUp -- RedBot Experiment 8.2 + * + * This is a fun demo of using the accelerometer to "wind up" the the redBot + * As you tilt the Redbot forward, it should speed up. When you place it flat + * it will race forward for 3 seconds and then stop. + * + * Hardware setup: + * You'll need to attach the RedBot Accelerometer board to hader on the upper + * right side of the mainboard. See the manual for details on how to do this. + * + * This sketch was written by SparkFun Electronics, with lots of help from + * the Arduino community. This code is completely free for any use. + * + * 8 Oct 2013 M. Hord + * Revised, 31 Oct 2014 B. Huang + * + * 8 Oct 2013 M. Hord + * + * This experiment was inspired by Paul Kassebaum at Mathworks, who made + * one of the very first non-SparkFun demo projects and brought it to the + * 2013 Open Hardware Summit in Boston. Thanks Paul! + ***********************************************************************/ + +#include +RedBotMotors motors; +int motorPower; // variable for setting the drive power + +// The RedBot library includes support for the accelerometer. We've tried +// to make using the accelerometer as easy as to use as possible. + +RedBotAccel accelerometer; + +void setup(void) +{ + Serial.begin(9600); +} + +void loop(void) +{ + accelerometer.read(); // updates the x, y, and z axis readings on the accelerometer + + int xAccel = accelerometer.x; + int yAccel = accelerometer.y; + int zAccel = accelerometer.z; + + float XZ = accelerometer.angleXZ; // read in the XZ angle + float YZ = accelerometer.angleYZ; // read in the YZ angle + float XY = accelerometer.angleXY; // read in the XY angle + + Serial.print(XZ, 2); // prints out floating point number with 2 decimal places + Serial.print("\t"); // tab + Serial.println(motorPower); // prints out motorPower + + // if the angle is greater than 20 degrees + if (XZ > 20) + { + // while the angle is greater than 20, speed up or down (match the speed to the angle) + while(XZ > 15) // 5 degree buffer + { + motorPower = map(XZ, 0, 90, 0, 255); + motors.drive(motorPower); // Adjust the motor power with the scaled + // value from the accelerometer. + accelerometer.read(); // Update the readings, so the while() loop + XZ = accelerometer.angleXZ; // Update the variable for the XZ angle + + // debug print statements + Serial.print(XZ, 2); // prints out XZ angle with 2 decimal places + Serial.print("\t"); // tab + Serial.println(motorPower); // prints out motorPower + delay(200); // give you a chance to set the robot down + } + } + // If our accelerometer reading is less than 1500, we just want to let + // the motor run, but slow it down a little bit at a time. + else + { + motors.drive(motorPower); + delay(200); // We don't want to slow the motor too fast, so while + // we're slowing the motor, let's put in a delay so we don't blow through loop() quite as fast. + if (motorPower > 50) + { + motorPower = motorPower - 1; // reduce motorSpeed by 1 each time -- until it is less than 50, then just stop. + } + else + { + motorPower = 0; + } + } +} + + diff --git a/libraries/SparkFun_RedBot_Library/examples/Exp9_SerialDrive/Exp9_SerialDrive.ino b/libraries/SparkFun_RedBot_Library/examples/Exp9_SerialDrive/Exp9_SerialDrive.ino new file mode 100644 index 0000000..57d0054 --- /dev/null +++ b/libraries/SparkFun_RedBot_Library/examples/Exp9_SerialDrive/Exp9_SerialDrive.ino @@ -0,0 +1,52 @@ +/*********************************************************************** + * Exp9_SerialDrive -- RedBot Experiment 9 + * + * The first step to controlling the RedBot remotely is to first drive it + * from the Serial Monitor in a tethered setup. + * + * Hardware setup: + * After uploading this sketch, keep the RedBot tethered to your computer with + * the USB cable. Open up the Seral Monitor to send commands to the RedBot to + * drive. + * + * This sketch was written by SparkFun Electronics, with lots of help from + * the Arduino community. This code is completely free for any use. + * + * 15 Dec 2014 B. Huang + * + * This experiment was inspired by Paul Kassebaum at Mathworks, who made + * one of the very first non-SparkFun demo projects and brought it to the + * 2013 Open Hardware Summit in Boston. Thanks Paul! + ***********************************************************************/ + +#include +RedBotMotors motors; +int leftPower; // variable for setting the drive power +int rightPower; +int data; // variable for holding incoming data from PC to Arduino + +void setup(void) +{ + Serial.begin(9600); + Serial.print("Enter in left and right motor power values and click [Send]."); + Serial.print("Separate values with a space or non-numeric character."); + Serial.println(); + Serial.print("Positive values spin the motor CW, and negative values spin the motor CCW."); +} + +void loop(void) +{ + // if there is data coming in on the Serial monitor, do something with it. + if(Serial.available() > 0) + { + leftPower = Serial.parseInt(); // read in the next numeric value + leftPower = constrain(leftPower, -255, 255); // constrain the data to -255 to +255 + + rightPower = Serial.parseInt(); // read in the next numeric value + rightPower = constrain(rightPower, -255, 255); // constrain the data to -255 to +255 + + motors.leftMotor(leftPower); + motors.rightMotor(rightPower); + + } +} \ No newline at end of file diff --git a/libraries/SparkFun_RedBot_Library/keywords.txt b/libraries/SparkFun_RedBot_Library/keywords.txt new file mode 100644 index 0000000..9c43e72 --- /dev/null +++ b/libraries/SparkFun_RedBot_Library/keywords.txt @@ -0,0 +1,55 @@ +####################################### +# Syntax Coloring Map For RedBot +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +RedBotMotors KEYWORD1 +RedBotSensor KEYWORD1 +RedBotEncoder KEYWORD1 +RedBotAccel KEYWORD1 +RedBotBumper KEYWORD1 +RedBotSoftwareSerial KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +drive KEYWORD2 +pivot KEYWORD2 +rightMotor KEYWORD2 +leftMotor KEYWORD2 +rightDrive KEYWORD2 +leftDrive KEYWORD2 +stop KEYWORD2 +coast KEYWORD2 +brake KEYWORD2 +rightStop KEYWORD2 +leftStop KEYWORD2 +rightCoast KEYWORD2 +leftCoast KEYWORD2 +leftBrake KEYWORD2 +rightBrake KEYWORD2 +clearEnc KEYWORD2 +getTicks KEYWORD2 +read KEYWORD2 +check KEYWORD2 +setBGLevel KEYWORD2 +setDetectLevel KEYWORD2 +calStatus KEYWORD2 +enableBump KEYWORD2 +checkBump KEYWORD2 +setBumpThresh KEYWORD2 +RedBotRadio KEYWORD2 + +####################################### +# Constants (LITERAL1) +####################################### + +NOT_IN_USE LITERAL1 +WHISKER LITERAL1 +LENCODER LITERAL1 +RENCODER LITERAL1 +SW_SERIAL LITERAL1 \ No newline at end of file diff --git a/libraries/SparkFun_RedBot_Library/library.json b/libraries/SparkFun_RedBot_Library/library.json new file mode 100644 index 0000000..1cd6a2a --- /dev/null +++ b/libraries/SparkFun_RedBot_Library/library.json @@ -0,0 +1,13 @@ +{ + "name": "RedBot", + "keywords": "robot, motor, driver", + "description": "The RedBot is a motor driver and Arduino combination with various headers and connections, eliminating the need to stack multiple shields", + "repository": + { + "type": "git", + "url": "https://github.com/sparkfun/SparkFun_RedBot_Arduino_Library.git" + }, + "version": "2.1.0", + "frameworks": "arduino", + "platforms": "atmelavr" +} \ No newline at end of file diff --git a/libraries/SparkFun_RedBot_Library/library.properties b/libraries/SparkFun_RedBot_Library/library.properties new file mode 100644 index 0000000..2a8895a --- /dev/null +++ b/libraries/SparkFun_RedBot_Library/library.properties @@ -0,0 +1,9 @@ +name=SparkFun RedBot Library +version=2.1.0 +author=SparkFun Electronics +maintainer=SparkFun Electronics +sentence=Provides control to the SparkFun RedBot. +paragraph=Includes examples for accelerometer, whisker bumpers, line followers, wheel encoders, and driving control. +category=Device Control +url=https://github.com/sparkfun/SparkFun_Redbot_Arduino_Library +architectures=* \ No newline at end of file diff --git a/libraries/SparkFun_RedBot_Library/src/RedBot.cpp b/libraries/SparkFun_RedBot_Library/src/RedBot.cpp new file mode 100644 index 0000000..cff7fab --- /dev/null +++ b/libraries/SparkFun_RedBot_Library/src/RedBot.cpp @@ -0,0 +1,331 @@ +/**************************************************************** +Main CPP for RedBot. This file handles the pin change interrupts +and how we multiplex between the different potential causes of +a pin change interrupt. + +This code is beerware; if you use it, please buy me (or any other +SparkFun employee) a cold beverage next time you run into one of +us at the local. + +21 Jan 2014- Mike Hord, SparkFun Electronics +Code developed in Arduino 1.0.5, on an SparkFun Redbot v12. +****************************************************************/ + +#include "RedBot.h" +#include +#include + +// We need to track what the prior state of our pins for various PCINTS was; +// this varies by interrupt. These values are initialized to the "all high" +// state; we don't want any low-to-high transitions at beginning of code +// execution to be caught. +volatile byte lastPC0PinState = 0x0E; // For pins 9, 10, 11, PB1-3 +volatile byte lastPC1PinState = 0x3F; // For pins A0-A5/14-19, PC0-5 +volatile byte lastPC2PinState = 0x08; // For pin 3, PD3 + +// We need some way to exclude short transients on the encoder inputs; we'll do +// that by capturing the most recent rise time with micros() and ignoring +// falling edges that happen within 20us of a rise. +volatile unsigned long lastRRise = 0; +volatile unsigned long lastLRise = 0; +volatile unsigned long lastBumpRise = 0; +#define ENC_HIGH_DELAY 50 +#define WHISKER_HIGH_DELAY 0 + +byte PBMask = 0; +byte PCMask = 0; +byte PDMask = 0; + +volatile byte pinFunction[10]; // Store the currently assigned function + // of the PCINT associated with each pin + // in this array. Array indices are of + // the type "PCINT_pinname". + +extern void (*whiskerAction[10])(void); // Declared in RedBotBumper.cpp + +extern RedBotEncoder *encoderObject; // Declared in RedBotEncoder.cpp +RedBotSoftwareSerial *RBSPObject=0; + + +// The RedBot uses pin change interrupts for detecting wheel encoder ticks and +// wire bumper contacts. The sources for these are normally high, so we want to +// look for falling edges and take an action when we see one. +ISR(PCINT0_vect) +{ + // The first thing we want to do is determine which interrupt(s) we're + // servicing, and what those interrupts are associated with. We can cheat, a + // bit, because we know which pins we care about: for PCINT0, it's only + // bits 1, 2, and 3, which are pins 9, 10, and 11 in Arduino-land, or pins + // PB1, PB2, and PB3. + // Since all the pins are in Port B, we can check for a low-to-high transition + // by masking out the pins on Port B we don't care about and returning if they + // are all high. + byte PBTemp = PINB & PBMask; // Capture the state of the pins-of-interest now, + // before they have a chance to change. + + PC0Handler(PBTemp); +} + +void PC0Handler(byte PBTemp) +{ + // Okay, now we have to figure out what changed, and if the change was a + // high-to-low or a low-to-high transition. + + // Was it pin 9, AKA PB1? + if ((lastPC0PinState & 0x02) && !(PBTemp & 0x02)) // a falling edge + { + pinFunctionHandler(PCINT_9); + } + + else if (!(lastPC0PinState & 0x02) && (PBTemp & 0x02)) // a rising edge + { + if (pinFunction[PCINT_9] == LENCODER) lastLRise = micros(); + if (pinFunction[PCINT_9] == RENCODER) lastRRise = micros(); + if (pinFunction[PCINT_9] == WHISKER) lastBumpRise = millis(); + } + // Was it pin 10, AKA PB2? + if ((lastPC0PinState & 0x04) && !(PBTemp & 0x04)) // a falling edge + { + pinFunctionHandler(PCINT_10); + } + else if (!(lastPC0PinState & 0x04) && (PBTemp & 0x04)) // a rising edge + { + if (pinFunction[PCINT_10] == LENCODER) lastLRise = micros(); + if (pinFunction[PCINT_10] == RENCODER) lastRRise = micros(); + if (pinFunction[PCINT_10] == WHISKER) lastBumpRise = millis(); + } + // Was it pin 11, AKA PB3? + if ((lastPC0PinState & 0x08) && !(PBTemp & 0x08)) // a falling edge + { + pinFunctionHandler(PCINT_11); + } + + else if (!(lastPC0PinState & 0x04) && (PBTemp & 0x04)) // a rising edge + { + if (pinFunction[PCINT_11] == LENCODER) lastLRise = micros(); + if (pinFunction[PCINT_11] == RENCODER) lastRRise = micros(); + if (pinFunction[PCINT_11] == WHISKER) lastBumpRise = millis(); + } + + lastPC0PinState = PBTemp; +} + +ISR(PCINT1_vect) +{ + + // The first thing we want to do is determine which interrupt(s) we're + // servicing, and what those interrupts are associated with. We can cheat, a + // bit, because we know which pins we care about: for PCINT1, it's only + // bits 0-5, PC0-PC5, or for Arduino, A0-A5/14-19. + // Since all the pins are in Port C, we can check for a low-to-high transition + // by masking out the pins on Port C we don't care about and returning if they + // are all high. + + byte PCTemp = PINC & PCMask; // Capture the state of the pins-of-interest now, + // before they have a chance to change. + + PC1Handler(PCTemp); +} + +void PC1Handler(byte PCTemp) +{ + // Okay, now we have to figure out what changed, and if the change was a + // high-to-low or a low-to-high transition. All these if() statements check + // for a high-to-low transition; we want to ignore the low-to-highs. + + // Was it pin A0/14, AKA PC0? + if ((lastPC1PinState & 0x01) && !(PCTemp & 0x01)) + { + pinFunctionHandler(PCINT_A0); + } + else if (!(lastPC1PinState & 0x01) && (PCTemp & 0x01)) + { + if (pinFunction[PCINT_A0] == LENCODER) lastLRise = millis(); + if (pinFunction[PCINT_A0] == RENCODER) lastRRise = millis(); + } + // Was it pin A1/15, AKA PC1? + if ((lastPC1PinState & 0x02) && !(PCTemp & 0x02)) + { + pinFunctionHandler(PCINT_A1); + } + else if (!(lastPC1PinState & 0x02) && (PCTemp & 0x02)) + { + if (pinFunction[PCINT_A1] == LENCODER) lastLRise = millis(); + if (pinFunction[PCINT_A1] == RENCODER) lastRRise = millis(); + } + // Was it pin A2/16, AKA PC2? + if ((lastPC1PinState & 0x04) && !(PCTemp & 0x04)) + { + pinFunctionHandler(PCINT_A2); + } + else if (!(lastPC1PinState & 0x04) && (PCTemp & 0x04)) + { + if (pinFunction[PCINT_A2] == LENCODER) lastLRise = millis(); + if (pinFunction[PCINT_A2] == RENCODER) lastRRise = millis(); + } + // Was it pin A3/17, AKA PC3? + if ((lastPC1PinState & 0x08) && !(PCTemp & 0x08)) + { + pinFunctionHandler(PCINT_A3); + } + else if (!(lastPC1PinState & 0x08) && (PCTemp & 0x08)) + { + if (pinFunction[PCINT_A3] == LENCODER) lastLRise = millis(); + if (pinFunction[PCINT_A3] == RENCODER) lastRRise = millis(); + } + // Was it pin A4/18, AKA PC4? + if ((lastPC1PinState & 0x10) && !(PCTemp & 0x10)) + { + pinFunctionHandler(PCINT_A4); + } + else if (!(lastPC1PinState & 0x10) && (PCTemp & 0x10)) + { + if (pinFunction[PCINT_A4] == LENCODER) lastLRise = millis(); + if (pinFunction[PCINT_A4] == RENCODER) lastRRise = millis(); + } + // Was it pin A5/19, AKA PC5? + if ((lastPC1PinState & 0x20) && !(PCTemp & 0x20)) + { + pinFunctionHandler(PCINT_A5); + } + else if (!(lastPC1PinState & 0x20) && (PCTemp & 0x20)) + { + if (pinFunction[PCINT_A5] == LENCODER) lastLRise = millis(); + if (pinFunction[PCINT_A5] == RENCODER) lastRRise = millis(); + } + lastPC1PinState = PCTemp; +} +ISR(PCINT2_vect) +{ + + // The first thing we want to do is determine which interrupt(s) we're + // servicing, and what those interrupts are associated with. We can cheat, a + // bit, because we know which pins we care about: for PCINT2, it's only + // bit 3, PD3 or pin 3 in Arduino-speke. + // First, check if that pin is high. If so, we don't need to know any more. + + byte PDTemp = PIND & PDMask;// Capture the state of the pin-of-interest now, + // before they have a chance to change. + PC2Handler(PDTemp); +} + +void PC2Handler(byte PDTemp) +{ + // Okay, now we know that at least one of our pin-of-interest is low. Which one + // has GONE low since the last time we called this function? + + // Was it pin 3, AKA PD3? + if ((lastPC2PinState & 0x08) && !(PDTemp & 0x08)) + { + pinFunctionHandler(PCINT_3); + } + else if (!(lastPC2PinState & 0x08) && (PDTemp & 0x08)) + { + if (pinFunction[PCINT_3] == LENCODER) lastLRise = millis(); + if (pinFunction[PCINT_3] == RENCODER) lastRRise = millis(); + } + + lastPC2PinState = PDTemp; +} + +void pinFunctionHandler(byte pinIndex) +{ + switch(pinFunction[pinIndex]) + { + case LENCODER: + if (lastLRise + ENC_HIGH_DELAY < micros()) encoderObject->wheelTick(LEFT); + //encoderObject->wheelTick(LEFT); + break; + case RENCODER: + if (lastRRise + ENC_HIGH_DELAY < micros()) encoderObject->wheelTick(RIGHT); + //encoderObject->wheelTick(RIGHT); + break; + case WHISKER: + (*whiskerAction[pinIndex])(); + break; + case SW_SERIAL: + RBSPObject->recv(); + case NOT_IN_USE: + break; + } +} + +void setPinChangeInterrupt(int pin, byte role) +{ + switch(pin) + { + // Start with the analog pins, and provide a means for either the analog + // name or the digital name to enter that case. + case A0: // PCINT 8: PCMSK1, bit 0, PC0 + PCMSK1 |= 0x01; // Enable the pin change interrupt for this pin. + PCICR |= 0x02; // Enable pin change interrupts for this group. + pinFunction[PCINT_A0] = role; // Set the role for this pin- ENCODER, + // whisker, serial, etc. + PCMask |= 0x01; // Add this pin to our mask bits for Port C. + break; + case A1: // PCINT 9: PCMSK1, bit 1, PC1 + PCMSK1 |= 0x02; // Enable the pin change interrupt for this pin. + PCICR |= 0x02; // Enable pin change interrupts for this group. + pinFunction[PCINT_A1] = role; // Set the role for this pin- ENCODER, + // whisker, serial, etc. + PCMask |= 0x02; // Add this pin to our mask bits for Port C. + break; + case A2: // PCINT 10: PCMSK1, bit 2, PC2 + PCMSK1 |= 0x04; // Enable the pin change interrupt for this pin. + PCICR |= 0x02; // Enable pin change interrupts for this group. + pinFunction[PCINT_A2] = role; // Set the role for this pin- ENCODER, + // whisker, serial, etc. + PCMask |= 0x04; // Add this pin to our mask bits for Port C. + break; + case A3: // PCINT 11: PCMSK1, bit 3, PC3 + PCMSK1 |= 0x08; // Enable the pin change interrupt for this pin. + PCICR |= 0x02; // Enable pin change interrupts for this group. + pinFunction[PCINT_A3] = role; // Set the role for this pin- ENCODER, + // whisker, serial, etc. + PCMask |= 0x08; // Add this pin to our mask bits for Port C. + break; + case A4: // PCINT 12: PCMSK1, bit 4 + PCMSK1 |= 0x10; // Enable the pin change interrupt for this pin. + PCICR |= 0x02; // Enable pin change interrupts for this group. + pinFunction[PCINT_A4] = role; // Set the role for this pin- ENCODER, + // whisker, serial, etc. + PCMask |= 0x10; // Add this pin to our mask bits for Port C. + break; + case A5: // PCINT 13: PCMSK1, bit 5 + PCMSK1 |= 0x20; // Enable the pin change interrupt for this pin. + PCICR |= 0x02; // Enable pin change interrupts for this group. + pinFunction[PCINT_A5] = role; // Set the role for this pin- ENCODER, + // whisker, serial, etc. + PCMask |= 0x20; // Add this pin to our mask bits for Port C. + break; + // On to the digital pins. + case 3: // PCINT 19: PCMSK2, bit 3 + PCMSK2 |= 0x08; // Enable the pin change interrupt for this pin. + PCICR |= 0x04; // Enable pin change interrupts for this group. + pinFunction[PCINT_3] = role; // Set the role for this pin- ENCODER, + // whisker, serial, etc. + PDMask |= 0x08; // Add this pin to our mask bits for Port D. + break; + case 9: // PCINT 1: PCMSK0, bit 1 + PCMSK0 |= 0x02; // Enable the pin change interrupt for this pin. + PCICR |= 0x01; // Enable pin change interrupts for this group. + pinFunction[PCINT_9] = role; // Set the role for this pin- ENCODER, + // whisker, serial, etc. + PBMask |= 0x02; // Add this pin to our mask bits for Port B. + break; + case 10: // PCINT 2: PCMSK0, bit 2 + PCMSK0 |= 0x04; // Enable the pin change interrupt for this pin. + PCICR |= 0x01; // Enable pin change interrupts for this group. + pinFunction[PCINT_10] = role; // Set the role for this pin- ENCODER, + // whisker, serial, etc. + PBMask |= 0x04; // Add this pin to our mask bits for Port B. + break; + case 11: // PCINT 3: PCMSK0, bit 3 + PCMSK0 |= 0x08; // Enable the pin change interrupt for this pin. + PCICR |= 0x01; // Enable pin change interrupts for this group. + pinFunction[PCINT_11] = role; // Set the role for this pin- ENCODER, + // whisker, serial, etc. + PBMask |= 0x08; // Add this pin to our mask bits for Port B. + break; + } +} diff --git a/libraries/SparkFun_RedBot_Library/src/RedBot.h b/libraries/SparkFun_RedBot_Library/src/RedBot.h new file mode 100644 index 0000000..241d219 --- /dev/null +++ b/libraries/SparkFun_RedBot_Library/src/RedBot.h @@ -0,0 +1,344 @@ +/**************************************************************** +Core header file for all the various RedBot functions. + +There is additional license info below regarding the use of the +SoftwareSerial library from Arduino 1.0.5; I had good and sound +reasons for creating a derivative class rather than asking users +to simply use the existing library, which are documented below. + +This code is beerware; if you use it, please buy me (or any other +SparkFun employee) a cold beverage next time you run into one of +us at the local. + +21 Jan 2014- Mike Hord, SparkFun Electronics + +Code developed in Arduino 1.0.5, on an SparkFun Redbot v12. +****************************************************************/ + +#ifndef RedBot_h +#define RedBot_h + +#include + +// Pin aliases for the motor controller. +#define L_CTRL_1 2 +#define L_CTRL_2 4 +#define PWM_L 5 + +#define R_CTRL_1 7 +#define R_CTRL_2 8 +#define PWM_R 6 + +// PCINT functionality aliases. Each PCINT has a value set up when the +// class member gets created, and the PCINT service routine will handle +// the choosing the appropriate response to the interrupt. + +#define NOT_IN_USE 0 +#define WHISKER 1 +#define LENCODER 2 +#define RENCODER 3 +#define SW_SERIAL 4 + +#define PCINT_A0 0 +#define PCINT_A1 1 +#define PCINT_A2 2 +#define PCINT_A3 3 +#define PCINT_A4 4 +#define PCINT_A5 5 +#define PCINT_3 6 +#define PCINT_9 7 +#define PCINT_10 8 +#define PCINT_11 9 + +enum WHEEL {LEFT, RIGHT, BOTH}; // Variable for which wheel you're interested in + // when you do things in the encoder class. + +// These three functions need to work from within multiple classes, so we keep +// them separate and add them as friend functions where appropriate. +void setPinChangeInterrupt(int pin, byte role); // The "role" of each pin is + // stored in an array which is accessed in the interrupt + // handler to determine what should be done on a falling edge + // PC interrupt. +void pinFunctionHandler(byte pinIndex); // This is the function which actually + // handles the legwork after the interrupt has identified + // which pin caught the interrupt. +void brake(void); // Globally accessible motor brake. I couldn't figure out how + // to set a function pointer to the RedBotMotors class + // function, and it's a small function, so I just made a + // global in the library. +void PC0Handler(byte PBTemp); +void PC1Handler(byte PCTemp); +void PC2Handler(byte PDTemp); + + +// This class handles motor functionality. I expect one instance of this at the +// start of a piece of RedBot code. +class RedBotMotors +{ + public: + RedBotMotors(); // Constructor. Mainly sets up pins. + void drive(int speed); // Drive in direction given by sign, at speed given + // by magnitude of the parameter. + void drive(int speed, int duration); // drive(), but with a delay(duration) + void pivot(int speed); // Pivot more or less in place. Turns motors in + void pivot(int speed, int duration); // pivot() with a delay(duration) + + void rightMotor(int speed); // Drive just the right motor. speed > 0 CW, speed < 0 CCW. + void leftMotor(int speed); // Drive just the left motor. speed > 0 CW, speed < 0 CCW. + + void rightMotor(int speed, int duration); // Drive just the right motor. speed > 0 CW, speed < 0 CCW. delay(duration) + void leftMotor(int speed, int duration); // Drive just the left motor. speed > 0 CW, speed < 0 CCW. delay(duration) + + void rightDrive(int speed); // Drive just the right motor. speed > 0 CW, speed < 0 CCW. + void leftDrive(int speed); // Drive just the left motor. speed > 0 CCW, speed < 0 CW. + + void stop(); // Stop motors, but allow them to coast to a halt. + void coast(); // Stop motors, but allow them to coast to a halt. + void brake(); // Quick-stop the motors, shorting the leads. + + void rightStop(); // Stop right motor, as with stop(). + void leftStop(); // Stop left motor, as with stop(). + + void rightCoast(); // Stop right motor, as with stop(). + void leftCoast(); // Stop left motor, as with stop(). + + void leftBrake(); // Quick-stop left motor, as with brake(). + void rightBrake(); // Quick-stop right motor, as with brake(). + private: + void leftFwd(byte speed); // These functions are pretty self-explanatory, + void leftRev(byte speed); // and are called by the above functions once + void rightFwd(byte speed);// sign has been used to determine direction. + void rightRev(byte speed); +}; + +// Handler for encoder sensors. Assume only one of this class is present. +// When a negative going edge happens on an encoder pin, a counter is +// incremented (or decremented), depending on the direction last determined +// by one of the motor direction commands. +class RedBotEncoder +{ + // We declare a couple of friends, so they can have access to the private + // members of this class. + friend class RedBotMotors; // Needs access to lDir and rDir. + friend void pinFunctionHandler(byte pinIndex); // Called from within the + // ISRs, this function increments the counts + // by calling wheelTick(). + public: + RedBotEncoder(int lPin, int rPin); // Constructor. Assigns pins, pin + // functions, zeroes counters, and adds a + // reference to the new encoder object for other + // library members to access. + void clearEnc(WHEEL wheel); // Zaps the encoder count for a given wheel (or + // for both wheels). + long getTicks(WHEEL wheel); // Returns the encoder count for a wheel. + private: + void wheelTick(WHEEL wheel); // Increment or decrement a wheel's counts, + // depending on which way the motor is turning. + long lCounts; // Holds the number of ticks for that wheel's + long rCounts; // encoder. + char lDir; // Direction is set by the motor class, according + char rDir; // to what the most recent motion direction for + // the given wheel was. +}; + +// This is a simple class to handle the button object on the robot. It has only one +// method, read(). This returns a boolean value for whether the button is pressed. +class RedBotButton +{ + public: + RedBotButton(); // Constructor. Mainly sets up pins. + boolean read(); // Drive in direction given by sign, at speed given +}; + +// This is the reflectance sensor used for eg line following and table edge +// detection. It's pretty crude, but since they're analog sensors, they're +// kind of hard to work with. +class RedBotSensor +{ + public: + RedBotSensor(int pin); // Configure a pin as a sensor. + int read(); // Return the current value of the pin. + boolean check(); // In theory, this will be true if a deviation from + // detectLevel is found; false otherwise. + int setBGLevel(); // You can calibrate the sensor to detect a deviation + int setDetectLevel(); // from detectLevel; these functions allow for that. + boolean calStatus(); // Have both calibrated levels been set yet? + private: + int _pin; + int _BGLevel; + int _detectLevel; +}; + +// This handles the physical wire-whisker type bumper. +class RedBotBumper +{ + public: + RedBotBumper(int pin); // Simple constructor; when the bumper gets hit, the + // motors will stop. + RedBotBumper(int pin, void(*functionPointer)(void)); // If the user wishes + // to do something other than stop on a whisker, + // bump, they can write a function to do so, and + // use this constructor. + boolean read(); + private: + int _pin; + void setBumpFunction(int pin, void(*functionPointer)(void)); +}; + +// We have a bunch of stuff associated with the accelerometer here. We're going +// to implement our own I2C functions, too, to make things easy on ourselves. +#define XL_ADDR 0x1D // I2C address of the MMA8452Q accelerometer +#define I2C_READ 0x01 // I2C read bit set +// Some values we'll load into TWCR a lot +#define START_COND 0xA4 // (1< +#include + +/****************************************************************************** +* Definitions +******************************************************************************/ + +#define _SS_MAX_RX_BUFF 64 // RX buffer size +#ifndef GCC_VERSION +#define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) +#endif + +class RedBotSoftwareSerial : public Stream +{ + friend void pinFunctionHandler(byte pinIndex); + + public: + // public methods + RedBotSoftwareSerial(); + ~RedBotSoftwareSerial(); + void begin(long speed); + void end(); + bool overflow() { bool ret = _buffer_overflow; _buffer_overflow = false; return ret; } + int peek(); + + virtual size_t write(uint8_t byte); + virtual int read(); + virtual int available(); + virtual void flush(); + + using Print::write; + + private: + // per object data + uint8_t _receivePin; + uint8_t _receiveBitMask; + volatile uint8_t *_receivePortRegister; + uint8_t _transmitBitMask; + volatile uint8_t *_transmitPortRegister; + + uint16_t _rx_delay_centering; + uint16_t _rx_delay_intrabit; + uint16_t _rx_delay_stopbit; + uint16_t _tx_delay; + + uint16_t _buffer_overflow:1; + + // static data + static char _receive_buffer[_SS_MAX_RX_BUFF]; + static volatile uint8_t _receive_buffer_tail; + static volatile uint8_t _receive_buffer_head; + static RedBotSoftwareSerial *active_object; + + // private methods + void recv(); + uint8_t rx_pin_read(); + void tx_pin_write(uint8_t pin_state); + void setTX(uint8_t transmitPin); + void setRX(uint8_t receivePin); + + // private static method for timing + static inline void tunedDelay(uint16_t delay); + +}; + +// We're going to create a special class now, to interface with the onboard +// XBee header. Since we've got the option of either software or hardware serial, +// I'm going to allow the user to choose between modes. + +class RedBotRadio +{ + public: + RedBotRadio(); // Constructor. + private: + +}; + +#endif \ No newline at end of file diff --git a/libraries/SparkFun_RedBot_Library/src/RedBotAccel.cpp b/libraries/SparkFun_RedBot_Library/src/RedBotAccel.cpp new file mode 100644 index 0000000..05b41d5 --- /dev/null +++ b/libraries/SparkFun_RedBot_Library/src/RedBotAccel.cpp @@ -0,0 +1,329 @@ +/**************************************************************** +Main CPP for RedBot accelerometer board. + +This code is beerware; if you use it, please buy me (or any other +SparkFun employee) a cold beverage next time you run into one of +us at the local. + +21 Jan 2014- Mike Hord, SparkFun Electronics + +Code developed in Arduino 1.0.5, on an SparkFun Redbot v12. +****************************************************************/ + +#include "RedBot.h" +#include + +RedBotAccel::RedBotAccel() +{ + byte buffer[2]; + // This sets the bit rate of the bus; I want 100kHz. See the + // datasheet for details on how I came up with this value. + TWBR = 72; + + // The very first thing we want to do is reset the accelerometer. Since + // we don't know what's happened since the last time we powered the + // thing up, the best we can do is reset it by twiddling the reset bit + // (bit 6) in CTRL2 register (0x2B). + buffer[0] = 0x40; + xlWriteBytes(0x2B, buffer, 1); + + // Accelerometer configuration- there are five registers (starting with + // 0x2A) that must be configured for the accelerometer to operate. We'll + // create a byte buffer and fill it with the values we need, then push it + // to the accelerometer. Only the first two registers need to be fiddled + // with, the rest are for interrupts and things we don't need to worry + // about. + + // Let's set the dynamic range to max out at 8g instead of 2. + buffer[0] = 0x02; + xlWriteBytes(0x0E, buffer, 1); + + // Now we'll populate our buffer. + // CTRL1 register, five settings here: + // 7:6 - sample rate during sleep mode (leave default 50Hz) + // 5:3 - data rate (leave default 800Hz) + // 2 - low noise mode (set to 1 to activate) + // 1 - 8-bit mode (leave 0 to disable) + // 0 - active/standby (leave 0, for now) + buffer[0] = 0x04; + + // CTRL2 register, five more settings: + // 7 - self-test (leave 0, disabled) + // 6 - software reset (leave 0, no reset) + // 5 - no use + // 4:3 - sleep mode (leave 00, normal mode) + // 2 - sleep enable (leave 0, disable auto sleep) + // 1:0 - active mode (set 10, hi-res oversample) + buffer[1] = 0x02; + + // Now that we've populated our buffer, we can push it to the accelerometer. + xlWriteBytes(0x2A, buffer, 2); + + // Now that we've set that up, we can enable the part by writing 0x05 to + // CTRL1. + buffer[0] = 0x05; + xlWriteBytes(0x2A, buffer, 1); +} + +// read() initiates a capture of the current x, y, and z values, and stores +// them in the appropriate class member variables. +void RedBotAccel::read() +{ + // The first step, the easy step, is to grab the values. We'll put 'em in + // a byte buffer. + byte buffer[6]; + xlReadBytes(0x01, buffer, 6); + + // Next, we need to copy the data into the member variables so they can be + // accessed by the user. + x = buffer[0]<<8 | buffer[1]; + y = buffer[2]<<8 | buffer[3]; + z = buffer[4]<<8 | buffer[5]; + + // Adding these three calculations adds ~ 700us to this process. + // This method takes ~856 us to run w/o them in and about 1532 us with + // these floating point operations. (BH) + angleXZ = 180*atan2(x,z)/PI; + angleXY = 180*atan2(x,y)/PI; + angleYZ = 180*atan2(y,z)/PI; + +} + +// For bump detection, we're looking for a transient in the Z direction. The +// bump should be pretty hard, so hopefully, we'll be able to distinguish +// between a bump and a tap. +void RedBotAccel::enableBump() +{ + byte buffer[8]; + + // The *very* first thing we need to do is disable the chip; otherwise, + // we can't change the register settings. + xlReadBytes(0x2A, buffer, 1); + buffer[0] &= 0xFE; + xlWriteBytes(0x2A, buffer, 1); + + // To enable tap detection, we need to write some data to registers + // 0x21-0x28. See Freescale app note 4072 for more info about setting + // this up. + + // The very first thing we'll do is enable the LPF for pulse detection. + // This is in register 0x0F. + buffer[0] = 0x10; + xlWriteBytes(0x0F, buffer, 1); + + // Since tap detection and bump detection use the same system resources, + // we need to fetch the data from the accelerometer before we can set up + // tap. + xlReadBytes(0x21, buffer, 8); + + // Now that we have the current settings, we can turn on z-axis tap detection + // by fiddling with the appropriate bits. + + // 0x21 (PULSE_CFG)- We need to set bit 6 (ELE, latch events into register) + // and bit 0 (XSPEFE, x-axis single pulse event function enable) + buffer[0] = 0x41; + + // 0x22 (PULSE_SRC)- we'll read this to check for pulses; it's read only, so + // we don't need to do anything with it here. + buffer[1] |= 0x00; // just a placeholder + + // 0x23- X pulse threshold- experimentally determined to be a good value for a + // threshold. + buffer[2] = 32; + // 0x24- Y pulse threshold + // Both of these can be ignored, and shouldn't be touched, in case they're + // configured for something else. + buffer[3] |= 0x00; // placeholder + + // 0x25 (PULSE_THSZ) + buffer[4] |= 0; + + // 0x26 (PULSE_TMLT)- maximum length a pulse must be to be detected as a tap. + // The length is dependent upon three things: the sampling rate (800Hz), + // whether Pulse_LPF is set or clear in register 0x0F (it's not), and the + // sampling mode (Hi-res). Charts on pp34-35 of the datasheet tell us that + // the maximum pulse length here is this register value times 0.625ms. + buffer[5] = 25; // maximum pulse length of 62.5ms + + // 0x27 (PULSE_LTCY)- lockout time after a pulse occurs before another one + // will be sensed. Charts for value are on page 35 of the datasheet. + buffer[6] = 50; // 125ms lockout period + + // 0x28 (PULSE_WIND)- window within which a second tap must occur to register + // a double tap event. We aren't worried about double taps (yet), so let's + // leave this unchanged. + buffer[7] |= 0x00; // placeholder + + // Write the values we just set up back into the accelerometer. + xlWriteBytes(0x21, buffer, 8); + + // Now we need to put the device back into active mode. + xlReadBytes(0x2A, buffer, 1); + buffer[0] |= 0x01; + xlWriteBytes(0x2A, buffer, 1); +} + +boolean RedBotAccel::checkBump() +{ + byte buffer = 0; + xlReadBytes(0x22, &buffer, 1); // check the PULSE_SRC register to see if a + // pulse event has been registered. This + // will clear all pulse events + if ((buffer&0x10)!=0) return true; // Mask for X events. + else return false; + +} + +void RedBotAccel::setBumpThresh(int xThresh) +{ + byte buffer; + // The *very* first thing we need to do is disable the chip; otherwise, + // we can't change the register settings. + xlReadBytes(0x2A, &buffer, 1); + buffer &= 0xFE; + xlWriteBytes(0x2A, &buffer, 1); + + // 0x23- X pulse threshold- experimentally determined to be a good value for a + // threshold. + buffer = (byte)xThresh; + + // Write the value we just set up back into the accelerometer. + xlWriteBytes(0x23, &buffer, 1); + + // Now we need to put the device back into active mode. + xlReadBytes(0x2A, &buffer, 1); + buffer |= 0x01; + xlWriteBytes(0x2A, &buffer, 1); +} + +// Private function that reads some number of bytes from the accelerometer. +void RedBotAccel::xlReadBytes(byte addr, byte *buffer, byte len) +{ + unsigned int timeout = 0; // We're going to use this to set a timeout on the + // amount of time we'll wait for the bus to become + // available. The minimum period here is about 4ms + // on a 16MHz device. + + // First, we need to write the address we want to read from, so issue a write + // with that address. That's two steps: first, the slave address: + TWCR = START_COND; // Send a start condition + while (!(TWCR&(1< + +void (*whiskerAction[10])(void); // Pointer to an array of functions + // describing what should happen if a + // given pin has a whisker event. + +// Standard class constructor, assumes that you want to halt the motors on a +// bump. A more skilled programmer than I could figure out the error message +// I get if I try to use the brake() function that's a part of the RedBotMotor +// class; I worked around it by making a globally available one. +RedBotBumper::RedBotBumper(int pin) +{ +// setPinChangeInterrupt(pin, WHISKER); + pinMode(pin, INPUT_PULLUP); + //setBumpFunction(pin, &brake); + _pin = pin; // set local variable for the pin + } + +// Bonus points constructor, which allows the user to connect a custom function +// to a bumper. +RedBotBumper::RedBotBumper(int pin, void(*functionPointer)(void)) +{ + setPinChangeInterrupt(pin, WHISKER); + pinMode(pin, INPUT_PULLUP); + setBumpFunction(pin, functionPointer); + _pin = pin; // set local variable for the pin +} + +boolean RedBotBumper::read() +{ + return(digitalRead(_pin)); +} +// Non-class function that puts the brakes on. This is the default behavior if +// the user doesn't specify a custom function for the bumper trigger. +void brake(void) +{ + digitalWrite(L_CTRL_1, HIGH); + digitalWrite(L_CTRL_2, HIGH); + analogWrite(PWM_L, 0); + digitalWrite(R_CTRL_1, HIGH); + digitalWrite(R_CTRL_2, HIGH); + analogWrite(PWM_R, 0); +} + +// I elected to create a function just to do this, rather than trying to wrap +// it into the setPinChangeInterrupt() function. When a bumper action occurs, +// a function will be called. By default, it's to brake the motors; users can +// write a function of their own and this function will point the interrupt +// at that custom function instead. +void RedBotBumper::setBumpFunction(int pin, void(*functionPointer)(void)) +{ + switch(pin) + { + case A0: + whiskerAction[PCINT_A0] = functionPointer; + break; + case A1: + whiskerAction[PCINT_A1] = functionPointer; + break; + case A2: + whiskerAction[PCINT_A2] = functionPointer; + break; + case A3: + whiskerAction[PCINT_A3] = functionPointer; + break; + case A4: + whiskerAction[PCINT_A4] = functionPointer; + break; + case A5: + whiskerAction[PCINT_A5] = functionPointer; + break; + case 3: + whiskerAction[PCINT_3] = functionPointer; + break; + case 9: + whiskerAction[PCINT_9] = functionPointer; + break; + case 10: + whiskerAction[PCINT_10] = functionPointer; + break; + case 11: + whiskerAction[PCINT_11] = functionPointer; + break; + } +} \ No newline at end of file diff --git a/libraries/SparkFun_RedBot_Library/src/RedBotButton.cpp b/libraries/SparkFun_RedBot_Library/src/RedBotButton.cpp new file mode 100644 index 0000000..4129b0f --- /dev/null +++ b/libraries/SparkFun_RedBot_Library/src/RedBotButton.cpp @@ -0,0 +1,30 @@ +/**************************************************************** +Main CPP for RedBot button control. + +This code is beerware; if you use it, please buy me (or any other +SparkFun employee) a cold beverage next time you run into one of +us at the local. + +04 Oct 2014- B. Huang, SparkFun Electronics + +Code developed in Arduino 1.0.6, on an SparkFun Redbot rev02 +****************************************************************/ + +#include "RedBot.h" +#include + +#define BUTTON_PIN 12 + +// Constructor. Mostly for pin setup; note that it's not necessary to configure +// PWM pins as they will be automatically configured with the analogWrite() +// function is called. +RedBotButton::RedBotButton() +{ + // Sets the "default" state of the button to be HIGH. + pinMode(BUTTON_PIN, INPUT_PULLUP); + } + +boolean RedBotButton::read() +{ + return(!digitalRead(BUTTON_PIN)); +} \ No newline at end of file diff --git a/libraries/SparkFun_RedBot_Library/src/RedBotEncoder.cpp b/libraries/SparkFun_RedBot_Library/src/RedBotEncoder.cpp new file mode 100644 index 0000000..5b8f030 --- /dev/null +++ b/libraries/SparkFun_RedBot_Library/src/RedBotEncoder.cpp @@ -0,0 +1,91 @@ +/**************************************************************** +Main CPP for RedBot wheel encoder. + +This code is beerware; if you use it, please buy me (or any other +SparkFun employee) a cold beverage next time you run into one of +us at the local. + +21 Jan 2014- Mike Hord, SparkFun Electronics + +Code developed in Arduino 1.0.5, on an SparkFun Redbot v12. +****************************************************************/ + +#include "RedBot.h" +#include + +RedBotEncoder *encoderObject=0; // Create a local pointer to an instance of this + // class, so we can edit the counts with other + // library functions. + +RedBotEncoder::RedBotEncoder(int lPin, int rPin) +{ + // RedBot only breaks out ten valid pins: + // A0-A5 a.k.a. D14-19 (PCINT 8-13) + // D3 (PCINT 19) + // D9-D11 (PCINT 1-3) + // We'll need a whopping case statement to set up the pin change interrupts + // for this; in fact, we'll need two, but I'll abstract it to a function. + // A call to setPinChangeInterrupt() enables pin change interrupts for that + // pin, and pin change interrupts for the group that pin is a part of. + pinMode(lPin, INPUT_PULLUP); + pinMode(rPin, INPUT_PULLUP); + setPinChangeInterrupt(lPin, LENCODER); + setPinChangeInterrupt(rPin, RENCODER); + lCounts = 0; + rCounts = 0; + encoderObject = this; // We want a local pointer to the class member that is + // instantiated in the sketch, so we can manipulate its + // private members with other classes. + lDir = 1; // default direction to forward -- used for encoder counting + rDir = 1; // default direction to forward -- used for encoder counting +} + +// This private function changes the counter when a tick happens. The direction +// is set by the functions that set the motor direction. +void RedBotEncoder::wheelTick(WHEEL wheel) +{ + switch(wheel) + { + case LEFT: + lCounts+= (long)lDir; + break; + case RIGHT: + rCounts+= (long)rDir; + break; + case BOTH: + break; + } +} + +// Public function to clear the encoder counts. +void RedBotEncoder::clearEnc(WHEEL wheel) +{ + switch(wheel) + { + case LEFT: + lCounts = 0; + break; + case RIGHT: + rCounts = 0; + break; + case BOTH: + lCounts = 0; + rCounts = 0; + break; + } +} + +// Public function to read the encoder counts for a given wheel. +long RedBotEncoder::getTicks(WHEEL wheel) +{ + switch(wheel) + { + case LEFT: + return lCounts; + case RIGHT: + return rCounts; + case BOTH: + return 0; + } + return 0; +} \ No newline at end of file diff --git a/libraries/SparkFun_RedBot_Library/src/RedBotMotors.cpp b/libraries/SparkFun_RedBot_Library/src/RedBotMotors.cpp new file mode 100644 index 0000000..d09b26f --- /dev/null +++ b/libraries/SparkFun_RedBot_Library/src/RedBotMotors.cpp @@ -0,0 +1,313 @@ +/**************************************************************** +Main CPP for RedBot motor control. + +This code is beerware; if you use it, please buy me (or any other +SparkFun employee) a cold beverage next time you run into one of +us at the local. + +21 Jan 2014- Mike Hord, SparkFun Electronics + +Code developed in Arduino 1.0.5, on an SparkFun Redbot v12. +****************************************************************/ + +#include "RedBot.h" +#include + +extern RedBotEncoder *encoderObject; // Declared in RedBotEncoder.cpp + +// Constructor. Mostly for pin setup; note that it's not necessary to configure +// PWM pins as they will be automatically configured with the analogWrite() +// function is called. +RedBotMotors::RedBotMotors() +{ + // The interface to the motor driver is kind of ugly. It's three pins per + // channel: two that define role (forward, reverse, stop, brake) and one + // PWM input for speed. + pinMode(R_CTRL_1, OUTPUT); + pinMode(R_CTRL_2, OUTPUT); + pinMode(L_CTRL_1, OUTPUT); + pinMode(L_CTRL_2, OUTPUT); +} + +// stop() allows the motors to coast to a stop, rather than trying to stop them +// quickly. As will be the case with functions affecting both motors, the +// global stop just calls the individual stop functions for each wheel. +void RedBotMotors::stop() +{ + leftStop(); + rightStop(); +} + +// coast() is the same as stop() -- but more descriptive of what this method does. +// it allows the motors to coast to a stop, rather than trying to stop them +// quickly. As will be the case with functions affecting both motors, the +// global stop just calls the individual stop functions for each wheel. This is +// exactly the same as the stop() method. stop() is retained for backwards compatibilty +void RedBotMotors::coast() +{ + leftStop(); + rightStop(); +} + + +// brake() effectively shorts the two leads of the motor together, which causes +// the motor to resist being turned. It stops quite quickly. +void RedBotMotors::brake() +{ + leftBrake(); + rightBrake(); +} + +// drive() starts both motors. It figures out whether the motors should go +// forward or revers, then calls the appropriate individual functions. Note +// the use of a 16-bit integer for the speed input; an 8-bit integer doesn't +// have the range to reach full speed. The calls to the actual drive functions +// are only 8-bit, since we only have 8-bit PWM. +void RedBotMotors::drive(int speed) +{ + if (speed > 0) + { + leftFwd((byte)(abs(speed))); + rightFwd((byte)(abs(speed))); + } + else + { + leftRev((byte)(abs(speed))); + rightRev((byte)(abs(speed))); + } +} + +void RedBotMotors::drive(int speed, int duration) +{ // this variant of drive() integrates a delay duration to allow for single commmand instruction. + if (speed > 0) + { + leftFwd((byte)(abs(speed))); + rightFwd((byte)(abs(speed))); + } + else + { + leftRev((byte)(abs(speed))); + rightRev((byte)(abs(speed))); + } + delay(duration); + leftStop(); + rightStop(); +} + + +// pivot() is very much like drive(), except the motors are driven in opposite +// directions, so as to pivot the motor on it's central axis. Positive numbers +// turn / rotate the robot clockwise (to the right) -- assuming the motors are hooked up properly. +void RedBotMotors::pivot(int speed) +{ + if (speed > 0) + { + leftFwd((byte)(abs(speed))); + rightRev((byte)(abs(speed))); + } + else + { + leftRev((byte)(abs(speed))); + rightFwd((byte)(abs(speed))); + } +} + +void RedBotMotors::pivot(int speed, int duration) +{ + if (speed > 0) + { + leftRev((byte)(abs(speed))); + rightFwd((byte)(abs(speed))); + } + else + { + leftFwd((byte)(abs(speed))); + rightRev((byte)(abs(speed))); + } + delay(duration); + leftStop(); + rightStop(); + +} + +// Basically the same as drive, but omitting the left motor. +void RedBotMotors::rightMotor(int speed) +{ + if (speed > 0) + { + rightFwd((byte)(abs(speed))); + } + else + { + rightRev((byte)(abs(speed))); + } +} + +void RedBotMotors::rightMotor(int speed, int duration) +{ + if (speed > 0) + { + rightFwd((byte)(abs(speed))); + } + else + { + rightRev((byte)(abs(speed))); + } + delay(duration); + rightStop(); +} + +// Basically the same as drive(), but omitting the right motor. +void RedBotMotors::leftMotor(int speed) +{ + if (speed > 0) + { + leftRev((byte)(abs(speed))); + } + else + { + leftFwd((byte)(abs(speed))); + } +} + +void RedBotMotors::leftMotor(int speed, int duration) +{ + if (speed > 0) + { + leftRev((byte)(abs(speed))); + } + else + { + leftFwd((byte)(abs(speed))); + } + delay(duration); + leftStop(); +} + +void RedBotMotors::rightDrive(int speed) +{ + if (speed > 0) + { + rightFwd((byte)(abs(speed))); + } + else + { + rightRev((byte)(abs(speed))); + } +}void RedBotMotors::leftDrive(int speed) +{ + if (speed > 0) + { + leftFwd((byte)(abs(speed))); + } + else + { + leftRev((byte)(abs(speed))); + } +} + + +// From here out, we deal with the nitty gritty details of telling the motor +// driver what to do. For more information about this, refer to the TB6612FNG +// datasheet. +void RedBotMotors::leftBrake() +{ + digitalWrite(L_CTRL_1, HIGH); + digitalWrite(L_CTRL_2, HIGH); + analogWrite(PWM_L, 0); +} + +void RedBotMotors::rightBrake() +{ + digitalWrite(R_CTRL_1, HIGH); + digitalWrite(R_CTRL_2, HIGH); + analogWrite(PWM_R, 0); +} + +void RedBotMotors::leftStop() // allows left motor to coast to a stop +{ + digitalWrite(L_CTRL_1, LOW); + digitalWrite(L_CTRL_2, LOW); + analogWrite(PWM_L, 0); +} + +void RedBotMotors::rightStop() // allows right motor to coast to a stop +{ + digitalWrite(R_CTRL_1, LOW); + digitalWrite(R_CTRL_2, LOW); + analogWrite(PWM_R, 0); +} + +void RedBotMotors::leftCoast() // same as rightStop() +{ + digitalWrite(L_CTRL_1, LOW); + digitalWrite(L_CTRL_2, LOW); + analogWrite(PWM_L, 0); +} + +void RedBotMotors::rightCoast() // same as leftStop() +{ + digitalWrite(R_CTRL_1, LOW); + digitalWrite(R_CTRL_2, LOW); + analogWrite(PWM_R, 0); +} + + +/****************************************************************************** +Private functions for RedBotMotor +******************************************************************************/ +// These are the motor-driver level abstractions for turning a given motor the +// right direction. Users never see them, and *should* never see them, so we +// make them private. + +void RedBotMotors::leftFwd(byte spd) +{ + digitalWrite(L_CTRL_1, HIGH); + digitalWrite(L_CTRL_2, LOW); + analogWrite(PWM_L, spd); + // If we have an encoder in the system, we want to make sure that it counts + // in the right direction when ticks occur. + if (encoderObject != 0) + { + encoderObject->lDir = 1; + } +} + +void RedBotMotors::leftRev(byte spd) +{ + digitalWrite(L_CTRL_1, LOW); + digitalWrite(L_CTRL_2, HIGH); + analogWrite(PWM_L, spd); + // If we have an encoder in the system, we want to make sure that it counts + // in the right direction when ticks occur. + if (encoderObject != 0) + { + encoderObject->lDir = -1; + } +} + +void RedBotMotors::rightFwd(byte spd) +{ + digitalWrite(R_CTRL_1, HIGH); + digitalWrite(R_CTRL_2, LOW); + analogWrite(PWM_R, spd); + // If we have an encoder in the system, we want to make sure that it counts + // in the right direction when ticks occur. + if (encoderObject != 0) + { + encoderObject->rDir = 1; + } +} + +void RedBotMotors::rightRev(byte spd) +{ + digitalWrite(R_CTRL_1, LOW); + digitalWrite(R_CTRL_2, HIGH); + analogWrite(PWM_R, spd); + // If we have an encoder in the system, we want to make sure that it counts + // in the right direction when ticks occur. + if (encoderObject != 0) + { + encoderObject->rDir = -1; + } +} diff --git a/libraries/SparkFun_RedBot_Library/src/RedBotRadio.cpp b/libraries/SparkFun_RedBot_Library/src/RedBotRadio.cpp new file mode 100644 index 0000000..0253fbb --- /dev/null +++ b/libraries/SparkFun_RedBot_Library/src/RedBotRadio.cpp @@ -0,0 +1,13 @@ +/**************************************************************** +CPP header for various RedBot radio modes. + +Not yet implemented: coming soon! + +This code is beerware; if you use it, please buy me (or any other +SparkFun employee) a cold beverage next time you run into one of +us at the local. + +21 Jan 2014- Mike Hord, SparkFun Electronics + +Code developed in Arduino 1.0.5, on an SparkFun Redbot v12. +****************************************************************/ \ No newline at end of file diff --git a/libraries/SparkFun_RedBot_Library/src/RedBotSensor.cpp b/libraries/SparkFun_RedBot_Library/src/RedBotSensor.cpp new file mode 100644 index 0000000..fa04141 --- /dev/null +++ b/libraries/SparkFun_RedBot_Library/src/RedBotSensor.cpp @@ -0,0 +1,83 @@ +/**************************************************************** +Main CPP for RedBot IR reflectance sensors. + +This code is beerware; if you use it, please buy me (or any other +SparkFun employee) a cold beverage next time you run into one of +us at the local. + +21 Jan 2014- Mike Hord, SparkFun Electronics + +Code developed in Arduino 1.0.5, on an SparkFun Redbot v12. +****************************************************************/ + +#include "RedBot.h" +#include + +RedBotSensor::RedBotSensor(int pin) +{ + _pin = pin; + _BGLevel = -1; + _detectLevel = -1; +} + +int RedBotSensor::read() +{ + return analogRead(_pin); +} + +// If both the background and detection levels for this sensor have been +// set, returns true. +boolean RedBotSensor::calStatus() +{ + if (_BGLevel != -1 && _detectLevel != -1) return true; + else return false; +} + +// An attempt at a decent single-call, quick return line detection function. +boolean RedBotSensor::check() +{ + // Collect a sample. + int level = analogRead(_pin); + + // Are we looking for something that is darker than our normal level (say, + // a table edge, or a black stripe on a white surface) or something that + // is brighter than our normal level (say, a piece of copper tape? on a + // dark floor)? + // Remember, the darker the surface, the higher the value returned. + if (_BGLevel < _detectLevel) // Light-on-dark situation + { + // For a light-on-dark detection, we're looking to see if the level is + // higher than _BGLevel. Our threshold will be a rise above the BGLevel + // of 1/4 the difference between background and detect levels. + int threshold = (_detectLevel - _BGLevel)>>2; + if (level-threshold > _BGLevel) return true; + else return false; + } + else // Dark-on-light situation + { + // For a dark-on-light detection, we'll do exactly the opposite: check to + // see if the level is lower than _BGLevel by at least 1/4 the difference + // between the levels. + int threshold = (_BGLevel - _detectLevel)>>2; + if (level+threshold < _BGLevel) return true; + else return false; + } +} + +// setBGLevel() is used to calibrate the level that we expect to see when we +// aren't seeing something interesting. +int RedBotSensor::setBGLevel() +{ + _BGLevel = analogRead(_pin); + + return _BGLevel; +} + +// setDetectLevel() works exactly the same as setBGLevel(), but with different +// variables. +int RedBotSensor::setDetectLevel() +{ + _detectLevel = analogRead(_pin); + + return _detectLevel; +} \ No newline at end of file diff --git a/libraries/SparkFun_RedBot_Library/src/RedBotSoftwareSerial.cpp b/libraries/SparkFun_RedBot_Library/src/RedBotSoftwareSerial.cpp new file mode 100644 index 0000000..72bc2b3 --- /dev/null +++ b/libraries/SparkFun_RedBot_Library/src/RedBotSoftwareSerial.cpp @@ -0,0 +1,885 @@ +/****************************************************************************** +RedBotSoftwareSerial.cpp +This portion of the RedBot library inherits heavily from the SoftwareSerial +library distributed with Arduino; specifically, it started life as the +SoftwareSerial library from Arduino 1.0.5. The original license and change +history can be found below this, and the entire contents of the file can be +found, commented out, at the bottom of the file. + +The reason for this change is that the RedBot uses the pin change interrupts +for other functions in, and so I needed to re-write this library to reflect that +fact. While I'm at it, though, I can reduce the size of the footprint, since I +know a few facts (target processor, processor speed, pin options, etc). I can +also tailor it to the application a bit more, so that rather than a soft serial +constructor, the user does a RedBotXBee constructor. You'll see, just wait. + +31 Oct 2013, Mike Hord, SparkFun Electronics + +SoftwareSerial.cpp (formerly NewSoftSerial.cpp) - +Multi-instance software serial library for Arduino/Wiring +-- Interrupt-driven receive and other improvements by ladyada + (http://ladyada.net) +-- Tuning, circular buffer, derivation from class Print/Stream, + multi-instance support, porting to 8MHz processors, + various optimizations, PROGMEM delay tables, inverse logic and + direct port writing by Mikal Hart (http://www.arduiniana.org) +-- Pin change interrupt macros by Paul Stoffregen (http://www.pjrc.com) +-- 20MHz processor support by Garrett Mace (http://www.macetech.com) +-- ATmega1280/2560 support by Brett Hagman (http://www.roguerobotics.com/) + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +The latest version of this library can always be found at +http://arduiniana.org. +*/ + +#include "RedBot.h" +#include +#include + +// +// Lookup table +// +typedef struct _DELAY_TABLE +{ + long baud; + unsigned short rx_delay_centering; + unsigned short rx_delay_intrabit; + unsigned short rx_delay_stopbit; + unsigned short tx_delay; +} DELAY_TABLE; + +// Once upon a time, there was an ifdef here; I got rid of it, because I only +// need the 16MHz table for the RedBot. + +static const DELAY_TABLE __attribute__((section(".progmem.data"))) table[] = +{ + // baud rxcenter rxintra rxstop tx + { 115200, 1, 17, 17, 12, }, + { 57600, 10, 37, 37, 33, }, + { 38400, 25, 57, 57, 54, }, + { 31250, 31, 70, 70, 68, }, + { 28800, 34, 77, 77, 74, }, + { 19200, 54, 117, 117, 114, }, + { 14400, 74, 156, 156, 153, }, + { 9600, 114, 236, 236, 233, }, + { 4800, 233, 474, 474, 471, }, + { 2400, 471, 950, 950, 947, }, + { 1200, 947, 1902, 1902, 1899, }, + { 600, 1902, 3804, 3804, 3800, }, + { 300, 3804, 7617, 7617, 7614, }, +}; + +const int XMIT_START_ADJUSTMENT = 5; + +// +// Statics +// +//RedBotSoftwareSerial *RedBotSoftwareSerial::active_object = 0; +extern RedBotSoftwareSerial *RBSPObject; +char RedBotSoftwareSerial::_receive_buffer[_SS_MAX_RX_BUFF]; +volatile uint8_t RedBotSoftwareSerial::_receive_buffer_tail = 0; +volatile uint8_t RedBotSoftwareSerial::_receive_buffer_head = 0; + +// +// Private methods +// + +// static +inline void RedBotSoftwareSerial::tunedDelay(uint16_t delay) { + uint8_t tmp=0; + + asm volatile("sbiw %0, 0x01 \n\t" + "ldi %1, 0xFF \n\t" + "cpi %A0, 0xFF \n\t" + "cpc %B0, %1 \n\t" + "brne .-10 \n\t" + : "+r" (delay), "+a" (tmp) + : "0" (delay) + ); +} + +// +// The receive routine called by the interrupt handler +// +void RedBotSoftwareSerial::recv() +{ + +#if GCC_VERSION < 40302 +// Work-around for avr-gcc 4.3.0 OSX version bug +// Preserve the registers that the compiler misses +// (courtesy of Arduino forum user *etracer*) + asm volatile( + "push r18 \n\t" + "push r19 \n\t" + "push r20 \n\t" + "push r21 \n\t" + "push r22 \n\t" + "push r23 \n\t" + "push r26 \n\t" + "push r27 \n\t" + ::); +#endif + + uint8_t d = 0; + + // Wait approximately 1/2 of a bit width to "center" the sample + tunedDelay(_rx_delay_centering); + + // Read each of the 8 bits + for (uint8_t i=0x1; i; i <<= 1) + { + tunedDelay(_rx_delay_intrabit); + uint8_t noti = ~i; + if (rx_pin_read()) + d |= i; + else // else clause added to ensure function timing is ~balanced + d &= noti; + } + + // skip the stop bit + tunedDelay(_rx_delay_stopbit); + + // if buffer full, set the overflow flag and return + if ((_receive_buffer_tail + 1) % _SS_MAX_RX_BUFF != _receive_buffer_head) + { + // save new data in buffer: tail points to where byte goes + _receive_buffer[_receive_buffer_tail] = d; // save new byte + _receive_buffer_tail = (_receive_buffer_tail + 1) % _SS_MAX_RX_BUFF; + } + else + { + _buffer_overflow = true; + } + +#if GCC_VERSION < 40302 +// Work-around for avr-gcc 4.3.0 OSX version bug +// Restore the registers that the compiler misses + asm volatile( + "pop r27 \n\t" + "pop r26 \n\t" + "pop r23 \n\t" + "pop r22 \n\t" + "pop r21 \n\t" + "pop r20 \n\t" + "pop r19 \n\t" + "pop r18 \n\t" + ::); +#endif +} + +void RedBotSoftwareSerial::tx_pin_write(uint8_t pin_state) +{ + if (pin_state == LOW) + *_transmitPortRegister &= ~_transmitBitMask; + else + *_transmitPortRegister |= _transmitBitMask; +} + +uint8_t RedBotSoftwareSerial::rx_pin_read() +{ + return *_receivePortRegister & _receiveBitMask; +} + +// +// Interrupt handling- this is the area that needs the biggest change for +// supporting the RedBot. After all, we need to be capable of dealing with +// PCINT triggers from three different sources: encoders, whisker bumpers, and +// the software serial port. HOWEVER, we can constrain things a bit: after all, +// we're only expecting software serial input on ONE pin: PC1, AKA pin 15. That, +// plus the pre-determined processing targeting knowledge of the processor, can +// allow for some serious streamlining of this. We can remove PCINTO, 2, and 3 +// from consideration entirely, and we know that we won't have more than one +// SWSP in operation, so we can remove some of the support for that, too. + +// +// Constructor +// +RedBotSoftwareSerial::RedBotSoftwareSerial() : + _rx_delay_centering(0), + _rx_delay_intrabit(0), + _rx_delay_stopbit(0), + _tx_delay(0), + _buffer_overflow(false) +{ + setPinChangeInterrupt(15, SW_SERIAL); + RBSPObject = this; + setTX(14); + setRX(15); +} + +// +// Destructor +// +RedBotSoftwareSerial::~RedBotSoftwareSerial() +{ + end(); +} + +void RedBotSoftwareSerial::setTX(uint8_t tx) +{ + pinMode(tx, OUTPUT); + digitalWrite(tx, HIGH); + _transmitBitMask = digitalPinToBitMask(tx); + uint8_t port = digitalPinToPort(tx); + _transmitPortRegister = portOutputRegister(port); +} + +void RedBotSoftwareSerial::setRX(uint8_t rx) +{ + pinMode(rx, INPUT); + _receivePin = rx; + _receiveBitMask = digitalPinToBitMask(rx); + uint8_t port = digitalPinToPort(rx); + _receivePortRegister = portInputRegister(port); +} + +// +// Public methods +// + +void RedBotSoftwareSerial::begin(long speed) +{ + _rx_delay_centering = _rx_delay_intrabit = _rx_delay_stopbit = _tx_delay = 0; + + for (unsigned i=0; i +#include +#include +#include +// +// Lookup table +// +typedef struct _DELAY_TABLE +{ + long baud; + unsigned short rx_delay_centering; + unsigned short rx_delay_intrabit; + unsigned short rx_delay_stopbit; + unsigned short tx_delay; +} DELAY_TABLE; + +#if F_CPU == 16000000 + +static const DELAY_TABLE PROGMEM table[] = +{ + // baud rxcenter rxintra rxstop tx + { 115200, 1, 17, 17, 12, }, + { 57600, 10, 37, 37, 33, }, + { 38400, 25, 57, 57, 54, }, + { 31250, 31, 70, 70, 68, }, + { 28800, 34, 77, 77, 74, }, + { 19200, 54, 117, 117, 114, }, + { 14400, 74, 156, 156, 153, }, + { 9600, 114, 236, 236, 233, }, + { 4800, 233, 474, 474, 471, }, + { 2400, 471, 950, 950, 947, }, + { 1200, 947, 1902, 1902, 1899, }, + { 600, 1902, 3804, 3804, 3800, }, + { 300, 3804, 7617, 7617, 7614, }, +}; + +const int XMIT_START_ADJUSTMENT = 5; + +#elif F_CPU == 8000000 + +static const DELAY_TABLE table[] PROGMEM = +{ + // baud rxcenter rxintra rxstop tx + { 115200, 1, 5, 5, 3, }, + { 57600, 1, 15, 15, 13, }, + { 38400, 2, 25, 26, 23, }, + { 31250, 7, 32, 33, 29, }, + { 28800, 11, 35, 35, 32, }, + { 19200, 20, 55, 55, 52, }, + { 14400, 30, 75, 75, 72, }, + { 9600, 50, 114, 114, 112, }, + { 4800, 110, 233, 233, 230, }, + { 2400, 229, 472, 472, 469, }, + { 1200, 467, 948, 948, 945, }, + { 600, 948, 1895, 1895, 1890, }, + { 300, 1895, 3805, 3805, 3802, }, +}; + +const int XMIT_START_ADJUSTMENT = 4; + +#elif F_CPU == 20000000 + +// 20MHz support courtesy of the good people at macegr.com. +// Thanks, Garrett! + +static const DELAY_TABLE PROGMEM table[] = +{ + // baud rxcenter rxintra rxstop tx + { 115200, 3, 21, 21, 18, }, + { 57600, 20, 43, 43, 41, }, + { 38400, 37, 73, 73, 70, }, + { 31250, 45, 89, 89, 88, }, + { 28800, 46, 98, 98, 95, }, + { 19200, 71, 148, 148, 145, }, + { 14400, 96, 197, 197, 194, }, + { 9600, 146, 297, 297, 294, }, + { 4800, 296, 595, 595, 592, }, + { 2400, 592, 1189, 1189, 1186, }, + { 1200, 1187, 2379, 2379, 2376, }, + { 600, 2379, 4759, 4759, 4755, }, + { 300, 4759, 9523, 9523, 9520, }, +}; + +const int XMIT_START_ADJUSTMENT = 6; + +#else + +#error This version of SoftwareSerial supports only 20, 16 and 8MHz processors + +#endif + +// +// Statics +// +SoftwareSerial *SoftwareSerial::active_object = 0; +char SoftwareSerial::_receive_buffer[_SS_MAX_RX_BUFF]; +volatile uint8_t SoftwareSerial::_receive_buffer_tail = 0; +volatile uint8_t SoftwareSerial::_receive_buffer_head = 0; + +// +// Debugging +// +// This function generates a brief pulse +// for debugging or measuring on an oscilloscope. +inline void DebugPulse(uint8_t pin, uint8_t count) +{ +#if _DEBUG + volatile uint8_t *pport = portOutputRegister(digitalPinToPort(pin)); + + uint8_t val = *pport; + while (count--) + { + *pport = val | digitalPinToBitMask(pin); + *pport = val; + } +#endif +} + +// +// Private methods +// + +// static +inline void SoftwareSerial::tunedDelay(uint16_t delay) { + uint8_t tmp=0; + + asm volatile("sbiw %0, 0x01 \n\t" + "ldi %1, 0xFF \n\t" + "cpi %A0, 0xFF \n\t" + "cpc %B0, %1 \n\t" + "brne .-10 \n\t" + : "+r" (delay), "+a" (tmp) + : "0" (delay) + ); +} + +// This function sets the current object as the "listening" +// one and returns true if it replaces another +bool SoftwareSerial::listen() +{ + if (active_object != this) + { + _buffer_overflow = false; + uint8_t oldSREG = SREG; + cli(); + _receive_buffer_head = _receive_buffer_tail = 0; + active_object = this; + SREG = oldSREG; + return true; + } + + return false; +} + +// +// The receive routine called by the interrupt handler +// +void SoftwareSerial::recv() +{ + +#if GCC_VERSION < 40302 +// Work-around for avr-gcc 4.3.0 OSX version bug +// Preserve the registers that the compiler misses +// (courtesy of Arduino forum user *etracer*) + asm volatile( + "push r18 \n\t" + "push r19 \n\t" + "push r20 \n\t" + "push r21 \n\t" + "push r22 \n\t" + "push r23 \n\t" + "push r26 \n\t" + "push r27 \n\t" + ::); +#endif + + uint8_t d = 0; + + // If RX line is high, then we don't see any start bit + // so interrupt is probably not for us + if (_inverse_logic ? rx_pin_read() : !rx_pin_read()) + { + // Wait approximately 1/2 of a bit width to "center" the sample + tunedDelay(_rx_delay_centering); + DebugPulse(_DEBUG_PIN2, 1); + + // Read each of the 8 bits + for (uint8_t i=0x1; i; i <<= 1) + { + tunedDelay(_rx_delay_intrabit); + DebugPulse(_DEBUG_PIN2, 1); + uint8_t noti = ~i; + if (rx_pin_read()) + d |= i; + else // else clause added to ensure function timing is ~balanced + d &= noti; + } + + // skip the stop bit + tunedDelay(_rx_delay_stopbit); + DebugPulse(_DEBUG_PIN2, 1); + + if (_inverse_logic) + d = ~d; + + // if buffer full, set the overflow flag and return + if ((_receive_buffer_tail + 1) % _SS_MAX_RX_BUFF != _receive_buffer_head) + { + // save new data in buffer: tail points to where byte goes + _receive_buffer[_receive_buffer_tail] = d; // save new byte + _receive_buffer_tail = (_receive_buffer_tail + 1) % _SS_MAX_RX_BUFF; + } + else + { +#if _DEBUG // for scope: pulse pin as overflow indictator + DebugPulse(_DEBUG_PIN1, 1); +#endif + _buffer_overflow = true; + } + } + +#if GCC_VERSION < 40302 +// Work-around for avr-gcc 4.3.0 OSX version bug +// Restore the registers that the compiler misses + asm volatile( + "pop r27 \n\t" + "pop r26 \n\t" + "pop r23 \n\t" + "pop r22 \n\t" + "pop r21 \n\t" + "pop r20 \n\t" + "pop r19 \n\t" + "pop r18 \n\t" + ::); +#endif +} + +void SoftwareSerial::tx_pin_write(uint8_t pin_state) +{ + if (pin_state == LOW) + *_transmitPortRegister &= ~_transmitBitMask; + else + *_transmitPortRegister |= _transmitBitMask; +} + +uint8_t SoftwareSerial::rx_pin_read() +{ + return *_receivePortRegister & _receiveBitMask; +} + +// +// Interrupt handling +// + +// static +inline void SoftwareSerial::handle_interrupt() +{ + if (active_object) + { + active_object->recv(); + } +} + +#if defined(PCINT0_vect) +ISR(PCINT0_vect) +{ + SoftwareSerial::handle_interrupt(); +} +#endif + +#if defined(PCINT1_vect) +ISR(PCINT1_vect) +{ + SoftwareSerial::handle_interrupt(); +} +#endif + +#if defined(PCINT2_vect) +ISR(PCINT2_vect) +{ + SoftwareSerial::handle_interrupt(); +} +#endif + +#if defined(PCINT3_vect) +ISR(PCINT3_vect) +{ + SoftwareSerial::handle_interrupt(); +} +#endif + +// +// Constructor +// +SoftwareSerial::SoftwareSerial(uint8_t receivePin, uint8_t transmitPin, bool inverse_logic) : + _rx_delay_centering(0), + _rx_delay_intrabit(0), + _rx_delay_stopbit(0), + _tx_delay(0), + _buffer_overflow(false), + _inverse_logic(inverse_logic) +{ + setTX(transmitPin); + setRX(receivePin); +} + +// +// Destructor +// +SoftwareSerial::~SoftwareSerial() +{ + end(); +} + +void SoftwareSerial::setTX(uint8_t tx) +{ + pinMode(tx, OUTPUT); + digitalWrite(tx, HIGH); + _transmitBitMask = digitalPinToBitMask(tx); + uint8_t port = digitalPinToPort(tx); + _transmitPortRegister = portOutputRegister(port); +} + +void SoftwareSerial::setRX(uint8_t rx) +{ + pinMode(rx, INPUT); + if (!_inverse_logic) + digitalWrite(rx, HIGH); // pullup for normal logic! + _receivePin = rx; + _receiveBitMask = digitalPinToBitMask(rx); + uint8_t port = digitalPinToPort(rx); + _receivePortRegister = portInputRegister(port); +} + +// +// Public methods +// + +void SoftwareSerial::begin(long speed) +{ + _rx_delay_centering = _rx_delay_intrabit = _rx_delay_stopbit = _tx_delay = 0; + + for (unsigned i=0; i +#include + +/****************************************************************************** +* Definitions +******************************************************************************/ + +#define _SS_MAX_RX_BUFF 64 // RX buffer size +#ifndef GCC_VERSION +#define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) +#endif + +class SoftwareSerial : public Stream +{ +private: + // per object data + uint8_t _receivePin; + uint8_t _receiveBitMask; + volatile uint8_t *_receivePortRegister; + uint8_t _transmitBitMask; + volatile uint8_t *_transmitPortRegister; + + uint16_t _rx_delay_centering; + uint16_t _rx_delay_intrabit; + uint16_t _rx_delay_stopbit; + uint16_t _tx_delay; + + uint16_t _buffer_overflow:1; + uint16_t _inverse_logic:1; + + // static data + static char _receive_buffer[_SS_MAX_RX_BUFF]; + static volatile uint8_t _receive_buffer_tail; + static volatile uint8_t _receive_buffer_head; + static SoftwareSerial *active_object; + + // private methods + void recv(); + uint8_t rx_pin_read(); + void tx_pin_write(uint8_t pin_state); + void setTX(uint8_t transmitPin); + void setRX(uint8_t receivePin); + + // private static method for timing + static inline void tunedDelay(uint16_t delay); + +public: + // public methods + SoftwareSerial(uint8_t receivePin, uint8_t transmitPin, bool inverse_logic = false); + ~SoftwareSerial(); + void begin(long speed); + bool listen(); + void end(); + bool isListening() { return this == active_object; } + bool overflow() { bool ret = _buffer_overflow; _buffer_overflow = false; return ret; } + int peek(); + + virtual size_t write(uint8_t byte); + virtual int read(); + virtual int available(); + virtual void flush(); + + using Print::write; + + // public only for easy access by interrupt handlers + static inline void handle_interrupt(); +}; + +// Arduino 0012 workaround +#undef int +#undef char +#undef long +#undef byte +#undef float +#undef abs +#undef round + +#endif diff --git a/libraries/readme.txt b/libraries/readme.txt new file mode 100644 index 0000000..96ce674 --- /dev/null +++ b/libraries/readme.txt @@ -0,0 +1 @@ +For information on installing libraries, see: http://www.arduino.cc/en/Guide/Libraries diff --git a/libraries/tm1638-library/.DS_Store b/libraries/tm1638-library/.DS_Store new file mode 100644 index 0000000..4d1769b Binary files /dev/null and b/libraries/tm1638-library/.DS_Store differ diff --git a/libraries/tm1638-library/CHANGES.txt b/libraries/tm1638-library/CHANGES.txt new file mode 100644 index 0000000..75e614b --- /dev/null +++ b/libraries/tm1638-library/CHANGES.txt @@ -0,0 +1,81 @@ +Version 2.2.0 + +- Support for common anode TM1638 module (QYF-TM1638); + +Version 2.1.3 + +- ISSUE #26: Added a define TM1638_COLOR_NONE for clarity when clearing a single LED. + +Version 2.1.2 + +- ISSUE #22: Corrected setDisplayDigit repeatedly with the dot set to true; +- ISSUE #24: Correction on setDisplayToDecNumber bug (thanks to hbx3485). + +Version 2.1.1 + +- ISSUE #21: Problems on clearDisplay and setupDisplay. + +Version 2.1.0 + +- ISSUE #20: Support for negative decimal numbers; +- ISSUE #19: Corrected setDisplayToString dot bug. + +Version 2.0.1 + +- ISSUE #15: Backwards compatibility with previous Arduino IDE (pure virtual functions were not supported); +- ISSUE #18: Corrected problem with setLEDs() caused by changes in the previous version. + +Version 2.0.0 + +- Support for the TM1640; +- Restructuring for supporting other modules. + +Version 1.6.0 + +- ISSUE #10: Support for starting position on setDisplayToString() methods. + WARNING: setDisplayToString methods have changed prototype and are incompatible with code compiled for 1.5.x (if you specified a custom font) + +Version 1.5.2 + +- ISSUE #9: Backwards compatibility with previous Arduino IDE. + +Version 1.5.1 + +- ISSUE #7: New character data in default font for '*'; +- Minor optimization on inverted TM1638. + +Version 1.5.0 + +- support for inverted module; +- support for Arduino IDE 1.0; +- some restructuring. + +Version 1.4.0 + +- ISSUE #6: Support for specifying dots on setDisplayToString + WARNING: setDisplayToString methods have changed prototype and are incompatible with code compiled for 1.3.x (if you specified a custom font) + +Version 1.3.2 + +- ISSUE #5: Correction of string library import. + +Version 1.3.1 + +- changed pins on the examples to match schematics on project site. + +Version 1.3.0 + +- added support for String Object; +- added support for removing leading zeros in displaying numbers. + +Version 1.2.0 + +- added support for writing text. + +Version 1.1.0 + +- restructuring and added 2 proper examples, available in the arduino ide. + +Version 1.0.0 + +- initial release. diff --git a/libraries/tm1638-library/InvertedTM1638.cpp b/libraries/tm1638-library/InvertedTM1638.cpp new file mode 100644 index 0000000..56b7504 --- /dev/null +++ b/libraries/tm1638-library/InvertedTM1638.cpp @@ -0,0 +1,57 @@ +/* +InvertedTM1638.cpp - Library implementation for inverted TM1638. + +Copyright (C) 2011 Ricardo Batista (rjbatista gmail com) + +This program is free software: you can redistribute it and/or modify +it under the terms of the version 3 GNU General Public License as +published by the Free Software Foundation. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +#if defined(ARDUINO) && ARDUINO >= 100 + #include "Arduino.h" +#else + #include "WProgram.h" +#endif + +#include "InvertedTM1638.h" + +InvertedTM1638::InvertedTM1638(byte dataPin, byte clockPin, byte strobePin, boolean activateDisplay, + byte intensity) : TM1638(dataPin, clockPin, strobePin, activateDisplay, intensity) +{ + // nothing to do +} + +void InvertedTM1638::setLED(byte color, byte pos) +{ + sendData(((7 - pos) << 1) + 1, color); +} + +byte InvertedTM1638::getButtons() +{ + byte buttons = TM1638::getButtons(); + + // swap each other + buttons = (buttons & 0b01010101) << 1 | (buttons & 0b10101010) >> 1; + + // swap each pair + buttons = (buttons & 0b00110011) << 2 | (buttons & 0b11001100) >> 2; + + // swap each quad + buttons = (buttons & 0b00001111) << 4 | (buttons & 0b11110000) >> 4; + + return buttons; +} + +void InvertedTM1638::sendChar(byte pos, byte data, boolean dot) +{ + TM1638::sendChar(7 - pos, data & 0xC0 | (data & 0x07) << 3 | (data & 0x38) >> 3, dot); +} diff --git a/libraries/tm1638-library/InvertedTM1638.h b/libraries/tm1638-library/InvertedTM1638.h new file mode 100644 index 0000000..8b32b4f --- /dev/null +++ b/libraries/tm1638-library/InvertedTM1638.h @@ -0,0 +1,45 @@ +/* +InvertedTM1638.h - Library for an inverted TM1638. + +Copyright (C) 2011 Ricardo Batista + +This program is free software: you can redistribute it and/or modify +it under the terms of the version 3 GNU General Public License as +published by the Free Software Foundation. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +#ifndef InvertedTM1638_h +#define InvertedTM1638_h + +#if defined(ARDUINO) && ARDUINO >= 100 + #include "Arduino.h" +#else + #include "WProgram.h" +#endif + +#include "TM1638.h" + +class InvertedTM1638 : public TM1638 +{ + public: + /** Instantiate an inverted tm1638 module specifying the display state, the starting intensity (0-7) data, clock and stobe pins. */ + InvertedTM1638(byte dataPin, byte clockPin, byte strobePin, boolean activateDisplay = true, byte intensity = 7); + + /** Set the LED at pos to color (TM1638_COLOR_RED, TM1638_COLOR_GREEN or both) */ + virtual void setLED(byte color, byte pos); + /** Returns the pressed buttons as a bit set (left to right). */ + virtual byte getButtons(); + + protected: + virtual void sendChar(byte pos, byte data, boolean dot); +}; + +#endif diff --git a/libraries/tm1638-library/README.txt b/libraries/tm1638-library/README.txt new file mode 100644 index 0000000..88c781e --- /dev/null +++ b/libraries/tm1638-library/README.txt @@ -0,0 +1,31 @@ +TM1638 library +-------------- +Ricardo Batista +Email: rjbatista(at)gmail.com +URL: https://github.com/rjbatista/tm1638-library/ + +A library for interacting an arduino with a TM1638/TM1640. + +Includes: +- Support for the TM1638 and TM1640; +- Support for common anode TM1638 module; +- Helper methods for displaying numbers in decimal, hexadecimal and binary; +- Support for multiple chained tm1638; +- Reading simultaneous button presses; +- Support for dimming the display and LEDs; +- Support for writing text; +- Support for module in inverted position. + +See: TM1638 Display/LED module at http://www.dealextreme.com/p/81873?r=68099021 +See: TM1640 Display module at http://www.dealextreme.com/p/104311?r=68099021 + +USAGE NOTES +----------- + +Just put the files on a TM1638 directory under "arduino/libraries" on your arduino IDE instalation + + +PROJECT HOME +------------ + +https://github.com/rjbatista/tm1638-library/ diff --git a/libraries/tm1638-library/TM1638.cpp b/libraries/tm1638-library/TM1638.cpp new file mode 100644 index 0000000..4c3c3b4 --- /dev/null +++ b/libraries/tm1638-library/TM1638.cpp @@ -0,0 +1,135 @@ +/* +TM1638.cpp - Library implementation for TM1638. + +Copyright (C) 2011 Ricardo Batista (rjbatista gmail com) + +This program is free software: you can redistribute it and/or modify +it under the terms of the version 3 GNU General Public License as +published by the Free Software Foundation. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +#if defined(ARDUINO) && ARDUINO >= 100 + #include "Arduino.h" +#else + #include "WProgram.h" +#endif + +#include "TM1638.h" +#include "string.h" + +TM1638::TM1638(byte dataPin, byte clockPin, byte strobePin, boolean activateDisplay, byte intensity) + : TM16XX(dataPin, clockPin, strobePin, 8, activateDisplay, intensity) +{ + // nothing else to do - calling super is enough +} + +void TM1638::setDisplayToHexNumber(unsigned long number, byte dots, boolean leadingZeros, + const byte numberFont[]) +{ + for (int i = 0; i < displays; i++) { + if (!leadingZeros && number == 0) { + clearDisplayDigit(displays - i - 1, (dots & (1 << i)) != 0); + } else { + setDisplayDigit(number & 0xF, displays - i - 1, (dots & (1 << i)) != 0, numberFont); + number >>= 4; + } + } +} + +void TM1638::setDisplayToDecNumberAt(unsigned long number, byte dots, byte startingPos, boolean leadingZeros, + const byte numberFont[]) +{ + if (number > 99999999L) { + setDisplayToError(); + } else { + for (int i = 0; i < displays - startingPos; i++) { + if (number != 0) { + setDisplayDigit(number % 10, displays - i - 1, (dots & (1 << i)) != 0, numberFont); + number /= 10; + } else { + if (leadingZeros) { + setDisplayDigit(0, displays - i - 1, (dots & (1 << i)) != 0, numberFont); + } else { + clearDisplayDigit(displays - i - 1, (dots & (1 << i)) != 0); + } + } + } + } +} + +void TM1638::setDisplayToDecNumber(unsigned long number, byte dots, boolean leadingZeros, + const byte numberFont[]) +{ + setDisplayToDecNumberAt(number, dots, 0, leadingZeros, numberFont); +} + +void TM1638::setDisplayToSignedDecNumber(signed long number, byte dots, boolean leadingZeros, + const byte numberFont[]) +{ + if (number >= 0) { + setDisplayToDecNumberAt(number, dots, 0, leadingZeros, numberFont); + } else { + if (-number > 9999999L) { + setDisplayToError(); + } else { + setDisplayToDecNumberAt(-number, dots, 1, leadingZeros, numberFont); + sendChar(0, MINUS, (dots & (0x80)) != 0); + } + } +} + +void TM1638::setDisplayToBinNumber(byte number, byte dots, const byte numberFont[]) +{ + for (int i = 0; i < displays; i++) { + setDisplayDigit((number & (1 << i)) == 0 ? 0 : 1, displays - i - 1, (dots & (1 << i)) != 0, numberFont); + } +} + +void TM1638::setLED(byte color, byte pos) +{ + sendData((pos << 1) + 1, color); +} + +void TM1638::setLEDs(word leds) +{ + for (int i = 0; i < displays; i++) { + byte color = 0; + + if ((leds & (1 << i)) != 0) { + color |= TM1638_COLOR_RED; + } + + if ((leds & (1 << (i + 8))) != 0) { + color |= TM1638_COLOR_GREEN; + } + + setLED(color, i); + } +} + +byte TM1638::getButtons(void) +{ + byte keys = 0; + + digitalWrite(strobePin, LOW); + send(0x42); + for (int i = 0; i < 4; i++) { + keys |= receive() << i; + } + digitalWrite(strobePin, HIGH); + + return keys; +} + +void TM1638::sendChar(byte pos, byte data, boolean dot) +{ + sendData(pos << 1, data | (dot ? 0b10000000 : 0)); +} diff --git a/libraries/tm1638-library/TM1638.h b/libraries/tm1638-library/TM1638.h new file mode 100644 index 0000000..51c567c --- /dev/null +++ b/libraries/tm1638-library/TM1638.h @@ -0,0 +1,68 @@ +/* +TM1638.h - Library for TM1638. + +Copyright (C) 2011 Ricardo Batista + +This program is free software: you can redistribute it and/or modify +it under the terms of the version 3 GNU General Public License as +published by the Free Software Foundation. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +#ifndef TM1638_h +#define TM1638_h + +#if defined(ARDUINO) && ARDUINO >= 100 + #include "Arduino.h" +#else + #include "WProgram.h" +#endif + +#include "TM16XX.h" +#include "TM16XXFonts.h" + +#define TM1638_COLOR_NONE 0 +#define TM1638_COLOR_RED 1 +#define TM1638_COLOR_GREEN 2 + +class TM1638 : public TM16XX +{ + public: + /** Instantiate a tm1638 module specifying the display state, the starting intensity (0-7) data, clock and stobe pins. */ + TM1638(byte dataPin, byte clockPin, byte strobePin, boolean activateDisplay = true, byte intensity = 7); + + /** Set the display to a unsigned hexadecimal number (with or without leading zeros) */ + void setDisplayToHexNumber(unsigned long number, byte dots, boolean leadingZeros = true, + const byte numberFont[] = NUMBER_FONT); + /** Set the display to a unsigned decimal number (with or without leading zeros) */ + void setDisplayToDecNumber(unsigned long number, byte dots, boolean leadingZeros = true, + const byte numberFont[] = NUMBER_FONT); + /** Set the display to a signed decimal number (with or without leading zeros) */ + void setDisplayToSignedDecNumber(signed long number, byte dots, boolean leadingZeros = true, + const byte numberFont[] = NUMBER_FONT); + /** Set the display to a unsigned binary number */ + void setDisplayToBinNumber(byte number, byte dots, + const byte numberFont[] = NUMBER_FONT); + + /** Set the LED at pos to color (TM1638_COLOR_RED, TM1638_COLOR_GREEN or both) */ + virtual void setLED(byte color, byte pos); + /** Set the LEDs. MSB byte for the green LEDs, LSB for the red LEDs */ + void setLEDs(word led); + + /** Returns the pressed buttons as a bit set (left to right). */ + virtual byte getButtons(); + + protected: + virtual void sendChar(byte pos, byte data, boolean dot); + void setDisplayToDecNumberAt(unsigned long number, byte dots, byte startingPos, + boolean leadingZeros, const byte numberFont[]); +}; + +#endif diff --git a/libraries/tm1638-library/TM1638QYF.cpp b/libraries/tm1638-library/TM1638QYF.cpp new file mode 100644 index 0000000..af3e59e --- /dev/null +++ b/libraries/tm1638-library/TM1638QYF.cpp @@ -0,0 +1,151 @@ +/* +TM1638.cpp - Library implementation for TM1638. + +Copyright (C) 2011 Ricardo Batista (rjbatista gmail com) + +This program is free software: you can redistribute it and/or modify +it under the terms of the version 3 GNU General Public License as +published by the Free Software Foundation. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +#if defined(ARDUINO) && ARDUINO >= 100 + #include "Arduino.h" +#else + #include "WProgram.h" +#endif + +#include "TM1638QYF.h" +#include "string.h" + +TM1638QYF::TM1638QYF(byte dataPin, byte clockPin, byte strobePin, boolean activateDisplay, byte intensity) + : TM16XX(dataPin, clockPin, strobePin, 8, activateDisplay, intensity) +{ + // nothing else to do - calling super is enough +} + +void TM1638QYF::setDisplay(const byte values[], unsigned int length) +{ + for (int i = 0; i < displays; i++) { + int val = 0; + + for (int j = 0; j < length; j++) { + val |= ((values[j] >> i) & 1) << (displays - j - 1); + } + + sendData(i << 1, val); + } +} + +void TM1638QYF::clearDisplay() +{ + setDisplay(NULL, 0); +} + +void TM1638QYF::setDisplayToString(const char* string, const word dots, const byte ignored, const byte font[]) +{ + byte values[displays]; + boolean done = false; + + memset(values, 0, displays * sizeof(byte)); + + for (int i = 0; i < displays; i++) { + if (!done && string[i] != '\0') { + values[i] = font[string[i] - 32] | (((dots >> (displays - i - 1)) & 1) << 7); + } else { + done = true; + values[i] = (((dots >> (displays - i - 1)) & 1) << 7); + } + } + + setDisplay(values, displays); +} + +void TM1638QYF::setDisplayToString(String string, const word dots, const byte ignored, const byte font[]) +{ + byte values[displays]; + int stringLength = string.length(); + + memset(values, 0, displays * sizeof(byte)); + + for (int i = 0; i < displays; i++) { + if (i < stringLength) { + values[i] = font[string.charAt(i) - 32] | (((dots >> (displays - i - 1)) & 1) << 7); + } else { + values[i] = (((dots >> (displays - i - 1)) & 1) << 7); + } + } + + setDisplay(values, displays); +} + +void TM1638QYF::setDisplayToBinNumber(byte number, byte dots, const byte numberFont[]) +{ + byte values[displays]; + + memset(values, 0, displays * sizeof(byte)); + + for (int i = 0; i < displays; i++) { + values[i] = numberFont[(number >> (displays - i - 1)) & 1] | (((dots >> (displays - i - 1)) & 1) << 7); + } + + setDisplay(values, displays); +} + +void TM1638QYF::setDisplayToHexNumber(unsigned long number, byte dots, boolean leadingZeros, + const byte numberFont[]) +{ + char values[displays + 1]; + + snprintf(values, displays + 1, leadingZeros ? "%08X" : "%X", number); // ignores display size + + setDisplayToString(values, dots, 0, numberFont); +} + +void TM1638QYF::setDisplayToDecNumber(unsigned long number, byte dots, boolean leadingZeros, + const byte numberFont[]) +{ + char values[displays + 1]; + + snprintf(values, displays + 1, leadingZeros ? "%08ld" : "%ld", number); // ignores display size + + Serial.println(values); + + setDisplayToString(values, dots, 0, numberFont); +} + +void TM1638QYF::setDisplayToSignedDecNumber(signed long number, byte dots, boolean leadingZeros, + const byte numberFont[]) +{ + char values[displays + 1]; + + snprintf(values, displays + 1, leadingZeros ? "%08d" : "%d", number); // ignores display size + + setDisplayToString(values, dots, 0, numberFont); +} + +word TM1638QYF::getButtons(void) +{ + word keys = 0; + + digitalWrite(strobePin, LOW); + send(0x42); // B01000010 Read the key scan data + for (int i = 0; i < 4; i++) { + byte rec = receive(); + + rec = (((rec & B1000000) >> 3 | (rec & B100)) >> 2) | (rec & B100000) | (rec & B10) << 3; + + keys |= ((rec & 0x000F) << (i << 1)) | (((rec & 0x00F0) << 4) << (i << 1)); + } + digitalWrite(strobePin, HIGH); + + return keys; +} + diff --git a/libraries/tm1638-library/TM1638QYF.h b/libraries/tm1638-library/TM1638QYF.h new file mode 100644 index 0000000..71a0625 --- /dev/null +++ b/libraries/tm1638-library/TM1638QYF.h @@ -0,0 +1,75 @@ +/* +TM1638QYF.h - Library for TM1638. + +Copyright (C) 2011 Ricardo Batista + +This program is free software: you can redistribute it and/or modify +it under the terms of the version 3 GNU General Public License as +published by the Free Software Foundation. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +#ifndef TM1638QYF_h +#define TM1638QYF_h + +#if defined(ARDUINO) && ARDUINO >= 100 + #include "Arduino.h" +#else + #include "WProgram.h" +#endif + +#include "TM16XX.h" +#include "TM16XXFonts.h" + +class TM1638QYF : public TM16XX +{ + public: + /** Instantiate a tm1638 module specifying the display state, the starting intensity (0-7) data, clock and stobe pins. */ + TM1638QYF(byte dataPin, byte clockPin, byte strobePin, boolean activateDisplay = true, byte intensity = 7); + + /** Set the display to a unsigned hexadecimal number (with or without leading zeros) */ + void setDisplayToHexNumber(unsigned long number, byte dots, boolean leadingZeros = true, + const byte numberFont[] = FONT_DEFAULT); + /** Set the display to a unsigned decimal number (with or without leading zeros) */ + void setDisplayToDecNumber(unsigned long number, byte dots, boolean leadingZeros = true, + const byte numberFont[] = FONT_DEFAULT); + /** Set the display to a signed decimal number (with or without leading zeros) */ + void setDisplayToSignedDecNumber(signed long number, byte dots, boolean leadingZeros = true, + const byte numberFont[] = FONT_DEFAULT); + /** Set the display to a unsigned binary number */ + void setDisplayToBinNumber(byte number, byte dots, + const byte numberFont[] = NUMBER_FONT); + + /** Clear the display */ + virtual void clearDisplay(); + /** Set the display to the String (defaults to built in font) - pos is ignored in common anode */ + virtual void setDisplayToString(const char* string, const word dots = 0, const byte pos = 0, + const byte font[] = FONT_DEFAULT); + /** Set the display to the String (defaults to built in font) - pos is ignored in common anode */ + virtual void setDisplayToString(String string, const word dots = 0, const byte pos = 0, + const byte font[] = FONT_DEFAULT); + + /** Returns the pressed buttons as a bit set (left to right). */ + virtual word getButtons(); + + protected: + /** Set the display to the values (left to right) */ + virtual void setDisplay(const byte values[], unsigned int length = 8); + + private: + // unsupported in common anode design + virtual void setDisplayDigit(byte digit, byte pos, boolean dot, const byte numberFont[] = NUMBER_FONT) { setDisplayToError(); }; + // unsupported in common anode design + virtual void clearDisplayDigit(byte pos, boolean dot) { setDisplayToError(); }; + // unsupported in common anode design + virtual void sendChar(byte pos, byte data, boolean dot) { setDisplayToError(); } +}; + +#endif diff --git a/libraries/tm1638-library/TM1638en.pdf b/libraries/tm1638-library/TM1638en.pdf new file mode 100644 index 0000000..780ee8e Binary files /dev/null and b/libraries/tm1638-library/TM1638en.pdf differ diff --git a/libraries/tm1638-library/TM1640.cpp b/libraries/tm1638-library/TM1640.cpp new file mode 100644 index 0000000..fc5f061 --- /dev/null +++ b/libraries/tm1638-library/TM1640.cpp @@ -0,0 +1,54 @@ +/* +TM1640.cpp - Library implementation for TM1640. + +Copyright (C) 2011 Ricardo Batista (rjbatista gmail com) + +This program is free software: you can redistribute it and/or modify +it under the terms of the version 3 GNU General Public License as +published by the Free Software Foundation. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +#if defined(ARDUINO) && ARDUINO >= 100 + #include "Arduino.h" +#else + #include "WProgram.h" +#endif + +#include "TM1640.h" +#include "string.h" + + +TM1640::TM1640(byte dataPin, byte clockPin, boolean activateDisplay, byte intensity) + : TM16XX(dataPin, clockPin, dataPin, 16, activateDisplay, intensity) +{ + // nothing else to do - calling super is enough +} + +void TM1640::sendChar(byte pos, byte data, boolean dot) +{ + sendData(pos, data | (dot ? 0b10000000 : 0)); + + // necessary for the TM1640 + digitalWrite(strobePin, LOW); + digitalWrite(clockPin, LOW); + digitalWrite(clockPin, HIGH); + digitalWrite(strobePin, HIGH); +} + +void TM1640::clearDisplay() +{ + digitalWrite(strobePin, LOW); + send(0xC0); + for (int i = 0; i < 16; i++) { + send(0x00); + } + digitalWrite(strobePin, HIGH); +} diff --git a/libraries/tm1638-library/TM1640.h b/libraries/tm1638-library/TM1640.h new file mode 100644 index 0000000..5526647 --- /dev/null +++ b/libraries/tm1638-library/TM1640.h @@ -0,0 +1,43 @@ +/* +TM1640.h - Library for TM1640. + +Copyright (C) 2011 Ricardo Batista + +This program is free software: you can redistribute it and/or modify +it under the terms of the version 3 GNU General Public License as +published by the Free Software Foundation. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +#ifndef TM1640_h +#define TM1640_h + +#if defined(ARDUINO) && ARDUINO >= 100 + #include "Arduino.h" +#else + #include "WProgram.h" +#endif + +#include "TM16XX.h" +#include "TM16XXFonts.h" + +class TM1640 : public TM16XX +{ + public: + /** Instantiate a tm1640 module specifying the display state, the starting intensity (0-7) data and clock pins. */ + TM1640(byte dataPin, byte clockPin, boolean activateDisplay = true, byte intensity = 7); + /** Clear the display */ + virtual void clearDisplay(); + + protected: + virtual void sendChar(byte pos, byte data, boolean dot); +}; + +#endif diff --git a/libraries/tm1638-library/TM16XX.cpp b/libraries/tm1638-library/TM16XX.cpp new file mode 100644 index 0000000..39f912d --- /dev/null +++ b/libraries/tm1638-library/TM16XX.cpp @@ -0,0 +1,179 @@ +/* +TM16XX.cpp - Library implementation for TM16XX. + +Copyright (C) 2011 Ricardo Batista (rjbatista gmail com) + +This program is free software: you can redistribute it and/or modify +it under the terms of the version 3 GNU General Public License as +published by the Free Software Foundation. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +#if defined(ARDUINO) && ARDUINO >= 100 + #include "Arduino.h" +#else + #include "WProgram.h" +#endif + +#include "TM16XX.h" +#include "string.h" + +TM16XX::TM16XX(byte dataPin, byte clockPin, byte strobePin, byte displays, boolean activateDisplay, + byte intensity) +{ + this->dataPin = dataPin; + this->clockPin = clockPin; + this->strobePin = strobePin; + this->displays = displays; + + pinMode(dataPin, OUTPUT); + pinMode(clockPin, OUTPUT); + pinMode(strobePin, OUTPUT); + + digitalWrite(strobePin, HIGH); + digitalWrite(clockPin, HIGH); + + sendCommand(0x40); + sendCommand(0x80 | (activateDisplay ? 8 : 0) | min(7, intensity)); + + digitalWrite(strobePin, LOW); + send(0xC0); + for (int i = 0; i < 16; i++) { + send(0x00); + } + digitalWrite(strobePin, HIGH); +} + +void TM16XX::setupDisplay(boolean active, byte intensity) +{ + sendCommand(0x80 | (active ? 8 : 0) | min(7, intensity)); + + // necessary for the TM1640 + digitalWrite(strobePin, LOW); + digitalWrite(clockPin, LOW); + digitalWrite(clockPin, HIGH); + digitalWrite(strobePin, HIGH); +} + +void TM16XX::setDisplayDigit(byte digit, byte pos, boolean dot, const byte numberFont[]) +{ + sendChar(pos, numberFont[digit & 0xF], dot); +} + +void TM16XX::setDisplayToError() +{ + setDisplay(ERROR_DATA, 8); + + for (int i = 8; i < displays; i++) { + clearDisplayDigit(i, 0); + } +} + +void TM16XX::clearDisplayDigit(byte pos, boolean dot) +{ + sendChar(pos, 0, dot); +} + +void TM16XX::setDisplay(const byte values[], unsigned int size) +{ + for (int i = 0; i < size; i++) { + sendChar(i, values[i], 0); + } +} + +void TM16XX::clearDisplay() +{ + for (int i = 0; i < displays; i++) { + sendData(i << 1, 0); + } +} + +void TM16XX::setDisplayToString(const char* string, const word dots, const byte pos, const byte font[]) +{ + for (int i = 0; i < displays - pos; i++) { + if (string[i] != '\0') { + sendChar(i + pos, font[string[i] - 32], (dots & (1 << (displays - i - 1))) != 0); + } else { + break; + } + } +} + +void TM16XX::setDisplayToString(const String string, const word dots, const byte pos, const byte font[]) +{ + int stringLength = string.length(); + + for (int i = 0; i < displays - pos; i++) { + if (i < stringLength) { + sendChar(i + pos, font[string.charAt(i) - 32], (dots & (1 << (displays - i - 1))) != 0); + } else { + break; + } + } +} + +void TM16XX::sendCommand(byte cmd) +{ + digitalWrite(strobePin, LOW); + send(cmd); + digitalWrite(strobePin, HIGH); +} + +void TM16XX::sendData(byte address, byte data) +{ + sendCommand(0x44); + digitalWrite(strobePin, LOW); + send(0xC0 | address); + send(data); + digitalWrite(strobePin, HIGH); +} + +void TM16XX::send(byte data) +{ + for (int i = 0; i < 8; i++) { + digitalWrite(clockPin, LOW); + digitalWrite(dataPin, data & 1 ? HIGH : LOW); + data >>= 1; + digitalWrite(clockPin, HIGH); + } +} + +byte TM16XX::receive() +{ + byte temp = 0; + + // Pull-up on + pinMode(dataPin, INPUT); + digitalWrite(dataPin, HIGH); + + for (int i = 0; i < 8; i++) { + temp >>= 1; + + digitalWrite(clockPin, LOW); + + if (digitalRead(dataPin)) { + temp |= 0x80; + } + + digitalWrite(clockPin, HIGH); + } + + // Pull-up off + pinMode(dataPin, OUTPUT); + digitalWrite(dataPin, LOW); + + return temp; +} + +#if !defined(ARDUINO) || ARDUINO < 100 +// empty implementation instead of pure virtual for older Arduino IDE +void TM16XX::sendChar(byte pos, byte data, boolean dot) {} +#endif + diff --git a/libraries/tm1638-library/TM16XX.h b/libraries/tm1638-library/TM16XX.h new file mode 100644 index 0000000..1188929 --- /dev/null +++ b/libraries/tm1638-library/TM16XX.h @@ -0,0 +1,81 @@ +/* +TM16XX.h - Library for TM1638. + +Copyright (C) 2011 Ricardo Batista + +This program is free software: you can redistribute it and/or modify +it under the terms of the version 3 GNU General Public License as +published by the Free Software Foundation. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +#ifndef TM16XX_h +#define TM16XX_h + +#if defined(ARDUINO) && ARDUINO >= 100 + #include "Arduino.h" +#else + #include "WProgram.h" +#endif + +#include "TM16XXFonts.h" + +class TM16XX +{ + public: + /** + * Instantiate a tm16xx module specifying the number of displays, display state, + * the starting intensity (0-7) data, clock and stobe pins. + */ + TM16XX(byte dataPin, byte clockPin, byte strobePin, byte displays, boolean activateDisplay = true, + byte intensity = 7); + + /** Set the display (segments and LEDs) active or off and intensity (range from 0-7). */ + virtual void setupDisplay(boolean active, byte intensity); + + /** Set a single display at pos (starting at 0) to a digit (left to right) */ + virtual void setDisplayDigit(byte digit, byte pos, boolean dot, const byte numberFont[] = NUMBER_FONT); + /** Set the display to an error message */ + virtual void setDisplayToError(); + /** Clear a single display at pos (starting at 0, left to right) */ + virtual void clearDisplayDigit(byte pos, boolean dot); + /** Set the display to the values (left to right) */ + virtual void setDisplay(const byte values[], unsigned int length = 8); + /** Clear the display */ + virtual void clearDisplay(); + + /** Set the display to the string (defaults to built in font) */ + virtual void setDisplayToString(const char* string, const word dots = 0, const byte pos = 0, + const byte font[] = FONT_DEFAULT); + /** Set the display to the String (defaults to built in font) */ + virtual void setDisplayToString(String string, const word dots = 0, const byte pos = 0, + const byte font[] = FONT_DEFAULT); + + protected: + #if defined(ARDUINO) && ARDUINO >= 100 + // pure virtual is NOT supported in older Arduino IDE + virtual void sendChar(byte pos, byte data, boolean dot) = 0; + #else + virtual void sendChar(byte pos, byte data, boolean dot); + #endif + + + virtual void sendCommand(byte led); + virtual void sendData(byte add, byte data); + virtual void send(byte data); + virtual byte receive(); + + byte displays; + byte dataPin; + byte clockPin; + byte strobePin; +}; + +#endif diff --git a/libraries/tm1638-library/TM16XXFonts.h b/libraries/tm1638-library/TM16XXFonts.h new file mode 100644 index 0000000..1b1489d --- /dev/null +++ b/libraries/tm1638-library/TM16XXFonts.h @@ -0,0 +1,166 @@ +/* +TM16XXFonts.h - Font definition for TM16XX. + +Copyright (C) 2011 Ricardo Batista (rjbatista gmail com) + +This program is free software: you can redistribute it and/or modify +it under the terms of the version 3 GNU General Public License as +published by the Free Software Foundation. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . + + +The bits are displayed by mapping bellow + -- 0 -- +| | +5 1 + -- 6 -- +4 2 +| | + -- 3 -- .7 + +*/ + +#ifndef TM16XXFonts_h +#define TM16XXFonts_h + +// definition for standard hexadecimal numbers +const byte NUMBER_FONT[] = { + 0b00111111, // 0 + 0b00000110, // 1 + 0b01011011, // 2 + 0b01001111, // 3 + 0b01100110, // 4 + 0b01101101, // 5 + 0b01111101, // 6 + 0b00000111, // 7 + 0b01111111, // 8 + 0b01101111, // 9 + 0b01110111, // A + 0b01111100, // B + 0b00111001, // C + 0b01011110, // D + 0b01111001, // E + 0b01110001 // F +}; + +const byte MINUS = 0b01000000; + +// definition for error +const byte ERROR_DATA[] = { + 0b01111001, // E + 0b01010000, // r + 0b01010000, // r + 0b01011100, // o + 0b01010000, // r + 0, + 0, + 0 +}; + +// definition for the displayable ASCII chars +const byte FONT_DEFAULT[] = { + 0b00000000, // (32) + 0b10000110, // (33) ! + 0b00100010, // (34) " + 0b01111110, // (35) # + 0b01101101, // (36) $ + 0b00000000, // (37) % + 0b00000000, // (38) & + 0b00000010, // (39) ' + 0b00110000, // (40) ( + 0b00000110, // (41) ) + 0b01100011, // (42) * + 0b00000000, // (43) + + 0b00000100, // (44) , + 0b01000000, // (45) - + 0b10000000, // (46) . + 0b01010010, // (47) / + 0b00111111, // (48) 0 + 0b00000110, // (49) 1 + 0b01011011, // (50) 2 + 0b01001111, // (51) 3 + 0b01100110, // (52) 4 + 0b01101101, // (53) 5 + 0b01111101, // (54) 6 + 0b00100111, // (55) 7 + 0b01111111, // (56) 8 + 0b01101111, // (57) 9 + 0b00000000, // (58) : + 0b00000000, // (59) ; + 0b00000000, // (60) < + 0b01001000, // (61) = + 0b00000000, // (62) > + 0b01010011, // (63) ? + 0b01011111, // (64) @ + 0b01110111, // (65) A + 0b01111111, // (66) B + 0b00111001, // (67) C + 0b00111111, // (68) D + 0b01111001, // (69) E + 0b01110001, // (70) F + 0b00111101, // (71) G + 0b01110110, // (72) H + 0b00000110, // (73) I + 0b00011111, // (74) J + 0b01101001, // (75) K + 0b00111000, // (76) L + 0b00010101, // (77) M + 0b00110111, // (78) N + 0b00111111, // (79) O + 0b01110011, // (80) P + 0b01100111, // (81) Q + 0b00110001, // (82) R + 0b01101101, // (83) S + 0b01111000, // (84) T + 0b00111110, // (85) U + 0b00101010, // (86) V + 0b00011101, // (87) W + 0b01110110, // (88) X + 0b01101110, // (89) Y + 0b01011011, // (90) Z + 0b00111001, // (91) [ + 0b01100100, // (92) \ (this can't be the last char on a line, even in comment or it'll concat) + 0b00001111, // (93) ] + 0b00000000, // (94) ^ + 0b00001000, // (95) _ + 0b00100000, // (96) ` + 0b01011111, // (97) a + 0b01111100, // (98) b + 0b01011000, // (99) c + 0b01011110, // (100) d + 0b01111011, // (101) e + 0b00110001, // (102) f + 0b01101111, // (103) g + 0b01110100, // (104) h + 0b00000100, // (105) i + 0b00001110, // (106) j + 0b01110101, // (107) k + 0b00110000, // (108) l + 0b01010101, // (109) m + 0b01010100, // (110) n + 0b01011100, // (111) o + 0b01110011, // (112) p + 0b01100111, // (113) q + 0b01010000, // (114) r + 0b01101101, // (115) s + 0b01111000, // (116) t + 0b00011100, // (117) u + 0b00101010, // (118) v + 0b00011101, // (119) w + 0b01110110, // (120) x + 0b01101110, // (121) y + 0b01000111, // (122) z + 0b01000110, // (123) { + 0b00000110, // (124) | + 0b01110000, // (125) } + 0b00000001, // (126) ~ +}; + +#endif diff --git a/libraries/tm1638-library/examples/tm1638_functions_example/tm1638_functions_example.pde b/libraries/tm1638-library/examples/tm1638_functions_example/tm1638_functions_example.pde new file mode 100644 index 0000000..c66fc43 --- /dev/null +++ b/libraries/tm1638-library/examples/tm1638_functions_example/tm1638_functions_example.pde @@ -0,0 +1,102 @@ +/* +Library examples for TM1638. + +Copyright (C) 2011 Ricardo Batista + +This program is free software: you can redistribute it and/or modify +it under the terms of the version 3 GNU General Public License as +published by the Free Software Foundation. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +#include +#include + +#define NO_MODULES 2 + +// define a regular module and a inverted module +TM1638 module1(3, 2, 4); +InvertedTM1638 module2(3, 2, 5); +TM1638* modules[NO_MODULES] = { + &module1, + &module2 +}; +byte modes[NO_MODULES]; + +unsigned long startTime; + +void setup() { + startTime = millis(); + + for (int i = 0; i < NO_MODULES; i++) { + modules[i]->setupDisplay(true, 7); + modes[i] = 0; + } +} + +void update(TM1638* module, byte* mode) { + byte buttons = module->getButtons(); + unsigned long runningSecs = (millis() - startTime) / 1000; + + // button pressed - change mode + if (buttons != 0) { + *mode = buttons >> 1; + module->clearDisplay(); + module->setLEDs(0); + } + + switch (*mode) { + case 0: + module->setDisplayToDecNumber(runningSecs, 1 << 7); + break; + case 1: + module->setDisplayToDecNumber(runningSecs, 1 << 6, false); + break; + case 2: + module->setDisplayToHexNumber(runningSecs, 1 << 5); + break; + case 4: + module->setDisplayToHexNumber(runningSecs, 1 << 4, false); + break; + case 8: + module->setDisplayToBinNumber(runningSecs, 1 << 3); + break; + case 16: + module->clearDisplayDigit((runningSecs - 1) % 8, 0); + module->setDisplayDigit(runningSecs % 8, runningSecs % 8, 0); + break; + case 32: + char s[8]; + sprintf(s, "Secs %03d", runningSecs % 999); + module->setDisplayToString(s); + break; + case 64: + if (runningSecs % 2 == 0) { + module->setDisplayToString("TM1638 "); + } else { + module->setDisplayToString("LIBRARY "); + } + + module->setLED(0, (runningSecs - 1) % 8); + module->setLED(1 + runningSecs % 3, runningSecs % 8); + break; + case 65: + module->setDisplayToError(); + break; + + } +} + +void loop() { + for (int i = 0; i < NO_MODULES; i++) { + update(modules[i], &modes[i]); + } +} + diff --git a/libraries/tm1638-library/examples/tm1638_one_module_example/tm1638_one_module_example.pde b/libraries/tm1638-library/examples/tm1638_one_module_example/tm1638_one_module_example.pde new file mode 100644 index 0000000..84eb9d1 --- /dev/null +++ b/libraries/tm1638-library/examples/tm1638_one_module_example/tm1638_one_module_example.pde @@ -0,0 +1,34 @@ +/* +Library examples for TM1638. + +Copyright (C) 2011 Ricardo Batista + +This program is free software: you can redistribute it and/or modify +it under the terms of the version 3 GNU General Public License as +published by the Free Software Foundation. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +#include + +// define a module on data pin 8, clock pin 9 and strobe pin 7 +TM1638 module(8, 9, 7); + +void setup() { + // display a hexadecimal number and set the left 4 dots + module.setDisplayToHexNumber(0x1234ABCD, 0xF0); +} + +void loop() { + byte keys = module.getButtons(); + + // light the first 4 red LEDs and the last 4 green LEDs as the buttons are pressed + module.setLEDs(((keys & 0xF0) << 8) | (keys & 0xF)); +} diff --git a/libraries/tm1638-library/examples/tm1638_scrolling_modules_example/tm1638_scrolling_modules_example.pde b/libraries/tm1638-library/examples/tm1638_scrolling_modules_example/tm1638_scrolling_modules_example.pde new file mode 100644 index 0000000..ceee1d2 --- /dev/null +++ b/libraries/tm1638-library/examples/tm1638_scrolling_modules_example/tm1638_scrolling_modules_example.pde @@ -0,0 +1,56 @@ +/* +Library examples for TM1638. + +Copyright (C) 2011 Ricardo Batista + +This program is free software: you can redistribute it and/or modify +it under the terms of the version 3 GNU General Public License as +published by the Free Software Foundation. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +#include + +#define MODULES 4 + +// define a modules +TM1638 modules[] = { + TM1638(8, 9, 7), + TM1638(8, 9, 6), + TM1638(8, 9, 5), + TM1638(8, 9, 4) +}; + +void setup() { +} + +const char string[] = " SEE LIBRARY PROJECT AT CODE.GOOGLE.COM "; +int base = 0; + +void loop() { + for (int i = 0; i < MODULES; i++) { + const char* pos = string + base + (i * 8); + + if (pos >= string && pos + 8 < string + sizeof(string)) { + modules[i].setDisplayToString(pos); + } else { + modules[i].clearDisplay(); + } + } + + base++; + + if (base == sizeof(string) - 8) { + base = -MODULES * 8; + } + + delay(100); +} + diff --git a/libraries/tm1638-library/examples/tm1638_two_modules_example/tm1638_two_modules_example.pde b/libraries/tm1638-library/examples/tm1638_two_modules_example/tm1638_two_modules_example.pde new file mode 100644 index 0000000..d358f83 --- /dev/null +++ b/libraries/tm1638-library/examples/tm1638_two_modules_example/tm1638_two_modules_example.pde @@ -0,0 +1,101 @@ +/* +Library examples for TM1638. + +Copyright (C) 2011 Ricardo Batista + +This program is free software: you can redistribute it and/or modify +it under the terms of the version 3 GNU General Public License as +published by the Free Software Foundation. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +#include "TM1638.h" + +// hello segments for display +const byte hello[] = { + 0b00000000, 0b01110110, 0b01111001, 0b00111000, 0b00111000, 0b00111111, 0b00000000, 0b00000000 +}; + +// define the first module +TM1638 module1(8, 9, 7); +// to chain modules, use the same clk and data - just specify a different strobe pin +TM1638 module2(8, 9, 6); + +unsigned long value = 0L; +boolean state = true; + +void setup() +{ + // display the hello segments on module 1 + module1.setDisplay(hello); + // display the hello segments on module 2 + module2.setDisplay(hello); + + // light the lower 5 red LEDs and the top 5 green LEDs + module1.setLEDs(0b00011111 | 0b11111000 << 8); + + // light the 3rd red LED + module2.setLED(TM1638_COLOR_RED, 3); + // light the 5th green LED + module2.setLED(TM1638_COLOR_GREEN, 5); + // light the 7th red and green LEDs + module2.setLED(TM1638_COLOR_RED | TM1638_COLOR_GREEN, 7); +} + +void loop() +{ + byte key1, key2; + + // read the buttons from the first module + key1 = module1.getButtons(); + // read the buttons from the second module + key2 = module2.getButtons(); + + // both pressed + if (key1 != 0 && key2 != 0) { + value = 0; + + // set the display to 0 on both modules if they have buttons pressed simultaneously + module1.setDisplayToHexNumber(value, 0b10101010); + module2.setDisplayToDecNumber(value, 0b01010101); + } else { + // check the first module buttons + if (key1 != 0) { + // show the pressed buttons of the first module on its display + module2.setDisplayToBinNumber(key1, 0); + // and on the LEDs + module1.setLEDs(key1); + + // check to see if it's the last button pressed + if (key1 & 128) { + // toggle the display state on/off + state = !state; + delay(200); // just wait for button up + } + + // set the intensity and display state + module1.setupDisplay(state, key1 >> 1); + } + + // check the second module buttons + if (key2 != 0) { + // just add it to the display value + value += key2; + + // display it as an hexadecimal on the first module + module1.setDisplayToHexNumber(value, 0b10101010); + // and as a decimal on the second module + module2.setDisplayToDecNumber(value, 0b01010101); + + // light the LEDs + module2.setLEDs(key2 << 8); + } + } +} diff --git a/libraries/tm1638-library/examples/tm1638qyf_functions_example/tm1638qyf_functions_example.ino b/libraries/tm1638-library/examples/tm1638qyf_functions_example/tm1638qyf_functions_example.ino new file mode 100644 index 0000000..7a7af40 --- /dev/null +++ b/libraries/tm1638-library/examples/tm1638qyf_functions_example/tm1638qyf_functions_example.ino @@ -0,0 +1,93 @@ +/* +Library examples for TM1638. + +Copyright (C) 2011 Ricardo Batista + +This program is free software: you can redistribute it and/or modify +it under the terms of the version 3 GNU General Public License as +published by the Free Software Foundation. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +#include +#include + +TM1638QYF module(3, 2, 5); +word mode; + +unsigned long startTime; + +void setup() { + startTime = millis(); + + module.setupDisplay(true, 7); + mode = 0; +} + +void update(TM1638QYF* module, word* mode) { + word buttons = module->getButtons(); + unsigned long runningSecs = (millis() - startTime) / 1000; + + // button pressed - change mode + if (buttons != 0) { + *mode = buttons >> 1; + + if (*mode < 128) { + module->clearDisplay(); + delay(100); + } + } + + switch (*mode) { + case 0: + module->setDisplayToDecNumber(runningSecs, 1 << 6); + break; + case 1: + module->setDisplayToDecNumber(runningSecs, 1 << 5, false); + break; + case 2: + module->setDisplayToHexNumber(runningSecs, 1 << 4); + break; + case 4: + module->setDisplayToHexNumber(runningSecs, 1 << 3, false); + break; + case 8: + module->setDisplayToBinNumber(runningSecs, 1 << 2); + break; + case 16: + char s[9]; + sprintf(s, "Secs %03d", runningSecs % 999); + module->setDisplayToString(s, 1 << 1); + break; + case 32: + if (runningSecs % 2 == 0) { + module->setDisplayToString("TM1638QY", 1); + } else { + module->setDisplayToString(String("LIBRARY "), 1); + } + + break; + case 64: + module->setDisplayToError(); + break; + case 128: + module->setDisplayToDecNumber(*mode, 0); + break; + case 256: + module->setDisplayToString("ABCDE", 1 << (runningSecs % 8)); + break; + default: + module->setDisplayToBinNumber(buttons & 0xF, buttons >> 8); + } +} + +void loop() { + update(&module, &mode); +} diff --git a/libraries/tm1638-library/examples/tm1640_example/tm1640_example.pde b/libraries/tm1638-library/examples/tm1640_example/tm1640_example.pde new file mode 100644 index 0000000..f6e7e24 --- /dev/null +++ b/libraries/tm1638-library/examples/tm1640_example/tm1640_example.pde @@ -0,0 +1,36 @@ +/* +Library examples for TM1640. + +Copyright (C) 2011 Ricardo Batista + +This program is free software: you can redistribute it and/or modify +it under the terms of the version 3 GNU General Public License as +published by the Free Software Foundation. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ +#include // required because the way arduino deals with libraries +#include + +// define a module on data pin 3, clock pin 2 +TM1640 module(3, 2); + +void setup() +{ + // nothing to do here +} + +void loop() +{ + char text[17]; + + sprintf(text, "testing %u", millis()); + + module.setDisplayToString(text); +} diff --git a/libraries/tm1638-library/keywords.txt b/libraries/tm1638-library/keywords.txt new file mode 100644 index 0000000..b6a8b30 --- /dev/null +++ b/libraries/tm1638-library/keywords.txt @@ -0,0 +1,40 @@ +####################################### +# Syntax Coloring Map For TM1638 Lib +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +TM16XX KEYWORD1 +TM1638 KEYWORD1 +TM1640 KEYWORD1 +InvertedTM1638 KEYWORD1 +TM1638QYF KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +setupDisplay KEYWORD2 +setDisplayToHexNumber KEYWORD2 +setDisplayToDecNumber KEYWORD2 +setDisplayToBinNumber KEYWORD2 +setDisplayDigit KEYWORD2 +setDisplay KEYWORD2 +clearDisplay KEYWORD2 +setDisplayToString KEYWORD2 +setLED KEYWORD2 +setLEDs KEYWORD2 +getButtons KEYWORD2 +sendCommand KEYWORD2 +sendData KEYWORD2 +sendChar KEYWORD2 +send KEYWORD2 +receive KEYWORD2 + +####################################### +# Constants (LITERAL1) +####################################### +TM1638_COLOR_RED LITERAL1 +TM1638_COLOR_GREEN LITERAL1