'*** inzet van thuisbatterij, period augustus/september 2026
'versie 16/09/2026 door Henko
'
init_prog()
nd=read_data() ! n=nd+1
nogeens:
dim v1(n),v2(n),v3(n),la(n),t(n),c(n)
' v1 = verbruik vanaf panelen
' v2 = verbruik vanuit nuts
' v3 = verbruik uit de accu
' la  = daglading vanuit panelen naar de accu
' t  = teruglevering vanuit panelen naar nuts
' c  = acculading einde dag
for i=1 to nd ! proces(i,cmax,pan(i),bruik(i)) ! next i
refresh() ! kosten()
do slowdown
  for i=1 to 8
    if switch_changed(str$(i)) then refresh()
    next i
  if slider_changed("accu") then
    cmax=int(100*slider_value("accu"))/10
    set_accuslider()
    goto nogeens
    end if
  if slider_changed("direct") then
    direct=int(100*slider_value("direct"))/100
    set_directslider()
    goto nogeens
    end if
    if b_p("b1") then
      .v2kost=numpad(.v2kost,0.05,1)
      button ("b1") text .v2kost
      kosten()
      end if
    if b_p("b2") then
      .tvergoed=numpad(.tvergoed,0,1)
      button ("b2") text .tvergoed
      kosten()
      end if
    if b_p("b3") then
      .tkost=numpad(.tkost,0,1)
      button ("b3") text .tkost
      kosten()
      end if
  until b_p("stop")
set orientation portrait
end

def init_prog()
graphics ! graphics clear .9,.9,.9
set orientation landscape ! set toolbar off
get screen size .sw,.sh
user_tools()
init_numpad(350,200,50,.2,.5,.5,.8)
end def

def read_data()
n=0
restore to prod_data
read .naam$
while data_exist() ! n+=1 ! read x,x ! end while
dim .pan(n+1),.bruik(n+1)
   ' pan   = dagopbrengsten panelen
   ' bruik = totale verbruiken per dag
restore to prod_data
read .naam$
for i=1 to n
  read .pan(i),.bruik(i)
  .pan(0)+=.pan(i) ! .bruik(0)+=.bruik(i)
  next i
'
prod_data:
data "Simulatie thuisbatterij (nazomer)"
data  6.2,  7.2 
data  11.4, 4.9 
data  10,   4.8 
data  8.6,  6.6 
data  13.1, 7 
data  14.8, 6.9 
data  9.7,  5.1 
data  11.5, 6.5 
data  3.3,  5.5 
data  6.6,  5.4 
data  7.6,  5.5 
data  7.1,  6.9 
data  9,    7.8 
data  9.4,  6.2 
data  5.3,  5.9 
data  3.2,  6.5 
data  9.7,  6.4 
data  11.5, 8.8 
data  10.7, 6.2 
data  3.1,  5.8 
data  9.2,  4.7 
data  9.8,  5 
data  4.3,  6.5 
data  10.1, 5.7 
data  7.4,  6.4
data  9.9,  6.5
data  9.8,  6.4
data  7.2,  6.7
data  4.5,  5.8
data  5.6,  5.1
'
draw font size 30
draw text .naam$ at 25,20
draw font size 20
pmax=0 ! for i=1 to n ! pmax=max(pmax,.pan(i)) ! next i
.pmax=int(pmax) ! .sy=380/.pmax
return n
end def

def proces(i,cmax,p,v)
' c = lading in accu einde dag
v1=min(.direct*v,p) ! v-=v1 ! p-=v1   ' direct verbruik
la=min(p,cmax-c) ! p-=la ! c+=la      ' lading naar accu
v3=min(v,c) ! v-=v3 ! c-=v3           ' verbruik uit accu
if v>0 then v2=v else v2=0 ! v-=v2    ' verbruik uit nuts
if p>0 then t=p else t=0 ! p-=t       ' teruglevering
.v1(i)=v1 ! .v2(i)=v2 ! .v3(i)=v3
.v1(0)+=v1 ! .v2(0)+=v2 ! .v3(0)+=v3
.la(i)=la ! .t(i)=t ! .c(i)=c
.la(0)+=la ! .t(0)+=t ! .c(0)+=c
end def

def user_tools()
dim stroom$(9),.col(9)
restore to switch
for i=1 to 8 ! read stroom$(i),.col(i) ! next i
switch:
         '*** switches
