├── .gitignore ├── LICENSE ├── README.md ├── entities ├── __init__.py ├── buzzer.py ├── digital_display.py ├── digital_display_tm1637.py ├── dip_switch_2bit.py ├── ds18b20.py ├── ic_74hc595.py ├── ic_tm1637.py ├── led.py ├── led_74hc595.py └── tact.py ├── examples └── main.py ├── saks-v1.x ├── examples │ └── main.py ├── sakshat.py └── sakspins.py ├── sakshat.py └── sakspins.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | 5 | # C extensions 6 | *.so 7 | 8 | # Distribution / packaging 9 | .Python 10 | env/ 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | *.egg-info/ 23 | .installed.cfg 24 | *.egg 25 | 26 | # PyInstaller 27 | # Usually these files are written by a python script from a template 28 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 29 | *.manifest 30 | *.spec 31 | 32 | # Installer logs 33 | pip-log.txt 34 | pip-delete-this-directory.txt 35 | 36 | # Unit test / coverage reports 37 | htmlcov/ 38 | .tox/ 39 | .coverage 40 | .coverage.* 41 | .cache 42 | nosetests.xml 43 | coverage.xml 44 | *,cover 45 | 46 | # Translations 47 | *.mo 48 | *.pot 49 | 50 | # Django stuff: 51 | *.log 52 | 53 | # Sphinx documentation 54 | docs/_build/ 55 | 56 | # PyBuilder 57 | target/ 58 | .idea/ 59 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | {description} 294 | Copyright (C) {year} {fullname} 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | {signature of Ty Coon}, 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | 341 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SAKS-SDK 2 | 距离出上一个[瑞士军刀扩展板 SAKS](http://shumeipai.nxez.com/swiss-army-knife-shield-for-raspberry-pi) 的[教程](http://shumeipai.nxez.com/swiss-army-knife-shield-for-raspberry-pi-diy-tutorials)已经过去很久了,在这期间我们在思考一个问题——既然 SAKS 的设计定位于上手快、DIY可能性多,那么为何不做得彻底一点?之所以觉得之前的教程有某些“不够彻底”,是因为当遇到较复杂的需求时,创客们不得不用代码重复去实现一些数码管动态扫描、开关检测、传感器状态读取等硬件的操作逻辑。终于我们决定开发一套SDK,将以上需要重复造的轮子进行科学封装,从而达到让创客们集中精力专心实现功能,而不用为关注底层的操控逻辑而分心。 3 | 4 | 终于我们完成了这个 SDK 并在此基础上实现了SAKS的SDK,基于Python语言用面向对象的方法实现(由于封装程度高,即便你没有系统学习过面向对象的开发方法也完全不用担心不会使用)。接下来我们会通过既定的一些例程(例如[树莓派 SAKS 扩展板实用应用 之 CPU 温度显示和警报](http://shumeipai.nxez.com/2015/09/21/cpu-temperature-display-and-alarm.html)),介绍如何基于 SAKS SDK 实现例程中的功能。 5 | 6 | 树莓派瑞士军刀扩展板 SAKS SDK 已经通过 Github 开源(GPL v2.0 许可方式)并提供下载: 7 | https://github.com/spoonysonny/SAKS-SDK 8 | 9 | 也可通过以下命令下载。 10 | git clone https://github.com/spoonysonny/SAKS-SDK.git 11 | 12 | 树莓派瑞士军刀扩展板购买请移步此处: 13 | http://link.nxez.com/spoony/cps-products-saks 14 | 15 | 随后我们将陆续完善开发文档并推出更多教程,敬请关注。同时我们非常期待有兴趣的创客、树莓派学习者深度参与进来,基于此SDK创造自己的作品、完善SDK本身! 16 | 17 | 树莓派实验室 QQ 群号 62335986 18 | 19 | ![](http://shumeipai.nxez.com/wp-content/uploads/2015/03/P1100536-0.jpg) 20 | -------------------------------------------------------------------------------- /entities/__init__.py: -------------------------------------------------------------------------------- 1 | from .buzzer import Buzzer 2 | from .led import Led 3 | from .led import LedRow 4 | from .ds18b20 import DS18B20 5 | from .digital_display import DigitalDisplay 6 | from .dip_switch_2bit import DipSwitch2Bit 7 | from .tact import Tact 8 | from .tact import TactRow 9 | from .ic_74hc595 import IC_74HC595 10 | from .led_74hc595 import Led74HC595 11 | from .ic_tm1637 import IC_TM1637 12 | from .digital_display_tm1637 import DigitalDisplayTM1637 13 | -------------------------------------------------------------------------------- /entities/buzzer.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | # 4 | # Copyright (c) 2015 NXEZ.COM. 5 | # http://www.nxez.com 6 | # 7 | # Licensed under the GNU General Public License, Version 2.0 (the "License"); 8 | # you may not use this file except in compliance with the License. 9 | # You may obtain a copy of the License at 10 | # 11 | # http://www.gnu.org/licenses/gpl-2.0.html 12 | # 13 | # Unless required by applicable law or agreed to in writing, software 14 | # distributed under the License is distributed on an "AS IS" BASIS, 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | 19 | import RPi.GPIO as GPIO 20 | import time 21 | 22 | class Buzzer(object): 23 | ''' 24 | Buzzer class 25 | ''' 26 | __pin = 0 27 | __real_true = GPIO.HIGH 28 | __is_on = False 29 | 30 | def __init__(self, pin, real_true = GPIO.HIGH): 31 | ''' 32 | Init the buzzer 33 | :param pin: pin number 34 | :param real_true: GPIO.HIGH or GPIO.LOW 35 | :return: void 36 | ''' 37 | self.__pin = pin 38 | self.__real_true = real_true 39 | 40 | #Stauts. 41 | @property 42 | def is_on(self): 43 | ''' 44 | Return the status of buzzer 45 | :return: void 46 | ''' 47 | return self.__is_on 48 | 49 | #Verbs. 50 | def on(self): 51 | ''' 52 | Set buzzer on 53 | :return: void 54 | ''' 55 | GPIO.output(self.__pin, self.__real_true) 56 | self.__is_on = True 57 | 58 | def off(self): 59 | ''' 60 | Set buzzer off 61 | :return: void 62 | ''' 63 | GPIO.output(self.__pin, not self.__real_true) 64 | self.__is_on = False 65 | 66 | #functions. 67 | def beep(self, seconds): 68 | ''' 69 | Beep one time 70 | :param seconds: beep time 71 | :return: void 72 | ''' 73 | self.on() 74 | time.sleep(seconds) 75 | self.off() 76 | 77 | def beepAction(self, secs, sleepsecs, times): 78 | ''' 79 | Beep in a rhythm 80 | e.g. beepAction(0.02,0.02,30) 81 | :param secs: beep time 82 | :param sleepsecs: break time 83 | :param times: repeat times 84 | :return: void 85 | ''' 86 | for i in range(times): 87 | self.beep(secs) 88 | time.sleep(sleepsecs) 89 | -------------------------------------------------------------------------------- /entities/digital_display.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | # 4 | # Copyright (c) 2015 NXEZ.COM. 5 | # http://www.nxez.com 6 | # 7 | # Licensed under the GNU General Public License, Version 2.0 (the "License"); 8 | # you may not use this file except in compliance with the License. 9 | # You may obtain a copy of the License at 10 | # 11 | # http://www.gnu.org/licenses/gpl-2.0.html 12 | # 13 | # Unless required by applicable law or agreed to in writing, software 14 | # distributed under the License is distributed on an "AS IS" BASIS, 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | 19 | import RPi.GPIO as GPIO 20 | import time 21 | import re 22 | from threading import Thread 23 | 24 | class DigitalDisplay(object): 25 | ''' 26 | Digital display class 27 | ''' 28 | __pins = {'seg':[], 'sel':[]} 29 | __real_true = GPIO.HIGH 30 | __numbers = [] 31 | __is_flushing = False 32 | __number_code = [0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x00, 0x40] 33 | 34 | def __init__(self, pins, real_true = GPIO.HIGH): 35 | ''' 36 | Init the digital display 37 | :param pin: pin numbers in array 38 | :param real_true: GPIO.HIGH or GPIO.LOW 39 | :return: void 40 | ''' 41 | self.__pins = pins 42 | self.__real_true = real_true 43 | try: 44 | t1 = Thread(target = self.flush_4bit) 45 | t1.setDaemon(True) 46 | t1.start() 47 | except: 48 | print("Error: Unable to start thread by DigitalDisplay") 49 | 50 | #Stauts. 51 | @property 52 | def numbers(self): 53 | ''' 54 | Get the current numbers array showing 55 | :return: numbers array 56 | ''' 57 | return self.__numbers 58 | 59 | #@numbers.setter 60 | def set_numbers(self, value): 61 | ''' 62 | Set the numbers array to show 63 | :return: void 64 | ''' 65 | pattern = re.compile(r'[-|#|\d]\.?') 66 | matches = pattern.findall(value) 67 | #del self.__numbers 68 | self.__numbers = [] 69 | for i in range(len(matches)): 70 | self.__numbers.append(matches[i]) 71 | #print(self.__numbers) 72 | 73 | #@numbers.deleter 74 | #def numbers(self): 75 | # del self.__numbers 76 | 77 | #Verbs. 78 | def on(self): 79 | ''' 80 | Set display on 81 | :return: void 82 | ''' 83 | self.__is_flushing = True 84 | 85 | def off(self): 86 | ''' 87 | Set display off 88 | :return: void 89 | ''' 90 | self.__is_flushing = False 91 | for p in self.__pins['sel'] + self.__pins['seg']: 92 | GPIO.output(p, not self.__real_true) 93 | 94 | def show(self, str): 95 | ''' 96 | Set the numbers array to show and enable the display 97 | :return: void 98 | ''' 99 | self.__is_flushing = False 100 | self.set_numbers(str) 101 | self.__is_flushing = True 102 | #print(self.__numbers) 103 | 104 | 105 | def flush_bit(self, sel, num, dp): 106 | if num == '#': 107 | num = 10 108 | elif num == '-': 109 | num = 11 110 | else: 111 | num = int(num) 112 | 113 | GPIO.output(self.__pins['sel'][sel], self.__real_true) 114 | n = self.__number_code[num] 115 | 116 | if dp: 117 | n = n | 10000000 118 | 119 | for i in range(8): 120 | if (n & (1 << i)): 121 | GPIO.output(self.__pins['seg'][i], self.__real_true) 122 | 123 | GPIO.output(self.__pins['sel'][sel], not self.__real_true) 124 | 125 | for i in self.__pins['seg']: 126 | GPIO.output(i, not self.__real_true) 127 | 128 | def flush_4bit(self): 129 | while True: 130 | if self.__is_flushing: 131 | #print(self.__numbers) 132 | #print(range(min(4, len(self.__numbers)))) 133 | try: 134 | for i in range(min(4, len(self.__numbers))): 135 | self.flush_bit(i, self.__numbers[i].replace('.',''), True if self.__numbers[i].count('.') > 0 else False) 136 | time.sleep(0.001) 137 | except: 138 | pass 139 | -------------------------------------------------------------------------------- /entities/digital_display_tm1637.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | # 4 | # Copyright (c) 2016 NXEZ.COM. 5 | # http://www.nxez.com 6 | # 7 | # Licensed under the GNU General Public License, Version 2.0 (the "License"); 8 | # you may not use this file except in compliance with the License. 9 | # You may obtain a copy of the License at 10 | # 11 | # http://www.gnu.org/licenses/gpl-2.0.html 12 | # 13 | # Unless required by applicable law or agreed to in writing, software 14 | # distributed under the License is distributed on an "AS IS" BASIS, 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | 19 | import RPi.GPIO as GPIO 20 | import re 21 | from .ic_tm1637 import IC_TM1637 as IC_TM1637 22 | 23 | class DigitalDisplayTM1637(object): 24 | ''' 25 | Digital display class 26 | ''' 27 | 28 | __ic_tm1637 = None 29 | __numbers = [] 30 | __number_code = [0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x00, 0x40] 31 | __address_code = [0xc0, 0xc1, 0xc2, 0xc3] 32 | __is_on = False 33 | 34 | 35 | def __init__(self, pins, real_true = GPIO.HIGH): 36 | ''' 37 | Init the digital display 38 | :param pin: pin numbers in array 39 | :param real_true: GPIO.HIGH or GPIO.LOW 40 | :return: void 41 | ''' 42 | self.__ic_tm1637 = IC_TM1637(pins, real_true) 43 | 44 | #Stauts. 45 | @property 46 | def is_on(self): 47 | ''' 48 | Get the current status of the digital display 49 | ''' 50 | return self.__is_on 51 | 52 | @property 53 | def numbers(self): 54 | ''' 55 | Get the current numbers array showing 56 | :return: numbers array 57 | ''' 58 | return self.__numbers 59 | 60 | #@numbers.setter 61 | def set_numbers(self, value): 62 | ''' 63 | Set the numbers array to show 64 | :return: void 65 | ''' 66 | pattern = re.compile(r'[-|#|\d]\.?') 67 | matches = pattern.findall(value) 68 | #del self.__numbers 69 | self.__numbers = [] 70 | for i in range(len(matches)): 71 | self.__numbers.append(matches[i]) 72 | #print(self.__numbers) 73 | 74 | #@numbers.deleter 75 | #def numbers(self): 76 | # del self.__numbers 77 | 78 | @property 79 | def ic(self): 80 | ''' 81 | Return the instance of ic 82 | :return: ic 83 | ''' 84 | return self.__ic_tm1637 85 | 86 | #Verbs. 87 | def on(self): 88 | ''' 89 | Set display on 90 | :return: void 91 | ''' 92 | self.__ic_tm1637.set_command(0x8f) 93 | self.__is_on = True 94 | 95 | def off(self): 96 | ''' 97 | Set display off 98 | :return: void 99 | ''' 100 | self.__ic_tm1637.clear() 101 | self.__is_on = False 102 | 103 | def show(self, str): 104 | ''' 105 | Set the numbers array to show and enable the display 106 | :return: void 107 | ''' 108 | self.set_numbers(str) 109 | #print(self.__numbers) 110 | 111 | self.__ic_tm1637.set_command(0x44) 112 | 113 | for i in range(min(4, len(self.__numbers))): 114 | dp = True if self.__numbers[i].count('.') > 0 else False 115 | num = self.__numbers[i].replace('.','') 116 | if num == '#': 117 | num = 10 118 | elif num == '-': 119 | num = 11 120 | else: 121 | num = int(num) 122 | 123 | if dp: 124 | self.__ic_tm1637.set_data(self.__address_code[i], self.__number_code[num]|0x80) 125 | else: 126 | self.__ic_tm1637.set_data(self.__address_code[i], self.__number_code[num]) 127 | 128 | self.on() 129 | -------------------------------------------------------------------------------- /entities/dip_switch_2bit.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | # 4 | # Copyright (c) 2015 NXEZ.COM. 5 | # http://www.nxez.com 6 | # 7 | # Licensed under the GNU General Public License, Version 2.0 (the "License"); 8 | # you may not use this file except in compliance with the License. 9 | # You may obtain a copy of the License at 10 | # 11 | # http://www.gnu.org/licenses/gpl-2.0.html 12 | # 13 | # Unless required by applicable law or agreed to in writing, software 14 | # distributed under the License is distributed on an "AS IS" BASIS, 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | 19 | import RPi.GPIO as GPIO 20 | import time 21 | from threading import Thread 22 | 23 | class DipSwitch2Bit(object): 24 | ''' 25 | Dip switch (2bit) class 26 | ''' 27 | __pins = [] 28 | __real_true = GPIO.HIGH 29 | __status = [] 30 | 31 | __observers = [] 32 | 33 | def __init__(self, pins, real_true = GPIO.HIGH): 34 | ''' 35 | Init the dip switch 36 | :param pin: pin numbers in array 37 | :param real_true: GPIO.HIGH or GPIO.LOW 38 | :return: void 39 | ''' 40 | self.__pins = pins 41 | self.__real_true = real_true 42 | for p in pins: 43 | self.__status.append(not real_true) 44 | 45 | if self.__real_true: 46 | self.__status[0] = GPIO.input(self.__pins[0]) 47 | self.__status[1] = GPIO.input(self.__pins[1]) 48 | else: 49 | self.__status[0] = not GPIO.input(self.__pins[0]) 50 | self.__status[1] = not GPIO.input(self.__pins[1]) 51 | 52 | 53 | GPIO.add_event_detect(self.__pins[0], GPIO.BOTH, callback = self.make_event, bouncetime = 50) 54 | GPIO.add_event_detect(self.__pins[1], GPIO.BOTH, callback = self.make_event, bouncetime = 50) 55 | 56 | try: 57 | t1 = Thread(target = self.watching) 58 | t1.setDaemon(True) 59 | #t1.start() 60 | except: 61 | print("Error: Unable to start thread by DipSwitch") 62 | 63 | #Stauts. 64 | @property 65 | def is_on(self): 66 | ''' 67 | Get the status of each bit 68 | :return: the status array 69 | ''' 70 | return self.__status 71 | 72 | #Events 73 | def register(self, observer): 74 | if observer not in self.__observers: 75 | self.__observers.append(observer) 76 | 77 | def deregister(self, observer): 78 | if observer in self.__observers: 79 | self.__observers.remove(observer) 80 | 81 | def notify_observers(self): 82 | for o in self.__observers: 83 | #o.update(self.__status) 84 | o.on_dip_switch_2bit_status_changed(self.__status) 85 | 86 | def status_changed(self): 87 | self.notify_observers() 88 | 89 | def make_event(self, channel): 90 | if self.__real_true: 91 | if GPIO.input(self.__pins[0]) != self.__status[0]: 92 | self.__status[0] = GPIO.input(self.__pins[0]) 93 | self.status_changed() 94 | 95 | if GPIO.input(self.__pins[1]) != self.__status[1]: 96 | self.__status[1] = GPIO.input(self.__pins[1]) 97 | self.status_changed() 98 | else: 99 | if GPIO.input(self.__pins[0]) == self.__status[0]: 100 | self.__status[0] = not GPIO.input(self.__pins[0]) 101 | self.status_changed() 102 | 103 | if GPIO.input(self.__pins[1]) == self.__status[1]: 104 | self.__status[1] = not GPIO.input(self.__pins[1]) 105 | self.status_changed() 106 | 107 | def watching(self): 108 | if self.__real_true: 109 | while True: 110 | if GPIO.input(self.__pins[0]) != self.__status[0]: 111 | self.__status[0] = GPIO.input(self.__pins[0]) 112 | self.status_changed() 113 | 114 | if GPIO.input(self.__pins[1]) != self.__status[1]: 115 | self.__status[1] = GPIO.input(self.__pins[1]) 116 | self.status_changed() 117 | 118 | time.sleep(0.05) 119 | else: 120 | while True: 121 | if GPIO.input(self.__pins[0]) == self.__status[0]: 122 | self.__status[0] = not GPIO.input(self.__pins[0]) 123 | self.status_changed() 124 | 125 | if GPIO.input(self.__pins[1]) == self.__status[1]: 126 | self.__status[1] = not GPIO.input(self.__pins[1]) 127 | self.status_changed() 128 | 129 | time.sleep(0.05) 130 | -------------------------------------------------------------------------------- /entities/ds18b20.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | # 4 | # Copyright (c) 2015 NXEZ.COM. 5 | # http://www.nxez.com 6 | # 7 | # Licensed under the GNU General Public License, Version 2.0 (the "License"); 8 | # you may not use this file except in compliance with the License. 9 | # You may obtain a copy of the License at 10 | # 11 | # http://www.gnu.org/licenses/gpl-2.0.html 12 | # 13 | # Unless required by applicable law or agreed to in writing, software 14 | # distributed under the License is distributed on an "AS IS" BASIS, 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | 19 | import time 20 | import subprocess 21 | import os 22 | import glob 23 | 24 | class DS18B20(object): 25 | ''' 26 | DS18B20 class 27 | ''' 28 | __pin = 0 #it's no use 29 | #__device_file = '' 30 | #__temperature = -128 31 | 32 | def __init__(self, pin = 4): 33 | ''' 34 | Init the DS18b20 35 | :param pin: pin number 36 | :return: void 37 | ''' 38 | self.__pins = pin 39 | os.system('sudo modprobe w1-gpio') 40 | os.system('sudo modprobe w1-therm') 41 | 42 | #Verbs. 43 | def get_device_file(self, index = 0): 44 | base_dir = '/sys/bus/w1/devices/' 45 | #fix "IndexError: list index out of range" 46 | if not glob.glob(base_dir + '28*'): 47 | return False 48 | if glob.glob(base_dir + '28*')[index] is not None: 49 | device_folder = glob.glob(base_dir + '28*')[index] 50 | return device_folder + '/w1_slave' 51 | else: 52 | return False 53 | 54 | def read_temp_raw(self, index = 0): 55 | df = self.get_device_file(index) 56 | if not df: 57 | return False 58 | catdata = subprocess.Popen(['cat', df], stdout = subprocess.PIPE, stderr = subprocess.PIPE) 59 | out,err = catdata.communicate() 60 | out_decode = out.decode('utf-8') 61 | lines = out_decode.split('\n') 62 | return lines 63 | 64 | def read_temp(self, index = 0): 65 | tr = self.read_temp_raw(index) 66 | if not tr: 67 | return False 68 | lines = tr 69 | while lines[0].strip()[-3:] != 'YES': 70 | time.sleep(0.2) 71 | tr = self.read_temp_raw(index) 72 | if not tr: 73 | return False 74 | lines = tr 75 | equals_pos = lines[1].find('t=') 76 | if equals_pos != -1: 77 | temp_string = lines[1][equals_pos+2:] 78 | temp_c = float(temp_string) / 1000.0 79 | temp_f = temp_c * 9.0 / 5.0 + 32.0 80 | #return temp_c, temp_f 81 | return temp_c 82 | 83 | #Stauts. 84 | @property 85 | def is_exist(self, index = 0): 86 | ''' 87 | Return true if the ds18b20 is exist 88 | :param index: from 0 to n 89 | :return: Return true if the ds18b20 is exist 90 | ''' 91 | #if not os.path.exists(self.__device_file): 92 | # return False 93 | #else: 94 | # return True 95 | return self.get_device_file(index) 96 | 97 | @property 98 | def temperature(self, index = 0): 99 | ''' 100 | Get the temperature from ds18b20 101 | :param index: from 0 to n 102 | :return: Return the temperature from ds18b20, return -128 means get a error. 103 | ''' 104 | if not self.is_exist: 105 | return -128.0 106 | else: 107 | return self.read_temp(index) 108 | -------------------------------------------------------------------------------- /entities/ic_74hc595.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | # 4 | # Copyright (c) 2016 NXEZ.COM. 5 | # http://www.nxez.com 6 | # 7 | # Licensed under the GNU General Public License, Version 2.0 (the "License"); 8 | # you may not use this file except in compliance with the License. 9 | # You may obtain a copy of the License at 10 | # 11 | # http://www.gnu.org/licenses/gpl-2.0.html 12 | # 13 | # Unless required by applicable law or agreed to in writing, software 14 | # distributed under the License is distributed on an "AS IS" BASIS, 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | 19 | import RPi.GPIO as GPIO 20 | 21 | class IC_74HC595(object): 22 | ''' 23 | IC_74HC595 class 24 | ''' 25 | __pins = {'ds':0, 'shcp':0, 'stcp':0} 26 | __real_true = GPIO.HIGH 27 | __data = 0x00 28 | 29 | def __init__(self, pins, real_true = GPIO.HIGH): 30 | ''' 31 | Init the ic 32 | :param pin: pin number 33 | :param real_true: GPIO.HIGH or GPIO.LOW 34 | :return: void 35 | ''' 36 | self.__pins = pins 37 | self.__real_true = real_true 38 | 39 | #Stauts. 40 | @property 41 | def data(self): 42 | ''' 43 | Return the data 44 | :return: void 45 | ''' 46 | return self.__data 47 | 48 | #Verbs. 49 | def flush_shcp(self): 50 | ''' 51 | Flush a shcp 52 | :return: void 53 | ''' 54 | GPIO.output(self.__pins['shcp'], not self.__real_true) 55 | GPIO.output(self.__pins['shcp'], self.__real_true) 56 | 57 | def flush_stcp(self): 58 | ''' 59 | Flush a stcp 60 | :return: void 61 | ''' 62 | GPIO.output(self.__pins['stcp'], not self.__real_true) 63 | GPIO.output(self.__pins['stcp'], self.__real_true) 64 | 65 | def set_bit(self, bit): 66 | ''' 67 | Set a bit 68 | :param bit: bit 69 | :return: void 70 | ''' 71 | GPIO.output(self.__pins['ds'], bit) 72 | self.flush_shcp() 73 | 74 | def set_data(self, data): 75 | ''' 76 | Set a byte 77 | :param data: data 78 | :return: void 79 | ''' 80 | self.__data = data 81 | for i in range (0, 8): 82 | self.set_bit((self.__data >> i) & 0x01) 83 | 84 | self.flush_stcp() 85 | 86 | 87 | def clear(self): 88 | ''' 89 | Clear the data 90 | :return: void 91 | ''' 92 | self.set_data(0x00) 93 | -------------------------------------------------------------------------------- /entities/ic_tm1637.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | # 4 | # Copyright (c) 2016 NXEZ.COM. 5 | # http://www.nxez.com 6 | # 7 | # Licensed under the GNU General Public License, Version 2.0 (the "License"); 8 | # you may not use this file except in compliance with the License. 9 | # You may obtain a copy of the License at 10 | # 11 | # http://www.gnu.org/licenses/gpl-2.0.html 12 | # 13 | # Unless required by applicable law or agreed to in writing, software 14 | # distributed under the License is distributed on an "AS IS" BASIS, 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | 19 | import RPi.GPIO as GPIO 20 | import time 21 | 22 | class IC_TM1637(object): 23 | ''' 24 | IC_TM1637 class 25 | ''' 26 | __pins = {'di' : 0, 'clk' : 0} 27 | __real_true = GPIO.HIGH 28 | 29 | def __init__(self, pins, real_true = GPIO.HIGH): 30 | ''' 31 | Init the ic 32 | :param pin: pin number 33 | :param real_true: GPIO.HIGH or GPIO.LOW 34 | :return: void 35 | ''' 36 | self.__pins = pins 37 | self.__real_true = real_true 38 | 39 | #Verbs. 40 | def bus_delay(self): 41 | ''' 42 | Delay 43 | :return: void 44 | ''' 45 | time.sleep(0.001) 46 | 47 | def start_bus(self): 48 | ''' 49 | Start bus 50 | :return: void 51 | ''' 52 | GPIO.output(self.__pins['clk'], self.__real_true) 53 | GPIO.output(self.__pins['di'], self.__real_true) 54 | self.bus_delay() 55 | GPIO.output(self.__pins['di'], not self.__real_true) 56 | self.bus_delay() 57 | GPIO.output(self.__pins['clk'], not self.__real_true) 58 | self.bus_delay() 59 | 60 | def stop_bus(self): 61 | ''' 62 | Stop bus 63 | :return: void 64 | ''' 65 | GPIO.output(self.__pins['clk'], not self.__real_true) 66 | self.bus_delay() 67 | GPIO.output(self.__pins['di'], not self.__real_true) 68 | self.bus_delay() 69 | GPIO.output(self.__pins['clk'], self.__real_true) 70 | self.bus_delay() 71 | GPIO.output(self.__pins['di'], self.__real_true) 72 | self.bus_delay() 73 | 74 | def set_bit(self, bit): 75 | ''' 76 | Set a bit 77 | :param bit: bit 78 | :return: void 79 | ''' 80 | GPIO.output(self.__pins['clk'], not self.__real_true) 81 | self.bus_delay() 82 | GPIO.output(self.__pins['di'], bit) 83 | self.bus_delay() 84 | GPIO.output(self.__pins['clk'], self.__real_true) 85 | self.bus_delay() 86 | 87 | def set_byte(self, data): 88 | ''' 89 | Set a byte 90 | :param data: data 91 | :return: void 92 | ''' 93 | for i in range (0, 8): 94 | self.set_bit((data >> i) & 0x01) 95 | 96 | GPIO.output(self.__pins['clk'], not self.__real_true) 97 | self.bus_delay() 98 | 99 | GPIO.output(self.__pins['di'], self.__real_true) 100 | self.bus_delay() 101 | 102 | GPIO.output(self.__pins['clk'], self.__real_true) 103 | self.bus_delay() 104 | 105 | def set_command(self, command): 106 | ''' 107 | Set command 108 | :param command: command code 109 | :return: void 110 | ''' 111 | self.start_bus() 112 | self.set_byte(command) 113 | self.start_bus() 114 | 115 | def set_data(self, address, data): 116 | ''' 117 | Set data with address and data 118 | :param address: address 119 | :param data: data 120 | :return: void 121 | ''' 122 | self.start_bus() 123 | self.set_byte(address) 124 | self.set_byte(data) 125 | self.start_bus() 126 | 127 | def clear(self): 128 | ''' 129 | Clear the data 130 | :return: void 131 | ''' 132 | self.set_command(0x80) 133 | -------------------------------------------------------------------------------- /entities/led.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | # 4 | # Copyright (c) 2015 NXEZ.COM. 5 | # http://www.nxez.com 6 | # 7 | # Licensed under the GNU General Public License, Version 2.0 (the "License"); 8 | # you may not use this file except in compliance with the License. 9 | # You may obtain a copy of the License at 10 | # 11 | # http://www.gnu.org/licenses/gpl-2.0.html 12 | # 13 | # Unless required by applicable law or agreed to in writing, software 14 | # distributed under the License is distributed on an "AS IS" BASIS, 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | 19 | import RPi.GPIO as GPIO 20 | import time 21 | from threading import Thread 22 | 23 | class Led(object): 24 | ''' 25 | Led class 26 | ''' 27 | __pin = 0 28 | __real_true = GPIO.HIGH 29 | __pwm = None 30 | __is_on = False 31 | __is_pulse = None 32 | 33 | def __init__(self, pin, real_true = GPIO.HIGH): 34 | ''' 35 | Init the led 36 | :param pin: pin numbers in array 37 | :param real_true: GPIO.HIGH or GPIO.LOW 38 | :return: void 39 | ''' 40 | self.__pin = pin 41 | self.__real_true = real_true 42 | 43 | #Stauts. 44 | @property 45 | def is_on(self): 46 | ''' 47 | Get the current status of the led 48 | ''' 49 | return self.__is_on 50 | 51 | #Verbs. 52 | def on(self): 53 | ''' 54 | Set the led on 55 | ''' 56 | if not self.__is_pulse: 57 | GPIO.output(self.__pin, self.__real_true) 58 | self.__is_on = True 59 | 60 | def off(self): 61 | ''' 62 | Set the led off 63 | ''' 64 | if self.__is_pulse: 65 | self.__is_pulse = False 66 | self.__pwm.stop() 67 | time.sleep(0.1) 68 | GPIO.output(self.__pin, not self.__real_true) 69 | self.__is_on = False 70 | 71 | #functions. 72 | def flash(self, seconds): 73 | ''' 74 | Flash one time 75 | :param seconds: on time 76 | :return: void 77 | ''' 78 | self.on() 79 | time.sleep(seconds) 80 | self.off() 81 | 82 | #e.g. flashAction(0.02,0.02,30) 83 | def flashAction(self, secs, sleepsecs, times): 84 | ''' 85 | Flash in a rhythm 86 | e.g. flashAction(0.02,0.02,30) 87 | :param secs: on time 88 | :param sleepsecs: break time 89 | :param times: repeat times 90 | :return: void 91 | ''' 92 | for i in range(times): 93 | self.flash(secs) 94 | time.sleep(sleepsecs) 95 | 96 | def pulse(self, hertz=50, pause_time=0.01): 97 | ''' 98 | Breath until led off 99 | :param hertz: GPIO PWM hertz 100 | :param pause_time: breath pause time 101 | :return: void 102 | ''' 103 | if self.__pwm == None: 104 | self.__pwm = GPIO.PWM(self.__pin, hertz) 105 | else: 106 | self.__pwm.ChangeFrequency(hertz) 107 | self.__pwm.start(0) 108 | if self.__is_pulse == None: 109 | def pulse_worker(): 110 | while True: 111 | if self.__is_pulse: 112 | try: 113 | for i in xrange(0, 101, 1): 114 | self.__pwm.ChangeDutyCycle(i) 115 | # off 116 | time.sleep(pause_time) 117 | time.sleep(1) 118 | for i in xrange(100, -1, -1): 119 | self.__pwm.ChangeDutyCycle(i) 120 | # on 121 | time.sleep(pause_time) 122 | except: 123 | continue 124 | try: 125 | pulse_thread = Thread(target = pulse_worker) 126 | pulse_thread.setDaemon(True) 127 | pulse_thread.start() 128 | except: 129 | print('Error: Unable to start thread by Led') 130 | self.__is_pulse = True 131 | self.__is_on = True 132 | 133 | class LedRow(object): 134 | ''' 135 | Class of leds in row 136 | ''' 137 | __leds = [] 138 | __pins = [] 139 | __real_true = GPIO.HIGH 140 | 141 | def __init__(self, pins, real_true = GPIO.HIGH): 142 | ''' 143 | Init the leds 144 | :param pin: pin numbers in array 145 | :param real_true: GPIO.HIGH or GPIO.LOW 146 | :return: void 147 | ''' 148 | self.__pins = pins 149 | self.__real_true = real_true 150 | for p in pins: 151 | self.__leds.append(Led(p, real_true)) 152 | 153 | #Stauts. 154 | #@property 155 | def is_on(self, index): 156 | ''' 157 | Get status of led in ledrow by index 158 | :param index: index of the led 159 | :return: status in boolean 160 | ''' 161 | if index >= len(self.__leds): 162 | return False 163 | return self.__leds[index].is_on 164 | 165 | @property 166 | def row_status(self): 167 | ''' 168 | Get status array of the ledrow 169 | :return: status array 170 | ''' 171 | r = [] 172 | for l in self.__leds: 173 | r.append(l.is_on) 174 | return r 175 | 176 | @property 177 | def items(self): 178 | ''' 179 | Get the instances of the leds in ledrow 180 | :return: instances array 181 | ''' 182 | return self.__leds 183 | 184 | #Verbs. 185 | #@multimethod() 186 | def on(self): 187 | ''' 188 | Set all the leds on 189 | :return: void 190 | ''' 191 | for l in self.__leds: 192 | l.on() 193 | 194 | #@multimethod() 195 | def off(self): 196 | ''' 197 | Set all the leds off 198 | :return: void 199 | ''' 200 | for l in self.__leds: 201 | l.off() 202 | 203 | #@multimethod(int) 204 | def on_for_index(self, index): 205 | ''' 206 | Set the led on by index in the ledrow 207 | :return: void 208 | ''' 209 | self.__leds[index].on() 210 | 211 | #@multimethod(int) 212 | def off_for_index(self, index): 213 | ''' 214 | Set the led off by index in the ledrow 215 | :return: void 216 | ''' 217 | self.__leds[index].off() 218 | 219 | def set_row(self, status): 220 | ''' 221 | Set the ledrow's status in boolean array 222 | :param status: boolean array 223 | :return: void 224 | ''' 225 | for i in range(len(status)): 226 | #print(str(i) + str(status[i])) 227 | if status[i] is None: 228 | continue 229 | if status[i]: 230 | self.__leds[i].on() 231 | else: 232 | self.__leds[i].off() 233 | -------------------------------------------------------------------------------- /entities/led_74hc595.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | # 4 | # Copyright (c) 2016 NXEZ.COM. 5 | # http://www.nxez.com 6 | # 7 | # Licensed under the GNU General Public License, Version 2.0 (the "License"); 8 | # you may not use this file except in compliance with the License. 9 | # You may obtain a copy of the License at 10 | # 11 | # http://www.gnu.org/licenses/gpl-2.0.html 12 | # 13 | # Unless required by applicable law or agreed to in writing, software 14 | # distributed under the License is distributed on an "AS IS" BASIS, 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | 19 | import RPi.GPIO as GPIO 20 | from .ic_74hc595 import IC_74HC595 as IC_74HC595 21 | 22 | class Led74HC595(object): 23 | ''' 24 | Class of leds in 74HC595 25 | ''' 26 | __ic_74hc595 = None 27 | 28 | def __init__(self, pins, real_true = GPIO.HIGH): 29 | ''' 30 | Init the leds 31 | :param pin: pin numbers in array 32 | :param real_true: GPIO.HIGH or GPIO.LOW 33 | :return: void 34 | ''' 35 | self.__ic_74hc595 = IC_74HC595(pins, real_true) 36 | 37 | #Stauts. 38 | @property 39 | def ic(self): 40 | return self.__ic_74hc595 41 | 42 | def is_on(self, index): 43 | ''' 44 | Get status of led in ledrow by index 45 | :param index: index of the led 46 | :return: status in boolean 47 | ''' 48 | if index >= 8: 49 | return False 50 | return self.__ic_74hc595.data >> index & 0x01 51 | 52 | @property 53 | def row_status(self): 54 | ''' 55 | Get status array of the ledrow 56 | :return: status array 57 | ''' 58 | r = [] 59 | for i in range (0, 8): 60 | r.append(self.__ic_74hc595.data >> i & 0x01) 61 | return r 62 | 63 | #Verbs. 64 | def on(self): 65 | ''' 66 | Set all the leds on 67 | :return: void 68 | ''' 69 | self.__ic_74hc595.set_data(0xff) 70 | 71 | def off(self): 72 | ''' 73 | Set all the leds off 74 | :return: void 75 | ''' 76 | self.__ic_74hc595.clear() 77 | 78 | def on_for_index(self, index): 79 | ''' 80 | Set the led on by index in the ledrow 81 | :return: void 82 | ''' 83 | self.__ic_74hc595.set_data(self.__ic_74hc595.data | (0x01 << (index))) 84 | 85 | def off_for_index(self, index): 86 | ''' 87 | Set the led off by index in the ledrow 88 | :return: void 89 | ''' 90 | arr = [0xfe, 0xfd, 0xfb, 0xf7, 0xef, 0xdf, 0xbf, 0x7f] 91 | self.__ic_74hc595.set_data(self.__ic_74hc595.data & arr[index]) 92 | 93 | def set_row(self, status): 94 | ''' 95 | Set the ledrow's status in boolean array 96 | :param status: boolean array 97 | :return: void 98 | ''' 99 | for i in range(len(status)): 100 | #print(str(i) + str(status[i])) 101 | if status[i] is None: 102 | continue 103 | if status[i]: 104 | self.on_for_index(i) 105 | else: 106 | self.off_for_index(i) 107 | -------------------------------------------------------------------------------- /entities/tact.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | # 4 | # Copyright (c) 2015 NXEZ.COM. 5 | # http://www.nxez.com 6 | # 7 | # Licensed under the GNU General Public License, Version 2.0 (the "License"); 8 | # you may not use this file except in compliance with the License. 9 | # You may obtain a copy of the License at 10 | # 11 | # http://www.gnu.org/licenses/gpl-2.0.html 12 | # 13 | # Unless required by applicable law or agreed to in writing, software 14 | # distributed under the License is distributed on an "AS IS" BASIS, 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | 19 | import RPi.GPIO as GPIO 20 | import time 21 | from threading import Thread 22 | 23 | class Tact(object): 24 | ''' 25 | Tact class 26 | ''' 27 | __pin = 0 28 | __real_true = GPIO.HIGH 29 | __status = False 30 | 31 | __observers = [] 32 | 33 | def __init__(self, pin, real_true = GPIO.HIGH): 34 | ''' 35 | Init the tact 36 | :param pin: pin number in array 37 | :param real_true: GPIO.HIGH or GPIO.LOW 38 | :return: void 39 | ''' 40 | self.__pin = pin 41 | self.__real_true = real_true 42 | 43 | if self.__real_true: 44 | self.__status = GPIO.input(self.__pin) 45 | else: 46 | self.__status = not GPIO.input(self.__pin) 47 | 48 | GPIO.add_event_detect(pin, GPIO.BOTH, callback = self.make_event, bouncetime = 1) 49 | 50 | try: 51 | t1 = Thread(target = self.watching) 52 | t1.setDaemon(True) 53 | #t1.start() 54 | except: 55 | print("Error: Unable to start thread by Tact") 56 | 57 | 58 | #Stauts. 59 | @property 60 | def is_on(self): 61 | ''' 62 | Get current of tact 63 | ''' 64 | if self.__real_true: 65 | if self.__status != GPIO.input(self.__pin): 66 | self.__status = GPIO.input(self.__pin) 67 | else: 68 | if self.__status == GPIO.input(self.__pin): 69 | self.__status = not GPIO.input(self.__pin) 70 | 71 | return self.__status 72 | 73 | #Events 74 | def register(self, observer): 75 | if observer not in self.__observers: 76 | self.__observers.append(observer) 77 | 78 | def deregister(self, observer): 79 | if observer in self.__observers: 80 | self.__observers.remove(observer) 81 | 82 | def notify_observers(self, status): 83 | for o in self.__observers: 84 | o.on_tact_event(self.__pin, status) 85 | 86 | def event(self, action): 87 | self.notify_observers(action) 88 | 89 | def make_event(self, channel): 90 | self.notify_observers(self.__real_true if GPIO.input(self.__pin) else not self.__real_true) 91 | if self.__real_true: 92 | if self.__status != GPIO.input(self.__pin): 93 | self.__status = GPIO.input(self.__pin) 94 | #self.notify_observers(self.__real_true if self.__status else not self.__real_true) 95 | else: 96 | if self.__status == GPIO.input(self.__pin): 97 | self.__status = not GPIO.input(self.__pin) 98 | #self.notify_observers(self.__real_true if not self.__status else not self.__real_true) 99 | 100 | def watching(self): 101 | if self.__real_true: 102 | while True: 103 | if GPIO.input(self.__pin) != self.__status: 104 | self.__status = GPIO.input(self.__pin) 105 | self.notify_observers(self.__real_true if self.__status else not self.__real_true) 106 | time.sleep(0.05) 107 | else: 108 | while True: 109 | if GPIO.input(self.__pin) == self.__status: 110 | self.__status = not GPIO.input(self.__pin) 111 | self.notify_observers(self.__real_true if not self.__status else not self.__real_true) 112 | time.sleep(0.05) 113 | 114 | class TactRow(object): 115 | ''' 116 | Class of tacts in row 117 | ''' 118 | __tacts = [] 119 | __pins = [] 120 | __real_true = GPIO.HIGH 121 | 122 | def __init__(self, pins, real_true = GPIO.HIGH): 123 | ''' 124 | Init the tacts 125 | :param pin: pin numbers in array 126 | :param real_true: GPIO.HIGH or GPIO.LOW 127 | :return: void 128 | ''' 129 | self.__pins = pins 130 | self.__real_true = real_true 131 | for p in pins: 132 | self.__tacts.append(Tact(p, real_true)) 133 | 134 | #Stauts. 135 | def is_on(self, index): 136 | ''' 137 | Get status of tact in tactrow by index 138 | :param index: index of the tact 139 | :return: status in boolean 140 | ''' 141 | if index >= len(self.__tacts): 142 | return False 143 | return self.__tacts[index].is_on 144 | 145 | @property 146 | def row_status(self): 147 | ''' 148 | Get status array of the tactrow 149 | :return: status array 150 | ''' 151 | r = [] 152 | for l in self.__tacts: 153 | r.append(l.is_on) 154 | return r 155 | 156 | @property 157 | def items(self): 158 | ''' 159 | Get the instances of the tacts in tactrow 160 | :return: instances array 161 | ''' 162 | return self.__tacts 163 | -------------------------------------------------------------------------------- /examples/main.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | # 4 | # Copyright (c) 2016 NXEZ.COM. 5 | # http://www.nxez.com 6 | # 7 | # Licensed under the GNU General Public License, Version 2.0 (the "License"); 8 | # you may not use this file except in compliance with the License. 9 | # You may obtain a copy of the License at 10 | # 11 | # http://www.gnu.org/licenses/gpl-2.0.html 12 | # 13 | # Unless required by applicable law or agreed to in writing, software 14 | # distributed under the License is distributed on an "AS IS" BASIS, 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | 19 | __author__ = 'Spoony' 20 | __version__ = 'version 0.0.1' 21 | __license__ = 'Copyright (c) 2016 NXEZ.COM' 22 | 23 | from sakshat import SAKSHAT 24 | import time 25 | import commands 26 | 27 | #Declare the SAKS Board 28 | SAKS = SAKSHAT() 29 | 30 | def dip_switch_status_changed_handler(status): 31 | ''' 32 | called while the status of dip switch changed 33 | :param status: current status 34 | :return: void 35 | ''' 36 | print('on_dip_switch_status_changed:') 37 | print(status) 38 | pass 39 | 40 | def tact_event_handler(pin, status): 41 | ''' 42 | called while the status of tacts changed 43 | :param pin: pin number which stauts of tact is changed 44 | :param status: current status 45 | :return: void 46 | ''' 47 | print('tact_event_handler') 48 | print("%d - %s" % (pin, status)) 49 | 50 | #def handler(signum, frame): 51 | # SAKS.digital_display.off() 52 | # print "receive a signal %d"%(signum) 53 | 54 | def get_cpu_temp(): 55 | tempFile = open( "/sys/class/thermal/thermal_zone0/temp" ) 56 | cpu_temp = tempFile.read() 57 | tempFile.close() 58 | return float(cpu_temp)/1000 59 | # Uncomment the next line if you want the temp in Fahrenheit 60 | #return float(1.8*cpu_temp)+32 61 | 62 | def get_gpu_temp(): 63 | gpu_temp = commands.getoutput( '/opt/vc/bin/vcgencmd measure_temp' ).replace( 'temp=', '' ).replace( '\'C', '' ) 64 | return float(gpu_temp) 65 | # Uncomment the next line if you want the temp in Fahrenheit 66 | # return float(1.8* gpu_temp)+32 67 | 68 | if __name__ == "__main__": 69 | print("main") 70 | #SAKS = SAKSController() 71 | #print(PINS.BUZZER) 72 | #print(SAKS.appRoot) 73 | SAKS.dip_switch_status_changed_handler = dip_switch_status_changed_handler 74 | SAKS.tact_event_handler = tact_event_handler 75 | b = SAKS.buzzer 76 | b.beep(1) 77 | #SAKS.ledrow.ic.set_data(0x08) 78 | SAKS.ledrow.on() 79 | time.sleep(3) 80 | SAKS.ledrow.off() 81 | time.sleep(3) 82 | SAKS.ledrow.set_row([True, False, True, False, True, False, True, False]) 83 | time.sleep(2) 84 | SAKS.ledrow.set_row([None, True, False, None, None, None, None, True]) 85 | print( SAKS.ledrow.row_status) 86 | print( SAKS.ledrow.is_on(1)) 87 | print( SAKS.ledrow.is_on(2)) 88 | 89 | print( SAKS.ledrow.is_on(3)) 90 | print( SAKS.ledrow.is_on(4)) 91 | 92 | #SAKS.ledrow.items[7].flashAction(0.02,0.02,30) 93 | #SAKS.ledrow.on() 94 | print(SAKS.ds18b20.temperature) 95 | #SAKS.digital_display.show('#.1#.234') 96 | #print(SAKS.dip_switch.is_on) 97 | 98 | # 将显示“1234”4位数字,并且每一位右下角的小点点亮 99 | SAKS.digital_display.show("1.2.3.4.") 100 | time.sleep(1) 101 | # 将显示“1234”4位数字,并且数字2后面的小点点亮 102 | SAKS.digital_display.show("12.34") 103 | time.sleep(1) 104 | # 在第4位数码管显示“1”,其他3位数码管不显示 105 | SAKS.digital_display.show("###1") 106 | time.sleep(1) 107 | SAKS.digital_display.off() 108 | time.sleep(1) 109 | SAKS.digital_display.on() 110 | 111 | ''' 112 | while True: 113 | SAKS.digital_display.show("%d%d%d%d" % (time.gmtime().tm_min / 10, time.gmtime().tm_min % 10, time.gmtime().tm_sec / 10, time.gmtime().tm_sec % 10)) 114 | time.sleep(0.5) 115 | ''' 116 | 117 | ''' 118 | while True: 119 | t = get_cpu_temp() 120 | print("%3.1f" % t) 121 | #SAKS.digital_display.show("%3.2f" % t) 122 | if t > 38: 123 | b.beepAction(0.02,0.02,30) 124 | time.sleep(1) 125 | ''' 126 | 127 | input("Enter any keys to exit...") 128 | -------------------------------------------------------------------------------- /saks-v1.x/examples/main.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | # 4 | # Copyright (c) 2015 NXEZ.COM. 5 | # http://www.nxez.com 6 | # 7 | # Licensed under the GNU General Public License, Version 2.0 (the "License"); 8 | # you may not use this file except in compliance with the License. 9 | # You may obtain a copy of the License at 10 | # 11 | # http://www.gnu.org/licenses/gpl-2.0.html 12 | # 13 | # Unless required by applicable law or agreed to in writing, software 14 | # distributed under the License is distributed on an "AS IS" BASIS, 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | 19 | __author__ = 'Spoony' 20 | __version__ = 'version 0.0.1' 21 | __license__ = 'Copyright (c) 2015 NXEZ.COM' 22 | 23 | from sakshat import SAKSHAT 24 | import time 25 | import commands 26 | 27 | #Declare the SAKS Board 28 | SAKS = SAKSHAT() 29 | 30 | def dip_switch_status_changed_handler(status): 31 | ''' 32 | called while the status of dip switch changed 33 | :param status: current status 34 | :return: void 35 | ''' 36 | print('on_dip_switch_status_changed:') 37 | print(status) 38 | pass 39 | 40 | def tact_event_handler(pin, status): 41 | ''' 42 | called while the status of tacts changed 43 | :param pin: pin number which stauts of tact is changed 44 | :param status: current status 45 | :return: void 46 | ''' 47 | print('tact_event_handler') 48 | print("%d - %s" % (pin, status)) 49 | 50 | #def handler(signum, frame): 51 | # SAKS.digital_display.off() 52 | # print "receive a signal %d"%(signum) 53 | 54 | def get_cpu_temp(): 55 | tempFile = open( "/sys/class/thermal/thermal_zone0/temp" ) 56 | cpu_temp = tempFile.read() 57 | tempFile.close() 58 | return float(cpu_temp)/1000 59 | # Uncomment the next line if you want the temp in Fahrenheit 60 | #return float(1.8*cpu_temp)+32 61 | 62 | def get_gpu_temp(): 63 | gpu_temp = commands.getoutput( '/opt/vc/bin/vcgencmd measure_temp' ).replace( 'temp=', '' ).replace( '\'C', '' ) 64 | return float(gpu_temp) 65 | # Uncomment the next line if you want the temp in Fahrenheit 66 | # return float(1.8* gpu_temp)+32 67 | 68 | if __name__ == "__main__": 69 | print("main") 70 | #SAKS = SAKSController() 71 | #print(PINS.BUZZER) 72 | #print(SAKS.appRoot) 73 | SAKS.dip_switch_status_changed_handler = dip_switch_status_changed_handler 74 | SAKS.tact_event_handler = tact_event_handler 75 | b = SAKS.buzzer 76 | b.beep(1) 77 | SAKS.ledrow.set_row([True, False, True, False, True, False, True, False]) 78 | time.sleep(10) 79 | SAKS.ledrow.set_row([None, True, False, None, None, None, None, True]) 80 | #SAKS.ledrow.items[7].flashAction(0.02,0.02,30) 81 | #SAKS.ledrow.on() 82 | print(SAKS.ds18b20.temperature) 83 | #SAKS.digital_display.show('#.1#.234') 84 | #print(SAKS.dip_switch.is_on) 85 | 86 | ''' 87 | while True: 88 | SAKS.digital_display.show("%d%d%d%d" % (time.gmtime().tm_min / 10, time.gmtime().tm_min % 10, time.gmtime().tm_sec / 10, time.gmtime().tm_sec % 10)) 89 | time.sleep(0.5) 90 | ''' 91 | while True: 92 | t = get_cpu_temp() 93 | print("%3.1f" % t) 94 | #SAKS.digital_display.show("%3.2f" % t) 95 | if t > 38: 96 | b.beepAction(0.02,0.02,30) 97 | time.sleep(1) 98 | 99 | input("Enter any keys to exit...") 100 | 101 | -------------------------------------------------------------------------------- /saks-v1.x/sakshat.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | # 4 | # Copyright (c) 2015 NXEZ.COM. 5 | # http://www.nxez.com 6 | # 7 | # Licensed under the GNU General Public License, Version 2.0 (the "License"); 8 | # you may not use this file except in compliance with the License. 9 | # You may obtain a copy of the License at 10 | # 11 | # http://www.gnu.org/licenses/gpl-2.0.html 12 | # 13 | # Unless required by applicable law or agreed to in writing, software 14 | # distributed under the License is distributed on an "AS IS" BASIS, 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | 19 | __author__ = 'Spoony' 20 | __version__ = 'version 0.0.1' 21 | __license__ = 'Copyright (c) 2015 NXEZ.COM' 22 | 23 | #sys.path.append(sys.path[0] + "/entities/") 24 | #from entities import * 25 | import RPi.GPIO as GPIO 26 | from sakspins import SAKSPins as PINS 27 | import entities 28 | 29 | class SAKSHAT(object): 30 | ''' 31 | SAKS HAT class, some useful function are declared. 32 | ''' 33 | #appRoot = sys.path[0] 34 | buzzer = None 35 | ledrow = None 36 | ds18b20 = None 37 | digital_display = None 38 | dip_switch = None 39 | tactrow = None 40 | 41 | def saks_gpio_init(self): 42 | #print 'saks_gpio_init' 43 | GPIO.setwarnings(False) 44 | GPIO.cleanup() 45 | GPIO.setmode(GPIO.BCM) 46 | 47 | GPIO.setup(PINS.BUZZER, GPIO.OUT) 48 | GPIO.output(PINS.BUZZER, GPIO.HIGH) 49 | 50 | for p in [PINS.BUZZER, PINS.TACT_RIGHT, PINS.TACT_LEFT, PINS.DIP_SWITCH_1, PINS.DIP_SWITCH_2]: 51 | GPIO.setup(p, GPIO.OUT) 52 | GPIO.output(p, GPIO.HIGH) 53 | 54 | for p in PINS.LEDS + PINS.DIGITAL_DISPLAY + PINS.DIGITAL_DISPLAY_SELECT: 55 | GPIO.setup(p, GPIO.OUT) 56 | GPIO.output(p, GPIO.HIGH) 57 | 58 | for p in [PINS.TACT_RIGHT, PINS.TACT_LEFT, PINS.DIP_SWITCH_1, PINS.DIP_SWITCH_2]: 59 | GPIO.setup(p, GPIO.IN, pull_up_down = GPIO.PUD_UP) 60 | #GPIO.setup(p, GPIO.IN) 61 | 62 | #由于SAKS的蓝色LED和数码管共享引脚,此处将数码管位选关闭,只让信号作用于LED 63 | #GPIO.setup(17, GPIO.OUT, initial = GPIO.HIGH) 64 | #GPIO.setup(27, GPIO.OUT, initial = GPIO.HIGH) 65 | #GPIO.setup(22, GPIO.OUT, initial = GPIO.HIGH) 66 | #GPIO.setup(10, GPIO.OUT, initial = GPIO.HIGH) 67 | 68 | def __init__(self): 69 | self.saks_gpio_init() 70 | self.buzzer = entities.Buzzer(PINS.BUZZER, GPIO.LOW) 71 | self.ledrow = entities.LedRow(PINS.LEDS, GPIO.LOW) 72 | self.ds18b20 = entities.DS18B20(PINS.DS18B20) 73 | self.digital_display = entities.DigitalDisplay({'seg': PINS.DIGITAL_DISPLAY, 'sel': PINS.DIGITAL_DISPLAY_SELECT}, GPIO.LOW) 74 | self.dip_switch = entities.DipSwitch2Bit([PINS.DIP_SWITCH_1, PINS.DIP_SWITCH_2], GPIO.LOW) 75 | self.dip_switch.register(self) 76 | self.tactrow = entities.TactRow([PINS.TACT_LEFT, PINS.TACT_RIGHT], GPIO.LOW) 77 | for t in self.tactrow.items: 78 | t.register(self) 79 | 80 | #print('self.dip_switch.register(self)') 81 | 82 | dip_switch_status_changed_handler = None 83 | def on_dip_switch_2bit_status_changed(self, status): 84 | #print('on_dip_switch_2bit_status_changed') 85 | if self.dip_switch_status_changed_handler is not None: 86 | self.dip_switch_status_changed_handler(status) 87 | 88 | tact_event_handler = None 89 | def on_tact_event(self, pin, status): 90 | #print('on_tact_event') 91 | if self.tact_event_handler is not None: 92 | self.tact_event_handler(pin, status) 93 | -------------------------------------------------------------------------------- /saks-v1.x/sakspins.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | # 4 | # Copyright (c) 2015 NXEZ.COM. 5 | # http://www.nxez.com 6 | # 7 | # Licensed under the GNU General Public License, Version 2.0 (the "License"); 8 | # you may not use this file except in compliance with the License. 9 | # You may obtain a copy of the License at 10 | # 11 | # http://www.gnu.org/licenses/gpl-2.0.html 12 | # 13 | # Unless required by applicable law or agreed to in writing, software 14 | # distributed under the License is distributed on an "AS IS" BASIS, 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | 19 | __author__ = 'Spoony' 20 | __version__ = 'version 0.0.1' 21 | __license__ = 'Copyright (c) 2015 NXEZ.COM' 22 | 23 | class SAKSPins(object): 24 | ''' 25 | SAKS Pins Code With BCM for Raspberry Pi Model B. 26 | ''' 27 | ''' 28 | _inst = None 29 | def __new__(cls, var ): 30 | if cls._inst is None: 31 | cls._inst = object.__new__(cls) 32 | cls._inst.var = var 33 | return cls._inst 34 | instance = pinstable() 35 | def __new__(cls): 36 | if cls._inst is None: 37 | cls._inst = object.__new__(cls) 38 | return cls._inst 39 | ''' 40 | 41 | LED_BLUE_1 = 5 42 | LED_BLUE_2 = 6 43 | LED_BLUE_3 = 13 44 | LED_BLUE_4 = 19 45 | LED_GREEN_1 = 0 46 | LED_GREEN_2 = 1 47 | LED_YELLOW = 7 48 | LED_RED = 8 49 | 50 | BUZZER = 11 51 | 52 | TACT_RIGHT = 23 53 | TACT_LEFT = 18 54 | DIP_SWITCH_1 = 24 55 | DIP_SWITCH_2 = 25 56 | 57 | DIGITAL_DISPLAY_A = 21 58 | DIGITAL_DISPLAY_B = 16 59 | DIGITAL_DISPLAY_C = 19 60 | DIGITAL_DISPLAY_D = 6 61 | DIGITAL_DISPLAY_E = 5 62 | DIGITAL_DISPLAY_F = 20 63 | DIGITAL_DISPLAY_G = 26 64 | DIGITAL_DISPLAY_DP = 13 65 | DIGITAL_DISPLAY_SELECET_1 = 17 66 | DIGITAL_DISPLAY_SELECET_2 = 27 67 | DIGITAL_DISPLAY_SELECET_3 = 22 68 | DIGITAL_DISPLAY_SELECET_4 = 10 69 | 70 | IR_SENDER = 12 71 | IR_RECEIVER = 9 72 | DS18B20 = 4 73 | UART_TXD = 14 74 | UART_RXD = 15 75 | I2C_SDA = 2 76 | I2C_SLC = 3 77 | 78 | LEDS = ( 79 | LED_BLUE_1, 80 | LED_BLUE_2, 81 | LED_BLUE_3, 82 | LED_BLUE_4, 83 | LED_GREEN_1, 84 | LED_GREEN_2, 85 | LED_YELLOW, 86 | LED_RED 87 | ) 88 | 89 | DIGITAL_DISPLAY = ( 90 | DIGITAL_DISPLAY_A, 91 | DIGITAL_DISPLAY_B, 92 | DIGITAL_DISPLAY_C, 93 | DIGITAL_DISPLAY_D, 94 | DIGITAL_DISPLAY_E, 95 | DIGITAL_DISPLAY_F, 96 | DIGITAL_DISPLAY_G, 97 | DIGITAL_DISPLAY_DP 98 | ) 99 | 100 | DIGITAL_DISPLAY_SELECT = ( 101 | DIGITAL_DISPLAY_SELECET_1, 102 | DIGITAL_DISPLAY_SELECET_2, 103 | DIGITAL_DISPLAY_SELECET_3, 104 | DIGITAL_DISPLAY_SELECET_4, 105 | ) 106 | -------------------------------------------------------------------------------- /sakshat.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | # 4 | # Copyright (c) 2016 NXEZ.COM. 5 | # http://www.nxez.com 6 | # 7 | # Licensed under the GNU General Public License, Version 2.0 (the "License"); 8 | # you may not use this file except in compliance with the License. 9 | # You may obtain a copy of the License at 10 | # 11 | # http://www.gnu.org/licenses/gpl-2.0.html 12 | # 13 | # Unless required by applicable law or agreed to in writing, software 14 | # distributed under the License is distributed on an "AS IS" BASIS, 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | 19 | __author__ = 'Spoony' 20 | __version__ = 'version 0.0.1' 21 | __license__ = 'Copyright (c) 2016 NXEZ.COM' 22 | 23 | import RPi.GPIO as GPIO 24 | from sakspins import SAKSPins as PINS 25 | import entities 26 | 27 | class SAKSHAT(object): 28 | ''' 29 | SAKS HAT class, some useful function are declared. 30 | ''' 31 | buzzer = None 32 | ledrow = None 33 | ds18b20 = None 34 | digital_display = None 35 | dip_switch = None 36 | tactrow = None 37 | 38 | def saks_gpio_init(self): 39 | #print 'saks_gpio_init' 40 | GPIO.setwarnings(False) 41 | GPIO.cleanup() 42 | GPIO.setmode(GPIO.BCM) 43 | 44 | GPIO.setup(PINS.BUZZER, GPIO.OUT) 45 | GPIO.output(PINS.BUZZER, GPIO.HIGH) 46 | 47 | for p in [PINS.IC_TM1637_DI, PINS.IC_TM1637_CLK, PINS.IC_74HC595_DS, PINS.IC_74HC595_SHCP, PINS.IC_74HC595_STCP]: 48 | GPIO.setup(p, GPIO.OUT) 49 | GPIO.output(p, GPIO.LOW) 50 | 51 | for p in [PINS.BUZZER, PINS.TACT_RIGHT, PINS.TACT_LEFT, PINS.DIP_SWITCH_1, PINS.DIP_SWITCH_2]: 52 | GPIO.setup(p, GPIO.OUT) 53 | GPIO.output(p, GPIO.HIGH) 54 | 55 | for p in [PINS.TACT_RIGHT, PINS.TACT_LEFT, PINS.DIP_SWITCH_1, PINS.DIP_SWITCH_2]: 56 | GPIO.setup(p, GPIO.IN, pull_up_down = GPIO.PUD_UP) 57 | 58 | def __init__(self): 59 | self.saks_gpio_init() 60 | self.buzzer = entities.Buzzer(PINS.BUZZER, GPIO.LOW) 61 | self.ledrow = entities.Led74HC595({'ds': PINS.IC_74HC595_DS, 'shcp': PINS.IC_74HC595_SHCP, 'stcp': PINS.IC_74HC595_STCP}, GPIO.HIGH) 62 | self.ds18b20 = entities.DS18B20(PINS.DS18B20) 63 | self.digital_display = entities.DigitalDisplayTM1637({'di': PINS.IC_TM1637_DI, 'clk': PINS.IC_TM1637_CLK}, GPIO.HIGH) 64 | self.dip_switch = entities.DipSwitch2Bit([PINS.DIP_SWITCH_1, PINS.DIP_SWITCH_2], GPIO.LOW) 65 | self.dip_switch.register(self) 66 | self.tactrow = entities.TactRow([PINS.TACT_LEFT, PINS.TACT_RIGHT], GPIO.LOW) 67 | for t in self.tactrow.items: 68 | t.register(self) 69 | 70 | #print('self.dip_switch.register(self)') 71 | 72 | dip_switch_status_changed_handler = None 73 | def on_dip_switch_2bit_status_changed(self, status): 74 | #print('on_dip_switch_2bit_status_changed') 75 | if self.dip_switch_status_changed_handler is not None: 76 | self.dip_switch_status_changed_handler(status) 77 | 78 | tact_event_handler = None 79 | def on_tact_event(self, pin, status): 80 | #print('on_tact_event') 81 | if self.tact_event_handler is not None: 82 | self.tact_event_handler(pin, status) 83 | -------------------------------------------------------------------------------- /sakspins.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | # 4 | # Copyright (c) 2016 NXEZ.COM. 5 | # http://www.nxez.com 6 | # 7 | # Licensed under the GNU General Public License, Version 2.0 (the "License"); 8 | # you may not use this file except in compliance with the License. 9 | # You may obtain a copy of the License at 10 | # 11 | # http://www.gnu.org/licenses/gpl-2.0.html 12 | # 13 | # Unless required by applicable law or agreed to in writing, software 14 | # distributed under the License is distributed on an "AS IS" BASIS, 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | 19 | class SAKSPins(object): 20 | ''' 21 | SAKS Pins Code With BCM for Raspberry Pi. 22 | ''' 23 | 24 | IC_74HC595_DS = 6 25 | IC_74HC595_SHCP = 19 26 | IC_74HC595_STCP = 13 27 | 28 | IC_TM1637_DI = 25 29 | IC_TM1637_CLK = 5 30 | 31 | BUZZER = 12 32 | 33 | TACT_RIGHT = 20 34 | TACT_LEFT = 16 35 | DIP_SWITCH_1 = 21 36 | DIP_SWITCH_2 = 26 37 | 38 | IR_SENDER = 17 39 | IR_RECEIVER = 9 40 | DS18B20 = 4 41 | UART_TXD = 14 42 | UART_RXD = 15 43 | I2C_SDA = 2 44 | I2C_SLC = 3 --------------------------------------------------------------------------------