sabato 27 aprile 2019

Design Patterns: Composite Patter

Composite patter essentially implements a tree of objects and you can use it everytime you need a tree data structure.
At first you define a class for a node with private information for the node and the list of child node. In the constructor you pass the information to create the node, also you need a method to add a child node that add the child node in the private list of child nodes and also a method to get all the child nodes. You also need an class for the root node to create the tree. the root node class has a private node that is the root node. you need a method to get this root node, then you call rootnode.add to add child nodes to root node and build the tree. A classic use of composite pattern is associated to the filesystem that is a tree data structure.

public class Node
{
      private NodeInfo nodeInfo;
      private List<Node> childNodes;
      public void Node(NodeInfo info)
     {
           nodeInfo = info;
           childNodes = new List<Node>();
     }

     public NodeInfo GetInfo()
    {
           return nodeInfo;
    }

     public void Add(Node node)
     {
            childNodes.Add(node);
     }

     public IEnumerable<Node> GetChildNodes()
    {
            foreach(var node in childNodes)
                   yield return node;
    }

}

pubic class Root
{
      private Node root;

      public Root(Node node)
      {
             root = node;
      }

      public Node getRoot()
      {
             return root;
      }
}

Usage:
var rootNode = new Node(nodeInfo);
var firstChild = new Node(nodeInfo1);
var secondChild = new Node(nodeInfo2);
var tree = new Root(rootNode);
var node = tree.getRoot(),
node.Add(firstChild);
node.Add(secondChild);
foreach(n in node.GetChildNodes())
     Console.WriteNode(n);

Design Patterns: Strategy Pattern

In the latest years design pattern programming seems a must if you want to work in a development team, especially java and C#. What I think is that a good programmer only need common sense.

This is very simplified explanation of strategy pattern:
Strategy pattern is when you want use different strategy or algorithms to process data. For example you want to use different types of sort: quick sort, bubble sort,etc, in this case the strategy is the sort algorithm. You have to create an abstract class Strategy with a method to apply the strategy, then you have to implement the strategies classes inherit from Strategy class that overload the same method with their strategy.
At the end you have to create a context class that has a private Strategy member, in the constructor you pass the strategy class (Ex: quick sort) and a method to implement the strategy that will call the strategy.strategy method.

public abstract class Strategy
{
public abstract double applyAlgorithm(int a,int b),
}

public class FirstAlghorithm extends Strategy
{
@Override
         public double ApplyAlgorithm(int a,int b)
         {
// do calculation
}
}

public class SecondAlghorithm extends Strategy
{
@Override
         public double ApplyAlgorithm(int a,int b)
         {
// do calculation
}
}

public class Context
{
         private Strategy strategy,
   
         public Context(Strategy strategy)
         {
                this.strategy = strategy;
         } 

         public double applyAlgorithm(int a,int b)
         {
return strategy.ApplyAlgorithm(a,b);
}
}

Usage:

Context ctx = new Context( new FirstStrategy());

double result = ctx.ApplyAlgorithm( 4, 2);


In case of sorting strategies the previous example  become:

public abstract class StrategySort
{
public abstract void Sort(int[] array),
}

public class MergeSort extends StrategySort
{
@Override
        public void Sort(int[] array)         
        {
// do merge sort in array
}
}

public class QuickSort extends StrategySort
{
@Override
        public void Sort(int[] array)         
        {
// do quicksort in array
}
}

public class Context
{
         private StrategySort strategy,
   
         public Context(StrategySort strategy)
         {
                this.strategy = strategy;
         } 

        public void Sort(int[] array)         
        {
strategy.Sort(array);
}
}

Usage:

Context ctx = new Context( new QuickSort());

double result = ctx.Sort(array);





mercoledì 27 marzo 2019

ESP32 AND C#

I installed nanoframework in Visual Studio 2017 and I can program ESP32 using C#.
The API for ESP32 is quite limited and with Arduino IDE you can do much more, but C# is high level programming language,  it's highly OOP oriented and it's a good solution  for complex applications. ESP32 has a lot of memory and double core CPU. If you use it with LUA or javascipt you are limited  by the language.
Also, if you have a ESP32 board with JTAG, you can  use the step by step debugger of Visual Studio.
You have to download a specific firmware on ESP32 to make it  work with nanoframework, the firmware is in nanoframework git repository.

domenica 27 gennaio 2019

SPARKFUN MICRO PRO


Dear friends,
I was  googling about how  to  use esp32 like  a usb  keyboard, but it doesn't seem feasible and youncan't use  the  arduino keyboard  library.
It seems only certain arduinos can use keyboard  library: Leonardo, micro, arduino 2, mkr serie.
I opted for sparkfun micro pro, because I found it in  the electronic  shop behind the corner.
At first glance I was very surprised for the size,  it's incredible  small, apart  that, keyboard libraries  works  perfectly on it.
You will wonder why am I interested  in such a device?
The reason is because I red of a device  called bash bunny.
That'an interesting concept,  because the idea of simply connect to the computer an usb keyboard and automagically control  it is the fantasy of my childhood and it  is used  in many Hollywood movies.
In past  I thought to simulate a cdrom with  an  arduino, because  it you pur certain  files  on a cdrom  the  old versions  of windows  executed it when you3 inserted the  cdrom  without asking, but  the latest  versions of windows  asks.
Basically  as usb  key  emulator you can do anything on the computer. You  can login, activate the command prompt or the  powershell  prompt  and write your  program and execute  it. Top can have the total control  of the system.
For example it you want to start  the command  prompt, voi nave to use this code:
include "Keyboard.h"
...
Keyboard.begin();
Keyboard.press(KEY_LEFT_GUI);
Keyboard.press('r');
Keyboard.releaseAll();
delay(2000);
Keyboard.press(KEY_RETURN);
Keyboard.releaseAll();
delay(2000);
Now you can write your commands
...
It you want to type many characters, you need an  sd reader to  connect to  the micro pro. Usually sd card  readers requires 5volt.  You  can  use  the  usb  to  obtain this Voltage. I bought the 3.3 volts  version  of the micro  pro because I wanted to connect  it to the esp32, but there is also  a 5v version.

sabato 19 gennaio 2019

FACE EDITOR

Hey guys,
I found a program that uses unsupervised learning neural network to generate random faces.
https://github.com/HackerPoet/FaceEditor
These faces are not faces of real people, these are faces of unexisting people. You click a button and it generates a random face,then you can change this face using many switches.
These are some faces I generated:

What happens if someone put a unexisting face in a fake facebook profile?


sabato 29 dicembre 2018

NEURONA


This is an ESP32 board with OLED display. It's cool.
I was trying to program this board by Zerynth Studio, but the display on this board uses I2C, while the libraries for Zerynth Studio works only with SPI interface. In order to avoid to manage the I2C protocol from the base class, I had to move to Arduino IDE and forget Python.
Has been a long time since I used Arduino Ide and I discovered an interesting neural network implementation, it is distributed as Arduino library: Neurona.
This library implements a supervised neural network for Arduino boards and also it works for ESP32, my crush board. The usage of the library is quite simple, you initialize it with the number of inputs, the number of hidden layers and the number of outputs. You have to provide the trained weights, it does not train on Arduino, you have to use an external software to train the network and obtains the weight, then you pass the weight to the library.

sabato 15 dicembre 2018

EVOLUTION FOR ANDROID

I found this app on  Google Play:
You can build a creature by adding joints,  bones and muscles.
The game uses neural network to evolve your creature to walk,  jump  and avoid obstacles.
It's very funny and additive.