In [1]:
(version)
Out[1]:
"8.3"
In [28]:
(require racket
         racket/list
         racket/format
         (prefix-in pict: pict))
In [3]:
(require bhdl)
#<procedure:parameter-procedure>
In [7]:
(require "BHDL-Key-lib.scm")
In [9]:
(define global
  (make-circuit
   (pin GND 3V3 5V VCC USB5V)))

The keyboard matrix

In [10]:
;; This is a mini-language defined in BHDL-Key.scm
(define-key-matrix matrix
  ([(esc 1.5)    1 2 3 4    5       -         -        6 7 8  9  0   (backspace 1.5)]
   [(tab 1.5)    Q  W  E  R     T        -         -        Y  U  I   O   P    (\\ 1.5)]
   [(caps 1.5)   A  S  D  F     G        (lmid1 1.5) (rmid1 1.5)        H  J  K   L   \;  (enter 1.5)]
   [(lshift 1.5) Z  X  C  V     B        (lmid2 1.5) (rmid2 1.5)        N  M  \, \. \/  (rshift 1.5)]
   [lctrl lfn lsuper lalt lTBD (lspace 2.75) - - (rspace 2.75) rTBD ralt rsuper rfn rctrl]))
"DEBUG: /var/folders/jd/pqbc6_7j0y5gc2kjxjntw8c40000gn/T/PKGS-src-ROOT-BHDL-syst----iVc-fpio-main.rkt-63-15_16388403191638840319607"
"DEBUG: /var/folders/jd/pqbc6_7j0y5gc2kjxjntw8c40000gn/T/PKGS-src-ROOT-BHDL-syst----iVc-fpio-main.rkt-63-15_16388403191638840319620"
In [11]:
kQ
Out[11]:
#<Composite-54291>
In [12]:
(show-layout kQ)
Out[12]:
In [13]:
;; FIXME the size is bigger. This might create problems
(show-layout klspace)
Out[13]:
In [15]:
(define matrix-module
  (make-circuit
    (pin row[5] col[14])
    (layout (make-fitboard-layout matrix))
    (wire (for/list ([x (range 5)])
            (filter-not
              void?
              (for/list ([y (range 14)])
                (let ([key (keyboard-xy matrix x y)])
                  (when key
                    (series
                      self.col[y]
                      key
                      self.row[x])))))))))
In [16]:
circuit-plot
Out[16]:
#<procedure:circuit-plot>
In [17]:
(circuit-plot matrix-module)
DEBUG:  (Number of conflicts: 0)
generating pdf ..
link: /tmp/out.pdf
Out[17]:
In [18]:
(parameterize ([current-directory "./out/bhdl-key-matrix-module"])
    (circuit-export matrix-module
                    #:auto-place #f
                    #:formats '(kicad pdf png)))
generating KiCAD PCB ..
"DEBUG: /var/folders/jd/pqbc6_7j0y5gc2kjxjntw8c40000gn/T/PKGS-src-ROOT-BHDL-syst----iVc-fpio-main.rkt-95-15_16388403871638840387512"
"DEBUG: /var/folders/jd/pqbc6_7j0y5gc2kjxjntw8c40000gn/T/PKGS-src-ROOT-BHDL-syst----iVc-fpio-main.rkt-95-15_16388403871638840387515"
link: /Users/hebi/git/bhdl.org/notebooks/out/bhdl-key-matrix-module/out.kicad_pcb
DEBUG:  (Number of conflicts: 0)
generating pdf ..
link: /Users/hebi/git/bhdl.org/notebooks/out/bhdl-key-matrix-module/out.pdf
link: /Users/hebi/git/bhdl.org/notebooks/out/bhdl-key-matrix-module/out.png
Out[18]:
In [19]:
(show-layout matrix-module)
Out[19]:

MCU module

In [20]:
;; FIXME I actually should not combine two atoms into one, because 
(define (icsp-header)
   (make-circuit 
    (part [h (PinHeader2 3)]
                         )
    (pin MOSI SCK MISO RESET VCC GND)
                 ;; FIXME !!!!!!! should report error
                   (wire (bus 
;;                               (self (MOSI SCK RESET))
                                 (self (MISO VCC SCK MOSI RESET GND))
                                 (h [1 2 3 4 5 6])))
                   (layout h)))
In [21]:
(show-layout (icsp-header))
"DEBUG: /var/folders/jd/pqbc6_7j0y5gc2kjxjntw8c40000gn/T/PKGS-src-ROOT-BHDL-syst----iVc-fpio-main.rkt-63-15_16388404281638840428332"
Out[21]:
In [22]:
(show-layout (ATmega32U4 #:FP "TQFP-44"))
"DEBUG: /var/folders/jd/pqbc6_7j0y5gc2kjxjntw8c40000gn/T/PKGS-src-ROOT-BHDL-syst----iVc-fpio-main.rkt-63-15_16388404311638840431967"
Out[22]:
In [23]:
(footprint->pict (lcsc->fp "C44854"))
"DEBUG: /var/folders/jd/pqbc6_7j0y5gc2kjxjntw8c40000gn/T/PKGS-src-ROOT-BHDL-syst----iVc-fpio-main.rkt-63-15_16388404331638840433419"
Out[23]:
In [24]:
(footprint->pict (uuid->fp "74ebea1b3f170a1f74baae3036bac561"))
"DEBUG: /var/folders/jd/pqbc6_7j0y5gc2kjxjntw8c40000gn/T/PKGS-src-ROOT-BHDL-syst----iVc-fpio-main.rkt-63-15_16388404351638840435278"
Out[24]:
In [41]:
(define mcu-module 
  (make-circuit 
    (part [mcu (ATmega32U4 #:FP "TQFP-44")]
          [usb (USB-Micro)]
          [icsp (icsp-header)]
          [xtal (Crystal-4 '16MHz)])
    (pin row[5] col[14])
    (layout (vc-append 50 (rotate usb pi) mcu icsp))
    (wire (bus (self row [0 4])
               (mcu [PB0 PB4 PB5 PB6 PB7]))
          (bus (self col [0 13])
               (mcu [PD0 PD1 PD2 PD3 PD4 PD5 PD6 PD7 PF0 PF1 PF4 PF5 PF6 PF7]))
          (series mcu.PC6 (R '1k) (LED0603 'red) global.GND)
          (bus (mcu (MOSI SCLK MISO RESET VCC GND))
               (icsp (MOSI SCK MISO RESET VCC GND)))
          (list (*- mcu.VCC mcu.UVCC mcu.AVCC global.VCC)
                (*- mcu.GND mcu.UGND global.GND)
                ;; reset circuit and switch
                (*- mcu.RESET (*< (LL4148)
                                  (R '10k)) global.5V)
                ;; FIXME [-1] because (Switch) is parsed as the index otherwise
                (*- mcu.RESET[-1] (Switch) global.GND)
                ;; capacitors
                (*- mcu.UCAP (C '1uf) global.GND)
                (*- global.VCC (*< (C '1uf)
                                   (C '100nf)
                                   (C '100nf)
                                   (C '100nf)
                                   (C '100nf))
                               global.GND)
                ;; crystal
                ;; FIXME Crystal-2 has no 16MHz
                (*- mcu.XTAL1 xtal.XIN)
                (*- xtal.XOUT mcu.XTAL2)
                (*- xtal.GND global.GND)
                (*- mcu.XTAL1 (C '22pf) global.GND)
                (*- mcu.XTAL2 (C '22pf) global.GND)
                ;; USB
                (*- mcu.D+ (R 22) usb.D+)
                (*- mcu.D- (R 22) usb.D-)
                ;; just use USB for supplying power
                (*- mcu.VBUS usb.VBUS global.VCC)
                (*- mcu.UGND usb.GND global.GND)))))
In [42]:
(show-layout mcu-module)
Out[42]:

Integrate into One board

In [43]:
;; connect MCU with matrix
(define keyboard
  (make-circuit
    (layout (ct-superimpose matrix-module mcu-module))
    (wire (list matrix-module mcu-module
                (bus (matrix-module row[0 4])
                     (mcu-module row[0 4]))
                (bus (matrix-module col[0 13])
                     (mcu-module col[0 13]))
                ))))
In [44]:
(show-layout keyboard)
Out[44]: