Android Question LED bar VU meter

freetoair

Member
Licensed User
Longtime User
Can someone help me change the progress bar software in LED bar VU meter. Digitized sound coming from the Arduino via serial port?
 

DonManfred

Expert
Licensed User
Longtime User
You are asking about WHICH library? You should post a link to the library or maybe just upload your project so we can see the dependencies.
 
Upvote 0

freetoair

Member
Licensed User
Longtime User
Firstly my problem is very complex. I have Android which is part of the defective machine for car diagnostics. After many attempts, I managed to get him to work as a "normal" Android machine. I am fascinated by the graphics capabilities of Android, but as hardware men can not easily understand the use of graphics in B4A. So far I have used the Arduino with some libraries for FFT and I managed to get results at this level. Now I'd attempted to show the level of the signal to start. I tried to use VUmeter but B4A constantly returned a message that's missing library audiorecording although I entered and turned on. My every attempt to graphical display LED VU meters resulted in unusable low speed response. Considering that on my Android machines have no audio input I use a stream from arduino through the serial port.
 
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
If you prefer a purely owner-drawn solution, you can always do something like this:

ledmeter.png


B4X:
Sub Process_Globals
End Sub
Sub Globals
Private moButton As Button
Private moSlider As SeekBar
Private moActCan As Canvas
End Sub
Sub Activity_Create(FirstTime As Boolean)
moButton.Initialize("btnExit")
Activity.AddView(moButton, Activity.Width - 105dip, Activity.Height - 55dip, 100dip, 50dip)
moButton.Text = "EXIT"
moSlider.Initialize("skbValue")
Activity.AddView(moSlider, 30dip, (moButton.Top - 55dip), (Activity.Width - 60dip), 50dip)
moSlider.Visible = True
moSlider.Max = 100
moSlider.Value = 0
moActCan.Initialize(Activity)
moActCan.DrawColor(Colors.Black)
End Sub
Sub Activity_Resume
skbValue_ValueChanged(moSlider.Value, False)
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Private Sub btnExit_Click
Activity.Finish
End Sub
private Sub skbValue_ValueChanged(Value As Int, UserChanged As Boolean)
Dim poRect As Rect
poRect.Initialize(20dip, 20dip, Activity.Width - 40dip, 60dip)
DrawLEDMeter(moActCan, poRect, Value)
' do this so the changes we drew are updated, if you're using a panel or whatever change from "activity" to that.
Activity.Invalidate
End Sub
' Draws LED Meter. CurrValue is percent value, 0 to 100.
Sub DrawLEDMeter(oCan As Canvas, ControlPosition As Rect, CurrValue As Int)
Dim poBorderGD As GradientDrawable
Dim piBorderCol(3) As Int = Array As Int(Colors.RGB(239, 239, 239), Colors.RGB(126, 126, 126), Colors.RGB(126, 126, 126))
Dim poHighlightGD As GradientDrawable
Dim piHighlightCol(2) As Int = Array As Int(Colors.ARGB(83, 250, 250, 250), Colors.ARGB(63, 250, 250, 250))
Dim poRect1 As Rect
Dim piX As Int
Dim piY As Int
Dim piBaseX As Int
Dim piBaseY As Int
Dim piWidth As Int
Dim piHeight As Int
Dim piSegWidth As Int
Dim piRight As Int
Dim piBottom As Int
Dim piBorderWidth As Int = 5dip
Dim piGlassOffset As Int = 8dip
Dim piLEDOffset As Int = 5dip
' how much to round each bar segment
Dim piCornerRad As Int = 3dip
' number of bar segments
Dim piSegments As Int = 25
Dim piGreenOn(3) As Int = Array As Int(Colors.RGB(0, 161, 0), Colors.RGB(0, 255, 0), Colors.RGB(0, 161, 0))
Dim piGreenOff(3) As Int = Array As Int(Colors.RGB(0, 81, 0), Colors.RGB(0, 81, 0), Colors.RGB(0, 81, 0))
Dim piRedOn(3) As Int = Array As Int(Colors.RGB(161, 0, 0), Colors.RGB(255, 0, 0), Colors.RGB(161, 0, 0))
Dim piRedOff(3) As Int = Array As Int(Colors.RGB(81, 0, 0), Colors.RGB(81, 0, 0), Colors.RGB(81, 0, 0))
Dim piYellowOn(3) As Int = Array As Int(Colors.RGB(161, 161, 0), Colors.RGB(255, 250, 0), Colors.RGB(161, 161, 0))
Dim piYellowOff(3) As Int = Array As Int(Colors.RGB(81, 81, 0), Colors.RGB(81, 81, 0), Colors.RGB(81, 81, 0))
Dim piLed As Int
Dim piCurrVal As Int
Dim piDrawVal As Int
Dim poGD As GradientDrawable
Dim piDrawWidth As Int = ControlPosition.Right - ControlPosition.Left + 1
Dim piDrawHeight As Int = ControlPosition.Bottom - ControlPosition.Top + 1
' Color bar percentages
Dim piGreenMax As Int = 60
Dim piYellowMax As Int = 80
Dim piDrawPercent As Int
' Color to draw in loop
Dim piDrawColors() As Int
' draw gray color gradient to serve as border
poBorderGD.Initialize("TOP_BOTTOM", piBorderCol)
poBorderGD.CornerRadius = 5dip
oCan.DrawDrawable(poBorderGD, ControlPosition)
' black out "intierior" of control
poRect1.Initialize((ControlPosition.Left + piBorderWidth), (ControlPosition.Top + piBorderWidth), (ControlPosition.Left + piDrawWidth - piBorderWidth), (ControlPosition.Top + piDrawHeight - piBorderWidth))
oCan.DrawRect(poRect1, Colors.Black, True, 1dip)
' validate percentage parameter
If CurrValue < 0 Then
piCurrVal = 0
Else
If CurrValue > 100 Then
piCurrVal = 100
Else
piCurrVal = CurrValue
End If
End If
' calculate LED poisition
piBaseX = ControlPosition.Left + piBorderWidth + piLEDOffset
piBaseY = ControlPosition.Top + piBorderWidth + (piLEDOffset / 2)
piWidth = piDrawWidth - ((piBorderWidth + piLEDOffset) * 2)
piHeight = piDrawHeight - ((piBorderWidth + (piLEDOffset / 2)) * 2)
piSegWidth = (piWidth / piSegments) + 0.5
piBaseX = piBaseX + ((piWidth + 3dip - (piSegWidth * piSegments)) / 2)
' draw LEDs
For piLed = 0 To piSegments - 1
piDrawPercent = (piLed / (piSegments - 1)) * 100
If piDrawPercent <= piGreenMax Then
' GREEN BAR
If piCurrVal > piDrawPercent Then
piDrawColors = piGreenOn
Else
piDrawColors = piGreenOff
End If
Else
If piDrawPercent <= piYellowMax Then
' YELLOW BAR
If piCurrVal > piDrawPercent Then
piDrawColors = piYellowOn
Else
piDrawColors = piYellowOff
End If
Else
' RED BAR
If piCurrVal >= piDrawPercent Then
piDrawColors = piRedOn
Else
piDrawColors = piRedOff
End If
End If
End If
poGD.Initialize("TOP_BOTTOM", piDrawColors)
poGD.CornerRadius = piCornerRad
piX = piBaseX + (piLed * piSegWidth)
piY = piBaseY
piRight = piX + piSegWidth - 3dip
piBottom = piY + piHeight
poRect1.Initialize(piX, piY, piRight, piBottom)
oCan.DrawDrawable(poGD, poRect1)
piDrawVal = piDrawVal + 1
Next
' draw "glass reflection" on top of image
poHighlightGD.Initialize("TOP_BOTTOM", piHighlightCol)
poHighlightGD.CornerRadius = 5dip
piX = ControlPosition.Left + piGlassOffset
piY = ControlPosition.Top + piGlassOffset
piRight = piX + piDrawWidth - (piGlassOffset * 2)
piBottom = piY + ((piDrawHeight - (piGlassOffset * 2)) * 0.33) ' 1/3 area reflection
poRect1.Initialize(piX, piY, piRight, piBottom)
oCan.DrawDrawable(poHighlightGD, poRect1)
End Sub

You should be able to alter the parameters in this to produce the style of meter you are interested in displaying.
 
Upvote 0
Top