mit-cml / appinventor-extensions

Source code of extensions published for MIT App Inventor
Apache License 2.0
87 stars 96 forks source link

Test existing micro bit LED project and upgrade it to work with micro bit V2 if needed. #51

Open ellelili2025 opened 3 years ago

ellelili2025 commented 3 years ago

Please make sure the attached micro bit LED project work with latest micro bit V2. MicrobitLED.aia.zip

Instructions: http://iot.appinventor.mit.edu/assets/howtos/MIT_App_Inventor_Microbit_LED.pdf

xyyshh commented 3 years ago

Dear Li,

I found that the function of showing text on the LED matrix on micro:bit doesn't work. I tried to fix it by using the blocks in Microbit_Uart extension instead of Microbit_Led extension. But I didn't find the reason why the blocks in picture 1 do not work.

May I ask whether it is ok to use the blocks in Microbit_Uart extension (picture 2) to realize the function of scrolling text on the LED matrix? It can achieve the expected result in the document.

picture 1: image

picture 2: image

Thank you!

Best regards, Ying

ellelili2025 commented 3 years ago

@xyyshh I would like to at least get a full understanding of why writeLEDText doesn't work. I will take a look and let you know.

xyyshh commented 3 years ago

@ellelili2025 Hi Li. I find a way to make writeLEDText work by editing the source code. I changed two parts of the file "Microbit_Led.java" at appinventor-extensions/appinventor/components/src/com/bbc/microbit/profile/.

  1. I changed some variables from type "String" to type "Integer". The code segments are from line 57 to line 63.

Initial code:

private final BluetoothLE.BLEResponseHandler<String> lEDTextHandler = new BluetoothLE.BLEResponseHandler<String>() { @Override public void onWrite(String serviceUuid, String characteristicUuid, List<String> values) { WroteLEDText(values.get(0)); } };

Revised code:

private final BluetoothLE.BLEResponseHandler<Integer> lEDTextHandler = new BluetoothLE.BLEResponseHandler<Integer>() { @Override public void onWrite(String serviceUuid, String characteristicUuid, List<Integer> values) { WroteLEDText(values); } };

  1. I changed the method of sending data to Micro:bit from sending String to sending Bytes. The code segments are from line 194 to line 200.

Initial code:

public void WriteLEDText(final String LED_Text_Value) { if (bleConnection != null) { bleConnection.ExWriteStringValuesWithResponse(LED_SERVICE_UUID, LED_TEXT_CHARACTERISTIC_UUID, false, LED_Text_Value, lEDTextHandler); } else { reportNullConnection("WriteLEDText"); } }

Revised code:

public void WriteLEDText(final String LED_Text_Value) { if (bleConnection != null) { bleConnection.ExWriteByteValuesWithResponse(LED_SERVICE_UUID, LED_TEXT_CHARACTERISTIC_UUID, false, LED_Text_Value, lEDTextHandler); } else { reportNullConnection("WriteLEDText"); } }

I think the main bug is that the BLE extension cannot directly send strings to microbit. Instead, it can send characters by sending bytes. That is also how the microbit Uart extension works. I tried to find why function "ExWriteStringValuesWithResponse()" in BLE extension doesn't work but I still have no idea about it.

I generated an .aix file from the revised code and tested it in app inventor. It works well to scroll text on micro:bit with no need to change the app inventor code and the tutorial. The initial code, revised code, .aix file are also attached: codeand.aix.zip

I'm not sure whether my changes to the code are proper, while it works well. Could you please take a look at my code? Thank you very much.

Regards, Ying

xyyshh commented 3 years ago

@ellelili2025 Hi Li. I've tested the LED extension on microbit V1. For the writeLEDText function, the V1 also doesn't work with the original extension, while it works well with modified version. The other functions works well. Thanks.