Category Archives : OOP

Qt C++: Threaded Communication with Artema Hybrid on Linux

Communicating with an Artema Hybrid CC Payment device is actually deceptively simple. I’ve written an application what uses a webkit widget exclusively for the UI display, and so I hook into the Javascript to provide some extra functionality to the app, in this case, reading and writing to/from the Artema Hybrid device. The documentation that I received was in German exclusively, so this took a bit of work to get going, but once you have it down it’s very easy. The device is connected via it’s POS connection Serial->RJ45 connection to a Serial->USB (FTDI chipset) converter that gets plugged into a usb port. You use the standard open, read, write and close functions. The Artema Hybrid constantly communicates with your system, so you’ll need to create a QThread to run in the background reading from the device once every second. The Artema Hybrid will send you an ENQ (0×05) and you need to respond with either an ACK (0×06) or an STX (0×02). With the way that I have it set up, the c++ code does the absolute minimum required which is it reads from the device and emits a dataRead() signal which is connected in Javascript. If I read ENQ, then I emit dataRead(“ENQ”). and in the Javascript function that is connected to the dataRead() signal, I see if the string passed up == ENQ. In the javascript, I have an array of data to write to the device, if that array is empty, I just write back to the Device ACK, otherwise, I […]

Ruby on Rails Session in Models: Not So Evil Actually 3

I just finished reading this post from on how to put your session into your models. First off, I love this guy, and his site, and most of what he says, but I’ve had it up to here with this nonsense. I also kind of felt a little dissed by the PHP comment. I understand he was joking, and it was kind of funny, PHP programmers are NOTORIOUS for using $_SESSION to store EVERYTHING. At the same time, as Voorhaus says, all comedy is truth and pain, and the truth is that PHP programmers do that, and the pain is that many programmers coming to Ruby and Rails feel alienated by that community because they are so opinionated (And not always right). Rails is kind of Opinionated by design, and that’s okay, but more often than not, the Rails and Ruby community is uneccessarily sadistic in its treatment of contradictory opinions, they have a “my way or the highway” kind of speech when in fact, you can do it anyway you want, and all they are doing is looking like douche bags for treating you as a sub-human for having a contradictory opinion. As I see it, at the core of the above issue, sessions in the model, is global variables, and the irrational fear of them. Phear Globalz There is some practicality in not depending on global variables, especially when you are TALKING about programming. When you are doing it, it’s another story. They can theoretically be the source of problems. Yes I […]

HowTo ActionScript 3 (as3 flash or flex or AIR) Class and Object Inheritance, extends

It seems I have been getting alot of hits due to my post on AS3 inheritance, and I notice that most of the documentation and examples out there don’t really cover the whys and the hows in a thorough manner. There are a couple of parts to this little tutorial, the first being a kind of very basic demonstration of extending, and then some concrete realworld samples. You can jump ahead to the realworld samples if you are just looking to see how/why you might use extend to solve some real problems. Basic/intro to oop and extending in as3 extending event dispatcher to manage your own event queue extending event to create a custom event and finally extending Panel to make a drag/drop Panel Having classes and objects does not equal OOP. To understand a class, you need to understand that an object without state or behavior is meaningless, so to be an object, it must have those things. The object itself is an environment, it is like a scope, it’s a container which imparts a kind of semantic meaning to the function and variables attached to it. Having objects is very nice, but there needs to be something more, and that something more is inheritance. Without inheritance, objects are simply wrappers, and it would be just as well to create functions and simply pass in some kind of key to ameliorate what to do. With inheritance, you share state and behavior between objects. The common example is with a person, people have names, and […]

How To Flash CS5: Keyboard Navigation Action Script 3 KeyboardEvents 6

Well, I was watching an interesting little tutorial on keyboard navigation with AS3. Now, the guy’s tutorial was actually really good, however, it wasn’t really what I would call AS3. It was very on target for what it was doing, but didn’t leave much room for expansion in my opinion. So I decided to try my hand at changing it up a littler bit. Here is the code: This is put on the main timeline, in the first frame on an actions layer. That is, you just create a new layer, and name it actions, it’s just a convention. import; import; var currentKeyDown:uint = 0; var Keys:Object = {up: 38, down: 40, left: 37, right: 39,r:82, shift: 16, ctrl: 17, z:90, x:88}; var CurrentKeysDown:Object = {up: false, down: false, left: false, right: false, shift: false, ctrl:false}; stage.addEventListener(KeyboardEvent.KEY_DOWN,setCurrentKeyDown); stage.addEventListener(KeyboardEvent.KEY_UP,clearCurrentKeyDown); setTimeout(updateCanvas,100); function setCurrentKeyDown(event:KeyboardEvent):void { trace(event.keyCode); for (var key:String in Keys) { if (Keys[key] == event.keyCode) { CurrentKeysDown[key] = true; } } } function clearCurrentKeyDown(event:KeyboardEvent):void { for (var key:String in Keys) { if (Keys[key] == event.keyCode) { CurrentKeysDown[key] = false; } } } function moveEntity(entity:MovieClip):void { currentKeyDown = 0; var x:Number = 0; var y:Number = 0; for (var key:String in Keys) { if (CurrentKeysDown[key]) { currentKeyDown = Keys[key]; } switch (currentKeyDown) { case Keys.shift: entity.speed += entity.speed * .05; break; case Keys.ctrl: entity.speed -= entity.speed * .05; break; case Keys.z: entity.rotationSpeed += entity.speed * .06; break; case Keys.x: entity.rotationSpeed -= entity.speed * .06; break; case Keys.r: entity.x = 200; entity.y = 200; break; case Keys.left: […]

Simple PHP closure/lamda/anonymous function usage.

This is a fun little example of the new Closure features in php. class TArray { public $pMembers = array(); public $pCurrentIndex = 0; public function __construct() { $args = func_get_args(); if (empty($args)) return; if (count($args) == 1) $args = $args[0]; foreach ($args as $arg) array_push($this->pMembers,$arg); } public function push() { $args = func_get_args(); foreach ($args as $arg) { array_push($this->pMembers,$arg); } } public function pop() { return array_pop($this->pMembers); } public function each() { $args = func_get_args(); if (empty($args)) { if ($this->pCurrentIndex > count($this->pMembers)) { $this->pCurrentIndex = 0; return null; } $ret = $this->pMembers[$this->pCurrentIndex]; $this->pCurrentIndex++; return $ret; } else if (is_callable($args[0])) { array_walk($this->pMembers,$args[0]); } else { throw new Exception(“Array->each does not understand that…”); } } public function resetIndex() { $this->pCurrentIndex = 0; } } $array = new TArray(1,2,3,4,5,6); while ($i = $array->each()) { echo “$i\n”; } $array->each(function ($elem) { echo “$elem\n”;}); We can go a little further, and do like this. class Person { public $name; public function __construct($name) { $this->name = $name; } } $array = new TArray(new Person(‘John’), new Person(‘Jane’)); $array->each(function ($person) { echo “{$person->name}\n”;}); An issue that comes up is, how do I get the $this variable in there. The short answer is, you don’t. You can finaigle it after a fashion, but it’s just ugly. The issue is, people want the $this var, because they want to be able to manipulate the values, in a permanent fashion, or do something else. How you accomplish it, in a pretty way is: 1. Give up the ghost, you can’t have $this, it’s just a […]

AS3 Multiple Inheritance/Mixins 2

Well, I just got done reading this post about multiple inheritance in as3 which probably should have been titled AS3 mixin technique, because that is more or less what it is, check out the wikipedia article on mixins for some history and pros and cons. I’ll will admit straight out that I am a supporter of MI and Mixins and the like. I am not much for the aristotelian idea of the universe, sometimes A is A, and sometimes it’s both A and B and MI, when used responsibly helps you to find more elegant and concise solutions to a great many problems. Just because people use something incorrectly in some instances doesn’t make that thing inherently evil, take for instance perscription drugs, would you rather painkillers where completely banned or never prescribed just because some or even alot of people abuse them? No, a great many things when used excessively with little understanding of what it is and how it works can be called evil, but they aren’t, the people who abuse them are evil, let’s start calling a spade a spade. The problem with MI and Mixins is not that they are evil, it’s that evil people use them for nefarious purposes. Anyway, back onto topic, I liked the guys article, and to be honest, he was pretty brave for putting it up, the minute you say something about MI, either way, a “discussion” read “flamegeddon” ensues, not always, but it’s generally a risk when you start throwing words like Inheritance around with […]

ActionScript 3.0 Wishlist

ActionScript 3.0 is just, well, pretty damn awesome. Nevertheless, as I’ve been working in AS3, I have come across a few limitations of the language, that while not show stoppers, leave just a little something to be desired. 1. No control over GC. That doesn’t sound important to your average Joe Programmer, but being able to commandeer, or at least influence Garbage Collection would be so totally awesome. For instance, I am working on a framework that more or less enforces a singleton based architecture, everything should be a singleton, except in 1 or 2 small circumstances that the framework controls. I.e. I have a special object wrapper that controls an inner object and can do some fancy footwork about it, all without you really having to worry about what is in the object. I would love for the user to be able to call SpecialObject.destroy(), and have it completely obliterated from memory. As it stands right now, this is not possible. Further more, it’s not really possible to make sure anything is removed from memory, because if it has even 1 single existing reference in your entire app, it will not be GCed, as was pointed out in the post about the AS3 Garbage Collector. Furthermore, if it has an event attached to it, it can’t be destroyed, which causes all kinds of issues, including one I’ll talk about in a moment. One method of fighting against this, that I am working on, but unsure of, is to discourage putting objects into variables […]