Monday, 16 November 2015

[tutorial] How to create directive for logic gate OR

In this arctical I am going to show you how to make a directive for logic gate OR.

Directive

In "Angular speech", the directive is piece of code, which solve something useful. It can even manipulate the DOM. It extends HTML language by either new tag or new attribute.

Logic gate OR

A logic gate implements Boolean logic function; the gate OR implements function OR (F = A OR B).

Directive for logic gate OR

The goal is to create useful directive, which implements OR function and has the graphical "interface", the gate itself, implemented as SVG.

Disclaimer: This is a proof-of-concept, not the best solution ever...

The name of the directive should be clear; so I decide to name it "logOr2" (the digit 2 means it uses two inputs). 
I use the isolated scope; this give us the ability to assign the inputs of the directive with the input button, input toggle, input variable, etc. independently to the directive itself.

.directive('logOr2', function(){
    return {
        scope: {
            x: '=',
            y: '=',
            a: '=',
            b: '=',
            out: '=',
            hl: '='
        },

What all this variables mean?
The x, y stands for the x- and y- coordinates inside SVG picture. We can use any position of our gate OR inside SVG.
The a, b are the inputs for logic gate OR.
The out should be used in case we need the result of the A OR B outside the directive (optinal).
The hl variable is for formatting the logic values. Somebody prefers "1, 0", somebody else "H, L" (high, low), somebody "T, F" (true, false).

Because we use this directive as a part of SVG picture, we need use:

        replace: true,
        templateNamespace: 'svg',

Now becomes an interesting part, the link function.

        link: function($scope){
            $scope.x = $scope.x || 0;
            $scope.y = $scope.y || 0;

            $scope.sign = "\u2265"+"1";
            $scope.setHL = function(){
                if ($scope.hl === '1, 0') {
                    $scope.H = 1;
                    $scope.L = 0;
                    return;
                };
                if ($scope.hl === 'H, L') {
                    $scope.H = 'H';
                    $scope.L = 'L';
                    return;
                };            
                if ($scope.hl === 'T, F') {
                    $scope.H = 'T';
                    $scope.L = 'F';
                    return;
                };
            
            }();

            $scope.strLog = function(value){
                if (value === undefined || value === null) return null;
                return value ? $scope.H : $scope.L;
            };
            
            $scope.Q = function(){
                if ($scope.a === undefined || $scope.a === null || $scope.b === undefined || $scope.b === null ) return null;
                var res = $scope.a || $scope.b;
                $scope.out = res;
                return res;
            };

        }

The $scope.x, $scope.y are equal to value defined from HTML code or are set to zero.
The $scope.sign is used as the "mark" or "type" of the logic gate.
The $scope.setHL() is the function for formatting logic values (one, zero) to "1", "0", or "H", "L", or "T", "F".
The $scope.strLog() is the function which return true/false as defined in $scope.setHL() function.
The $scope.Q() is the function with two results; it solves the Boolean OR function as a result of $scope.Q() function; and it assign its value to the output variable $scope.out (see the "scope section" above). The return value of $scope.Q() function is then used inside the SVG part of the directive.

The SVG part is easy to make: the rectangle with two inputs, two outputs, the sign (type of the logic gate) inside and the values (input, output). It should look like:


        template: '<g stroke="black" transform="translate({{x}}, {{y}})">'+
                    '<line x1="10" y1="20" x2="40" y2="20" stroke-width="2"/>' + 
                    '<text x="10" y="17">{{strLog(a)}}</text>' + 
                    '<line x1="10" y1="50" x2="40" y2="50"  stroke-width="2"/>' + 
                    '<text x="10" y="47">{{strLog(b)}}</text>' + 
                    '<rect x="40" y="5" width="40" height="60" fill="none" stroke-width="2" />' + 
                    '<text x="75" y="25" stroke-width="1.5" font-size="1.2em" text-anchor="end">{{sign}}</text>' + 
                    '<line x1="80" y1="35" x2="110" y2="35" stroke-width="2"/>' + 
                    '<text x="100" y="32">{{strLog(Q())}}</text>' + 
                '</g>'

And that's all.

We can use our directive from HTML page as a new tag:

<svg width="120" height="80">
 <log-or2 x="20" y="20" a="lab.data.A" b="lab.data.B" out="lab.out" hl="lab.HighLow()"></log-or2>

</svg>

Where lab.data.A, lab.data.B, lab.out are the variables assigned to the controller using the "controller as" syntax.


My app Boolean Lab

This directive is used in my android app Boolean Lab. It is the laboratory of logic Boolean functions in mobile/tablet; free, no ads, four languages - English, Spanish, Czech, Portugal.





Boolean Lab: more than 1200 users

I have one favorite app (I am the author) - Boolean Lab - it now has more than 1200 users from all the world; USA: 196, Brasil: 114, Mexico: 108. This app uses four languages: English, Spanish, Czech, Portugal.

Wednesday, 28 October 2015

Ionic - Prepare your development computer

Prepare your development computer for programming mobile hybrid apps using Ionic.






Friday, 23 October 2015

Sunday, 13 September 2015

Boolean lab - my android app

Boolean lab

I have published my android app - Boolean lab - in Google Play.

How is it written?

This app is created using Ionic, it means it is an hybrid app (HTML + CSS + JS). Using this app, there is no difference between hybrid and native app.

Who is this for?

It is intended for students and others who want to know how Boolean logic works. If you are not familiar with the term "Boolean logic", you maybe know "logic function, logic variable, logic operator" such as OR, AND, XOR etc.

How it works?

Boolean lab is fully interactive. When you change the input(s), the output(s) change immediately. You can understand how logic gates work; the gates as are included together with logic functions. There are 24 logic circuits as well for better understanding.

How it looks like?




From the Google Play:

Boolean lab - a small laboratory in your mobile / tablet. 

Interactive and visual explanation of the boolean variables, logic functions and their behavior. For each logic function is shown its type, truth table and gate. Everything is interactive - input variables change is immediately reflected in the truth table and on the input(s) and output(s) of gates. 

You can set the label logical values either in shape or H-L 1-0 or T-F, marking the log. function F or Y, the indication of the clock signal T, C or E.

It contains 24 interactive engagement logic circuits.

Four languages

The app is is localized into four languages: English, Spanish (Castellano), Czech, Portuguese.




Thursday, 3 September 2015

Framework

Framework

Framework is an library of CSS and/or JS. 

CSS framework defines common visual rules and could offer new "components". It is designed for "nice look" of web page and/or hybrid app.

JS framework defines functions useful for developer. Using JS framework means less work and better efficiency.

There are many frameworks, some of them are famous and useful.
There are many differences across frameworks, so the question Which one is the best? could not be answered. It depens on many factors.

I personally use Ionic framework, which I recommend for developing hybrid mobile apps.





Wednesday, 2 September 2015

Hybrid mobile apps

Hybrid mobile apps

Are written in combination of HTML + CSS + JS. 

HTML stands for HyperText Markup Language and is used for the web pages. CSS is Cascading Style Sheets language for defining visual rules on the web page. JS means JavaScript. 

How can be hybrid app run on mobile devices as we know it is the web page? The answer is simple. There is the source code of our app (HTML + CSS + JS) and it is transformed to the package native-like-app. It means, we need transform ("compile") the source code to the destination format needed for the destination platform. For Android it is the .apk file format.

There are two services for that transformation ("compilation"):
For the first point of view we can assume they both are the same.