#Region Module Attributes
#FullScreen: False
#IncludeTitle: True
#ApplicationLabel: ScrollChartExample
#VersionCode: 1
#VersionName:
#SupportedOrientations: landscape
#CanInstallToExternalStorage: False
#End Region
' Scroll Chart Example, started with Klaus Oscillioscope example
' Changes by KitCarlson to make Plot Scroll using canvas, on single Panel
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim ProgName As String : ProgName = "Test"
Dim ProgVersion As String : ProgVersion = "Test"
Dim Timer1 As Timer
Dim d As Timer
Dim NC As Int : NC = 3
Type Curves (Name As String, Color As Int, Width As Float, Scale As Double, Offset As Double, Draw As Boolean)
Dim CurveVal(NC, 101) As Double
Dim Curve(NC) As Curves
Dim CurvesNb As Int : CurvesNb = NC-1 '
Dim CurvesNbDisp As Int
Dim y1(NC) As Float ' old plot points
Dim y2(NC) As Float ' new plot points
Dim w(NC) As Double
Dim a(NC) As Double
Dim Border, PltW As Int
Dim GridX0, GridX1, GridY0, GridY1, GridYm, GridW, GridH As Int
Dim DivX, DivY As Int
Dim NbDivX, NbDivY As Int
Dim GridCnt As Int
Dim ScreenCol, GridLineCol As Int ' for grid and screen colors
Dim t, dt As Double
Dim ii As Float
Dim Stopped As Boolean : Stopped = True
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim btnStart As Button
Dim pnlChart As Panel
Dim cvsGraph As Canvas
Dim rectGrid As Rect
Dim SrcRec, DestRec, PltRec As Rect
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.Title = ProgName & " " & ProgVersion
dt = 0.01
t = 0
Timer1.Initialize("Timer1", 1000)'1000
d.Initialize("d", 50)
d.Enabled = True
Timer1.Enabled = True
NbDivX = 20 ' number of divisions displayed
NbDivY = 10
DivY = Floor(90%y / NbDivY)
DivX = Floor(90%x / NbDivX)
GridW = DivX * NbDivX
GridH = DivY * NbDivY
PltW = 3dip
Border = 6dip
GridY0 = Border
GridY1 = GridY0 + GridH
GridX0 = Border
GridX1 = GridX0 + GridW
GridYm = GridH / 2
GridCnt = 0
rectGrid.Initialize(GridX0, GridY0, GridX1, GridY1) ' initialize rectangle for plot cover
SrcRec.Initialize(GridX0 + PltW, GridY0, GridX1, GridY1) ' initialize rectangle for copy
DestRec.Initialize(GridX0, GridY0, GridX1 - PltW, GridY1)' initialize rectangle for paste
PltRec.Initialize(GridX1 - PltW, GridY0, GridX1, GridY1) ' initialize rectangle for plot
ScreenCol = Colors.Black ' for erase
GridLineCol = Colors.Gray ' for grid
pnlChart.Initialize("")
Activity.AddView(pnlChart, 0, 0, 100%x, 100%y)
pnlChart.Color = Colors.DarkGray ' back ground color for panel
cvsGraph.Initialize(pnlChart)
btnStart.Initialize("btnStart") ' Start/Stop button
btnStart.Text = "Start"
pnlChart.AddView(btnStart, 0,GridY1,60,40)
InitCalcCurves
InitCurves
End Sub
Sub Activity_Resume
InitHGrid
cvsGraph.DrawLine(GridX1-PltW, GridY0, GridX1-PltW, GridY1, GridLineCol, 1dip) ' first vert. grid
End Sub
Sub Activity_Pause (UserClosed As Boolean)
' If Stopped = False Then ' if running
' btnStart_Click ' stop
' End If
End Sub
Sub InitHGrid ' Horizontal grid lines
Dim i, y As Int
For i = 0 To NbDivY
y = GridY0 + i * DivY
cvsGraph.DrawLine(GridX0, y, GridX1, y, GridLineCol, 1dip)
Next
End Sub
'Sub btnStart_Click
' If Stopped Then
' Stopped = False
' ii = 0
' d.Enabled = True
' Timer1.Enabled = True
' EraseCurves
' btnStart.Text = "Stop"
'' Else
'' Timer1.Enabled = False
'' Stopped = True
'' btnStart.Text = "Start"
' End If
'End Sub
Sub d_Tick
DrawCurves
End Sub
Sub Timer1_Tick
ii = ii + 1
GridCnt = GridCnt + 1
t = t + dt
If ii > 100 Then ii = 100
GetValues
' DrawCurves
InitCalcCurves
End Sub
Sub DrawCurves
Dim i As Int
cvsGraph.DrawBitmap(cvsGraph.Bitmap,SrcRec,DestRec) ' copy scroll grid and plots
cvsGraph.DrawRect(PltRec, ScreenCol, True, 1) ' erase last plot area
If GridCnt = NbDivX Then ' add vert grid
GridCnt = 0
cvsGraph.DrawLine(GridX1-PltW, GridY0, GridX1-PltW, GridY1, GridLineCol, 1dip)
End If
InitHGrid
For i = 0 To CurvesNb
If Curve(i).Draw = True Then
y2(i) = GridYm + (-Curve(i).Offset - CurveVal(i, ii)) * Curve(i).Scale
cvsGraph.DrawLine(GridX1, y2(i),GridX1 - PltW, y1(i),Curve(i).Color, 2dip) ' plot new
y1(i) = y2(i) ' save for start point next curve
End If
Next
pnlChart.Invalidate
DoEvents
End Sub
Sub GetValues
Dim i As Int
For i = 0 To CurvesNb
CurveVal(i, ii) = a(i) * Sin(w(i) * t)
Next
End Sub
Sub EraseCurves
cvsGraph.DrawRect(rectGrid, Colors.Transparent, True, 1)
GridCnt = 0
InitHGrid ' add horizontal grid
cvsGraph.DrawLine(GridX1-PltW, GridY0, GridX1-PltW, GridY1, GridLineCol, 1dip)' vertical grid
End Sub
Sub InitCurves
Curve(0).Draw = True
' Curve(1).Draw = True
' Curve(2).Draw = True
'
Curve(0).Color = Colors.Red
' Curve(1).Color = Colors.Blue
' Curve(2).Color = Colors.Green
Curve(0).Scale = 30
' Curve(1).Scale = 30
' Curve(2).Scale = 30
Curve(0).Offset = 1
' Curve(1).Offset = 0
' Curve(2).Offset = 1
Curve(0).Draw = True
' Curve(1).Draw = True
' Curve(2).Draw = True
End Sub
Sub InitCalcCurves
w(0) = Rnd(0,1000)
Log(w(0))
' w(1) = cPI * 3.0
' w(2) = cPI * 4.0
' y1(0) = GridYm
' y1(1) = GridYmASF
' y1(2) = GridYm
'
a(0) = 5.0
' a(1) = 2.0
' a(2) = 3.0
End Sub