Here is a set of value procedures you can use and customize.
The result is a list of decimal values you can send using the BT SendBytes block.
Test procedures are included. math.aia (36.7 KB)
Hello, thank you for your attention to this topic. I need some time to study and test the ones presented here. I want to ask you how I can use the math.aia file in AppInventor? I can attach the code used in arduino that sends correctly data to STM32. I want to replace arduino with a HC-05 bluetooth module for wireless communication. In HC-05 I can't change the code. HC-05 will take the packets from BT and send them on the serial interface to STM, these packets must be similar to those sent by arduino on the serial. P.S. CRC in the sense of checksum arduino send serial.txt (672 Bytes)
The blocks I posted are draggable directly into the blocks editor, all except the screen clips. Or you can find them in my math project and copy them through the back pack.
HC-05 is simply a Bluetooth module for communication of data between devices. The STM32 receives the BT data packets via the HC-05. The STM32 needs a script to define how to receive and interpret the data (and what to do with it, such as trigger an event).
There is also the possibility to change the code in STM32 but then I will have to take into account these changes when I change the firmware versions on STM32. And I don't want that.
There is a small bug in negative numbers (-200 Segment: Start (1) + length (8) -1 exceeds text lenght (4)) Here I tried to modify get8BitsAsDecimal to use at min length (0, length-index) but there are still some issues (negative index, "" not decimal value for length 0)
I built test applications on arduino and compared the 2 Arduino / AppInventor BT streams
Remarks:
A.
On the Arduino
-if Dir / Speed = 0 is sent bit_4 / bit_6 = 0
1010101111001101, 0, 0, 1010101111001101
-if Dir = 30 / Speed = 50
1010101111001101, 11110, 110010, 1010101111100001
-for values 255 bit_3 / bit _5 are 0 and do not need to be sent
1010101111001101, 11111111, 11111111, 1010101111001101
-for values 256 bit_3 / bit _5 are 1
1010101111001101, 100000000, 100000000, 1010101111001101
-for values 511/512
1010101111001101, 111111111, 1000000000, 1010100000110010
On BT
-if Dir / Speed = 0
1010101111001101, 0, 0, 0, 0, 1010101111001101
-for values 255 bit_3 / bit _5 are 0
1010101111001101, 0, 11111111, 0, 11111111, 1010101111001101
B.
There are some differences at bit_7, bit_8
for dir = 200 and speed = 30 arduino calculates checksum
1010101100011011
and the AppInventor application sends via BT
1010101111011
I noticed differences at bit_8
00011011 arduino vs
11011 AppInventor
Conclusions:
In the serial package there is a bit separator. On Arduino it appears every 2 bytes. On BT it appears after every byte.
I filled each bit in the list with a binary value (00101011). If it starts with 0 bits 0 in front are not sent, and when bit_7 is soldered with bit_8 0 is lost in the middle.
For my problem I need Cal.BT.Send2bytes.List otherwise even if I build a dynamic list with separator every 2 bytes I don't have a component to send it to BT (Set.BT.BufferSize, Cal.BT.SendData).
Practically now the problem is unresolved with AppInventor in its current form.
It's 2.10 AM, thank you for your help and I wish you a good night's sleep!
The solution does not depend on me and only solves the problem of those who will send x2Bytes packages, probably a small group of programmers. Ideally I could build structures of 1,2, ...n in bytes and send them by Cal.BT.SendData.
The code on STM is open source can be modified but will create incompatibilities with future versions that are not created by me.
Additional arduino hardware interposed between HC-05 and STM.
HC-05 sends 4 packets of 2bytes of type Dir, value / Speed, value arduino separates the values for Dir and Speed packs them in the structure known for STM and sends them on the serial interface to it. Then it receives the fedback from STM and sends it to HC-05 to get to AppInventor.
Until new components appear in AppInventor, I think I will choose solution 3. I wish you a wonderful day.
I have no guesses for what's happening in the BlueTooth layer, but if you post Download Blocks Images of your copies of the conversion procedures, I can double check those.
After reviewing your test results, I suspect you are relying on the AI2 CONVERT BASE 10 TO BINARY block to inspect bit patterns.
Unfortunately, that block has a 'helpful' feature where it removes leading 0's from its output. I can't blame it, because it is meant to work with non-negative integers of unlimited size, so it has no concept of byte boundaries that would be needed to do left padding with 0's.
Using that block to generate your bit level dumps would cause the shortening of bit strings that you report.
After sleeping on your problem, I see how to calculate Most Significant Byte (MSB) and Least Significant Byte (LSB) of an integer in the range -(2^15) to +(2^15) without using AI2 base 10 to binary conversion, using the trick of adding 2^16 to the input and masking (bitwise AND) and shifting (QUOTIENT/256). Code....
... actually, not out of the woods on the BT side of things:
SendBytes(list) Takes each element from the given list, converts it to a String, decodes the String to an integer, and writes it as one byte to the output stream.*
If an element could not be decoded to an integer, or the integer would not fit in one byte, then the Form’s Error Occurred event is triggered and this method returns without writing any bytes to the output stream.
Prof_Info needs all the values to be delivered in one BT packet (max 20bytes).
So we need an extension to send a list of 2 Byte numbers.
Hi, the project for STM is here https://github.com/EFeru/hoverboard-firmware-hack-FOC. (USART) Let me tell you something about my project. I am studying the possibility of making a wireless remote controllable vehicle using the platform of a hoverboard .
The aim of the project is to learn and understand more about electric vehicles and to share these with groups of students in STEM classes. I managed for the moment to send text (Dir200Speed500) with AppInventor via BT. This text is taken over by arduino and processed with Dir = BTSerial.parseInt (), Speed = BTSerial.parseInt () which extracts the 2 values and sends them serially to STM.Robo now executes commands to change the direction of direction and speed but is far from being able to be controlled. Carefully code inside the STM to use gyroscopic sensors in position and distance control possibly adding new sensors. There is much to study and learn from here. I was very pleased with the ease with which you can work with AppInventor to have a lot of knowledge about the soft implementation of hardware components. It is a wonderful thing especially for beginners (students) but I accidentally discovered some limitations. Last night I tried to use a Slider I couldn't manage to position it vertically. All this should be pointed out somewhere to make AppInventor better. It would be interesting to have an AppInventor that works locally without a Scratch-like internet connection. I solved my problem with Robo. The topic can remain open only for the initial discussion how we can control the data sent through BT.