Ver Fonte

add: 新增air8000整机开发板蓝牙的demo

梁健 há 9 meses atrás
pai
commit
c4e1fd5e5b

+ 0 - 66
module/Air8000/project/整机开发板出厂工程/user/air.lua

@@ -1,66 +0,0 @@
-local aircall = {}
-
-dnsproxy = require("dnsproxy")
-dhcpsrv = require("dhcpsrv")
-httpplus = require("httpplus")
-local run_state = false  -- 判断本UI DEMO 是否运行
-
-
-local function stop_call()
-
-end
-
-local function recv_call()
-
-end
-
-local function start_call()
-
-end
-
-
-
-function aircall.run()       
-    log.info("aircall.run")
-    lcd.setFont(lcd.font_opposansm12_chinese) -- 设置中文字体
-    run_state = true
-    while true do
-        sys.wait(10)
-        -- airlink.statistics()
-        lcd.clear(_G.bkcolor) 
-        lcd.drawStr(0,80,"需要代码内修改拨打的号码" )
-
-        --[
-        -- 此处可填写demo ui
-        --]
-
-
-        lcd.showImage(20,360,"/luadb/back.jpg")
-        lcd.showImage(130,303,"/luadb/stop.jpg")    -- 挂断&拒接电话
-        lcd.showImage(130,350,"/luadb/start.jpg")   -- 接电话
-        lcd.showImage(130,397,"/luadb/start.jpg")   -- 拨打电话
-
-        lcd.showImage(0,448,"/luadb/Lbottom.jpg")
-        lcd.flush()
-        
-        if not  run_state  then    -- 等待结束,返回主界面
-            return true
-        end
-    end
-end
-
-
-
-function aircall.tp_handal(x,y,event)       
-    if x > 20 and  x < 100 and y > 360  and  y < 440 then
-        run_state = false
-    elseif x > 130 and  x < 230 and y > 303  and  y < 350 then
-        sysplus.taskInitEx(stop_call, "stop_call")
-    elseif x > 130 and  x < 230 and y > 350  and  y < 397 then
-        sysplus.taskInitEx(recv_call, "recv_call")
-    elseif x > 130 and  x < 230 and y > 397  and  y < 444 then
-        sysplus.taskInitEx(start_call, "start_call")
-    end
-end
-
-return aircall

+ 140 - 0
module/Air8000/project/整机开发板出厂工程/user/airble.lua

