Skip to content

Commit

Permalink
Merge pull request #22 from ZY2516/master
Browse files Browse the repository at this point in the history
优化红外遥控
  • Loading branch information
elecfreaks1 authored Feb 14, 2025
2 parents e41ac48 + 736343b commit 4c1cfa0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion IR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void remote_decode(void){
while(uBit.io.P16.getDigitalValue()){//高电平等待
nowtime = system_timer_current_time_us();
if((nowtime - lasttime) > 100000){//超过100 ms,表明此时没有按键按下
ir_code = 0xff00;
ir_code = 0xffff;
return;
}
}
Expand Down
20 changes: 16 additions & 4 deletions cutebot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ namespace cuteBot {
* IR controller button
*/
export enum IRButtons {
//% blcok="Off"
Off = 1,
//% blcok="Menu"
Menu = 2,
//% blcok="Up"
Expand Down Expand Up @@ -479,16 +481,24 @@ namespace cuteBot {
function irCode(): number {
return 0;
}

let IR_handling_flag = false
//% weight=25
//% block="On IR receiving"
export function IR_callback(handler: () => void) {
pins.setPull(DigitalPin.P16, PinPullMode.PullUp)
control.onEvent(98, 3500, handler)
control.onEvent(98, 3500, ()=>{
handler()
IR_handling_flag = false;
})
control.inBackground(() => {
while (true) {
IR_Val = irCode()
if (IR_Val != 0xff00) {
control.raiseEvent(98, 3500, EventCreationMode.CreateAndFire)
if (!IR_handling_flag){
IR_Val = irCode()
if (IR_Val == 0xff00 || (IR_Val & 0x00ff) <= 30 && (IR_Val & 0x00ff) != 0) {
IR_handling_flag = true
control.raiseEvent(98, 3500, EventCreationMode.CreateAndFire)
}
}
basic.pause(20)
}
Expand All @@ -500,6 +510,8 @@ namespace cuteBot {
//% block="IR Button %Button is pressed"
//% weight=15
export function IR_Button(Button: IRButtons): boolean {
if (IR_Val == 0xff00)
IR_Val = 0x0001
return (IR_Val & 0x00ff) == Button
}
function initEvents(): void {
Expand Down
2 changes: 1 addition & 1 deletion pxt.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
],
"public": true,
"targetVersions": {
"target": "6.0.17",
"target": "7.0.57",
"targetId": "microbit"
},
"supportedTargets": [
Expand Down

0 comments on commit 4c1cfa0

Please sign in to comment.