data "panelen",9,  "tot. verbruik",0,  "vanuit panelen",50
data "vanuit net",900,  "vanuit accu",66
data "accu laden",660,  "teruglevering",909
data "accu lading",633
xo=.sw-280 ! yo=62 ! dy=60
for i=1 to 8
  switch i state 0 at xo,yo+(i-1)*dy
  draw_color(.col(i))
  draw text " - "&stroom$(i) at xo+50,yo+(i-1)*dy
  next i
switch 1 state 1 ! switch 2 state 1
          '*** graphics panel
draw color 0,0,0 ! draw size 3
draw rect 25,65 to .sw-300,510
          '**** sliders
xs=650 ! ysa=550
.cmax=5 ! slider "accu" value .cmax/10 at xs,ysa size 350
draw text "Max. bruikbare accucapaciteit" at xs,ysa+35
draw text "0" at xs+10,ysa-15 ! draw text "10" at xs+320,ysa-15
set_accuslider()
ysd=640
.direct=0.3 ! slider "direct" value .direct at xs,ysd size 350
draw text "Direct verbruik vanaf panelen" at xs,ysd+35
draw text "0" at xs+5,ysd-15 ! draw text "1" at xs+332,ysd-15
set_directslider()
            '*** buttons kWh kosten
x1=25 ! x2=195 ! x3=310 ! x4=490 ! yb=530 ! dy=40 ! yl=yb+2*dy+32
.v2kost=0.27 ! .tvergoed=0.022 ! .tkost=0.02
draw text "prijs / kWH :" at x1,yb+5
draw text "terugl. opbr:" at x1,yb+dy+5
draw text "terugl. kost:" at x1,yb+2*dy+5
button "b1" text .v2kost at x2,yb size 80,30
button "b2" text .tvergoed at x2,yb+dy size 80,30
button "b3" text .tkost at x2,yb+2*dy size 80,30
            ' *** financieel
draw text "nuts levering:" at x3,yb+5
draw text "terugl. opbr.:" at x3,yb+dy+5
draw text "terugl. kost :" at x3,yb+2*dy+5
draw text "  tot. kosten:" at x3,yl+10
field "f1" text "" at x4,yb size 120,30 RO
field "f2" text "" at x4,yb+dy size 120,30 RO
field "f3" text "" at x4,yb+2*dy size 120,30 RO
field "f4" text "" at x4,yl+5 size 120,30 RO
for i=1 to 4 ! field "f"&i back color .9,.9,.9 ! next i
draw line x4-5,yl to x4+60,yl
button "stop" text "Stop" at .sw-100,.sh-55 size 80,40
end def

def grid()
fill color .9,.9,.9 ! fill alpha 1
fill rect 27,67 to .sw-302,508
xo=60 ! yo=480 ! sy=.sy ! sx=630/.nd
draw size 2 ! draw color 0,0,0
draw line xo-8,yo to 700,yo   ' x-as
draw line xo,yo+8 to xo,94    ' y-as
draw text "kWh" at xo-30,70
draw text "dag" at 680,yo+4
draw color .7,.7,.7 ! draw size 1
draw font size 14
for i=1 to .pmax
  x=xo-5 ! y=yo-i*sy
  draw color .7,.7,.7 ! draw line x,y to 700,y
  i$=str$(i) ! if i<10 then i$=" "&str$(i)
  draw color 0,0,0 ! draw text i$ at xo-23,y-8
  next i
for i=1 to .nd 
  y=yo+5 ! x=xo+i*sx
  draw color .7,.7,.7 ! draw line x,y to x,95
  if i%2=0 then
    i$=str$(i) ! if i<10 then i$=" "&str$(i)
    draw color 0,0,0 ! draw text i$ at x-sx-10,yo+8
    end if
  next i
end def

def refresh()
grid()
if switch_state("1")=1 then graph(1,.pan)
if switch_state("2")=1 then graph(2,.bruik)
if switch_state("3")=1 then graph(3,.v1)
if switch_state("4")=1 then graph(4,.v2)
if switch_state("5")=1 then graph(5,.v3)
if switch_state("6")=1 then graph(6,.la)
if switch_state("7")=1 then graph(7,.t)
if switch_state("8")=1 then graph(8,.c)
end def

def graph(c,val())
draw size 2 ! draw_color(.col(c))
xo=grid.xo ! yo=grid.yo ! sx=grid.sx ! sy=grid.sy
draw to xo,yo-sy*val(1)
for i=2 to .nd
  draw line to xo+(i-1)*sx,yo-sy*val(i)
  next i
y=yo-sy*val(0)/.nd
draw size 5 ! draw line .sw-320,y to .sw-301,y
end def

def set_accuslider()
xs=user_tools.xs ! ys=user_tools.ysa ! draw font size 14
v=slider_value("accu") ! w$=str$(int(100*v)/10)
fill color .9,.9,.9 ! draw color 0,0,0
fill rect xs+22,ys-13 to xs+320,ys-4
if v>.05 and v<.91 then draw text w$ at v*320+xs+5,ys-15
draw font size 20
end def

def set_directslider()
xs=user_tools.xs ! ys=user_tools.ysd ! draw font size 14
v=.direct ! w$=str$(v)
fill color .9,.9,.9 ! draw color 0,0,0
fill rect xs+22,ys-13 to xs+323,ys-4
if v>.05 and v<.91 then draw text w$ at v*320+xs+5,ys-15
draw font size 20
end def

def kosten()
ktot=0
k1=.v2(0)*.v2kost ! field "f1" text "€ "&int(100*k1)/100
ktot+=k1
k2=.t(0)*.tvergoed ! field "f2" text "-€ "&int(100*k2)/100
ktot-=k2
k3=.t(0)*.tkost ! field "f3" text "€ "&int(100*k3)/100
ktot+=k3 ! field "f4" text "€ "&int(100*ktot)/100
end def

def draw_color(v)
v=max(0,int(v%1000))
r=floor(v/100) ! v-=100*r ! r/=9 ! 
g=floor(v/10)  ! b=(v-10*g)/9 ! g/=9
draw color r,g,b
end def

def init_numpad(xtop,ytop,bs,R,G,B,alpha)
name$="numpad" ! cn=10
page name$ set 
page name$ frame xtop,ytop,0,0
set buttons custom
if bs<20 then bs=20
sp=4 ! th=.5*bs+4 ! ww=4*bs+5*sp ! hh=th+4*bs+6*sp
fsize=.5*bs
draw font size fsize ! set buttons font size fsize
draw color 1,1,1 ! fill color .7,.7,.7
button "rec" title "" at 0,0 size ww,hh
button "res" title "" at 0,0 size ww,th+4
fill color R,G,B ! fill alpha alpha
button "0" title "0" at sp,th+3*bs+5*sp size bs,bs
for k=1 to 9
  x=(k-1)%3 ! y=2-floor((k-1)/3)
  button k title k at (x+1)*sp+x*bs,th+y*bs+(y+2)*sp size bs,bs
  next k
button "-" title "-" at 2*sp+bs,th+3*bs+5*sp size bs,bs
button "." title "." at 3*sp+2*bs,th+3*bs+5*sp size bs,bs
button "Cl" title "C" at 4*sp+3*bs,th+2*sp size bs,bs
button "del" title chr$(11064) at 4*sp+3*bs,th+bs+3*sp size bs,bs
button "ok" title "ok" at 4*sp+3*bs,th+2*bs+4*sp size bs,2*bs+sp
page name$ hide
page name$ frame xtop,ytop,ww,hh
set buttons default ! set buttons font size 20
draw font size 20 ! draw color 0,0,0
end def

def numpad(in,minval,maxval)
page "numpad" set ! page "numpad" show
a$=str$(in) ! button "res" text a$
pflag=0 ! sflag=0 ! ob=1-option_base()
nump1:
if b_p("ok") then
  number=val(a$) ! a$="" ! button "res" text ""
  if minval<>0 or maxval<>0 then
    if number<minval or number>maxval then
      button "res" text "range error"
      pflag=0 ! a$="" ! pause 1
      button "res" text ""
      goto nump1
      end if
    end if
  page "numpad" hide ! page "" set
  return number
  end if
if b_p("Cl") then
  a$ = "" ! pflag=0 ! sflag=0 ! goto nump3
  end if
if b_p("del") and len(a$) then
  ll=len(a$) ! if substr$(a$,ll-ob,ll-ob)="." then pflag=0
  a$ = left$(a$,ll-1) ! sflag=0 ! goto nump3
  end if
if b_p("-") then
  a$ = "-" ! pflag=0 ! sflag=0 ! goto nump3
  end if
if b_p(".") and not pflag and not sflag then
  a$ &= "." ! pflag=1 ! goto nump3
  end if
for k=0 to 9
  t$=k
  if b_p(t$) and not sflag then
    a$ &= t$ ! goto nump3
    end if
  next k
goto nump1
nump3:
if len(a$)>10 then ! sflag=1 ! goto nump1 ! end if
button "res" text a$
goto nump1
end def


def db ! debug pause ! end def
def b_p(a$) = button_pressed(a$)
