├── .gitignore ├── LICENSE ├── README.md ├── build_docs.sh ├── docs ├── .html ├── clj-bots.hello-world.blink.html ├── clj-bots.hello-world.custom-mappings.html ├── clj-bots.hello-world.sim.html ├── clj-bots.pin-ctrl-bbb.implementation.html ├── clj-bots.pin-ctrl-simulator.implementation.html ├── clj-bots.pin-ctrl.html ├── clj-bots.pin-ctrl.implementation.html ├── clj-bots.pin-ctrl.protocols.html ├── clj-bots.pin-ctrl.state-wrapper.html ├── styles.css ├── toc.html └── uberdoc.html ├── project.clj ├── resources ├── bbb-pin-mappings.csv ├── bbb-pin-mappings.edn └── rpi-pin-mappings.edn ├── src └── clj_bots │ ├── hello_world │ ├── blink.clj │ ├── custom_mappings.clj │ └── sim.clj │ ├── pin_ctrl.clj │ ├── pin_ctrl │ ├── channels │ │ └── core.clj │ ├── implementation.clj │ ├── protocols.clj │ └── state_wrapper.clj │ ├── pin_ctrl_bbb │ └── implementation.clj │ ├── pin_ctrl_rpi │ └── implementation.clj │ └── pin_ctrl_simulator │ └── implementation.clj └── test └── clj_bots └── pin_ctrl_test.clj /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /classes 3 | /checkouts 4 | pom.xml 5 | pom.xml.asc 6 | *.jar 7 | *.class 8 | /.lein-* 9 | /.nrepl-port 10 | .hgignore 11 | .hg/ 12 | ignore 13 | .floo 14 | .floobits 15 | .floobits 16 | .flooignore 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC 2 | LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM 3 | CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. 4 | 5 | 1. DEFINITIONS 6 | 7 | "Contribution" means: 8 | 9 | a) in the case of the initial Contributor, the initial code and 10 | documentation distributed under this Agreement, and 11 | 12 | b) in the case of each subsequent Contributor: 13 | 14 | i) changes to the Program, and 15 | 16 | ii) additions to the Program; 17 | 18 | where such changes and/or additions to the Program originate from and are 19 | distributed by that particular Contributor. A Contribution 'originates' from 20 | a Contributor if it was added to the Program by such Contributor itself or 21 | anyone acting on such Contributor's behalf. Contributions do not include 22 | additions to the Program which: (i) are separate modules of software 23 | distributed in conjunction with the Program under their own license 24 | agreement, and (ii) are not derivative works of the Program. 25 | 26 | "Contributor" means any person or entity that distributes the Program. 27 | 28 | "Licensed Patents" mean patent claims licensable by a Contributor which are 29 | necessarily infringed by the use or sale of its Contribution alone or when 30 | combined with the Program. 31 | 32 | "Program" means the Contributions distributed in accordance with this 33 | Agreement. 34 | 35 | "Recipient" means anyone who receives the Program under this Agreement, 36 | including all Contributors. 37 | 38 | 2. GRANT OF RIGHTS 39 | 40 | a) Subject to the terms of this Agreement, each Contributor hereby grants 41 | Recipient a non-exclusive, worldwide, royalty-free copyright license to 42 | reproduce, prepare derivative works of, publicly display, publicly perform, 43 | distribute and sublicense the Contribution of such Contributor, if any, and 44 | such derivative works, in source code and object code form. 45 | 46 | b) Subject to the terms of this Agreement, each Contributor hereby grants 47 | Recipient a non-exclusive, worldwide, royalty-free patent license under 48 | Licensed Patents to make, use, sell, offer to sell, import and otherwise 49 | transfer the Contribution of such Contributor, if any, in source code and 50 | object code form. This patent license shall apply to the combination of the 51 | Contribution and the Program if, at the time the Contribution is added by the 52 | Contributor, such addition of the Contribution causes such combination to be 53 | covered by the Licensed Patents. The patent license shall not apply to any 54 | other combinations which include the Contribution. No hardware per se is 55 | licensed hereunder. 56 | 57 | c) Recipient understands that although each Contributor grants the licenses 58 | to its Contributions set forth herein, no assurances are provided by any 59 | Contributor that the Program does not infringe the patent or other 60 | intellectual property rights of any other entity. Each Contributor disclaims 61 | any liability to Recipient for claims brought by any other entity based on 62 | infringement of intellectual property rights or otherwise. As a condition to 63 | exercising the rights and licenses granted hereunder, each Recipient hereby 64 | assumes sole responsibility to secure any other intellectual property rights 65 | needed, if any. For example, if a third party patent license is required to 66 | allow Recipient to distribute the Program, it is Recipient's responsibility 67 | to acquire that license before distributing the Program. 68 | 69 | d) Each Contributor represents that to its knowledge it has sufficient 70 | copyright rights in its Contribution, if any, to grant the copyright license 71 | set forth in this Agreement. 72 | 73 | 3. REQUIREMENTS 74 | 75 | A Contributor may choose to distribute the Program in object code form under 76 | its own license agreement, provided that: 77 | 78 | a) it complies with the terms and conditions of this Agreement; and 79 | 80 | b) its license agreement: 81 | 82 | i) effectively disclaims on behalf of all Contributors all warranties and 83 | conditions, express and implied, including warranties or conditions of title 84 | and non-infringement, and implied warranties or conditions of merchantability 85 | and fitness for a particular purpose; 86 | 87 | ii) effectively excludes on behalf of all Contributors all liability for 88 | damages, including direct, indirect, special, incidental and consequential 89 | damages, such as lost profits; 90 | 91 | iii) states that any provisions which differ from this Agreement are offered 92 | by that Contributor alone and not by any other party; and 93 | 94 | iv) states that source code for the Program is available from such 95 | Contributor, and informs licensees how to obtain it in a reasonable manner on 96 | or through a medium customarily used for software exchange. 97 | 98 | When the Program is made available in source code form: 99 | 100 | a) it must be made available under this Agreement; and 101 | 102 | b) a copy of this Agreement must be included with each copy of the Program. 103 | 104 | Contributors may not remove or alter any copyright notices contained within 105 | the Program. 106 | 107 | Each Contributor must identify itself as the originator of its Contribution, 108 | if any, in a manner that reasonably allows subsequent Recipients to identify 109 | the originator of the Contribution. 110 | 111 | 4. COMMERCIAL DISTRIBUTION 112 | 113 | Commercial distributors of software may accept certain responsibilities with 114 | respect to end users, business partners and the like. While this license is 115 | intended to facilitate the commercial use of the Program, the Contributor who 116 | includes the Program in a commercial product offering should do so in a 117 | manner which does not create potential liability for other Contributors. 118 | Therefore, if a Contributor includes the Program in a commercial product 119 | offering, such Contributor ("Commercial Contributor") hereby agrees to defend 120 | and indemnify every other Contributor ("Indemnified Contributor") against any 121 | losses, damages and costs (collectively "Losses") arising from claims, 122 | lawsuits and other legal actions brought by a third party against the 123 | Indemnified Contributor to the extent caused by the acts or omissions of such 124 | Commercial Contributor in connection with its distribution of the Program in 125 | a commercial product offering. The obligations in this section do not apply 126 | to any claims or Losses relating to any actual or alleged intellectual 127 | property infringement. In order to qualify, an Indemnified Contributor must: 128 | a) promptly notify the Commercial Contributor in writing of such claim, and 129 | b) allow the Commercial Contributor tocontrol, and cooperate with the 130 | Commercial Contributor in, the defense and any related settlement 131 | negotiations. The Indemnified Contributor may participate in any such claim 132 | at its own expense. 133 | 134 | For example, a Contributor might include the Program in a commercial product 135 | offering, Product X. That Contributor is then a Commercial Contributor. If 136 | that Commercial Contributor then makes performance claims, or offers 137 | warranties related to Product X, those performance claims and warranties are 138 | such Commercial Contributor's responsibility alone. Under this section, the 139 | Commercial Contributor would have to defend claims against the other 140 | Contributors related to those performance claims and warranties, and if a 141 | court requires any other Contributor to pay any damages as a result, the 142 | Commercial Contributor must pay those damages. 143 | 144 | 5. NO WARRANTY 145 | 146 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON 147 | AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER 148 | EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR 149 | CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A 150 | PARTICULAR PURPOSE. Each Recipient is solely responsible for determining the 151 | appropriateness of using and distributing the Program and assumes all risks 152 | associated with its exercise of rights under this Agreement , including but 153 | not limited to the risks and costs of program errors, compliance with 154 | applicable laws, damage to or loss of data, programs or equipment, and 155 | unavailability or interruption of operations. 156 | 157 | 6. DISCLAIMER OF LIABILITY 158 | 159 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY 160 | CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, 161 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION 162 | LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 163 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 164 | ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE 165 | EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY 166 | OF SUCH DAMAGES. 167 | 168 | 7. GENERAL 169 | 170 | If any provision of this Agreement is invalid or unenforceable under 171 | applicable law, it shall not affect the validity or enforceability of the 172 | remainder of the terms of this Agreement, and without further action by the 173 | parties hereto, such provision shall be reformed to the minimum extent 174 | necessary to make such provision valid and enforceable. 175 | 176 | If Recipient institutes patent litigation against any entity (including a 177 | cross-claim or counterclaim in a lawsuit) alleging that the Program itself 178 | (excluding combinations of the Program with other software or hardware) 179 | infringes such Recipient's patent(s), then such Recipient's rights granted 180 | under Section 2(b) shall terminate as of the date such litigation is filed. 181 | 182 | All Recipient's rights under this Agreement shall terminate if it fails to 183 | comply with any of the material terms or conditions of this Agreement and 184 | does not cure such failure in a reasonable period of time after becoming 185 | aware of such noncompliance. If all Recipient's rights under this Agreement 186 | terminate, Recipient agrees to cease use and distribution of the Program as 187 | soon as reasonably practicable. However, Recipient's obligations under this 188 | Agreement and any licenses granted by Recipient relating to the Program shall 189 | continue and survive. 190 | 191 | Everyone is permitted to copy and distribute copies of this Agreement, but in 192 | order to avoid inconsistency the Agreement is copyrighted and may only be 193 | modified in the following manner. The Agreement Steward reserves the right to 194 | publish new versions (including revisions) of this Agreement from time to 195 | time. No one other than the Agreement Steward has the right to modify this 196 | Agreement. The Eclipse Foundation is the initial Agreement Steward. The 197 | Eclipse Foundation may assign the responsibility to serve as the Agreement 198 | Steward to a suitable separate entity. Each new version of the Agreement will 199 | be given a distinguishing version number. The Program (including 200 | Contributions) may always be distributed subject to the version of the 201 | Agreement under which it was received. In addition, after a new version of 202 | the Agreement is published, Contributor may elect to distribute the Program 203 | (including its Contributions) under the new version. Except as expressly 204 | stated in Sections 2(a) and 2(b) above, Recipient receives no rights or 205 | licenses to the intellectual property of any Contributor under this 206 | Agreement, whether expressly, by implication, estoppel or otherwise. All 207 | rights in the Program not expressly granted under this Agreement are 208 | reserved. 209 | 210 | This Agreement is governed by the laws of the State of New York and the 211 | intellectual property laws of the United States of America. No party to this 212 | Agreement will bring a legal action under this Agreement more than one year 213 | after the cause of action arose. Each party waives its rights to a jury trial 214 | in any resulting litigation. 215 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # pin-ctrl 2 | 3 | Clojure library providing an abstraction layer for dealing with hardware and robotics pins (both analog and digital). 4 | 5 | ## Structure 6 | 7 | There are two fundamental concepts to the library: 8 | 9 | * `pin` - for dealing with pin state 10 | * `board` - for dealing with board state, and pin mappings and information at the board level. 11 | 12 | ## Implementations 13 | 14 | By itself, pin-ctrl does nothing. 15 | Because it is only an API abstraction layer, an implementation of the protocols herein is required for any functionality. 16 | These implementations are generally board specific, and so there will be separate libraries such as `clj-bots/pin-ctrl-firmata` and `clj-bots/pin-ctrl-rpi`, etc for each of the devices this may run on. 17 | 18 | However, while eventually these libraries will be separate, during active development, we'll be including them as separate namespaces here in this project. 19 | Once the API settles, we'll split them off. 20 | 21 | ## Status 22 | 23 | As mentioned above, this project is still in early alpha, and is likely to change and be buggy. 24 | However, the core API seems to be at least vaguely working with the simulation implementation. 25 | You can test this out for yourself by investigating and running the `clj-bots.hello-world.sim` namespace. 26 | This has some example usage of the simulator to see how things roughly work. 27 | 28 | Other implementations will be coming fully online soon. 29 | 30 | ## Usage 31 | 32 | For some usage sketches, please see the above mentioned `clj-bots.hello-world.sim` and other namespaces within `clj-bots.hello-world`. 33 | Additionally, take a look at the [Marginalia documentation](https://clj-bots.github.io/pin-ctrl) for a more complete overview of the public API. 34 | This documentation will be getting more fully fleshed out as the library matures. 35 | 36 | 37 | ## License 38 | 39 | Copyright © 2015 FIXME 40 | 41 | Distributed under the Eclipse Public License either version 1.0 or (at 42 | your option) any later version. 43 | 44 | -------------------------------------------------------------------------------- /build_docs.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | #lein marg -c styles.css 4 | lein marg -c styles.css -m src/clj_bots/pin_ctrl.clj src/clj_bots/pin_ctrl_simulator/implementation.clj src/clj_bots/pin_ctrl/implementation.clj src/clj_bots/pin_ctrl/protocols.clj src/clj_bots/pin_ctrl/state_wrapper.clj src/clj_bots/hello_world/sim.clj 5 | 6 | -------------------------------------------------------------------------------- /docs/styles.css: -------------------------------------------------------------------------------- 1 | 2 | body table tbody tr td.docs { 3 | width: 50%; 4 | } 5 | 6 | body table tbody tr td.docs code { 7 | font-size: 12px; 8 | } 9 | 10 | body table tbody tr td.docs pre code { 11 | overflow-x: auto; 12 | } 13 | -------------------------------------------------------------------------------- /project.clj: -------------------------------------------------------------------------------- 1 | (defproject clj-bots/pin-ctrl "0.1.0-SNAPSHOT" 2 | :description "Abstraction layer for hardware programming" 3 | :url "http://github.com/clj-bots/pin-ctrl" 4 | :license {:name "Eclipse Public License" 5 | :url "http://www.eclipse.org/legal/epl-v10.html"} 6 | :plugins [[lein-marginalia "0.8.0"]] 7 | :dependencies [[org.clojure/clojure "1.6.0"] 8 | [org.clojure/core.async "0.1.346.0-17112a-alpha"] 9 | [slingshot "0.12.2"] 10 | ; ## Here are dependencies specific to each of the individual implementations 11 | ; rpi dependencies 12 | ; bbb dependencies 13 | ; onboard-gpio-common dependencies 14 | [clj-gpio "0.1.0-SNAPSHOT"] 15 | ; firmata dependencies 16 | [clj-firmata "2.0.2-SNAPSHOT"] 17 | ]) 18 | -------------------------------------------------------------------------------- /resources/bbb-pin-mappings.csv: -------------------------------------------------------------------------------- 1 | #pins 11-21 are used by emmc,,,,,,,,,,, 2 | #27-46 are used by hdmi,,,,,,,,,,, 3 | header,pin,proc,name,reserved,mode0,mode1,mode2,mode3,mode4,mode6,mode7 4 | P8,1,GND,GND,,,,,,,, 5 | P8,2,GND,GND,,,,,,,, 6 | P8,3,R9,GPIO1_6,,gpmc_ad6,mmc1_dat6, , , , ,gpio1[6] 7 | P8,4,T9,GPIO1_7,,gpmc_ad7,mmc1_dat7, , , , ,gpio1[7] 8 | P8,5,R8,GPIO1_2,,gpmc_ad2,mmc1_dat2, , , , ,gpio1[2] 9 | P8,6,T8,GPIO1_3,,gpmc_ad3,mmc1_dat3, , , , ,gpio1[3] 10 | P8,7,R7,TIMER4,,gpmc_advn_ale, ,timer4, , , ,gpio2[2] 11 | P8,8,T7,TIMER7,,gpmc_oen_ren, ,timer7, , , ,gpio2[3] 12 | P8,9,T6,TIMER5,,gpmc_be0n_cle, ,timer5, , , ,gpio2[5] 13 | P8,10,U6,TIMER6,,gpmc_wen, ,timer6, , , ,gpio2[4] 14 | P8,11,R12,GPIO1_13,TRUE,gpmc_ad13,lcd_data18,mmc1_dat5*,mmc2_dat1,eQEP2B_in, ,gpio1[13] 15 | P8,12,T12,GPIO1_12,TRUE,gpmc_ad12,lcd_data19,mmc1_dat4*,mmc2_dat0,EQEP2A_IN, ,gpio1[12] 16 | P8,13,T10,EHRPWM2B,TRUE,gpmc_ad9,lcd_data22,mmc1_dat1*,mmc2_dat5,ehrpwm2B, ,gpio0[23] 17 | P8,14,T11,GPIO0_26,TRUE,gpmc_ad10,lcd_data21,mmc1_dat2*,mmc2_dat6,ehrpwm2_tripzone, ,gpio0[26] 18 | P8,15,U13,GPIO1_15,TRUE,gpmc_ad15,lcd_data16,mmc1_dat7*,mmc2_dat3,eQEP2_strobe, ,gpio1[15] 19 | P8,16,V13,GPIO1_14,TRUE,gpmc_ad14,lcd_data17,mmc1_dat6*,mmc2_dat2,eQEP2_index, ,gpio1[14] 20 | P8,17,U12,GPIO0_27,TRUE,gpmc_ad11,lcd_data20,mmc1_dat3*,mmc2_dat7,ehrpwm0_synco, ,gpio0[27] 21 | P8,18,V12,GPIO2_1,,gpmc_clk_mux0,lcd_memory_clk,gpmc_wait1,mmc2_clk, ,mcasp0_fsr,gpio2[1] 22 | P8,19,U10,EHRPWM2A,TRUE,gpmc_ad8,lcd_data23,mmc1_dat0*,mmc2_dat4,ehrpwm2A, ,gpio0[22] 23 | P8,20,V9,GPIO1_31,TRUE,gpmc_csn2,gpmc_be1n,mmc1_cmd*, , , ,gpio1[31] 24 | P8,21,U9,GPIO1_30,TRUE,gpmc_csn1,gpmc_clk,mmc1_clk*, , , ,gpio1[30] 25 | P8,22,V8,GPIO1_5,,gpmc_ad5,mmc1_dat5, , , , ,gpio1[5] 26 | P8,23,U8,GPIO1_4,,gpmc_ad4,mmc1_dat4, , , , ,gpio1[4] 27 | P8,24,V7,GPIO1_1,,gpmc_ad1,mmc1_dat1, , , , ,gpio1[1] 28 | P8,25,U7,GPIO1_0,,gpmc_ad0,mmc1_dat0, , , , ,gpio1[0] 29 | P8,26,V6,GPIO1_29,,gpmc_csn0, , , , , ,gpio1[29] 30 | P8,27,U5,GPIO2_22,TRUE,lcd_vsync*,gpmc_a8, , , , ,gpio2[22] 31 | P8,28,V5,GPIO2_24,TRUE,lcd_pclk*,gpmc_a10, , , , ,gpio2[24] 32 | P8,29,R5,GPIO2_23,TRUE,lcd_hsync*,gpmc_a9, , , , ,gpio2[23] 33 | P8,30,R6,GPIO2_25,TRUE,lcd_ac_bias_en*,gpmc_a11, , , , ,gpio2[25] 34 | P8,31,V4,UART5_CTSN,TRUE,lcd_data14*,gpmc_a18,eQEP1_index,mcasp0_axr1,uart5_rxd,uart5_ctsn,gpio0[10] 35 | P8,32,T5,UART5_RTSN,TRUE,lcd_data15*,gpmc_a19,eQEP1_strobe,mcasp0_ahclkx,mcasp0_axr3,uart5_rtsn,gpio0[11] 36 | P8,33,V3,UART4_RTSN,TRUE,lcd_data13*,gpmc_a17,eQEP1B_in,mcasp0_fsr,mcasp0_axr3,uart4_rtsn,gpio0[9] 37 | P8,34,U4,UART3_RTSN,TRUE,lcd_data11*,gpmc_a15,ehrpwm1B,mcasp0_ahclkr,mcasp0_axr2,uart3_rtsn,gpio2[17] 38 | P8,35,V2,UART4_CTSN,TRUE,lcd_data12*,gpmc_a16,eQEP1A_in,mcasp0_aclkr,mcasp0_axr2,uart4_ctsn,gpio0[8] 39 | P8,36,U3,UART3_CTSN,TRUE,lcd_data10*,gpmc_a14,ehrpwm1A,mcasp0_axr0, ,uart3_ctsn,gpio2[16] 40 | P8,37,U1,UART5_TXD,TRUE,lcd_data8*,gpmc_a12,ehrpwm1_tripzone,mcasp0_aclkx,uart5_txd,uart2_ctsn,gpio2[14] 41 | P8,38,U2,UART5_RXD,TRUE,lcd_data9*,gpmc_a13,ehrpwm0_synco,mcasp0_fsx,uart5_rxd,uart2_rtsn,gpio2[15] 42 | P8,39,T3,GPIO2_12,TRUE,lcd_data6*,gpmc_a6, ,eQEP2_index, , ,gpio2[12] 43 | P8,40,T4,GPIO2_13,TRUE,lcd_data7*,gpmc_a7, ,eQEP2_strobe,pr1_edio_data_out7, ,gpio2[13] 44 | P8,41,T1,GPIO2_10,TRUE,lcd_data4*,gpmc_a4, ,eQEP2A_in, , ,gpio2[10] 45 | P8,42,T2,GPIO2_11,TRUE,lcd_data5*,gpmc_a5, ,eQEP2B_in, , ,gpio2[11] 46 | P8,43,R3,GPIO2_8,TRUE,lcd_data2*,gpmc_a2, ,ehrpwm2_tripzone, , ,gpio2[8] 47 | P8,44,R4,GPIO2_9,TRUE,lcd_data3*,gpmc_a3, ,ehrpwm0_synco, , ,gpio2[9] 48 | P8,45,R1,GPIO2_6,TRUE,lcd_data0*,gpmc_a0, ,ehrpwm2A, , ,gpio2[6] 49 | P8,46,R2,GPIO2_7,TRUE,lcd_data1*,gpmc_a1, ,ehrpwm2B, , ,gpio2[7] 50 | #Expansion Header P9 Pinout,,,,,,,,,,, 51 | P9,1,2,GND,,,,,,,, 52 | P9,3,4,DC_3.3V,,,,,,,, 53 | P9,5,6,VDD_5V,,,,,,,, 54 | P9,7,8,SYS_5V,,,,,,,, 55 | P9,9,PWR_BUT,,,,,,,,, 56 | P9,10,A10,RESET_OUT,, , , , , , , 57 | P9,11,T17,gpmc_wait0,,mii2_crs,gpmc_csn4,rmii2_crs_dv,mmc1_sdcd,uart4_rxd_mux2,gpio0[30], 58 | P9,12,U18,gpmc_be1n,,mii2_col,gpmc_csn6,mmc2_dat3,gpmc_dir,mcasp0_aclkr_mux3,gpio1[28], 59 | P9,13,U17,gpmc_wpn,,mii2_rxerr,gpmc_csn5,rmii2_rxerr,mmc2_sdcd,uart4_txd_mux2,gpio0[31], 60 | P9,14,U14,gpmc_a2,,mii2_txd3,rgmii2_td3,mmc2_dat1,gpmc_a18,ehrpwm1A_mux1,gpio1[18], 61 | P9,15,R13,gpmc_a0,,gmii2_txen,rmii2_tctl,mii2_txen,gpmc_a16,ehrpwm1_tripzone,gpio1[16], 62 | P9,16,T14,gpmc_a3,,mii2_txd2,rgmii2_td2,mmc2_dat2,gpmc_a19,ehrpwm1B_mux1,gpio1[19], 63 | P9,17,A16,spi0_cs0,,mmc2_sdwp,I2C1_SCL,ehrpwm0_synci, , ,gpio0[5], 64 | P9,18,B16,spi0_d1,,mmc1_sdwp,I2C1_SDA,ehrpwm0_tripzone, , ,gpio0[4], 65 | P9,19,D17,uart1_rtsn,,timer5,dcan0_rx,I2C2_SCL,spi1_cs1, ,gpio0[13], 66 | P9,20,D18,uart1_ctsn,,timer6,dcan0_tx,I2C2_SDA,spi1_cs0, ,gpio0[12], 67 | P9,21,B17,spi0_d0,,uart2_txd,I2C2_SCL,ehrpwm0B, ,EMU3_mux1,gpio0[3], 68 | P9,22,A17,spi0_sclk,,uart2_rxd,I2C2_SDA,ehrpwm0A, ,EMU2_mux1,gpio0[2], 69 | P9,23,V14,gpmc_a1,,gmii2_rxdv,rgmii2_rxdv,mmc2_dat0,gpmc_a17,ehrpwm0_synco,gpio1[17], 70 | P9,24,D15,uart1_txd,,mmc2_sdwp,dcan1_rx,I2C1_SCL, , ,gpio0[15], 71 | P9,25,A14,mcasp0_ahclkx,,eQEP0_strobe,mcasp0_axr3,mcasp1_axr1,EMU4_mux2, ,gpio3[21], 72 | P9,26,D16,uart1_rxd,,mmc1_sdwp,dcan1_tx,I2C1_SDA, , ,gpio0[14], 73 | P9,27,C13,mcasp0_fsr,,eQEP0B_in,mcasp0_axr3,mcasp1_fsx,EMU2_mux2, ,gpio3[19], 74 | P9,28,C12,mcasp0_ahclkr,,ehrpwm0_synci,mcasp0_axr2,spi1_cs0,eCAP2_in_PWM2_out, ,gpio3[17], 75 | P9,29,B13,mcasp0_fsx,,ehrpwm0B, ,spi1_d0,mmc1_sdcd_mux1, ,gpio3[15], 76 | P9,30,D12,mcasp0_axr0,,ehrpwm0_tripzone, ,spi1_d1,mmc2_sdcd_mux1, ,gpio3[16], 77 | P9,31,A13,mcasp0_aclkx,,ehrpwm0A, ,spi1_sclk,mmc0_sdcd_mux1, ,gpio3[14], 78 | P9,32, ,VADC,,,,,,,, 79 | P9,33,C8,AIN4,,,,,,,, 80 | P9,34, ,AGND,,,,,,,, 81 | P9,35,A8,AIN6,,,,,,,, 82 | P9,36,B8,AIN5,,,,,,,, 83 | P9,37,B7,AIN2,,,,,,,, 84 | P9,38,A7,AIN3,,,,,,,, 85 | P9,39,B6,AIN0,,,,,,,, 86 | P9,40,C7,AIN1,,,,,,,, 87 | P9,41#,D14,xdma_event_intr1,, ,tclkin,clkout2,timer7_mux1,EMU3_mux0,gpio0[20], 88 | P9,D13,mcasp0_axr1,eQEP0_index,, ,Mcasp1_axr0,emu3, ,gpio3[20],, 89 | P9,42@,C18,eCAP0_in_PWM0_out,,uart3_txd,spi1_cs1,pr1_ecap0_ecap,,,, 90 | P9,_capin_apwm_o,spi1_sclk,xdma_event_intr2,,gpio0[7],,,,,, 91 | P9,B12,Mcasp0_aclkr,eQEP0A_in,,Mcaspo_axr2,Mcasp1_aclkx, , ,gpio3[18],, 92 | -------------------------------------------------------------------------------- /resources/bbb-pin-mappings.edn: -------------------------------------------------------------------------------- 1 | {:board-class :onboard 2 | :board-type :bbb 3 | :pins [[:P8 11] [:P8 12] [:P8 13] 4 | [:P9 1] [:P9 2] [:P9 3]] 5 | :pin-modes {[:P8 11] [:gpio] 6 | [:P8 12] [:gpio] 7 | [:P9 1] [:ain :pwm]} 8 | :gpio-mappigns {[:P8 11] 42 9 | [:P8 12] 11}} 10 | -------------------------------------------------------------------------------- /resources/rpi-pin-mappings.edn: -------------------------------------------------------------------------------- 1 | {:b1 2 | ; First, power pins 3 | {1 {:type :power 4 | :val :3.3v} 5 | 17 {:type :power 6 | :val :3.3v} 7 | 2 {:type :power 8 | :val :5v} 9 | 4 {:type :power 10 | :val :5v} 11 | ; Specifically ground pins 12 | 6 {:type :power 13 | :val :ground} 14 | 9 {:type :power 15 | :val :ground} 16 | 14 {:type :power 17 | :val :ground} 18 | 20 {:type :power 19 | :val :ground} 20 | 25 {:type :power 21 | :val :ground} 22 | 23 | ; i2c pins 24 | 3 {:type :i2c 25 | :gpio-pin 0} 26 | 5 {:type :i2c 27 | :gpio-pin 1} 28 | -------------------------------------------------------------------------------- /src/clj_bots/hello_world/blink.clj: -------------------------------------------------------------------------------- 1 | (ns clj-bots.hello-world.blink 2 | "# The hello world of hardware programming: a blinking LED" 3 | (:require [clj-bots.pin-ctrl :as pc] 4 | [clj-bots.pin-ctrl-bbb :as bbb])) 5 | 6 | ;; First we register the implementation. (Actually, it _should_ be the case that simply requiring the namespace 7 | ;; take care of this for us; there's also a way that requiring wouldn't be necessary, but it requires pin-ctrl 8 | ;; to have some knowledge of `known-implementations`. This is how `core.matrix` lets you just write something 9 | ;; like `(set-default-implementation! :vectorz)` without requiring or anything. Could do the same here, but it 10 | ;; needs we need to maintain a similar collection of known implementations.) 11 | (bbb/register-implementation) 12 | 13 | 14 | ;; Next we create a board object. This could be in a var, as we're doing here, or we could use something like 15 | ;; Component to manage its lifecycle. 16 | (def board (pc/create-board :bbb)) 17 | 18 | ;; Now let's say we want to initialize pin 14 on header :P8 as a GPIO pin, and use it to control an LED that we'll blink. 19 | ;; (We _could_ actually do this as `:P8_14` instead of `[:P8 14]`, but my thought is that the latter is a bit 20 | ;; more composable.). 21 | (def pin (pc/get-pin board [:P8 14])) 22 | (pc/set-mode! pin :gpio) 23 | 24 | ;; Now let's set up the blinking 25 | (def blink-thread 26 | (future 27 | (loop [] 28 | (pc/toggle! pin) 29 | (Thread/sleep 1000) 30 | (recur)))) 31 | 32 | ;; Yay! Let's bask in the glory for a few seconds and then turn the pin off 33 | (Thread/sleep 10000) 34 | (future-cancel blink-thread) 35 | (pc/set-mode! pin :off) 36 | 37 | ;; It's also possible to use the board to control pins directly. In fact, the functions which operate on the 38 | ;; Pin objects are actually just thin wrappers around the board object. This makes state management a lot 39 | ;; cleaner. 40 | (pc/set-mode! board [:P8 14] :gpio) 41 | 42 | (def blink-thread2 43 | (future 44 | (loop [] 45 | (pc/toggle! board [:P8 14]) 46 | (Thread/sleep 1000) 47 | (recur)))) 48 | 49 | (Thread/sleep 10000) 50 | (future-cancel blink-thread2) 51 | (pc/set-mode! board [:P8 14] :off) 52 | 53 | 54 | -------------------------------------------------------------------------------- /src/clj_bots/hello_world/custom_mappings.clj: -------------------------------------------------------------------------------- 1 | (ns clj-bots.hello-world.custom-mappings 2 | "# Demonstrating pin-ctrl's custom pin mappings" 3 | (:require [clj-bots.pin-ctrl :as pc] 4 | [clj-bots.pin-ctrl-rpi as rpi] 5 | [clj-bots.pin-ctrl-bbb as bbb])) 6 | 7 | ;; This feature was envisioned as a way of supporting cross device deployment. 8 | ;; The idea is that we would like a way of referring to specific pins abstractly, leaving the details of which 9 | ;; physical pin should be used on any given device up to the configuration of the user. 10 | 11 | ;; Say we want to fire off an LED and use GPIO pin `1` on the Pi, but but `[:P8 14]` on the BBB. 12 | ;; First, let's create a configuration map specifying all this. 13 | (def config 14 | ; Here are our BBB specific configurations 15 | {:bbb 16 | {:custom-mappings 17 | {:led [:P8 14]}} 18 | ; And now for the RPi 19 | :rpi 20 | {:custom-mappings 21 | {:led 1}}}) 22 | 23 | ;; Now we can simply use an environment variable to decide which kind of board we're using, and 24 | ;; subsequently, which pin configurations we'll be using. 25 | (def board-type (symbol (get (System/getenv) "BOARD_TYPE"))) 26 | 27 | ;; This should be either `:bbb` or `:rpi` 28 | 29 | ;; Now we can create the board with this configuration. 30 | (def board (create-board board-type (config board-type))) 31 | 32 | ;; Now that we've loaded our board with custom mappings, blinking the correct LED is agnostic to the 33 | ;; underlying board. 34 | (doseq [i (range 10)] 35 | (write-value! board :led :high) 36 | (Thread/sleep 500) 37 | (write-value! board :led :high) 38 | (Thread/sleep 500)) 39 | 40 | ;; In the code above, when we specify `:led` as the `pin-n` argument, it looks up the `:custom-mappings` 41 | ;; configuration we specified, and uses that to decide which actual pins to instantiate. 42 | ;; For this to make sense of course, there should be no key duplications between the keys of the 43 | ;; `:custom-mappings` and the standard pin mappings, which we should enforce in the API code. 44 | 45 | -------------------------------------------------------------------------------- /src/clj_bots/hello_world/sim.clj: -------------------------------------------------------------------------------- 1 | (ns clj-bots.hello-world.sim 2 | "# test of the simulator implementation" 3 | (:require [clj-bots.pin-ctrl :as pc] 4 | [clj-bots.pin-ctrl-simulator.implementation :as sim])) 5 | 6 | ;; Create a simulator board, vaguely modelling a small subset of a BBB's pins. 7 | (def board 8 | (pc/create-board :simulator 9 | {:board-class :onboard ; not too important yet 10 | :pin-modes {[:P8 14] [:gpio :ain] 11 | [:P8 15] [:ain :aout] 12 | [:P8 16] [:gpio]} 13 | :analog-bits {[:P8 14] 8 14 | [:P8 15] 8}})) 15 | 16 | ;; Now let's say we want to initialize pin 14 on header :P8 as a GPIO pin, and use it to control an LED that we'll blink. 17 | (def pin-n [:P8 14]) 18 | (def pin (pc/get-pin board pin-n)) 19 | (pc/set-mode! pin :output) 20 | 21 | 22 | ;; Now let's set up some blinking 23 | (def blink-thread 24 | (future 25 | (loop [] 26 | (pc/toggle! pin) 27 | (Thread/sleep 1000) 28 | (recur)))) 29 | 30 | ;; And while our write loop is running, let's monitor the state of the pin 31 | (let [interval 200 32 | duration 10000 33 | steps (/ duration interval)] 34 | (doseq [i (range steps)] 35 | (Thread/sleep 200) 36 | (println "Value at" (* i interval) (pc/read-value pin)))) 37 | 38 | ;; Now that we're done, let's close up shop. 39 | (future-cancel blink-thread) 40 | (pc/set-mode! pin :off) 41 | 42 | -------------------------------------------------------------------------------- /src/clj_bots/pin_ctrl.clj: -------------------------------------------------------------------------------- 1 | (ns clj-bots.pin-ctrl 2 | "# Public API for pin-ctrl" 3 | (:require [clj-bots.pin-ctrl 4 | [protocols :as p] 5 | [implementation :as impl] 6 | [state-wrapper :as sw]] 7 | [clojure.core.async :as async :refer [>!! ! analog-bits 26 | ;; remove-mult 27 | ;; updat set-edge! 28 | ;; 29 | ;; finshed: 30 | ;; read-value : arity taking mode 31 | ;; write-value : arity taking mode 32 | ;; added init! 33 | ;; changes: pin-modes -> available-modes 34 | ;; current-pin-modes -> pin-modes 35 | 36 | ;; # Board Implementation Protocols 37 | ;; 38 | ;; The following are the protocols for board implementations. 39 | ;; Those which are not required will be marked as such. 40 | 41 | (defprotocol PBoard 42 | "Basic board protocol, shared by any board, whether on board or over wire." 43 | (init! [this] "Do any necessary initialization. Not required. Should return the board.") 44 | (available-pin-modes [this] "Return a map of pin numbers to available pin modes.") 45 | ;; This should now be optional, with the default calling through to the recorded state 46 | (pin-modes [this] "Get the current pin mode values. Implementing this method is optional; default behaviour is to let the state wrapper track modes and return from there. Only implement this method if you want to manually implement functions that directly query the board for a pin's state.")) 47 | 48 | ; Need to have a good way of setting default nullary implementations of these 49 | (defprotocol POverwireBoard 50 | "Overwire boards are boards that run over the wire, like Arduino boards over Firmata. This 51 | protocol is for functions specific to these boards." 52 | (reset-board! [this] "Overwire boards, such as arduino boards over firmata, can be reset!")) 53 | 54 | (defprotocol PPinConfigure 55 | (set-mode! [board pin-n mode] "Set the mode of the pin, as long as it's supported by the pin's board.")) 56 | 57 | (defprotocol PReadablePin 58 | (read-value [board pin-n mode] "Read the binary or analog value of a pin with given mode. For gpio this should be :high or :low; for ain should be numeric between 0 and 1")) 59 | 60 | (defprotocol PWriteablePin 61 | (write-value! [board pin-n mode val] "Set the binary or analog value of a pin; for analog, should be the raw, non-normalized value.")) 62 | 63 | (defprotocol PEdgeDetectablePin 64 | "Edge detection allows efficient detection of GPIO state changes (such as from a button press). User recieves this information via callback fn." 65 | (set-edge! 66 | [board pin-n edge f] 67 | "Set the direction of the edge detection on a GPIO input pin. Should call the callback function f with the current state of the pin. Each time the 68 | function is called, previous callback functions should no longer be run. State management here is up to user.")) 69 | 70 | 71 | ;; # Stateful Board Wrapper Protocols 72 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 73 | ;; 74 | ;; The following protocols are not to be used for implementations proper. 75 | ;; They are for implementations of stateful boards, which is orthogonal to an implementation which takes care 76 | ;; of how things run on one type of board vs another. 77 | ;; We may eventually open things up so that stateful board wrappers could 78 | 79 | (defprotocol PStatefulPin 80 | "This Read and write without having to pass the mode" 81 | (stateful-read-value [board pin-n]) 82 | (stateful-write-value! [board pin-n val])) 83 | 84 | (defprotocol PChannelEdgeDetection 85 | (set-edge-chan! [board pin-n ch]) 86 | (get-edge-chan [board pin-n])) 87 | 88 | -------------------------------------------------------------------------------- /src/clj_bots/pin_ctrl/state_wrapper.clj: -------------------------------------------------------------------------------- 1 | (ns clj-bots.pin-ctrl.state-wrapper 2 | (:require [clj-bots.pin-ctrl.protocols :as pcp] 3 | [clojure.core.async :as async])) 4 | 5 | 6 | ;; We have this wrapper around the implementation boards. It also implements the board protocols, and defers 7 | ;; to the implementations for the juicy stuff. Really, the only job of this wrapper is to sanely handle state 8 | ;; _once_, so that individual implementations don't have to worry about it. The maintainence of state is 9 | ;; handled by a state-atom that is updated as the wrapped board does it's work. 10 | ;; 11 | ;; The state-atom should/can have the following keys: 12 | ;; {:modes "current mode settings for all the pins" 13 | ;; :custom-mappings "so you can do nice names like :led1" 14 | ;; :adapters "so you can create things like ain pins on pi, or software pwm; better drivers?"} 15 | ;; 16 | ;; We can also tease out any implementation specific config and pass that along. 17 | ;; Should these have to be registered by the implementations? 18 | ;; 19 | ;; This should also be the place we wrap things like validations so implementers don't have to worry about it. 20 | ;; So maybe this should be called something more general than `BoardWrapper`. 21 | 22 | (defrecord BoardWrapper [state-atom impl-board] 23 | pcp/PBoard 24 | (init! [board] 25 | (pcp/init! impl-board) 26 | board) 27 | (available-pin-modes [_] (pcp/available-pin-modes impl-board)) 28 | (pin-modes [_] 29 | ;; If pin-modes have been implemented, use that implementation 30 | (try 31 | (pcp/pin-modes impl-board) 32 | (catch Exception e 33 | (:pin-modes @state-atom)))) 34 | 35 | pcp/POverwireBoard 36 | (reset-board! [_] (pcp/reset-board! impl-board)) 37 | 38 | pcp/PPinConfigure 39 | (set-mode! [_ pin-n mode] 40 | (pcp/set-mode! impl-board pin-n mode) 41 | (swap! state-atom assoc pin-n mode)) 42 | 43 | pcp/PStatefulPin 44 | (stateful-read-value [board pin-n] 45 | (pcp/read-value impl-board (get (pcp/pin-modes board) pin-n) pin-n)) 46 | (stateful-write-value! [board pin-n val] 47 | (pcp/write-value! impl-board (get (pcp/pin-modes board) pin-n) pin-n val)) 48 | 49 | pcp/PEdgeDetectablePin 50 | (set-edge! [_ pin-n edge f] 51 | (pcp/set-edge! impl-board pin-n edge f))) 52 | 53 | 54 | (defn new-board-wrapper 55 | [impl-board] 56 | (BoardWrapper. (atom {}) impl-board)) 57 | 58 | 59 | -------------------------------------------------------------------------------- /src/clj_bots/pin_ctrl_bbb/implementation.clj: -------------------------------------------------------------------------------- 1 | (ns clj-bots.pin-ctrl-bbb.implementation 2 | (:require [gpio.core :as gpio] 3 | [clj-bots.pin-ctrl 4 | [protocols :as pcp] 5 | [implementation :as impl]] 6 | [clojure.java.io :as io])) 7 | 8 | 9 | (defrecord Board [pin-mapping config] 10 | pcp/PBoard 11 | (pin-modes [_] 12 | {:todo :XXX}) 13 | (get-config [this] 14 | config) 15 | ; XXX - Hmmm... do we actually need this? 16 | (update-config [this f] 17 | (swap! config f))) 18 | 19 | 20 | (let [inner-fn 21 | (memoize 22 | (fn [] 23 | (read-string (slurp (clojure.java.io/resource "bbb-pin-mappings.edn")))))] 24 | ; XXX - Hmm; should call this something else either here or in the Implementation protocol 25 | (defn- get-pin-mappings 26 | "Get pin mappings from configuration edn file" 27 | [] 28 | ((inner-fn)))) 29 | 30 | 31 | (defn new-board 32 | [config] 33 | (Board. (get-pin-mappings) config)) 34 | 35 | 36 | (def implementation 37 | (reify 38 | Object 39 | (toString [_] 40 | "") 41 | pcp/PPinCtrlImplementation 42 | (create-board [_ config] 43 | (Board. (get-pin-mappings) config)) 44 | (default-config [_] 45 | (get-pin-mappings)))) 46 | 47 | ;; This makes it possible to register the implementation by calling this function 48 | 49 | (defn register-implementation 50 | ([impl-key] 51 | (impl/register-implementation impl-key implementation)) 52 | ([] 53 | (register-implementation :bbb))) 54 | 55 | ;; Having this here means the implementation will get registered if this namespace is required 56 | 57 | (register-implementation) 58 | 59 | :OK 60 | 61 | -------------------------------------------------------------------------------- /src/clj_bots/pin_ctrl_rpi/implementation.clj: -------------------------------------------------------------------------------- 1 | (ns clj-bots.pin-ctrl-rpi.implementation 2 | "# Raspberry Pi pin-ctrl API implementation" 3 | (:require [clj-bots.pin-ctrl 4 | [protocols :as pcp] 5 | [implementation :as impl]] 6 | [clj-bots.pin-ctrl :as pc] 7 | [gpio.core :as gpio] 8 | [clojure.core.async :as async :refer [chan >!! ! go-loop]])) 9 | 10 | 11 | 12 | ;; First we'll set up some function declarations and multi-method signatures that we'll be using in the implementations. 13 | 14 | (declare writeable-pin? ok-val?) 15 | 16 | (defmulti read-pin* 17 | "Inner method for reading from a pin; dispatches on pin mode" 18 | (fn [board pin-n mode] mode)) 19 | 20 | (defmulti unset-mode* 21 | "Inner method for unsetting a pin mode, in particular for being run before moving to another mode or turned off" 22 | (fn [board pin-n mode] mode)) 23 | 24 | (defmulti set-mode* 25 | "Inner method for setting the mode of a pin on the board" 26 | (fn [board pin-n mode] mode)) 27 | 28 | (defmulti write-value* 29 | "Inner method for writing a value to a pin" 30 | (fn [board pin-n mode val] mode)) 31 | 32 | 33 | ;; Now let's hook these into the actual protocol implementations 34 | 35 | (defrecord RPiBoard 36 | [edge-channels config] 37 | pcp/PBoard 38 | (available-pin-modes [_] (:pin-modes config)) 39 | 40 | pcp/PPinConfigure 41 | (set-mode! [board pin-n mode] 42 | (set-mode* board pin-n mode)) 43 | 44 | pcp/PReadablePin 45 | (read-value [board pin-n mode] 46 | (read-pin* board pin-n mode)) 47 | 48 | pcp/PWriteablePin 49 | (write-value! [board pin-n mode val] 50 | (write-value* board pin-n val)) 51 | 52 | pcp/PEdgeDetectablePin 53 | (set-edge! [board pin-n edge f] 54 | (let [[chan-port edge-chan] 55 | (or (get @edge-channels pin-n) 56 | (let [chan-port (gpio/open-channel-port pin-n)] 57 | [chan-port (gpio/create-edge-channel chan-port)]))] 58 | (if (= :none edge) 59 | (do (async/close! edge-chan) 60 | (gpio/close! chan-port) 61 | (swap! edge-channels dissoc pin-n)) 62 | (do 63 | (gpio/set-direction! chan-port :in) 64 | (gpio/set-edge! chan-port edge) 65 | (go-loop [] 66 | (when-let [value (!! ! go-loop]])) 6 | 7 | 8 | ;; This stuff really needs to be part of the core library 9 | 10 | (def available-modes 11 | #{:input :output :ain :pwm}) 12 | 13 | ;; Here we're going to declare some of the things we'll need in the implementation that we'd rather leave at 14 | ;; the end of this namespace for logical flow. 15 | 16 | (declare pin-mode writeable-pin? ok-val?) 17 | 18 | ;; In addition to the standard protocol functions, we also need something which let's us set the state of 19 | ;; _input_ input pins for the purposes of simulation, since (for obvious reasons) this is not supported via 20 | ;; the standard protocols. 21 | 22 | (defprotocol PSimControl 23 | "Protocol for simulation control functions" 24 | (set-state! [this pin-n val] "Set the state of a digital or analog input pin")) 25 | 26 | 27 | ;; And now the implementation. 28 | 29 | (defrecord SimBoard 30 | [pin-state pin-modes edge-channels config] 31 | pcp/PBoard 32 | (init! [b] b) 33 | (available-pin-modes [_] (:pin-modes config)) 34 | (pin-modes [_] @pin-modes) 35 | 36 | pcp/POverwireBoard 37 | (reset-board! [_] 38 | (if (= (:board-class config) :overwire) 39 | (do (reset! pin-modes) 40 | (reset! pin-state)) 41 | (println "This option is not available for onboard boards"))) 42 | 43 | pcp/PPinConfigure 44 | (set-mode! [_ pin-n mode] 45 | (swap! pin-modes assoc pin-n mode)) 46 | 47 | pcp/PReadablePin 48 | (read-value [board pin-n mode] 49 | (get (deref (:pin-state board)) pin-n)) 50 | 51 | pcp/PWriteablePin 52 | (write-value! [board pin-n mode val] 53 | (swap! pin-state assoc pin-n val)) 54 | 55 | pcp/PEdgeDetectablePin 56 | (set-edge! [board pin-n edge ch] 57 | (let [match (case edge 58 | (:none "edge") #{} 59 | (:rising "rising") #{:high} 60 | (:falling "falling") #{:low} 61 | (:both "both") #{:high :low})] 62 | (add-watch pin-state 63 | ; May have to have board ids involved in this scheme for overwire; 64 | ; should have global registry of these board ids XXX 65 | (symbol (str "edge-detection-watch-simulator" pin-n)) 66 | (fn [_ _ _ new-val] 67 | (when (match new-val) 68 | (>!! ch new-val)))))) 69 | 70 | PSimControl 71 | (set-state! [board pin-n val] 72 | (assert (ok-val? board pin-n val) 73 | (str "The value " val " is not an acceptable value for pins of type " (pin-mode board pin-n))) 74 | (swap! pin-state assoc pin-n))) 75 | 76 | ;; Now we'll flesh out some of the various reading/writing functions 77 | 78 | (defn- pin-mode 79 | [board pin-n] 80 | (get (pcp/pin-modes board) pin-n)) 81 | 82 | (defn writeable-pin? 83 | [board pin-n] 84 | (#{:output :pwm} (pin-mode board pin-n))) 85 | 86 | (defn ok-val? 87 | [board pin-n val] 88 | (case (pin-mode board pin-n) 89 | :output (#{0 1 \0 \1 :high :low} val) 90 | :pwm #(and (<= 0 %) (>= 1 %)) 91 | false)) 92 | 93 | 94 | ;; How we create new boards 95 | 96 | (defn sim-board 97 | [config] 98 | (SimBoard. 99 | (atom {}) 100 | (atom {}) 101 | (atom {}) 102 | config)) 103 | 104 | (defn random-config 105 | [n-pins] 106 | {:pin-modes 107 | (into {} 108 | (for [i (range n-pins)] 109 | [[(rand-nth [:P8 :P9]) i] 110 | (filterv 111 | (fn [_] (> (rand) 0.68)) 112 | available-modes)]))}) 113 | 114 | 115 | ;; And register the implementation 116 | 117 | (def implementation 118 | (reify 119 | Object 120 | (toString [_] 121 | "") 122 | pcp/PPinCtrlImplementation 123 | (create-board [_ config] 124 | (sim-board config)) 125 | (default-config [_] 126 | (pcp/default-config 100)))) 127 | 128 | (impl/register-implementation :simulator implementation) 129 | 130 | 131 | -------------------------------------------------------------------------------- /test/clj_bots/pin_ctrl_test.clj: -------------------------------------------------------------------------------- 1 | (ns clj-bots.pin-ctrl-test 2 | (:require [clojure.test :refer :all] 3 | [clj-bots.pin-ctrl :refer :all])) 4 | 5 | (deftest a-test 6 | (testing "FIXME, I fail." 7 | (is (= 0 1)))) 8 | --------------------------------------------------------------------------------