05-03-2024, 07:45 AM
#include <Preferences.h>
Preferences prefs;
#include <wukongM700.h>
PointsTracking irtracker(4);
int irPoints[12];
int camx, camy;
uint8_t threshold = 200;
uint8_t delaytime = 20;
uint8_t gunid = 1;
String sentmess = "GG";
uint8_t touchKey[] = {0, 1, 2, 3, 8, 9, 10};
uint8_t keynum = 7;
uint8_t touchValue = 0;
uint8_t oldtouched = 0;
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
BLEServer *pServer = NULL;
BLECharacteristic * pTxCharacteristic;
bool deviceConnected = false;
bool oldDeviceConnected = false;
// See the following for generating UUIDs:
// https://www.uuidgenerator.net/
#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
};
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
}
};
class MyCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
String rxValue = pCharacteristic->getValue();
if (rxValue.length() > 0) {
Serial.print("serial received:" ); Serial.println(rxValue);
int head = rxValue.indexOf("AA");
int tail = rxValue.indexOf("BB");
if (head > -1 && tail > head) {
rxValue = rxValue.substring(head + 2 , tail);
//Serial.print("serial received:" ); Serial.println(addata);
int comdatal = rxValue.length();
if (comdatal > 1) {
switch (rxValue[0]) {
case 'T':
prefs.begin("gun", false);
threshold = rxValue.substring(1).toInt();
Serial.print("threshold:" ); Serial.println(threshold);
prefs.putInt("threshold", threshold);
prefs.end();
irtracker.set_threshold(threshold);
break;
case 'W':
prefs.begin("gun", false);
camx = rxValue.substring(1).toInt();
Serial.print("camx:" ); Serial.println(camx);
prefs.putInt("camx", camx);
prefs.end();
irtracker.set_xpixel(camx);
break;
case 'H':
prefs.begin("gun", false);
camy = rxValue.substring(1).toInt();
Serial.print("camy:" ); Serial.println(camy);
prefs.putInt("camy", camy);
prefs.end();
irtracker.set_ypixel(camy);
break;
case 'S':
prefs.begin("gun", false);
delaytime = rxValue.substring(1).toInt();
Serial.print("delytime:" ); Serial.println(delaytime);
prefs.putInt("speed", delaytime);
prefs.end();
break;
case 'M':
Serial.printf("GGS%dW%dW%dW%dHH", camx, camy, threshold, delaytime); Serial.println();
break;
default:
break;
}
}
}
}
}
};
void setup() {
Serial.begin(115200);
//get prefence
prefs.begin("gun", false);
threshold = prefs.getInt("threshold", 200);
delaytime = prefs.getInt("speed", 20);
camx = prefs.getInt("camx", 640);
camy = prefs.getInt("camy", 480);
prefs.end();
irtracker.DeviceInit();
if (irtracker.deviceIsOk) {
Serial.println("point tracting device is ok");
int ncamx = irtracker.get_xpixel();
int ncamy = irtracker.get_ypixel();
Serial.print("camx:"); Serial.print(camx); Serial.print(" y:"); Serial.println(camy);
Serial.print("n camx:"); Serial.print(ncamx); Serial.print(" y:"); Serial.println(ncamy);
if (ncamx != camx || ncamy != camy) {
irtracker.set_xpixel(camx);
irtracker.set_ypixel(camy);
}
}
else {
Serial.println("pleas check point tracting device' spi setting");
}
for (int i = 0; i < keynum; i++) {
pinMode(touchKey[i], INPUT_PULLUP);
}
// Create the BLE Device
BLEDevice::init("wukongGUN");
// Create the BLE Server
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// Create the BLE Service
BLEService *pService = pServer->createService(SERVICE_UUID);
// Create a BLE Characteristic
pTxCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_TX,
BLECharacteristic:ROPERTY_NOTIFY
);
pTxCharacteristic->addDescriptor(new BLE2902());
BLECharacteristic * pRxCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_RX,
BLECharacteristic:ROPERTY_WRITE
);
pRxCharacteristic->setCallbacks(new MyCallbacks());
// Start the service
pService->start();
// Start advertising
pServer->getAdvertising()->start();
Serial.println("Waiting a client connection to notify...");
}
void loop() {
// disconnecting
if (!deviceConnected && oldDeviceConnected) {
delay(500); // give the bluetooth stack the chance to get things ready
pServer->startAdvertising(); // restart advertising
Serial.println("start advertising");
oldDeviceConnected = deviceConnected;
}
// connecting
if (deviceConnected && !oldDeviceConnected) {
// do stuff here on connecting
oldDeviceConnected = deviceConnected;
}
touchValue = 0;
for (int i = 0; i < keynum; i++) {
if (!digitalRead(touchKey[i])) touchValue += pow(2, i);
}
sentmess = "GG";
sentmess.concat(gunid);
sentmess.concat("W");
if (irtracker.deviceIsOk) {
irtracker.get_mot_data(irPoints);
for (int i = 0; i < 4; i++)
{
if (irPoints[3 * i] >= 0 || irPoints[3 * i + 1] >= 0) {
sentmess.concat(irPoints[3 * i]);
sentmess.concat("W");
sentmess.concat(irPoints[3 * i + 1]);
sentmess.concat("W");
sentmess.concat(irPoints[3 * i + 2]);
sentmess.concat("W");
}
}
}
else {
sentmess.concat(-2);
sentmess.concat("W");
sentmess.concat(-2);
sentmess.concat("W");
sentmess.concat(-2);
sentmess.concat("W");
}
sentmess.concat(touchValue);
sentmess.concat("W");
sentmess.concat("HH");
oldtouched = touchValue;
Serial.println(sentmess);
if (deviceConnected) {
pTxCharacteristic->setValue(sentmess);
pTxCharacteristic->notify();
delay(10); // bluetooth stack will go into congestion, if too many packets are sent
}
delay(delaytime);
}
最新库,添加了光点尺寸大小的输出,采集输出4个光点,每个光点输出尺寸与坐标,共计12个组成一个数组
wukongM700.zip (大小: 6.89 KB / 下载: 2)
悟空学堂 专注交互技术分享