分享

#开发小记#Python中实现输入超时以及获取变量名字

 金刚光 2020-01-05

 #编程##笔记#开发中遇到了一个需求:程序运行到某处时需要用户确认, 但不能一直傻等, 后面的程序不能被一直阻塞, 需要有个超时限制, 也就是这个程序如果在一段时间后还没有得到用户输入就执行默认操作.

我就想到了用多线程的方式, 开启一个子线程用stdin获取用户输入, 主线程里设置线程启动和超时. Python中用多线程很方便, 只要threading.Threaded(函数, 参数表)然后thread.start就好了. 但这又出来一个问题, 如果不使用全局变量, 该如何在另一个函数里修改其参数对应的内容呢? 这里的重点归结起来是"函数如何修改自身参数的内容".

于是我想到了一个骚透了的方法——改变量字典…… 因为python的变量是基于标签的, 所以同一个内容可能会有多个标签(同时, 一个内存数据只要被引用那么自身也有个id), python维护这些标签和内容的对应关系可以通过字典的方式来读取和修改. 如果想要取某个变量的名字的话, 可以复制一份这个字典, 这时候使用id函数把这个复制的字典中的内容作为对象传入也就相当于这个复制的字典中的内容被引用了, 那么这个内容也就有了个id, 给这个内容对应的函数名作为这个id的值. 这样一来就相当于根据全局变量字典做了一个内容-变量名对照表, 那么查这个对照表中的内容id就能得到变量名了. 最后根据修改字典里标签的对应的内容就能修改这个变量值了.

以上只是一种省事的方法, 个人愚见, 如果有更好的方法还请分享一下, 一起学习一起进步.

函数demo

变量与标签的关系

Arduino®-Leonardo USB HID Keyboard & Mouse Tutorial and Advanced Serial Port Examples

NOTE: Arduino 1.01 and the Arduino Leonardo have been released! We’ll be updating these pages soon to reflect the release version of the Leonardo features. Adruino 1.01 supports modifier keys directly, so it’s a great improvement! The basic examples here should still work fine.

This tutorial covers the Arduino “Leonardo” platform which uses the new USB-enabled ATmega 32U4 MCU, with a very powerful bootloader environment that emulates both a USB mouse and keyboard (standard HID devices, no drivers needed), as well as a virtual COM port!

Along with the emulated USB HID devices, another really cool feature of the Leonardo is that it effectively has two COM ports! One is the emulated VCP port through the USB connection, the other is the hardware UART on the microcontroller itself. You can independently “talk” to two separate devices; one via USB, the other via serial or Bluetooth, for example.

FYI, here are links to three of our Leonardo-compatible boards:

StealthDuino! Leonardo BASIC with ATmega 32U4 – Arduino on a USB stick!
StealthDuino! Leonardo with ATmega 32U4 – Stealthy Arduino on a USB stick!
BreadBoarder-32U4 – Arduino-Leonardo Compatible Prototyping Board (Nano-esque!)

HID Keyboard Examples:

This is about the easiest way to have an Arduino Leonardo interact with a PC; press a key on the keyboard! There’s no limit to the cool things you could do with this!

In the most basic implementation, you could simply capture keystrokes into an open Notepad window, or any other application window! There are three keyboard-specific commands in Arduino 1.0:

  • Keyboard.write(variable or constant); // Writes the ASCII equivalent code to the keyboard

  • Keyboard.print(variable or string); // Prints either a variable value or string to the keyboard

  • Keyboard.println(variable or string); // Prints either a variable value or string to the keyboard, with a line-feed

FYI, there appears to be a way to send modifier keys (CTRL-ALT-DEL, etc.) as well, but this hasn’t been officially documented. (actually, none of this has been documented yet!

)

Here’s an adaptation/extension of a simple Arduino 1.0RC2 example sketch that watches digital pin 3 (D3) and types out a keyboard message every time pin D3 is pulled high (5V). It demonstrates the three functions above. If you have a Leonardo-compatible board that isn’t a StealthDuino!, just touch pin D3 to 5V to toggle the input. See: StealthDuino_Leonardo_Keyboard.ino

HID Mouse Examples:

A Leonardo-compatible board can also take over your mouse! Make your mouse pointer look like it’s controlled by a ghost!

The current HID mouse functions are:

  • Mouse.move(mouseX, mouseY, wheel);

  • Mouse.click(button); // single-click of a button: left, right, middle = 1,2,4 or “MOUSE_ALL”

  • Mouse.press(button); // press & hold selected button, numbered as above

  • Mouse.release(button); // release selected button, numbered as above

  • Mouse.isPressed(); // boolean test to see if any/all mouse buttons are pressed

With “Mouse.move()”: “mouseX” and “mouseY” are L/R/U/D displacements, negative for U/L and positive for D/R, and “wheel” is a negative/postive wheel movement. +/-1 displacement steps give a smooth movement.

Here’s a simple Arduino sketch that moves the mouse pointer in the outline of a little square five times, then stops! As it moves up & down it holds the left mouse button, moving left & right it holds the right mouse button. MS Paint, or some other drawing program is a neat way to see the effect. See: StealthDuino_Leonardo_Mouse.ino

Multiple Serial Ports:

With the Leonardo environment, there are two available serial ports; one is the emulated VCP port through the USB connection, the other is the hardware UART on the microcontroller itself.

The Arduino developers were presented with this option when the Mega series of Arduino boards were introduced (the ATmega 128 and 256 have multiple serial UARTs), and they quickly adapted the codebase to take advantage of it. We now can benefit from the same enhancements with the Leonardo!

The code is very simple. Below is an example, or here: Leonardo_Analog_Read_Serial.ino

/* Cal-Eng Leonardo dual-serial example Reads an analog input on pin 0, prints the result to the two serial ports on the Leonardo */ void setup() { pinMode(13, OUTPUT); Serial.begin(9600); Serial1.begin(9600); } void loop() { int sensorValue = analogRead(A0); Serial1.print("32U4 Hardware UART - Input A0: "); Serial1.println(sensorValue); delay(1000); digitalWrite(13, HIGH); // set the LED on Serial.print("Leonardo USB VCP - Input A0: "); Serial.println(sensorValue); delay(1000); digitalWrite(13, LOW); // set the LED off }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多