Archive

Archive for the ‘programming’ Category

Currency Converter in Python

November 30th, 2013 No comments

EDIT 02-Oct-2015: Code now works with the free API at http://fixer.io/

Hello everyone! Well, many times I find myself converting between EURO, USD and GBP. So I though, why don’t write a small program to do that? Of course there are many other solutions out there like but what’s the fun in that if you don’t do it yourself, right, RIGHT?

As I always do, I search and learn as I go. So first things first, I wanted this to be a program with GUI. There are a lot of solutions for that but I decided to use wxPython. I skimmed over the documentation and it was obvious that I had to spent more time that I wanted in order to do something useful. I searched for a ‘designer’ and I found one, called wxGlade. After a quick ‘sudo apt-get install python-wxglade’ in my beloved Ubuntu I was up and running in no time. Within few minutes I created the minimalistic GUI I wanted.

For the currency conversion I found some good info here and I used part of that code. I also needed to learn about threads since I wanted to fetch the exchange rates without blocking the program and again after a quick google search this was solved as well.

The little program works well and its fast. The code is below. Let me know if you like it or improve it.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# generated by wxGlade 0.6.5 on Thu Nov 28 12:44:35 2013
 
import wx
from threading import Thread
import time
import requests
 
# begin wxGlade: extracode
EUR_USD = 1
EUR_GBP = 1
USD_GBP = 1
def myfunc(i):
    global EUR_USD
    global EUR_GBP
    global USD_GBP
    # For the following 3 commands credit goes to http://www.boxcontrol.net/write-simple-currency-converter-in-python.html#.UpkVvapyQQ-
    url = 'http://api.fixer.io/latest?base=EUR&symbols=USD'
    r = requests.get(url)
    EUR_USD = r.json()['rates']['USD']
    time.sleep(2)
    url = 'http://api.fixer.io/latest?base=EUR&symbols=GBP'
    r = requests.get(url)
    EUR_GBP = r.json()['rates']['GBP']
    time.sleep(2)
    url = 'http://api.fixer.io/latest?base=USD&symbols=GBP'
    r = requests.get(url)
    USD_GBP = r.json()['rates']['GBP']
 
t = Thread(target=myfunc,args=(1,))
t.start()
# end wxGlade
 
class MyFrame1(wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: MyFrame1.__init__
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.EUR = wx.StaticText(self, wx.ID_ANY, "EUR")
        self.tc_EUR = wx.TextCtrl(self, wx.ID_ANY, "")
        self.USD = wx.StaticText(self, wx.ID_ANY, "USD")
        self.tc_USD = wx.TextCtrl(self, wx.ID_ANY, "")
        self.GBP = wx.StaticText(self, wx.ID_ANY, "GBP")
        self.tc_GBP = wx.TextCtrl(self, wx.ID_ANY, "")
 
        self.__set_properties()
        self.__do_layout()
 
        self.Bind(wx.EVT_TEXT, self.eh_EUR, self.tc_EUR)
        self.Bind(wx.EVT_TEXT, self.eh_USD, self.tc_USD)
        self.Bind(wx.EVT_TEXT, self.eh_GBP, self.tc_GBP)
        # end wxGlade
 
    def __set_properties(self):
        # begin wxGlade: MyFrame1.__set_properties
        self.SetTitle("CurrencyPy")
        # end wxGlade
 
    def __do_layout(self):
        # begin wxGlade: MyFrame1.__do_layout
        grid_sizer_1 = wx.GridSizer(3, 2, 2, 2)
        grid_sizer_1.Add(self.EUR, 0, wx.ALIGN_CENTER_HORIZONTAL, 0)
        grid_sizer_1.Add(self.tc_EUR, 0, 0, 0)
        grid_sizer_1.Add(self.USD, 0, wx.ALIGN_CENTER_HORIZONTAL, 0)
        grid_sizer_1.Add(self.tc_USD, 0, 0, 0)
        grid_sizer_1.Add(self.GBP, 0, wx.ALIGN_CENTER_HORIZONTAL, 0)
        grid_sizer_1.Add(self.tc_GBP, 0, 0, 0)
        self.SetSizer(grid_sizer_1)
        grid_sizer_1.Fit(self)
        self.Layout()
        self.Centre()
        # end wxGlade
 
    def eh_EUR(self, event):  # wxGlade: MyFrame1.
        if self.tc_EUR.GetValue() != '':
            self.tc_USD.ChangeValue(str(float(self.tc_EUR.GetValue())*EUR_USD))
            self.tc_GBP.ChangeValue(str(float(self.tc_EUR.GetValue())*EUR_GBP))
            event.Skip()
    def eh_USD(self, event):  # wxGlade: MyFrame1.
        if self.tc_USD.GetValue() != '':
            self.tc_EUR.ChangeValue(str(float(self.tc_USD.GetValue())/EUR_USD))
            self.tc_GBP.ChangeValue(str(float(self.tc_USD.GetValue())*USD_GBP))
            event.Skip()
    def eh_GBP(self, event):  # wxGlade: MyFrame1.
        if self.tc_GBP.GetValue() != '':
            self.tc_EUR.ChangeValue(str(float(self.tc_GBP.GetValue())/EUR_GBP))
            self.tc_USD.ChangeValue(str(float(self.tc_GBP.GetValue())/USD_GBP))
            event.Skip()
# end of class MyFrame1
if __name__ == "__main__":
    app = wx.PySimpleApp(0)
    wx.InitAllImageHandlers()
    frame_2 = MyFrame1(None, -1, "")
    app.SetTopWindow(frame_2)
    frame_2.Show()
    app.MainLoop()

Oh and here is a screenshot!

Incoming search terms:

  • python currency converter (169)
  • currency converter python (93)
  • how to make a currency converter in python (76)
  • python currency converter code (47)
  • currency converter python code (36)
  • currency converter in python (33)
  • python code for currency converter (21)
  • simple currency converter python (19)
  • currency converter code in python (15)
Categories: programming Tags:

Fixed Time Loop in Microcontrollers

March 31st, 2011 No comments

Hey.

Most of the times I am developing a piece of code for my PICs, there is a main loop in the flow. The code repeats it self indefinitely doing its thing and it is only interrupted temporarily by the hardware interrupts of the PIC. Now most of the times a simple while(1) { // code here } will do just fine. However, sometimes your need that loop to start at specific time interval for example every half second. This is pretty useful when somewhere in your maths you need a specific dt. Read more…

Categories: electronics, programming Tags:

Plans for my first line following robot

November 7th, 2010 No comments

Hi there. Now that some obligations are out of the way I am back to my drawing board 🙂

I am thinking of building a line following robot. Hmmm “simple” you might think. Well, maybe but it depends on what you are aiming for. You see the whole concept of the line following competitions is to keep the task simple. This way, you can concentrate on building the robot doing exactly that, as good as possible. As a result, you have many robots trying to do a “simple” (its not that simple actually) task but each one is programmed differently with smart twists, in an effort to be quicker than the rest.

Moreover, you have to keep in mind that a robot is the unification of different sectors (mechanical, electronics, electrical…) into one functional unit. Every part of the robot should work as expected, in order for the robot to function correctly as a whole. For example, the chassis of the robot. How big it should be? big is heavy, small is light but not much room for your electronics. Where do the motors go on the chassis? How will you mount them? Then you have to think about what kind of motors to use, how much torque, rpm, power, consumption do the have? Then you have to think about your electronics, about motor driver, the MCU and other stuff you might need. Then comes the programming part. How can you tell a robot to follow a line? After you figure that out, how can you do this process efficiently, giving you the best result relative to speed.

Anyway, sorry if I confused you or that didn’t make much sense. I just wanted to say that building a robot (even to do a simple task) is a multi-skill process. Don’t be afraid though, I am not saying you have to be an expert to do it. Just some careful thinking before buying your components. Sit down with a pencil and a piece of paper and write how you would build such a robot. Divide your design to the sectors we mentioned above. Try to learn from the designs that other people have done (but don’t copy them, otherwise the purpose is lost) and try to isolate each problem and deal with it.

For example, I had some hard time figuring out how to build my chassis. After some looking around I decided that for now, the cheapest and quickest solution, is to make my designs on google’s SketchUp and transfer it onto thick PVC.

Sometime soon I will start my construction and I will keep you posted.

Categories: electronics, programming Tags:

Simple Serial User Interface on PIC

March 30th, 2010 No comments

So today I needed to control some stuff on my PIC by using the terminal on my PC through RS-232 serial. I want to control the speed on 2 motors connected to the PIC, either separately or both of them together. To do that I divided the tasks that the User Interface (UI) had to provide. So from the main menu the user should be able to choose to control

  • Motor A
  • Motor B
  • Both Motors

In each of the sub-menus shown above the user should have some choices for acting on the motors. Before complicating this stuff more, I will show you the method I used to create this simple menu on my 18F2525 PIC. It doesn’t matter if you don’t have this PIC or even any other microcontroller. The concept is the same.

If you try to tackle the problem an if-then-else structure may seem like an easy solution but I can tell you it is going to messy pretty soon! We need a more clear, robust solution. But first let’s see what we need to do.

PIC User Interface Layout

The image pretty much explains it all. If we are at Main Menu the only valid keys are 1, 2 or 3. Each leads to a new sub menu. From there, there are again three choices 0 that takes us back to Main Menu and + or that will need to take an action (colored green). So if we implement this menu you will be able to implement more complex ones based on the same concept.

I got this idea based on Finite State Machine concept but its not quite right so we cannot call it like that! Anyway. Each of the menu item (Main Menu, Motor A, Motor B, Both Motors), is a different state in the program let assign them 0,1,2,3 accordingly. So if we are at state 0 we can go to state 1,2 or 3 with the right key pressed in the terminal. However if we are at state 1, we can only go back to state 0 or stay at the same state and complete some actions (forwards, backwards) with the right buttons pressed, once more.

So the way to implement this is by using switch/case. I think it will be much clearer if I show you the code. First let’s see how to we switch between the states. Read more…

How to install Box2D for BlitzMax

December 11th, 2009 7 comments

Well, generally when a deadline is approaching I tend to lose focus from work 😀 . Well having one close deadline, today I remember to refresh my BlitzMax knowledge! BlitzMax is a 2D game development environment that I bought some years ago. If you are interested on 2D game programming you should check it out. When I was doing my research, I found it to be at the exact abstract level that I needed it to be, hiding enough details from the ‘boring stuff’ and at the same time having great flexibility. Read more…

Incoming search terms:

  • how to install box2d (36)
  • box2d install (13)

How high is too high?

October 26th, 2008 No comments

Hello boys and girls.

A common question I come across quite often, is from a where a beginner should start his/her microcontroller programming adventure. I remember myself wondering exactly the same thing when I was starting out, not too long ago. So which is the best to program? Assembly or C or Basic or… ?

Well I am going to give you my opinion and how well it worked for me. Let’s face it, knowing what is going under the hood is the best experience anyone can get working on any kind of project. If you know exactly how the system works, then you will be able to squeeze every last drop of juice the microcontroller has to offer. Read more…

Categories: electronics, programming Tags: