Micropython: How to debounce the Pyboard USR switch? - debouncing

I currently have this MicroPython code running on a Pyboard v1.1:
import pyb
def toggle_led():
pyb.LED(3).toggle()
sw = pyb.Switch()
sw.callback(toggle_led)
However, this setup is susceptible to contact bounce.
I tried adding up to pyb.delay(500) to no avail.
Is there an elegant way to remedy USR switch bounce on the Pyboard?

Here is what worked for me. I got my inspiration from a procedure described in a post on the MicroPython forum.
import pyb
def toggle_led():
pyb.disable_irq()
pyb.delay(100)
if sw.value(): pyb.LED(3).toggle()
pyb.enable_irq()
sw = pyb.Switch()
sw.callback(toggle_led)
Better: uasyncio
There is a much better way without a need for interrupts.
Here is a link to the buttons example in Peter Hinch's excellent uasyncio tutorial.

Related

Unknown variable floor_tex on Gamemaker 8.1. How to fix it?

I have a very trouble with Gamemaker 8.1.
So recently, I've followed this video tutorial which shows creating a game 3D on its game engine.
https://www.youtube.com/watch?v=0Jo0JEEukhc
I tried to test my game, but an error message shows
this.
What caused that error? Here is the code so I hope you can fix it as well.
direction -= (display_mouse_get_x() -(display_get_width()/2))/5
zto -= (display_mouse_get_y() -(display_get_height()/2))/2
display_mouse_set(display_get_width()/2,display_get_height()/2)
xto = lengthdir_x(100,direction)+x
yto = lengthdir_y(100,direction)+y
d3d_set_projection(x,y,z,xto,yto,zto,0,0,1)
draw_set_color(c_white)
d3d_draw_floor(0,0,0,room_width,room_height,0,background_get_texture(floor_tex),room_width/128,room_height/128)
d3d_draw_floor(0,0,100,room_width,room_height,100,background_get_texture(root_tex),room_width/128,room_height/128)
Also, the version of Gamemaker is 8.1.141 (r11549)
Thanks
Cheers
-Sajad Sadiq Abd Amhamad
In the video, the author creates two Background resources around 0:25, called floor_tex and roof_tex (mistyped as root_tex in your code). You'd want to either name your resources accordingly or adjust your code to match the names in your project.

How to close a window when you click a button to open another window

I am working on a program that will allow someone to enter details in order to write a CV. I am using the Tkinter module (as extra practice) but am already stuck on the menu!
At the moment I have three different options the user can choose: Write CV, Review CV and Exit. I have created a button for each option and when the user presses the button it'll open, however the menu window remains open (there is a different subroutine for each option).
I understand that you need to do something like window.destroy(), however I'm not sure how to give a button two commands without doing something too fiddly like create more subroutines etc.?
The other option I think I'd prefer is is I could clear the menu screen?
Here is the programming I have at the moment:
def Main_Menu():
import tkinter
main_menu = tkinter.Tk()
main_menu.title("CV Writer")
main_menu.geometry("300x300")
main_menu.wm_iconbitmap('cv_icon.ico')
title = tkinter.Label(main_menu, text = "Main Menu", font=("Helvetica",25))
title.pack()
gap = tkinter.Label(main_menu, text = "")
gap.pack()
write_cv = tkinter.Button(main_menu, text = "1) Write CV", font=("Helvetica"), command=Write_CV)
write_cv.pack()
review_cv = tkinter.Button(main_menu, text = "2) Review CV", font=("Helvetica"), command=Review_CV)
review_cv.pack()
leave = tkinter.Button(main_menu, text = "3) Exit", font=("Helvetica"), command=Exit)
leave.pack()
main_menu.mainloop()
def Write_CV():
import tkinter
write_cv = tkinter.Tk()
write_cv.geometry("300x300")
write_cv.title("Write CV")
def Review_CV():
import tkinter
review_cv = tkinter.Tk()
review_cv.geometry("300x300")
review_cv.title("Review CV")
def Exit():
import tkinter
leave = tkinter.Tk()
leave.geometry("300x300")
leave.title("Exit")
Main_Menu()
Running the program should help make this question make more sense!
I am so sorry for the wordy question, but any kind of help would be appreciated! Please bear in mind I am only a GCSE student so simple language would also be so nice! Thank you!
I don't know why are you importing tkinter under each method, it's completely useless. Simply import it once at the beginning of your file with a syntax like this:
import tkinter as tk
So that you can refer to the widgets simply with the duo tk:
btn = tk.Button(None, text='I can simply refer to a widget with tk')
Apart from this, the structure of your program is really bad. In my opinion, you should not instantiate Tk inside your function Main_Menu, because it will only be visible inside it. If you want to refer to the master or root or whatever you want to call the instance of Tk, you can't, because it's a local instance, as I said above.
I usually instantiate Tk in the main function of my program, or in the following if __name__ == '__main__': construct:
if __name__ == '__main__':
master = tk.Tk() # note I am using "tk"
# create your objects or call your functions here
master.mainloop()
Your are creating an instance of Tkin each of your function, that is really a bad practice, never do that. You should only create one instance of Tk for each Tkinter application.
You should use the object-oriented paradigm or make all your widgets global to structure your application.
Except these details, you can simply call master.destroy() when you want to destroy your main window and all its children widgets, where master is the Tk instance.
In general, you have a lot of errors and bad practices. My advice is:
Read a tutorial on Python first and then on Tkinter, before
proceeding.

Work with widgets inside Qtabwidget

I see variant how program can work with QWidget,for example QLabel,QEdit,etc on C++. But i don't understand how can i work with other widgets on tab of tabwidget if i write programs with python & pyside?
I see variant with metaobject,read about variant with QObject::child,but i don't know right way for solving problem on python. I see variant with qobject_cast,but this function specific for C++ as i think. Sample code on C++:
try:
QTextEdit* edit = qobject_cast<QTextEdit*>(tabWidget->widget(index));
Well, I am not an expert of python, but did you try to use this:
yourObject = tabWidget.widget(index)
I guess it works with any type of object.

OpenCV + Qt + cv::mat::deallocate and cv::fastFree(void *) memory problems

I've got two error messages:
cv::mat::deallocate
cv::fastFree(void *)
While I was trying to compile my OpenCV and QT project. I am fighting with this problem for fifth day. Please guys if you know how I can solve this problem, write it here.
PS: I've uploaded an image.
http://postimg.org/image/wsyq8daf5/
After this day, my problem with the camera was totally solved.There are no more problems.Now i will continue my GUI project thanks to all of you guys.After 1-2 months i will start searching for how to write printer driver ;), but however.Thanks too all again. ;)
Finally i solved my problem using the QCamera class.Well guys, use QCamera, you don't need anything else to capture videos or images.Read everything about it and bye bye ;)

Using PyQt and Qt4, is this the proper way to get a throbber in a QTabWidget tab?

I have some code creating a QTabWidget from Python using PyQt4. I want to get a 'throbber' animated gif in the tab. The /only way/ I have found how to do this is the following convoluted method.
tabBar = self.tabReports.tabBar()
lbl = QtGui.QLabel(self.tabReports)
movie = QtGui.QMovie(os.path.join(self.basedir, "images\\throbber.gif"))
lbl.setMovie(movie)
QtCore.QObject.connect(movie, QtCore.SIGNAL("frameChanged(int)"), lambda i: movie.jumpToFrame(i))
movie.start()
log.debug("valid = %s"%(movie.isValid()))
tabBar.setTabButton(idxtab, QtGui.QTabBar.LeftSide, lbl)
The debugging call always returns true, but the throbber sometimes works, sometimes is blank, and sometimes has a large ugly delay between frames. In particular, I can't help but think connecting the frameChanged signal from the movie to a function that simply calls jumpToFrame on the same movie is not correct.
Even more distressing, if I simply drop the lambda (That is, make the line say QtCore.QObject.connect(movie, QtCore.SIGNAL("frameChanged(int)"), movie.jumpToFrame) it never renders even the first frame.
So, what am I doing wrong?
PS: I realize .tabBar() is a protected member, but I assumed (apparently correctly) that PyQt unprotects protected members :). I'm new to Qt, and i'd rather not subclass QTabWidget if I can help it.
I believe the problem with the code I initially posted was that the QMovie didn't have a parent, and thus scoping issues allowed the underlying C++ issue to be destroyed. It is also possible I had had threading issues - threading.thread and QThread do not play nice together. The working code I have now is below - no messing with signals nor slots needed.
def animateTab(self, tab_widget, enable):
tw = tab_widget
tabBar = tw.tabBar()
if enable:
lbl = QtGui.QLabel(tw)
movie = QtGui.QMovie("images\\throbber.gif"), parent=lbl)
movie.setScaledSize(QtCore.QSize(16, 16))
lbl.setMovie(movie)
movie.start()
else:
lbl = QtGui.QLabel(tw)
lbl.setMinimumSize(QtCore.QSize(16, 16))
tabBar.setTabButton(tab_section.index, QtGui.QTabBar.LeftSide, lbl)
I faced the same problem and this posting helped to make it work:
http://www.daniweb.com/forums/printthread.php?t=191210&pp=40
For me this seems to make the difference: QMovie("image.gif", QByteArray(), self)

Resources