7/20 Viewfinder面白かった
お手玉のガワを作るため、
ただのカプセルにFusionでねじを付けたやつを印刷します。そのまま印刷しても入らないのでナット側を0.3mm凹ませました。参考
完成品はこちら。
ちょっと隙間があるのは、時間短縮のためにサポート材無しで印刷したらぐちゃぐちゃになってしまったからです。ねじ山の方はサポート材あったほうがいいかも。
BLEでスティックとボタンの値を送信
プログラムはこんな感じ。サービスUUIDはハートレートプロファイルを使っているので、nRF Connectではハートマークが表示されます。
#include <ArduinoBLE.h>
BLEService myService("180D");
BLEStringCharacteristic stickCharacteristic("2A37", BLERead | BLENotify, 20);
BLEIntCharacteristic switchCharacteristic("2A38", BLERead | BLENotify);
const int stickXPin = A0;
const int stickYPin = A1;
const int stickPushPin = 2;
const int switchPin = 3;
void setup() {
Serial.begin(9600);
while (!Serial);
pinMode(stickXPin, INPUT);
pinMode(stickYPin, INPUT);
pinMode(stickPushPin, INPUT);
pinMode(switchPin, INPUT);
if (!BLE.begin()) {
Serial.println("starting BLE failed!");
while (1);
}
BLE.setLocalName("MyDevice");
BLE.setAdvertisedService(myService);
myService.addCharacteristic(stickCharacteristic);
myService.addCharacteristic(switchCharacteristic);
BLE.addService(myService);
BLE.advertise();
Serial.println("Bluetooth device active, waiting for connections...");
}
void loop() {
BLEDevice central = BLE.central();
if (central) {
Serial.print("Connected to central: ");
Serial.println(central.address());
while (central.connected()) {
int stickX = analogRead(stickXPin);
int stickY = analogRead(stickYPin);
int stickPush = digitalRead(stickPushPin);
int switchState = digitalRead(switchPin);
String stickData = String(stickX) + "," + String(stickY) + "," + String(stickPush);
stickCharacteristic.writeValue(stickData);
switchCharacteristic.writeValue(switchState);
Serial.print("Stick Data: " + stickData);
Serial.println("Switch State: " + String(switchState));
}
Serial.print("Disconnected from central: ");
Serial.println(central.address());
}
}
で、BLE Utilitiesプラグインを使って作ったアプリで接続してみると...
こんな感じで、Dataがすべて数字になっています。Arduinoのプログラムでは文字列で送信しているので、アプリ側に問題があります。
UE C++でバイト配列を文字列に変換する
残念ながらブループリントではできないっぽいので、UE C++を扱うためにVisual Studioを入れるとこから始めます。
UE4.27はVisual Studio 2019です。プログラムは以下のような感じになります。
FString UYourClass::BytesToString(const TArray<uint8>& InArray)
{
FString Result;
for (auto InChar : InArray)
{
Result += FString::Printf(TEXT("%c"), InChar);
}
return Result;
}
#pragma once
#include "CoreMinimal.h"
#include "YourClass.generated.h"
UCLASS()
class YOURPROJECT_API UYourClass : public UObject
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable, Category = "Your Category")
static FString BytesToString(const TArray<uint8>& InArray);
};
ChatGPT Plusにお金を払い続けているので有効活用しました。プログラマいらない
実行結果とキャラクタリスティックを2つ購読する方法は明日に回して帰ります。