module(..., package.seeall)--Introduce necessary library files (written in Lua), internal libraries do not require requirelocal sys = require "sys"--Configure and use the corresponding serial port IDlocal DATA_UART_ID = 1 -- select a different uarId based on the actual device--initializationlocal result1 = uart.setup(DATA_UART_ID, --serial port id115200, --Baud rate8, --data bits1--Stop bit)-- Receiving data will trigger a callback, where "receive" is a fixed valueuart.on(DATA_UART_ID, "receive", function(id, len) local s = ""repeat-- If it is air302, len is not trustworthy, pass 1024-- s = uart.read(id, 1024)s = uart.read(id, len) if #s > 0 then-- #s is to take the length of the string-- If binary/hexadecimal data is transmitted, some characters are not visible, which does not mean that they were not received.-- For information about sending and receiving hex values, please refer to https://doc.openluat.com/article/583log.info("DATA_UART", "receive", id, #s, s) --if s:toHex() == "010601220001E9FC" then-- log.info("start config mode")-- isInConfigMode = true-- intoConfigMode()--elseif s:toHex() == "010601220000283C" then-- log.info("end config mode")-- isInConfigMode = false-- intoRunMode()--else-- uart.write(DATA_UART_ID, "command error")-- log.info("command error")--endlocal data, result, errinfo = json.decode(s) if result and type(data) == "table" then--motorControl putFoodControl lightControllocal action = data.action if action == "motorControl" thenlocal a1 = tonumber(data.a1) local a2 = tonumber(data.a2)local b1 = tonumber(data.b1)local b2 = tonumber(data.b2)if a1 == nil or a2 == nil or b1 == nil or b2 == nil thenlog.error("Incomplete data") returnelsedriveMotor(a1, a2, b1, b2) endelseif action == "putFoodControl" thenputFood() elseif action == "lightControl" thenlocal lightState = data.lightState if lightState thenledOn() elseledOff() endelseif action == "getId" thenlocal unique_id = mcu.unique_id() local unique_data, unique_len = string.toHex(unique_id, " ")--Data conversionlog.info("unique_id:", unique_data) log.info("unique_id-length:", unique_len)local unique_data_result = {deviceId = unique_data}unique_data_result = json.encode(unique_data_result)uart.write(DATA_UART_ID, unique_data_result)elselog.error("control action error") endelselog.error("data is not json") endenduntil s == ""end)-- Not all devices support the sent eventuart.on(DATA_UART_ID, "sent", function(id) log.info("BLE_UART", "sent", id)end)
好神奇,电机驱动的代码放这里就没法提交工程,会触发拦截,还是放在附件中吧
The embedded source code and firmware are included in the attachment.
All reference designs on this site are sourced from major semiconductor manufacturers or collected online for learning and research. The copyright belongs to the semiconductor manufacturer or the original author. If you believe that the reference design of this site infringes upon your relevant rights and interests, please send us a rights notice. As a neutral platform service provider, we will take measures to delete the relevant content in accordance with relevant laws after receiving the relevant notice from the rights holder. Please send relevant notifications to email: bbs_service@eeworld.com.cn.
It is your responsibility to test the circuit yourself and determine its suitability for you. EEWorld will not be liable for direct, indirect, special, incidental, consequential or punitive damages arising from any cause or anything connected to any reference design used.
Supported by EEWorld Datasheet