Teensy – why you shouldn't plug anything to your USB port

ScreenshotWe have a Teensy and we know how to write program that could type something. What we can do with that? We can input some system commands.
Most simple example would be to open notepad and then type something in it.
First thing that we have to do in our program is to wait for a while – Windows don’t always recognize plugged keyboard immediately. Then we have to press Win+R, wait, type notepad, wait again and then type whatever we want. Czytaj dalej Teensy – why you shouldn't plug anything to your USB port

Teensy for beginners – playing with keyboard

Photo by: Jeroen Bennink

We have Teensy and know how to program it. So its time to do write some code that allow us to… write something 😉 Good place to start would be usb_keyboard example from official Teensy website. There is some simple API for using keyboard that we can later modify for our needs.

Basically to press key from Teens you have to call function:

int8_t usb_keyboard_press(uint8_t key, uint8_t modifier);

where first argument is number of key (like A or 5), and second one is modifier (like Shift or Ctrl). To get key number, you can check usb_keyboard.h file, where all keys are defined as KEY*. There is also lower level functions, that allow you to, for example, to keep pressed Ctrl while pressing other keys. But I didn’t need it, so lets leave it alone.
Czytaj dalej Teensy for beginners – playing with keyboard

Teensy for beginners (like me) – first step

Teensy 2.0 on desktopRecently I bought something called Teensy 2.0. I’m still not sure why would I need it, but I will figure it out soon. What is Teensy? It is small USB Development Board that could be programmed as USB HID recognized by PC (so it can pretend to be keyboard or mouse).
I’m not an expert in such stuff, so it is new challenge to learn how to do something with it.

So what to do with such board? First thing may be to try to plug it in into USB port – if this new board, then build in LED should start blinking. But after few seconds of watching LED it gets boring. Czytaj dalej Teensy for beginners (like me) – first step

Why I don’t like Visual Basic.NET – Part #2

As I have mentioned some time ago – I’m not a fan of VB.NET. Here is another example why:
Let say we have such function:

    Function CalculateAmount(ByVal items As List(Of Item)) As Decimal
        Dim amount As Decimal = GetSum(items) ''returns 15

        Dim tax As Decimal = CalculateTax(amount, items) ''returns 5
        Dim serviceCharge As Decimal = CalculateSC(amount, items) ''returns 2

        Return amount + tax + serviceCharge ''returns 27
    End Function

at first look everything looks fine. But, why there 27 as result, and not 22?
Oh, that’s obvious. Someone declared CalculateTax as:

    Function CalculateTax(ByRef amount As Decimal, ByVal items As List(Of Item)) As Decimal
        Dim tax As Decimal
        ''some magic
        amount += tax
        Return tax
    End Function

In above example it might be easy to find issue. But in more complex code, where developers don’t always care about it…

C# made it easier, you can either use ref or out, and you have to add this modifier when function is called. So much easier to read.

How to speed up build time for F# project with SQL TypeProvider

It is good to read documentation. There is so many useful information that might save a lot of time…
I’m working on some project, where I use F# TypeProviders to connect with some legacy database. And each time I built my project, I was reloading database scheme. So each build took too long. At first, I’ve moved my F# code to new solution, so I didn’t have to wait for it each time I was testing my app. But sometimes I still have to fix and rebuild part of the code with data access. I it still took a lot of time. And today comes realization: „Maybe there is some way to cache database scheme?”.
Of course it is, it is F#!
When declaring SqlDataConnection type provider, there two additional parameters that could be use:

  • LocalSchemaFile
  • ForceUpdate

First one is just path to file, second one could be used to force VS to regenerate cached file if there were some change in application. Nice thing is that such file could be shared amongst other F# projects, you just need to put correct path. As I have two projects in solution: first with data access for my application and second with some tests, my context definition for both projects looks like that:

type internal EntityConnection = SqlDataConnection<ConnectionStringName="appTypeProvider",ConfigFile="App.exe.config", LocalSchemaFile="../appSchema.dbml", ForceUpdate=false>

Explicit interface implementation