@@ -0,0 +1,140 @@
+local airble = {}
+
+dnsproxy = require("dnsproxy")
+dhcpsrv =  require("dhcpsrv")
+httpplus = require("httpplus")
+local run_state = false  -- 判断本UI DEMO 是否运行
+local ble_state = "未初始"
+
+local Characteristic1 = "EA01"
+local Characteristic1_read = nil
+local Characteristic1_write = nil
+local Characteristic2 = "EA02"
+local Characteristic2_write = nil
+local Characteristic3 = "EA03"
+local Characteristic3_read = nil
+local Characteristic4 = "EA04"
+local Characteristic4_read = nil
+local Characteristic4ind = nil
+
+
+local att_db = { -- Service
+    string.fromHex("FA00"), -- Service UUID
+    -- Characteristic
+    { -- Characteristic 1
+        string.fromHex(Characteristic1), -- Characteristic UUID Value
+        ble.NOTIFY | ble.READ | ble.WRITE -- Properties
+    }, { -- Characteristic 2
+        string.fromHex(Characteristic2), ble.WRITE
+    }, { -- Characteristic 3
+        string.fromHex(Characteristic3), ble.READ
+    }, { -- Characteristic 4
+        string.fromHex(Characteristic4), ble.IND | ble.READ
+    }
+}
+
+ble_stat = false
+
+local function ble_callback(dev, evt, param)
+    if evt == ble.EVENT_CONN then
+        log.info("ble", "connect 成功", param, param and param.addr and param.addr:toHex() or "unknow")
+        ble_stat = true
+    elseif evt == ble.EVENT_DISCONN then
+        log.info("ble", "disconnect")
+        ble_stat = false
+        -- 1秒后重新开始广播
+        sys.timerStart(function() dev:adv_start() end, 1000)
+    elseif evt == ble.EVENT_WRITE_REQ then
+        -- 收到写请求
+        log.info("ble", "接收到写请求", param.uuid_service:toHex(), param.data:toHex())
+        if param.uuid_service == Characteristic1 then
+            Characteristic1_write = param.data:toHex()
+        elseif param.uuid_service == Characteristic2 then
+            Characteristic2_write = param.data:toHex()
+        end
+
+    end
+end
+
+
+local function ble_peripheral_setup()
+    local ret = 0
+    sys.wait(500)
+    log.info("开始初始化蓝牙核心")
+    bluetooth_device = bluetooth.init()
+    sys.wait(100)
+    log.info("初始化BLE功能")
+    ble_device = bluetooth_device:ble(ble_callback)
+    if ble_device == nil then
+        log.error("当前固件不支持完整的BLE")
+        return
+    end
+    sys.wait(100)
+
+    log.info('开始创建GATT')
+    ret = ble_device:gatt_create(att_db)
+    log.info("创建的GATT", ret)
+
+    sys.wait(100)
+    log.info("开始设置广播内容")
+    ble_device:adv_create({
+        addr_mode = ble.PUBLIC,
+        channel_map = ble.CHNLS_ALL,
+        intv_min = 120,
+        intv_max = 120,
+        adv_data = {
+            {ble.FLAGS, string.char(0x06)},
+            {ble.COMPLETE_LOCAL_NAME, "LuatOS123"},
+            {ble.SERVICE_DATA, string.fromHex("FE01")},
+            {ble.MANUFACTURER_SPECIFIC_DATA, string.fromHex("05F0")}
+        }
+    })
+
+    sys.wait(100)
+    log.info("开始广播")
+    ble_device:adv_start()
+
+end
+
+local function start_notify()
+
+end
+
+
+
+function airble.run()       
+    log.info("airble.run")
+    lcd.setFont(lcd.font_opposansm12_chinese) -- 设置中文字体
+    run_state = true
+    sysplus.taskInitEx(ble_peripheral_setup,"airble")
+    while true do
+        sys.wait(10)
+        lcd.clear(_G.bkcolor) 
+        lcd.drawStr(0,80,"当前蓝牙状态:" .. ble_state )
+        lcd.drawStr(0,100,"特征:" .. Characteristic1  .. ",可读数据为:" ..  Characteristic1_read.. "被写入数据为:" .. Characteristic1_write)
+        lcd.drawStr(0,120,"特征:" .. Characteristic2   .. "被写入数据为:" .. Characteristic2_write)
+        lcd.drawStr(0,140,"特征:" .. Characteristic3   .. ",可读数据为:" .. Characteristic3_read)
+        lcd.drawStr(0,160,"特征:" .. Characteristic4   .. "可读数据为:" .. Characteristic4_read)
+
+
+
+        lcd.showImage(130,350,"/luadb/start.jpg")   -- EA01 发送数据
+        lcd.flush()
+        
+        if not  run_state  then    -- 等待结束,返回主界面
+            return true
+        end
+    end
+end
+
+
+
+function airble.tp_handal(x,y,event)       
+    if x > 20 and  x < 100 and y > 360  and  y < 440 then
+        run_state = false
+    elseif x > 130 and  x < 230 and y > 397  and  y < 444 then
+        sysplus.taskInitEx(start_notify, "start_notify")
+    end
+end
+
+return airble

+ 14 - 1
module/Air8000/project/整机开发板出厂工程/user/main.lua

@@ -24,6 +24,7 @@ local airaudio  = require "airaudio"
 local aircamera = require "aircamera"
 local airrus = require "russia"
 local airstatus = require "statusbar"
+local airble = require "airble"
 local airtestwlan = require "test_wlan"
 local airbuzzer = require "airbuzzer"
 local multi_network = require "multi_network"
@@ -133,6 +134,7 @@ local function handal_main(x,y)
   elseif key == 5 then
     cur_fun  = "aircall"
   elseif key == 6 then
+    cur_fun  = "airble"
   elseif key == 7 then
     cur_fun  = "airsms"
   elseif key == 8 then    --  tts
@@ -241,7 +243,8 @@ local function  tp_handal(tp_device,tp_data)
       airgsensor.tp_handal(tp_data[1].x,tp_data[1].y,tp_data[1].event)
     elseif cur_fun == "airpower" then
       airpower.tp_handal(tp_data[1].x,tp_data[1].y,tp_data[1].event)
-
+    elseif cur_fun == "airble" then
+      airble.tp_handal(tp_data[1].x,tp_data[1].y,tp_data[1].event)
     end
     lock_push = 1
   end
@@ -430,11 +433,19 @@ local function draw_multi_network()
   end
 end
 
+local function draw_airble()
+    if  airble.run()   then
+      cur_fun = "main"
+    end
+end
+
+
 local function draw()
   if cur_fun == "camshow" then
     return
   end
 
+
   lcd.clear(_G.bkcolor)    
   
   draw_statusbar()
@@ -473,6 +484,8 @@ local function draw()
     draw_power()
   elseif cur_fun == "multi_network" then
     draw_multi_network()    
+  elseif cur_fun == "multi_network" then
+    draw_airble()
   end
   
   lcd.showImage(0,448,"/luadb/Lbottom.jpg")

+ 2 - 1
script/libs/udpsrv.lua

@@ -7,7 +7,8 @@
 @demo    socket
 @tag LUAT_USE_NETWORK
 @usage
--- 具体用法请查阅demo
+-- 具体用法请查
+阅demo
 ]]
 
 local sys = require "sys"