
constants [[portb $38][ddrb $37]
           [cmd $70][arg1 $71]
           [pina $39][button-pin 8]]


constants [[npix 24]]


constants [[portb $38][ddrb $37]
           [cmd $70][arg1 $71]]

constants [[npix 24]]


to startup
ao
loop [
	print in7 wait 1
	checkval	
]
end 

to checkval
ifelse in7 = 1 [all 10 redraw] [all 40 redraw]
end




to go
let [n 0 h 0 m 0]
loop
 [make "m :n % 60
  make "h :n / 60 % 12
  if :h = 0 [make "h 12]
  digit 6 :m % 10
  digit 12 :m / 10  
  digit 0 :h % 10
  digit 18 :h / 10  
  make "n :n + 1
  redraw
  wait 10]
end


to digit :p :n
let [cn :n
     c 0]
brightness 40
if :n > 4 [make "cn :cn - 5]
if :cn = 0 [make "c 10] 				; yellow
if :cn = 1 [make "c 0]					; red
if :cn = 2 [make "c 85]				; purple
if :cn = 3 [make "c 40]				; blue
if :cn = 4 [brightness 10 make "c 40]			; cyan
repeat 6 [dot :p :c make "p :p + 1]
if :n < 5 [stop]
brightness 50
dot :p - 3 $100
dot :p - 4 $100
end




;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; clock
;;

constants [[porta $3b][ddra $3a][pina $39]
           [scl 4][sda 8]]

global [hour minute seconds ack]

to time
gettime
prf "|%d | hour
prf "|%d | minute
print seconds
end

to gettime
i2c-start i2c-putc $d0 i2c-putc $0 i2c-stop
i2c-start i2c-putc $d1 
setseconds from-bcd i2c-getc 0
setminute from-bcd i2c-getc 0
sethour from-bcd (i2c-getc 1) and $1f
i2c-stop
end

to settime :h :m 
i2c-start i2c-putc $d0 i2c-putc 0 
i2c-putc 0 
i2c-putc to-bcd :m 
i2c-putc to-bcd :h + 40
i2c-stop
end

to i2c-putc :n
let [ack 0]
repeat 8 
 [ifelse (:n and $80) [sda-high][sda-low]
  make "n lsh :n 1
  scl-high scl-high scl-low]
sda-hiz
scl-high
setack sda-tst
scl-low
sda-out
end

to i2c-getc :ack
let [res 0]
sda-hiz
repeat 8
 [make "res lsh :res 1
  scl-high
  make "res :res + sda-tst
  scl-low]
sda-out ifelse :ack [sda-high][sda-low]
scl-high
scl-low
output :res
end

define to-bcd [n][output :n / 10 * 16 + (:n % 10)]
define from-bcd [n][output :n / 16 * 10 + (:n % 16)]

define i2c-start [][sda-out scl-high sda-high sda-low scl-low]
define i2c-stop [][sda-low scl-high sda-high]

define sda-low [][bclrb porta sda]
define sda-high [][bsetb porta sda]
define sda-hiz [][bclrb ddra sda bsetb porta sda]
define sda-out [][bsetb ddra sda]
define sda-tst [][output btstb pina sda]

define scl-low [][bclrb porta scl]
define scl-high [][bsetb porta scl]

to setup-clock-pins
bset ddra 12
end

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; etc
;;

to all :n
let [addr 0]
repeat npix [dot :addr :n make "addr :addr + 1]
redraw
end

define ao [][alloff redraw]

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; pins
;;

constants
 [
  [ddrb $04][portb $00]
  [pinb $02][ddrb $04][portb $00]
  [didr0 $7e][adcsra $7a][admux $7c][adcl $78][adch $79]
  [tccr0a $44][tccr0b $45][ocr0a $47][ocr0b $48]
  [tccr2a $b0][tccr2b $b1][ocr2b $b4]]

to pin-init
bsetb ddrb $04		; pins pb2 as output / pb 0,1,3 as input
bsetb portb $0f		; turn on pullups for 8-11
bsetb didr0 $3f
writeb adcsra $86
writeb tccr2a 3		; set up pwm
writeb tccr2b 2
writeb tccr0a 3
writeb tccr0b 2
end

to adread :ch
writeb admux :ch + $40
bsetb adcsra $40
waituntil [not btstb adcsra $40]
output (readb adcl) + (lsh readb adch 8)
end


define in7 [][output btstb pina $80]

 