As they say: you live and learn. Some time ago I wrote about why I don’t like VB.NET, where I was complaining about strange VB.NET behaviour with implementing interfaces. And recently I found out that in C# there is also something called Explicit Interface Implementation.

So if we take example from my previous post:

interface IDoNothing
        void Method();

    class FirstClass : IDoNothing
        public void Method()

        void IDoNothing.Method()
    class PrivateClass : IDoNothing
        public void Method()

And then we can call it:

    public class Program
        public static void Main()
            Console.WriteLine("As Interface:");
            IDoNothing asInterface = new FirstClass();
            //Outputs: FirstClass.IDoNothing.Method

            Console.WriteLine("As Class:");
            FirstClass asClass = new FirstClass();
            //Outputs: FirstClass.Method
            //Outputs: FirstClass.ThisIsMethod

            Console.WriteLine("As Private Class:");
            IDoNothing asPrivateClass = new PrivateClass();
            //Outputs: PrivatClass.Method

It is quite similar to VB.NET, but for me it much more clearer and there are less possibilities for create obfuscated code (like defining private method which is actually public).
Czytaj dalej Explicit interface implementation

Dump data from SQL to CSV (or other format) with F#

Recently I started playing with F#. And when I looked up for a way of accessing data, I found something called TypeProvider. And I have to say it is amazing tool. Then I realized that I was looking for some simple way to dump part of my database to CSV file, so I can use this data in automated testing (I might write something about that in near future). And I could use F# to just do it!
The result of my work was code that could dump selected tables (or part of selected tables) into separate CSV files (or in any other format, if you change function that dumps data). It will automatically get all columns in your database, so you have to only list tables that you want to dump (or write some simple F# query to get only data you want – useful if you have tables with millions of records, and don’t want to dump all of them).

Here is description of how it is done: (or you can just look on full source code).
Czytaj dalej Dump data from SQL to CSV (or other format) with F#

Easy way to run SQL stored procedure in C# using dynamics

I don’t like standard way of calling stored procedures in .NET with SqlCommand. So when I was starting one of projects, I thought that maybe I could use some helper that would allow me to easier way of calling procedures. As I didn’t find anything interesting, I’ve created one by myself. And this is one of this moments, when you feel why dynamics in C# are so great thing.
Now if I want to save some data using stored procedure I can use code like that:

SqlHelper.SaveData("SP_SAVE_IMPORTANT_DATA", new
    firstParameter= "This is value",
    allData = 42

To get data fro Czytaj dalej Easy way to run SQL stored procedure in C# using dynamics

Why I don't like Visual Basic.NET – Part #1

In my work, I have to work quite often with Visual Basic. This language have some advantages over C#, but most of the time, it is very annoying.

Let’s take Interfaces for example:

Public Interface IDoNothing
    Sub Method()
End Interface

And we can implement two classes:

Public Class FirstClass
    Implements IDoNothing

    Public Sub Method()
    End Sub

    Public Sub ThisIsMethod() Implements IDoNothing.Method
    End Sub
End Class

Public Class PrivatClass
    Implements IDoNothing

    Private Sub Method() Implements IDoNothing.Method
    End Sub
End Class

Czytaj dalej Why I don't like Visual Basic.NET – Part #1

Models autoloading in CakePHP

CakePHP convention require to declare in each controller which model we want to use in it by declaring $uses array.

$uses = array('Model1', 'Model2');

In some of my projects, list of models required by some controllers, became bigger and bigger. So eventually they include most models from that project, and part of them was used only in some specific cases, like error handling. Instead of adding all required models to $uses array, I could import some of them explicit when they are needed using:

App::import('Model', 'ModelName');
$this->ModelName = new ModelName();

But there is another option: I could write some code that will auto load required models, when they are needed.

You have to add below function to your AppController to make it work:

public function &__get($name) {
$this->invertedModels = array_flip(Configure::listObjects('model'));

return $this->$name;

How does it work?
It is using one of PHP 5 magic functions – __get(). This functions is used when you try to access some nonexistent property in class. So if you add this code, and then you will try to access some undefined model in code, PHP will try to create this model then.

In similar way you can achieve components auto loading. But there could be one more thing needed. I will write about it soon.