how to display audio levels from simple bluetooth char values

6,489 views
Skip to first unread message

Ben Biles

unread,
Apr 30, 2019, 10:31:04 PM4/30/19
to MIT App Inventor Forum
i'd like to display audio levels as bars increasing and decreasing in length from simple 1 char values coming in from BT.

0x00 -> 0xff

Does anyone have an example of how this can be done? sort of a realtime graph?

Would i use sprites for this ? 

Ben Biles

unread,
May 1, 2019, 2:54:09 AM5/1/19
to MIT App Inventor Forum
I will try wiggling a few sprites about on long thin canvases to the incoming audio level data and report back here if it works.

Screen Shot 2019-05-01 at 15.47.31.png

ABG

unread,
May 1, 2019, 10:41:47 AM5/1/19
to MIT App Inventor Forum
It's important to capture the incoming byte value (0-255) into a variable,
and use the variable value for the bar display calculations.

Asking BlueTooth for more values during the calculation
would otherwise lose the previous value before you could plot it.

If you want to display a bar vertically, its height would be
(Incoming Byte / 256) * Canvas.Height

Remember that y values extend downwards, not upwards on a Canvas,
so adjust your math.

I would use individual canvases for each bar if there are only a few of them,
and erase the canvas before drawing a fat line for the bar.

You could alternatively erase a bar by drawing a long bar with the background color.

ABG


Ben Biles

unread,
May 2, 2019, 12:50:19 AM5/2/19
to mitappinv...@googlegroups.com
wow , that's great advice , thanks !

I'll make some variables and copy BT values into them before moving the sprite!

i'm doing 8 horizontal bars so I'll try  (Incoming Byte / 256) * Canvas.Width

I've never cleared a screen before. I imagine its just an option in the blocks.

I have 8 fill parent width canvases 6pix wide. 
I totally over looked having to clear the bar before drawing then next !

I'm just figuring out how to send the bytes over using DMA and trying to decide how many bytes per second each
meter should have. I'm thinking i'll try sending about 24 bytes per second per channel with a 3 byte packet header
to ID the packet of the 8 channels. 195bytes per second sounds reasonable for BT.

I would like to display something like 24 meters at once! not sure app inventor is fast enough for that!
I guess the BT connection can do 600Bytes per sec without to much trouble but more to do with drawing that many bars and clearing
the screens possibly.

I write back here when I get it working with pic of final block design.

EDIT , just realised your saying draw a fat line rather than moving a sprite...
OK, will try that, no idea how to draw a line :)





Screen Shot 2019-05-02 at 14.47.19.png

ABG

unread,
May 2, 2019, 3:09:59 PM5/2/19
to MIT App Inventor Forum
For drawing a bar on a Canvas, see
where I draw a Pong paddle as a fat line.
(Line width was pre-set wide.)

If you are building your own packets, 
and using pre-BLE BlueTooth and a Clock Timer,
be prepared to receive partial packets and to
assemble them in a buffer (global list). The BlueTooth
Get List of Bytes block would be appropriate,
along with the List blocks for Append (incoming bytes),
get Length of List (to see if enough arrived yet in buffer),
 Select List Item (to look at them),
 and Remove List Item (to flush used data from slot 1 of buffer)

Look in the Lists section of FAQ
for How to Work With Lists.

ABG

Ben Biles

unread,
May 3, 2019, 11:28:27 AM5/3/19
to MIT App Inventor Forum
Hi ,

The get list of bytes block sounds good. I'm assuming lists are basically arrays and you can somehow loop over them but haven't learnt how yet. 

for now i'll just have a simple text string as the header followed by 8 bytes ( 1 byte per audio meter ) I'll try receiving proper packets later.

i'm using 2 timers , one for sending BT data and one for receiving. I have the BT receive timer set to 100, but I guess it will need to be faster for realtime audio monitoring. priority on timer interrupts or app inventor 1st come 1st serve?






ABG

unread,
May 3, 2019, 12:39:09 PM5/3/19
to MIT App Inventor Forum
I'm assuming lists are basically arrays and you can somehow loop over them but haven't learnt how yet.


'm using 2 timers , one for sending BT data and one for receiving. I have the BT receive timer set to 100, but I guess it will need to be faster for realtime audio monitoring. priority on timer interrupts or app inventor 1st come 1st serve?


Multiple Clocks is a gamble, if you are time dependent.


A scheduling alternative:

The Clock1.SystemTime block provides a numerical value in milliseconds from 1970
for an Instant, easiest for math operations like setting deadlines for
different actions in different global variables.
That lets you use a single fast Clock Timer to examine a set of global
variables with future deadlines and associated actions when due.

Each Timer event, traverse the global variables round robin, checking each against
 Clock1.SystemTime to see if it's past due.  If it's past due, reset its global variable to the next 
deadline for that action, then perform the action.
Keep the most urgent deadlines early in the traversal list.

ABG

Ben Biles

unread,
May 4, 2019, 11:10:41 AM5/4/19
to MIT App Inventor Forum
I can see your describing a schedular but don't understand how you implement that in blocks.

does Clock1.SystemTime count up or down? how do I trigger the interrupt? with an if statement ? 
how do you check each Clock1.SystemTime to see if it's past due? do you have a block example of 2 routines being triggered by Clock1.SystemTime ?

for instance with BT  RX and TX routines do I just test with if statement Clock1.SystemTime for 2 values ( different points in time ) to make interrupt. what am i comparing to see if something is past due?

sorry if I'm missing something obvious here :)

ABG

unread,
May 4, 2019, 4:36:29 PM5/4/19
to MIT App Inventor Forum
Here's a scheduler for the only hardware available to me, myself.

It's a silly exercise routine, where I have to perform irregularly spaced actions,
signalling by button press completion of the action.

The frequency of the action requests is hard wired into the Clock1.Timer code.

ABG
blocks.png
Capture.PNG
Designer.PNG
exercise.aia

Ben Biles

unread,
May 4, 2019, 9:52:33 PM5/4/19
to MIT App Inventor Forum
That's great, thanks so much!
I'll try putting my BT tx rx routines into a cue with timer1 and get rid of the multiple clocks.

I'm thinking the priority is just how much time is dedicated to one routine compared to the others.


Reply all
Reply to author
Forward
0 new messages