藍牙摸魚神器(上手指南&程式碼)

嗨,如果您還沒有觀看演示視頻,可以播放上面的短片以了解這個專案的功能。

介紹

這個項目的靈感來自另一個開源項目 daytripper (link),它使用 2 個獨立的硬體設備來檢測移動物體並控制您的 PC切換應用程序。非常有趣,但我認為如果我們改用計算機視覺的解決方案,我們或許可能會將硬件數量減少到 1 個,我們甚至可以添加一些更酷的功能(例如人臉識別、速度檢測等)來讓它變得更有趣。

這就是我這個想法的來源——使用更輕量更易於部署的OpenMV和具有藍牙功能的IoT微控制器(Ameba RTL8722DM_MINI),我們可以實現除了daytripper的功能之外的很多功能。


組件

  1. Ameba RTL8722DM_MINI 開發板 x1
  2. OpenMV(任何型號)開發板 x1

連接很簡單,只需將 OpenMV 上的 P0 引腳連接到 Ameba 上的引腳 9


功能流程

這是這個專案的工作原理,:point_down:
image


程式碼

OpenMV

請下載OpenMV的IDE,然後把程式碼paste進去就好了

# Advanced Frame Differencing Example
#
# This example demonstrates using frame differencing with your OpenMV Cam. This
# example is advanced because it preforms a background update to deal with the
# backgound image changing overtime.

import sensor, image, pyb, os, time
from pyb import Pin

p_out = Pin('P0', Pin.OUT_PP)
p_out.low()

TRIGGER_THRESHOLD = 5

BG_UPDATE_FRAMES = 50 # How many frames before blending.
BG_UPDATE_BLEND = 128 # How much to blend by... ([0-256]==[0.0-1.0]).

sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.RGB565) # or sensor.RGB565
sensor.set_framesize(sensor.QVGA) # or sensor.QQVGA (or others)
sensor.skip_frames(time = 2000) # Let new settings take affect.
sensor.set_auto_whitebal(False) # Turn off white balance.
clock = time.clock() # Tracks FPS.

# Take from the main frame buffer's RAM to allocate a second frame buffer.
# There's a lot more RAM in the frame buffer than in the MicroPython heap.
# However, after doing this you have a lot less RAM for some algorithms...
# So, be aware that it's a lot easier to get out of RAM issues now. However,
# frame differencing doesn't use a lot of the extra space in the frame buffer.
# But, things like AprilTags do and won't work if you do this...
extra_fb = sensor.alloc_extra_fb(sensor.width(), sensor.height(), sensor.RGB565)

print("About to save background image...")
sensor.skip_frames(time = 2000) # Give the user time to get ready.
extra_fb.replace(sensor.snapshot())
print("Saved background image - Now frame differencing!")

triggered = False

frame_count = 0
while(True):
    clock.tick() # Track elapsed milliseconds between snapshots().
    img = sensor.snapshot() # Take a picture and return the image.

    frame_count += 1
    if (frame_count > BG_UPDATE_FRAMES):
        frame_count = 0
        # Blend in new frame. We're doing 256-alpha here because we want to
        # blend the new frame into the backgound. Not the background into the
        # new frame which would be just alpha. Blend replaces each pixel by
        # ((NEW*(alpha))+(OLD*(256-alpha)))/256. So, a low alpha results in
        # low blending of the new image while a high alpha results in high
        # blending of the new image. We need to reverse that for this update.
        img.blend(extra_fb, alpha=(256-BG_UPDATE_BLEND))
        extra_fb.replace(img)

    # Replace the image with the "abs(NEW-OLD)" frame difference.
    img.difference(extra_fb)

    hist = img.get_histogram()
    # This code below works by comparing the 99th percentile value (e.g. the
    # non-outlier max value against the 90th percentile value (e.g. a non-max
    # value. The difference between the two values will grow as the difference
    # image seems more pixels change.
    diff = hist.get_percentile(0.99).l_value() - hist.get_percentile(0.98).l_value()
    triggered = diff > TRIGGER_THRESHOLD

    if triggered == True:
        p_out.high()
    else:
        p_out.low()


    print(clock.fps(), triggered) # Note: Your OpenMV Cam runs about half as fast while
    # connected to your computer. The FPS should increase once disconnected.

Ameba

使用Arduino IDE,并下載最新的Package,

#include "BLEHIDDevice.h"
#include "BLEHIDKeyboard.h"
#include "BLEDevice.h"

BLEHIDKeyboard keyboardDev;
BLEAdvertData advdata;

#define ENABLE_PIN 9

void setup() {
  Serial.begin(115200);
  advdata.addFlags();
  advdata.addCompleteName("AMEBA_BLE_HID");
  advdata.addAppearance(GAP_GATT_APPEARANCE_HUMAN_INTERFACE_DEVICE);
  advdata.addCompleteServices(BLEUUID(HID_SERVICE_UUID));

  BLEHIDDev.init();

  BLE.init();
  BLE.configAdvert()->setAdvData(advdata);
  BLE.setDeviceName("AMEBA_BLE_HID");
  BLE.setDeviceAppearance(GAP_GATT_APPEARANCE_HUMAN_INTERFACE_DEVICE);
  BLE.configSecurity()->setPairable(true);
  BLE.configSecurity()->setAuthFlags(GAP_AUTHEN_BIT_BONDING_FLAG);
  BLE.configServer(3);
  BLE.addService(BLEHIDDev.hidService());
  BLE.addService(BLEHIDDev.battService());
  BLE.addService(BLEHIDDev.devInfoService());

  pinMode(ENABLE_PIN, INPUT);

  BLE.beginPeripheral();
}

int flag = 0;

void loop() {
  if (BLE.connected() && digitalRead(ENABLE_PIN) && flag == 0) {
    Serial.println("Sending keystrokes");
    keyboardDev.keyReleaseAll();
    delay(100);
    keyboardDev.keyPress(HID_KEY_ALT_LEFT);
    delay(100);
    keyboardDev.keyPress(HID_KEY_TAB);
    keyboardDev.keyReleaseAll();
    delay(100);
    flag = 1;
  } else {
    flag = 0;
    delay(100);
  }
}

結論

因為匆忙趕製,所以這個專案並不完美,所以如果有人想完善它,你可以隨心所欲地更改我的代碼,或者如果你有問題或想和我討論的話,請在下面發表評論~

Happy coding :sunglasses: