Android Oscillator Application Using Basic4Android - Tutorial Part 2

This is the 2nd part of my tutorial in creating an Oscillator Application in Android. Note that in the first part we haven't initialized to make a grid as a background to our curves. So here is the code for that.
  1. Sub InitGrid
  2.         Dim i As Int
  3.         Dim x, y As Float
  4.        
  5.         cvsScreen.DrawRect(rectScreen, ScreenCol, True, 1)
  6.         For i = 0 To NbDivY
  7.                 y = GridX0 + i * Div
  8.                 cvsScreen.DrawLine(GridX0, y, GridX1, y, GridLineCol, 1dip)
  9.         Next
  10.        
  11.         For i = 0 To NbDivX
  12.                 x = GridY0 + i * Div
  13.                 cvsScreen.DrawLine(x, GridY0, x, GridY1, GridLineCol, 1dip)
  14.         Next
  15.         pnlScreen.Invalidate
  16.  
  17.         cvsGraph.DrawRect(rectGrid, Colors.Transparent, True, 1)
  18.         pnlGraph.Invalidate
  19.        
  20.         cvsCursor.DrawRect(rectGrid, Colors.Transparent, True, 1)
  21.         pnlCursor.Invalidate
  22. End Sub
After initializing our grid as the background of our curves. We will also initialize a cursor in the panel that can be touched. Follow the code below:
  1. Sub pnlCursor_Touch (Action As Int, X As Float, Y As Float) As Boolean 'Return True to consume the event
  2.         If Stopped = False Then
  3.                 Return
  4.         End If
  5.        
  6.         Select Action
  7.         Case Activity.ACTION_DOWN
  8.                 pnlDispValues.Visible = True
  9.                 cx = X
  10.                 If X >= 0 AND X <= GridW Then
  11.                         DrawCursor(X)
  12.                         DispValues(X)
  13.                 End If
  14.         Case Activity.ACTION_MOVE
  15.                 If X >= 0 AND X <= GridW Then
  16.                         DrawCursor(X)
  17.                         DispValues(X)
  18.                 End If
  19.         Case Activity.ACTION_UP
  20.                 cvsCursor.DrawLine(cx, 0, cx, GridH, Colors.Transparent, 1)
  21.                 pnlCursor.Invalidate
  22.                 pnlDispValues.Visible = False
  23.         End Select     
  24.         Return True
  25. End Sub
Then, we shall code for our spinners in the Scale, TimeScale, and the SignalScale for the change of values to our curves. Type the code below:
  1. Sub spnTimeScale_ItemClick (Position As Int, Value As Object)
  2.         dt = Value / 10
  3.         t = 0
  4.         Timer1.Initialize("Timer1", dt * 1000)
  5. End Sub
  6.  
  7. Sub spnScale_ItemClick (Position As Int, Value As Object)
  8.         Dim spn As Spinner
  9.        
  10.         spn = Sender
  11.         Curve(spn.Tag).Scale = Div / Value
  12. End Sub
Next we will code for our RadiobuttonScale. Here's the code below:
  1. Sub rbtScope_CheckedChange(Checked As Boolean)
  2.         Dim rbt As RadioButton
  3.        
  4.         btnStop_Click
  5.         rbt = Sender
  6.         ScopeMode = rbt.Tag
  7.         btnStart_Click
  8. End Sub
We will code also for our textbox if the curves will change its scale for the offset. Type this one:
  1. Sub edtOffset_FocusChanged (HasFocus As Boolean)
  2.         Dim edt As EditText
  3.         Dim val As Double
  4.        
  5.         If HasFocus = False Then
  6.                 edt = Sender
  7.                 Curve(edt.Tag).Offset = edt.Text
  8.         End If
  9. End Sub
Next, we will now put an Event Name in our three buttons for the btnStart, btnStop, and btnSingleShot. In the btnStart, once the user will click this button, it will automatically start the curves to cast signal. This is the code for that:
  1. Sub btnStart_Click
  2.         Timer1.Enabled = True
  3.         SingleShot = False
  4.         Stopped = False
  5.         ii = -1
  6.         xx = -dx
  7.         EraseCurves
  8. End Sub
Then this is the code for the stopping the curves to move:
  1. Sub btnStop_Click
  2.         Timer1.Enabled = False
  3.         SingleShot = False
  4.         Stopped = True
  5. End Sub
And this one is for the single shot to make the only one curves showed in the Oscillator:
  1. Sub btnSingleShot_Click
  2.         Timer1.Enabled = True
  3.         SingleShot = True
  4.         Stopped = False
  5.         ii = -1
  6.         xx = -dx
  7.         EraseCurves
  8. End Sub
And to fully end this tutorial, we will have our Activity_Create code:
  1. Sub Activity_Create(FirstTime As Boolean)
  2.  
  3.         dt = 0.01
  4.         t = 0
  5.         Timer1.Initialize("Timer1", dt * 1000)
  6.        
  7.         NbDivX = 10
  8.         NbDivY = 8
  9.        
  10.         Div = Floor(80%y / NbDivY)
  11.         xx = 0
  12.         dx = Div / 10
  13.         GridW = Div * NbDivX
  14.         GridH = Div * NbDivY
  15.         Border = 6dip
  16.        
  17.         ScreenY0 = 0
  18.         ScreenH = GridH + 2 * Border
  19.         ScreenY1 = ScreenY0 + ScreenH
  20.        
  21.         ScreenX0 = 0
  22.         ScreenW = GridW + 2 * Border
  23.         ScreenX1 = ScreenX0 + ScreenW
  24.  
  25.         GridY0 = Border
  26.         GridY1 = GridY0 + GridH
  27.         GridX0 = Border
  28.         GridX1 = GridX0 + GridW
  29.         GridYm = GridH / 2
  30.        
  31.         rectGrid.Initialize(0, 0, GridW, GridH)
  32.         rectScreen.Initialize(0, 0, ScreenW, ScreenH)
  33.        
  34.         ScreenCol = Colors.White
  35.         GridLineCol = Colors.Gray
  36.        
  37.         pnlOcilloscope.Initialize("")
  38.         Activity.AddView(pnlOcilloscope, 0, 0, 100%x, 100%y)
  39.        
  40.         pnlScreen.Initialize("")
  41.         pnlOcilloscope.AddView(pnlScreen, ScreenX0, ScreenY0, ScreenW, ScreenH)
  42.         cvsScreen.Initialize(pnlScreen)
  43.        
  44.         pnlGraph.Initialize("")
  45.         pnlOcilloscope.AddView(pnlGraph, GridX0, GridY0, GridW, GridH)
  46.         cvsGraph.Initialize(pnlGraph)
  47.  
  48.         pnlCursor.Initialize("pnlCursor")
  49.         pnlOcilloscope.AddView(pnlCursor, GridX0, GridY0, GridW, GridH)
  50.         cvsCursor.Initialize(pnlCursor)
  51.        
  52.         pnlControl.Initialize("")
  53.         Activity.AddView(pnlControl, ScreenX1, 0, 100%x - ScreenX1, 54dip)
  54.         Dim cbg As ColorDrawable
  55.         cbg.Initialize(Colors.RGB(255, 196, 196), 0)
  56.         pnlControl.Background = cbg
  57.        
  58.         Dim w1, w2, w3, t1, h1 As Int
  59.         w1 = 4dip
  60.         w2 = (pnlControl.Width - 4 * w1) / 3
  61.         w3 = w1 + w2
  62.         t1 = 4dip
  63.         h1 = 52dip
  64.         btnStart.Initialize("btnStart")
  65.         pnlControl.AddView(btnStart, w1, t1, w2, h1)
  66.         btnStart.Text = "Start"
  67.        
  68.         btnStop.Initialize("btnStop")
  69.         pnlControl.AddView(btnStop, w1 + w3, t1, w2, h1)
  70.         btnStop.Text = "Stop"
  71.        
  72.         btnSingleShot.Initialize("btnSingleShot")
  73.         pnlControl.AddView(btnSingleShot, w1 + 2 * w3, t1, w2, h1)
  74.         btnSingleShot.Text = "Single Shot"
  75.        
  76.         scvControl.Initialize(480dip)
  77.         pnlOcilloscope.AddView(scvControl, ScreenX1, pnlControl.Height, 100%x - ScreenX1, 100%y - pnlControl.Height)
  78.         scvControl.Panel.LoadLayout("controls")
  79.         scvControl.Panel.Width = scvControl.Width
  80.         Dim cbg As ColorDrawable
  81.         cbg.Initialize(Colors.RGB(196, 196, 255), 0)
  82.         scvControl.Panel.Background = cbg
  83.         scvControl.Color = Colors.RGB(196, 196, 255)
  84.        
  85.         pnlCurveTools.Initialize("")
  86.         pnlOcilloscope.AddView(pnlCurveTools, 0, ScreenY1, ScreenX1, 100%y - ScreenY1)
  87.         Dim cbg As ColorDrawable
  88.         cbg.Initialize(Colors.RGB(255, 196, 196), 0)
  89.         pnlCurveTools.Background = cbg
  90.        
  91.         pnlDispValues.Initialize("")
  92.         pnlOcilloscope.AddView(pnlDispValues, 0, ScreenY1, ScreenX1, 100%y - ScreenY1)
  93.         Dim cbg As ColorDrawable
  94.         cbg.Initialize(Colors.RGB(255, 236, 153), 0)
  95.         pnlDispValues.Background = cbg
  96.         pnlDispValues.Visible = False
  97.        
  98.         InitCurves
  99.         InitCalcCurves
  100.         InitSpinners
  101.  
  102.         Dim ww As Float
  103.         ww = pnlDispValues.Width / 4
  104.         lblValue0.Initialize("")
  105.         pnlDispValues.AddView(lblValue0, 0, 0, ww, pnlDispValues.Height)
  106.         lblValue0.TextColor = Curve(0).Color
  107.         lblValue0.Gravity = Gravity.CENTER_HORIZONTAL + Gravity.CENTER_VERTICAL
  108.        
  109.         lblValue1.Initialize("")
  110.         pnlDispValues.AddView(lblValue1, ww, 0, ww, pnlDispValues.Height)
  111.         lblValue1.TextColor = Curve(1).Color
  112.         lblValue1.Gravity = Gravity.CENTER_HORIZONTAL + Gravity.CENTER_VERTICAL
  113.        
  114.         lblValue2.Initialize("")
  115.         pnlDispValues.AddView(lblValue2, 2 * ww, 0, ww, pnlDispValues.Height)
  116.         lblValue2.TextColor = Curve(2).Color
  117.         lblValue2.Gravity = Gravity.CENTER_HORIZONTAL + Gravity.CENTER_VERTICAL
  118.        
  119.         lblValue3.Initialize("")
  120.         pnlDispValues.AddView(lblValue3, 3 * ww, 0, ww, pnlDispValues.Height)
  121.         lblValue3.TextColor = Curve(3).Color
  122.         lblValue3.Gravity = Gravity.CENTER_HORIZONTAL + Gravity.CENTER_VERTICAL
  123.        
  124.         For i = 0 To 3
  125.                 Dim cbx As CheckBox
  126.                 cbx.Initialize("cbxDrawCurve")
  127.                 pnlCurveTools.AddView(cbx, 6dip + i * 66dip, 0, 60dip, 50dip)
  128.                 cbx.Tag = i
  129.                 cbx.Text = " " & (i + 1)
  130.                 cbx.Typeface = Typeface.DEFAULT_BOLD
  131.                 cbx.Color = Curve(i).Color
  132.                 cbx.Checked = True
  133.         Next
  134.         lblScale0.Color = Curve(0).Color
  135.         lblScale1.Color = Curve(1).Color
  136.         lblScale2.Color = Curve(2).Color
  137.         lblScale3.Color = Curve(3).Color
  138.  
  139.         lblOffset0.Color = Curve(0).Color
  140.         lblOffset1.Color = Curve(1).Color
  141.         lblOffset2.Color = Curve(2).Color
  142.         lblOffset3.Color = Curve(3).Color
  143.        
  144.         edtOffset0.Text = Curve(0).Offset
  145.         edtOffset1.Text = Curve(1).Offset
  146.         edtOffset2.Text = Curve(2).Offset
  147.         edtOffset3.Text = Curve(3).Offset
  148.        
  149.         Select ScopeMode
  150.         Case "SCOPE"
  151.                 rbtScopeScope.Checked = True
  152.         Case "MEM"
  153.                 rbtScopeMEM.Checked = True
  154.         Case "ROLL"
  155.                 rbtScopeROLL.Checked = True
  156.         End Select
  157. End Sub
So here is now our complete code for this tutorial:
  1. 'Activity module
  2. Sub Process_Globals
  3.         'These global variables will be declared once when the application starts.
  4.         'These variables can be accessed from all modules.
  5.         Dim Timer1 As Timer
  6.         Type Curves (Name As String, Color As Int, Width As Float, Scale As Double, Offset As Double, Draw As Boolean)
  7.         Dim CurveVal(4, 101) As Double
  8.         Dim ScreenX0, ScreenX1, ScreenY0, ScreenY1, ScreenW, ScreenH, Border As Int
  9.         Dim GridX0, GridX1, GridY0, GridY1, GridYm, GridW, GridH, Div As Int
  10.         Dim NbDivX, NbDivY As Int
  11.         Dim ScreenCol, GridLineCol As Int
  12.        
  13.         Dim t, dt As Double
  14.         Dim dx, xx, cx As Float
  15.         Dim ii As Int
  16.         Dim CurveI As Int                                                               ' curve index
  17.         Dim Curve(4) As Curves
  18.         Dim CurvesI(4) As Int
  19.         Dim CurvesNb As Int                                                     : CurvesNb = 3
  20.         Dim CurvesNbDisp As Int
  21.         Dim y1(4) As Float
  22.         Dim y2(4) As Float
  23.         Dim SingleShot As Boolean                               : SingleShot = False
  24.         Dim Stopped As Boolean                                  : Stopped = True
  25.         Dim ScopeMode As String                                 : ScopeMode = "ROLL"
  26.         Dim ScopeRolling As Boolean                     : ScopeRolling = False
  27.         Dim w(4) As Double
  28.         Dim a(4) As Double
  29.         Dim TimeScale(10) As Double
  30.         Dim SignalScale(10) As Double
  31.  
  32. End Sub
  33.  
  34. Sub Globals
  35.         'These global variables will be redeclared each time the activity is created.
  36.         'These variables can only be accessed from this module.
  37.         Dim btnStart, btnStop, btnSingleShot As Button
  38.         Dim pnlOcilloscope, pnlScreen, pnlGraph, pnlCursor, pnlControl As Panel
  39.         Dim pnlCurveTools, pnlDispValues As Panel
  40.         Dim cvsScreen, cvsGraph, cvsCursor As Canvas
  41.         Dim rectScreen, rectGrid As Rect
  42.         Dim spnTimeScale As Spinner
  43.         Dim spnScale0, spnScale1, spnScale2, spnScale3 As Spinner
  44.         Dim lblScale0, lblScale1, lblScale2, lblScale3 As Label
  45.         Dim lblValue0, lblValue1, lblValue2, lblValue3 As Label
  46.         Dim lblOffset0, lblOffset1, lblOffset2, lblOffset3 As Label
  47.         Dim edtOffset0, edtOffset1, edtOffset2, edtOffset3 As EditText
  48.         Dim rbtScopeScope, rbtScopeMEM, rbtScopeROLL As RadioButton
  49.         Dim scvControl As ScrollView
  50.         Dim bmpRoll As Bitmap
  51. End Sub
  52.  
  53. Sub Activity_Create(FirstTime As Boolean)
  54.  
  55.         dt = 0.01
  56.         t = 0
  57.         Timer1.Initialize("Timer1", dt * 1000)
  58.        
  59.         NbDivX = 10
  60.         NbDivY = 8
  61.        
  62.         Div = Floor(80%y / NbDivY)
  63.         xx = 0
  64.         dx = Div / 10
  65.         GridW = Div * NbDivX
  66.         GridH = Div * NbDivY
  67.         Border = 6dip
  68.        
  69.         ScreenY0 = 0
  70.         ScreenH = GridH + 2 * Border
  71.         ScreenY1 = ScreenY0 + ScreenH
  72.        
  73.         ScreenX0 = 0
  74.         ScreenW = GridW + 2 * Border
  75.         ScreenX1 = ScreenX0 + ScreenW
  76.  
  77.         GridY0 = Border
  78.         GridY1 = GridY0 + GridH
  79.         GridX0 = Border
  80.         GridX1 = GridX0 + GridW
  81.         GridYm = GridH / 2
  82.        
  83.         rectGrid.Initialize(0, 0, GridW, GridH)
  84.         rectScreen.Initialize(0, 0, ScreenW, ScreenH)
  85.        
  86.         ScreenCol = Colors.White
  87.         GridLineCol = Colors.Gray
  88.        
  89.         pnlOcilloscope.Initialize("")
  90.         Activity.AddView(pnlOcilloscope, 0, 0, 100%x, 100%y)
  91.        
  92.         pnlScreen.Initialize("")
  93.         pnlOcilloscope.AddView(pnlScreen, ScreenX0, ScreenY0, ScreenW, ScreenH)
  94.         cvsScreen.Initialize(pnlScreen)
  95.        
  96.         pnlGraph.Initialize("")
  97.         pnlOcilloscope.AddView(pnlGraph, GridX0, GridY0, GridW, GridH)
  98.         cvsGraph.Initialize(pnlGraph)
  99.  
  100.         pnlCursor.Initialize("pnlCursor")
  101.         pnlOcilloscope.AddView(pnlCursor, GridX0, GridY0, GridW, GridH)
  102.         cvsCursor.Initialize(pnlCursor)
  103.        
  104.         pnlControl.Initialize("")
  105.         Activity.AddView(pnlControl, ScreenX1, 0, 100%x - ScreenX1, 54dip)
  106.         Dim cbg As ColorDrawable
  107.         cbg.Initialize(Colors.RGB(255, 196, 196), 0)
  108.         pnlControl.Background = cbg
  109.        
  110.         Dim w1, w2, w3, t1, h1 As Int
  111.         w1 = 4dip
  112.         w2 = (pnlControl.Width - 4 * w1) / 3
  113.         w3 = w1 + w2
  114.         t1 = 4dip
  115.         h1 = 52dip
  116.         btnStart.Initialize("btnStart")
  117.         pnlControl.AddView(btnStart, w1, t1, w2, h1)
  118.         btnStart.Text = "Start"
  119.        
  120.         btnStop.Initialize("btnStop")
  121.         pnlControl.AddView(btnStop, w1 + w3, t1, w2, h1)
  122.         btnStop.Text = "Stop"
  123.        
  124.         btnSingleShot.Initialize("btnSingleShot")
  125.         pnlControl.AddView(btnSingleShot, w1 + 2 * w3, t1, w2, h1)
  126.         btnSingleShot.Text = "Single Shot"
  127.        
  128.         scvControl.Initialize(480dip)
  129.         pnlOcilloscope.AddView(scvControl, ScreenX1, pnlControl.Height, 100%x - ScreenX1, 100%y - pnlControl.Height)
  130.         scvControl.Panel.LoadLayout("controls")
  131.         scvControl.Panel.Width = scvControl.Width
  132.         Dim cbg As ColorDrawable
  133.         cbg.Initialize(Colors.RGB(196, 196, 255), 0)
  134.         scvControl.Panel.Background = cbg
  135.         scvControl.Color = Colors.RGB(196, 196, 255)
  136.        
  137.         pnlCurveTools.Initialize("")
  138.         pnlOcilloscope.AddView(pnlCurveTools, 0, ScreenY1, ScreenX1, 100%y - ScreenY1)
  139.         Dim cbg As ColorDrawable
  140.         cbg.Initialize(Colors.RGB(255, 196, 196), 0)
  141.         pnlCurveTools.Background = cbg
  142.        
  143.         pnlDispValues.Initialize("")
  144.         pnlOcilloscope.AddView(pnlDispValues, 0, ScreenY1, ScreenX1, 100%y - ScreenY1)
  145.         Dim cbg As ColorDrawable
  146.         cbg.Initialize(Colors.RGB(255, 236, 153), 0)
  147.         pnlDispValues.Background = cbg
  148.         pnlDispValues.Visible = False
  149.        
  150.         InitCurves
  151.         InitCalcCurves
  152.         InitSpinners
  153.  
  154.         Dim ww As Float
  155.         ww = pnlDispValues.Width / 4
  156.         lblValue0.Initialize("")
  157.         pnlDispValues.AddView(lblValue0, 0, 0, ww, pnlDispValues.Height)
  158.         lblValue0.TextColor = Curve(0).Color
  159.         lblValue0.Gravity = Gravity.CENTER_HORIZONTAL + Gravity.CENTER_VERTICAL
  160.        
  161.         lblValue1.Initialize("")
  162.         pnlDispValues.AddView(lblValue1, ww, 0, ww, pnlDispValues.Height)
  163.         lblValue1.TextColor = Curve(1).Color
  164.         lblValue1.Gravity = Gravity.CENTER_HORIZONTAL + Gravity.CENTER_VERTICAL
  165.        
  166.         lblValue2.Initialize("")
  167.         pnlDispValues.AddView(lblValue2, 2 * ww, 0, ww, pnlDispValues.Height)
  168.         lblValue2.TextColor = Curve(2).Color
  169.         lblValue2.Gravity = Gravity.CENTER_HORIZONTAL + Gravity.CENTER_VERTICAL
  170.        
  171.         lblValue3.Initialize("")
  172.         pnlDispValues.AddView(lblValue3, 3 * ww, 0, ww, pnlDispValues.Height)
  173.         lblValue3.TextColor = Curve(3).Color
  174.         lblValue3.Gravity = Gravity.CENTER_HORIZONTAL + Gravity.CENTER_VERTICAL
  175.        
  176.         For i = 0 To 3
  177.                 Dim cbx As CheckBox
  178.                 cbx.Initialize("cbxDrawCurve")
  179.                 pnlCurveTools.AddView(cbx, 6dip + i * 66dip, 0, 60dip, 50dip)
  180.                 cbx.Tag = i
  181.                 cbx.Text = " " & (i + 1)
  182.                 cbx.Typeface = Typeface.DEFAULT_BOLD
  183.                 cbx.Color = Curve(i).Color
  184.                 cbx.Checked = True
  185.         Next
  186.         lblScale0.Color = Curve(0).Color
  187.         lblScale1.Color = Curve(1).Color
  188.         lblScale2.Color = Curve(2).Color
  189.         lblScale3.Color = Curve(3).Color
  190.  
  191.         lblOffset0.Color = Curve(0).Color
  192.         lblOffset1.Color = Curve(1).Color
  193.         lblOffset2.Color = Curve(2).Color
  194.         lblOffset3.Color = Curve(3).Color
  195.        
  196.         edtOffset0.Text = Curve(0).Offset
  197.         edtOffset1.Text = Curve(1).Offset
  198.         edtOffset2.Text = Curve(2).Offset
  199.         edtOffset3.Text = Curve(3).Offset
  200.        
  201.         Select ScopeMode
  202.         Case "SCOPE"
  203.                 rbtScopeScope.Checked = True
  204.         Case "MEM"
  205.                 rbtScopeMEM.Checked = True
  206.         Case "ROLL"
  207.                 rbtScopeROLL.Checked = True
  208.         End Select
  209. End Sub
  210.  
  211. Sub Activity_Resume
  212.         InitGrid
  213. End Sub
  214.  
  215. Sub Activity_Pause (UserClosed As Boolean)
  216.         btnStop_Click
  217. End Sub
  218.  
  219. Sub InitGrid
  220.         Dim i As Int
  221.         Dim x, y As Float
  222.        
  223.         cvsScreen.DrawRect(rectScreen, ScreenCol, True, 1)
  224.         For i = 0 To NbDivY
  225.                 y = GridX0 + i * Div
  226.                 cvsScreen.DrawLine(GridX0, y, GridX1, y, GridLineCol, 1dip)
  227.         Next
  228.        
  229.         For i = 0 To NbDivX
  230.                 x = GridY0 + i * Div
  231.                 cvsScreen.DrawLine(x, GridY0, x, GridY1, GridLineCol, 1dip)
  232.         Next
  233.         pnlScreen.Invalidate
  234.  
  235.         cvsGraph.DrawRect(rectGrid, Colors.Transparent, True, 1)
  236.         pnlGraph.Invalidate
  237.        
  238.         cvsCursor.DrawRect(rectGrid, Colors.Transparent, True, 1)
  239.         pnlCursor.Invalidate
  240. End Sub
  241.  
  242. Sub btnStart_Click
  243.         Timer1.Enabled = True
  244.         SingleShot = False
  245.         Stopped = False
  246.         ii = -1
  247.         xx = -dx
  248.         EraseCurves
  249. End Sub
  250.  
  251. Sub btnStop_Click
  252.         Timer1.Enabled = False
  253.         SingleShot = False
  254.         Stopped = True
  255. End Sub
  256.  
  257. Sub btnSingleShot_Click
  258.         Timer1.Enabled = True
  259.         SingleShot = True
  260.         Stopped = False
  261.         ii = -1
  262.         xx = -dx
  263.         EraseCurves
  264. End Sub
  265.  
  266. Sub spnTimeScale_ItemClick (Position As Int, Value As Object)
  267.         dt = Value / 10
  268.         t = 0
  269.         Timer1.Initialize("Timer1", dt * 1000)
  270. End Sub
  271.  
  272. Sub spnScale_ItemClick (Position As Int, Value As Object)
  273.         Dim spn As Spinner
  274.        
  275.         spn = Sender
  276.         Curve(spn.Tag).Scale = Div / Value
  277. End Sub
  278.  
  279. Sub edtOffset_FocusChanged (HasFocus As Boolean)
  280.         Dim edt As EditText
  281.         Dim val As Double
  282.        
  283.         If HasFocus = False Then
  284.                 edt = Sender
  285.                 Curve(edt.Tag).Offset = edt.Text
  286.         End If
  287. End Sub
  288.  
  289. Sub rbtScope_CheckedChange(Checked As Boolean)
  290.         Dim rbt As RadioButton
  291.        
  292.         btnStop_Click
  293.         rbt = Sender
  294.         ScopeMode = rbt.Tag
  295.         btnStart_Click
  296. End Sub
  297.  
  298. Sub Timer1_Tick
  299.         Dim i, j As Int
  300.        
  301.         t = t + dt
  302.         xx = xx + dx
  303.         ii = ii + 1
  304.         If ii > 100 Then
  305.                 If SingleShot = True Then
  306.                         Timer1.Enabled = False
  307.                         SingleShot = False
  308.                         Stopped = True
  309.                         Return
  310.                 Else
  311.                         Select ScopeMode
  312.                         Case "MEM"
  313.                                 xx = 0
  314.                                 ii = 0
  315.                                 GetValues
  316.                                 DrawCurves
  317.                         Case "SCOPE"
  318.                                 EraseCurves
  319.                                 xx = 0
  320.                                 ii = 0
  321.                                 GetValues
  322.                                 DrawCurves
  323.                         Case "ROLL"
  324.                                 ii = 100
  325.                                 xx = 100 * dx
  326.                                 For i = 0 To 3
  327.                                         For j = 0 To 99
  328.                                                 CurveVal(i, j) = CurveVal(i, j + 1)
  329.                                         Next
  330.                                 Next
  331.                                 ScopeRolling = True
  332.                                 GetValues
  333.                                 DrawCurves
  334.                         End Select
  335.                         Return
  336.                 End If
  337.         End If
  338.         GetValues
  339.         DrawCurves
  340. End Sub
  341.  
  342. Sub DrawCurves
  343.         Dim i, j As Int
  344.         Dim r1, r2 As Rect
  345.         Dim x, yy1(4), yy2(4) As Float
  346.        
  347.         If SingleShot = False Then
  348.                 Select ScopeMode
  349.                 Case "MEM"
  350.                         r1.Initialize(xx, 0, xx + dx, GridH)
  351.                         cvsGraph.DrawRect(r1, Colors.Transparent, True, 1)
  352.                 Case "ROLL"
  353.                         If ScopeRolling = True Then
  354.                                 cvsGraph.DrawRect(rectGrid, Colors.Transparent, True, 1)
  355.                                 For i = 0 To CurvesNb
  356.                                         If Curve(i).Draw = True Then
  357.                                                 yy1(i) = GridYm + (-Curve(i).Offset - CurveVal(i, 0)) * Curve(i).Scale
  358.                                                 For j = 1 To 99
  359.                                                         x = j * dx
  360.                                                         yy2(i) = GridYm + (-Curve(i).Offset - CurveVal(i, j)) * Curve(i).Scale
  361.                                                         cvsGraph.DrawLine(x - dx, yy1(i), x, yy2(i), Curve(i).Color, Curve(i).Width)
  362.                                                         yy1(i) = yy2(i)                        
  363.                                                 Next
  364.                                         End If
  365.                                 Next
  366.                         End If
  367.                 End Select
  368.         End If
  369.         For i = 0 To CurvesNb
  370.                 If Curve(i).Draw = True Then
  371.                         y2(i) = GridYm + (-Curve(i).Offset - CurveVal(i, ii)) * Curve(i).Scale
  372.                         If ii > 0 Then
  373.                                 cvsGraph.DrawLine(xx - dx, y1(i), xx, y2(i), Curve(i).Color, Curve(i).Width)
  374.                         End If
  375.                         y1(i) = y2(i)
  376.                 End If
  377.         Next
  378.         pnlGraph.Invalidate
  379.         DoEvents
  380. End Sub
  381.  
  382. Sub GetValues
  383.         Dim i As Int
  384.        
  385.         For i = 0 To CurvesNb
  386.                 CurveVal(i, ii) = a(i) * Sin(w(i) * t)
  387.         Next
  388. End Sub
  389.  
  390. Sub EraseCurves
  391.         cvsGraph.DrawRect(rectGrid, Colors.Transparent, True, 1)
  392. End Sub
  393.  
  394. Sub cbxDrawCurve_CheckedChange(Checked As Boolean)
  395.         Dim cbx As CheckBox
  396.         cbx = Sender
  397.         Curve(cbx.Tag).Draw = Checked
  398. End Sub
  399.  
  400. Sub pnlCursor_Touch (Action As Int, X As Float, Y As Float) As Boolean 'Return True to consume the event
  401.         If Stopped = False Then
  402.                 Return
  403.         End If
  404.        
  405.         Select Action
  406.         Case Activity.ACTION_DOWN
  407.                 pnlDispValues.Visible = True
  408.                 cx = X
  409.                 If X >= 0 AND X <= GridW Then
  410.                         DrawCursor(X)
  411.                         DispValues(X)
  412.                 End If
  413.         Case Activity.ACTION_MOVE
  414.                 If X >= 0 AND X <= GridW Then
  415.                         DrawCursor(X)
  416.                         DispValues(X)
  417.                 End If
  418.         Case Activity.ACTION_UP
  419.                 cvsCursor.DrawLine(cx, 0, cx, GridH, Colors.Transparent, 1)
  420.                 pnlCursor.Invalidate
  421.                 pnlDispValues.Visible = False
  422.         End Select     
  423.         Return True
  424. End Sub
  425.  
  426. Sub DrawCursor(x As Float)
  427.         cvsCursor.DrawLine(cx, 0, cx, GridH, Colors.Transparent, 1)
  428.         cx = x
  429.         cvsCursor.DrawLine(cx, 0, cx, GridH, Colors.Red, 1)
  430.         pnlCursor.Invalidate
  431. End Sub
  432.  
  433. Sub DispValues(x As Int)
  434.         Dim i As Int
  435.  
  436.         i = 100 / GridW * x
  437.         lblValue0.Text = NumberFormat(CurveVal(0, i), 1, 6)
  438.         lblValue1.Text = NumberFormat(CurveVal(1, i), 1, 6)
  439.         lblValue2.Text = NumberFormat(CurveVal(2, i), 1, 6)
  440.         lblValue3.Text = NumberFormat(CurveVal(3, i), 1, 6)
  441. End Sub
  442.  
  443. Sub InitCurves
  444.         Curve(0).Color = Colors.Red
  445.         Curve(1).Color = Colors.Blue
  446.         Curve(2).Color = Colors.Black
  447.         Curve(3).Color = Colors.RGB(64, 192, 0)
  448.  
  449.         Curve(0).Width = 1dip
  450.         Curve(1).Width = 1dip
  451.         Curve(2).Width = 1dip
  452.         Curve(3).Width = 1dip
  453.        
  454.         Curve(0).Scale = 20
  455.         Curve(1).Scale = 20
  456.         Curve(2).Scale = 20
  457.         Curve(3).Scale = 20
  458.  
  459.         Curve(0).Offset = 0
  460.         Curve(1).Offset = 1
  461.         Curve(2).Offset = -1
  462.         Curve(3).Offset = 2
  463.        
  464.         Curve(0).Draw = True
  465.         Curve(1).Draw = True
  466.         Curve(2).Draw = True
  467.         Curve(3).Draw = True
  468. End Sub
  469.  
  470. Sub InitCalcCurves
  471.         w(0) = 2 * cPI * 2.1
  472.         w(1) = 2 * cPI * 3.7
  473.         w(2) = 2 * cPI * 4.3
  474.         w(3) = 2 * cPI * 5.7
  475.        
  476.         a(0) = 1.0
  477.         a(1) = 2.0
  478.         a(2) = -1.0
  479.         a(3) = 1.5
  480. End Sub
  481.  
  482. Sub InitSpinners
  483.         Dim i As Int
  484.        
  485.         TimeScale(0) = 10
  486.         TimeScale(1) = 5
  487.         TimeScale(2) = 2
  488.         TimeScale(3) = 1
  489.         TimeScale(4) = 0.5
  490.         TimeScale(5) = 0.2
  491.         TimeScale(6) = 0.1
  492.         TimeScale(7) = 0.05
  493.         TimeScale(8) = 0.02
  494.         TimeScale(9) = 0.01
  495.  
  496.         SignalScale(0) = 10
  497.         SignalScale(1) = 5
  498.         SignalScale(2) = 2
  499.         SignalScale(3) = 1
  500.         SignalScale(4) = .5
  501.         SignalScale(5) = .2
  502.         SignalScale(6) = .1
  503.         SignalScale(7) = .05
  504.         SignalScale(8) = .02
  505.         SignalScale(9) = .01
  506.  
  507.         For i = 0 To 9
  508.                 spnTimeScale.Add(TimeScale(i))
  509.                 spnScale0.Add(SignalScale(i))
  510.                 spnScale1.Add(SignalScale(i))
  511.                 spnScale2.Add(SignalScale(i))
  512.                 spnScale3.Add(SignalScale(i))
  513.         Next
  514.         spnTimeScale.SelectedIndex = 6
  515.        
  516.         spnScale0.SelectedIndex = 3
  517.         spnScale1.SelectedIndex = 3
  518.         spnScale2.SelectedIndex = 3
  519.         spnScale3.SelectedIndex = 3
  520.         Curve(0).Scale = Div / spnScale0.SelectedItem
  521.         Curve(1).Scale = Div / spnScale1.SelectedItem
  522.         Curve(2).Scale = Div / spnScale2.SelectedItem
  523.         Curve(3).Scale = Div / spnScale3.SelectedItem
  524. End Sub
Best regards, Engr. Lyndon R. Bermoy IT Instructor/System Developer/Android Developer STI College - Surigao City 09126450702 [email protected] Follow and add me in my Facebook Account: https://www.facebook.com/donzzsky Visit my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment