Visualizzazione post con etichetta Ubuntu Touch. Mostra tutti i post
Visualizzazione post con etichetta Ubuntu Touch. Mostra tutti i post

venerdì 10 aprile 2020

QML Mandelbrot in UBPorts / Ubuntu Touch (2)

Avevo gia' provato qui a creare l'insieme di Mandelbrot su UBPorts in QML ma il programma era scandalosamente lento ed ho provato a migliorare


In questo codice il risultato di calcolo per ogni pixel viene salvato in un array imgData che poi 
trasferito in un colpo sul Canvas. In questo modo il tempo di calcolo e' divenuto inferiore ad un terzo di quello della precedente prova

==============================================================
/*
 * Copyright (C) 2020  Luca Innocenit
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3.
 *
 * ubuntu-calculator-app is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import QtQuick 2.7
import Ubuntu.Components 1.3
//import QtQuick.Controls 2.2
import QtQuick.Layouts 1.3
import Qt.labs.settings 1.0
import QtQuick.Window 2.2

MainView {
    id: root
    objectName: 'mainView'
    applicationName: 'qtcpp.com.luca.innocenti'
    automaticOrientation: true

    visible: true
      width: Screen.width
      height: Screen.width
      Canvas {
          id: mycanvas
          anchors.centerIn: parent
          width: Screen.width
          height: Screen.width
          onPaint: {
              console.log("width "+ width.toString())
              console.log("height "+ height.toString())

              var ctx = getContext("2d");
              ctx.fillStyle = Qt.rgba(0, 0, 0, 1);
              ctx.fillRect(0, 0, width, height);
              var imgData = ctx.createImageData(height, width);

              ////////
              var re_min = -2.0;
             var im_min = -1.2;
             var re_max = 0.7;
             var im_max = 1.2;
             var iterazioni = 50;

             var r;
             var a,b;
             var x,y,x_new,y_new;
             var test;
             var re_factor = (re_max-re_min);
             var im_factor = (im_max-im_min);
             var indice = 0;

              for (var i=0;i<width;i++)
                          {
                          for (var j=0;j<height;j++)
                           {
                               a = re_min+(j*re_factor/height);
                               b = im_min+(i*im_factor/width);
                               x = 0;
                               y = 0;
                               test = 0;
                              imgData.data[indice+3]= 255;
                              //console.log(indice)

                               for (var k=0;k<iterazioni;k++)
                                    {
                                    x_new = (x*x)-(y*y)+a;
                                    y_new = (2*x*y)+b;
                                    if (((x_new*x_new)+(y_new*y_new))>4)
                                          {
                                            if (k%2)
                                                {
                                                  // colora il punto
                                                  imgData.data[indice]=255;
                                                  imgData.data[indice+1]=255;
                                                  imgData.data[indice+2]=255;  
                                                }

                                            break;
                                          }
                                    x = x_new;
                                    y = y_new;
                                    }
                               indice = indice + 4;
                           }
              }
              ///////
              console.log("terminato")
              ctx.drawImage(imgData, 0, 0);

          }
      }
}

sabato 28 marzo 2020

QTSensors su Ubuntu Touch (UBPorts) in QML

Visto che sto imparando a programmare UBPorts ho voluto provare a convertire questo progetto Android/Kotlin per QML. In pratica si tratta di leggere i valori dell'accelerometro e, ad intervalli regolari, inviare i dati ad server web con una richiesta GET


Per abilitare l'utilizzo della rete nella app si deve modificare il file apparmor
apparmor
===========================================
{
    "policy_groups": ["networking"],
    "policy_version": 16.04
}

===========================================


Main.qml
===========================================
/*
 * Copyright (C) 2020  Your FullName
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3.
 *
 * ubuntu-calculator-app is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import QtQuick 2.7
import Ubuntu.Components 1.3
//import QtQuick.Controls 2.2
import QtQuick.Layouts 1.3
import Qt.labs.settings 1.0

import QtSensors 5.0



MainView {
    id: root
    objectName: 'mainView'
    applicationName: 'acc2.luca.innocenti'
    automaticOrientation: true
   
    // variabili globali
    readonly property double radians_to_degrees: 180 / Math.PI
    property double pitch: 0.0
    property double roll: 0.0

    width: units.gu(45)
    height: units.gu(75)

    // un evento ogni 5 secondi
    Timer {
          interval: 5000
          running: true
          repeat: true
          onTriggered: {
            console.log("Timer")
            var xmlHttp = new XMLHttpRequest();
            xmlHttp.open("GET", "http://192.168.1.102/index.php?pitch="+pitch.toFixed(3)+"&roll="+roll.toFixed(3), true); // false for synchronous request
            xmlHttp.send();

          }
      }


    Accelerometer {
        id: accel
        dataRate: 100
        active:true

        onReadingChanged: {
            var x = accel.reading.x
            var y = accel.reading.y
            var z = accel.reading.z
            pitch = calcPitch(x,y,z)
            roll = calcRoll (x,y,z)
            //etichetta.text=""+accel.reading.x+"\n"+accel.reading.y+"\n"+accel.reading.z
            etichetta.text = "Pitch : "+pitch.toFixed(1)+"\nRoll : "+roll.toFixed(1)

            function calcPitch(x,y,z) {
                return Math.atan2(y, z)*radians_to_degrees;

                }
            function calcRoll(x,y,z) {
                return Math.atan2(-x, Math.sqrt((y*y)+(z*z)))*radians_to_degrees;
                }

          }


        }



    Page {
        anchors.fill: parent

        header: PageHeader {
            id: header
            title: i18n.tr('acc2')
        }


        Label {
            anchors {
                top: header.bottom
                left: parent.left
                right: parent.right
                bottom: parent.bottom
            }
            id: etichetta
            text: i18n.tr('Luca')

            verticalAlignment: Label.AlignVCenter
            horizontalAlignment: Label.AlignHCenter
        }

    }
}

QML Mandelbrot in UBPorts / Ubuntu Touch

Ovviamente, visto che mi sono messo a studiare UBPorts, non poteva mancare un porting di Mandelbrot. La prima scelta e' stata QML che in pratica e' un sottoinsieme di HTML5 per quanto riguarda il codice

Il progetto completo in Clickable si trova a questo link
https://github.com/c1p81/lucainnoc-gmail.com

Piccola nota: il progetto e' dannatamente lento


Le icone nel progetto sono in formato .svg (Gimp non supporta direttamente questo formato)


===================================================================
/*
 * Copyright (C) 2020  Your FullName
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3.
 *
 * ubuntu-calculator-app is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import QtQuick 2.7
import Ubuntu.Components 1.3
//import QtQuick.Controls 2.2
import QtQuick.Layouts 1.3
import Qt.labs.settings 1.0
import QtQuick.Window 2.2


MainView {
    id: root
    objectName: 'mainView'
    applicationName: 'pixel.luca.innocenti'
    automaticOrientation: true

    width: Screen.width
    height: Screen.height

    Page {
        anchors.fill: parent

        header: PageHeader {
            id: header
            title: i18n.tr('Mandelbrot QML')
        }

Canvas {
id: canvas
width:  Screen.width
height:  Screen.height
  onPaint: {
      var context=canvas.getContext("2d")
           context.fillStyle="rgb(0,0,0)";
           context.fillRect(0, 0, width, height);
           var re_min = -2.0;
           var im_min = -1.2;
           var re_max = 0.7;
           var im_max = 1.2;
           var iterazioni = 25;
           var colori = ["#FFFFFF","#FF0000","#00FF00","#0000FF","#FF00FF","#FFFF00","#00FFFF","#FFaa00","#abcedf","#fedc0a","#ab16dd","#d00fba","#edabcc","#ddacff"];

           var r;
           var a,b;
           var x,y,x_new,y_new;
           var test;
           var k,j,i;
           var re_factor = (re_max-re_min);
           var im_factor = (im_max-im_min);
           for (var i=0;i<width;i++)
            {
            for (var j=0;j<height;j++)
             {
                 a = re_min+(j*re_factor/height);
                 b = im_min+(i*im_factor/width);
                 x = 0;
                 y = 0;
                 test = 0;
                 for (var k=0;k<iterazioni;k++)
                      {
                      x_new = (x*x)-(y*y)+a;
                      y_new = (2*x*y)+b;
                      if (((x_new*x_new)+(y_new*y_new))>4)
                            {
                              // colora il punto
                              r = k%12;
                              context.beginPath();
                              context.fillRect(i-1,j-1,1,1);
                              context.fillStyle=colori[r];
                              context.stroke();
                              break;
                            }
                      x = x_new;
                      y = y_new;
                      }
             }
           }
      }
}
}
}




giovedì 26 marzo 2020

Log su Ubuntu Touch (Ubports)

Uno degli aspetti piu' critici per sviluppare le applicazione UBPorts con Clickable e' riuscire a fare il debug.  La cosa piu' semplice e' loggarsi in SSH in shell sul tablet ed andare nella directory

/home/phablet/.cache/upstart

in questa directory ci sono i file di log. Il file di log per esempio della applicazione acc2.luca.innocenti sviluppata con Clickable avra' un nome file del tipo application-click-{nome_app}.{versione}.log ovvero

application-click-acc2.luca.innocenti_acc2_1.0.0.log 

Altrimenti si puo' usare la app LogViewer  https://open-store.io/app/logviewer.neothethird ma l'uso e' decisamente piu' scomodo (e con molto meno dettaglio)




domenica 22 marzo 2020

Ubuntu Touch su Nexus 7 2 gen

Mi ero gia' occupato oramai molti anni fa del progetto Ubuntu Touch (all'epoca Ubuntu for phones) piu' che altro perche' lo sviluppo era legato a Qt.


Per installare Ubuntu Touch sui dispositivi supportati la soluzione piu' semplice e' utilizzare UBPorts Installer

Per avere la taskbar si deve fare swipe da sinistra a destra, per avere l'elenco delle applicazioni aperte swipe da sinistra a destra  (swipe lento cicla tra le app, swipe veloce fa vedere tutte le app aperte)

Swipe da destra



Swipe da sinistra


Lo screenshot corrisponde alla pressione contemporanea dei due tasti del volume

Le applicazioni si installano da OpenStore



La mancanza di software e' abbastanza devastante!!

Per disinstallare una app si preme a lungo l'icona, si aprira' una nuova finestra con l'opzione rimozione

Per abilitare la connessione SSH con il tablet si digita sul computer

ssh-keygen

poi si copiano le chiavi sul tablet

adb push ~/.ssh/id_rsa.pub /home/phablet/
sempre sul tablet si copiano le chiavi

mkdir /home/phablet/.ssh 
chmod 700 /home/phablet/.ssh 
cat /home/phablet/id_rsa.pub >> /home/phablet/.ssh/authorized_keys 
chmod 600 /home/phablet/.ssh/authorized_keys 
chown -R phablet.phablet /home/phablet/.ssh
sudo android-gadget-service enable ssh 

A questo punto per collegarsi via rete si prende l'ip del tablet e si entra in SSH

ssh phablet@ip_address


Per editare direttamente sul tablet via cavo USB si puo' usare semplicemente

adb shell

La password di root della shell e' il codice PIN

Si puo' effettuare un mirror dello schermo del tablet sul pc tramite il comando

adb exec-out timeout 120 mirscreencast -m /run/mir_socket --stdout --cap-interval 2 -s 384 640 | mplayer -demuxer rawvideo -rawvideo w=384:h=640:format=rgba -



Per programmare UBPorts non si usa piu' Ubuntu SDK ma si utilizza  Clickable

Per installare si procede  con il seguente codice

pip3 install git+https://gitlab.com/clickable/clickable.git

si deve poi mettere in Path la directory $HOME/.local/bin e si deve avere in Path anche Adb

 The scripts futurize and pasteurize are installed in '/home/luca/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  The script cookiecutter is installed in '/home/luca/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  The script jsonschema is installed in '/home/luca/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  The script clickable is installed in '/home/luca/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.

Sul tablet si devono abilitare i permessi da sviluppatore da Impsotazioni/Informazioni/Modalita' sviluppatore/  (ATTENZIONE: la modalita' sviluppatore non si puo' abilitare se non e' stato impostato un blocco schermo per esempio con Pin Code)

a questo punto per creare un progetto si usa

clickable create 

attenzione: per le voci AppName e Namespace non possono essere usati caratteri speciali (sono ammessi caratteri alfabetici ed il punto)

Per testare la applicazione si puo' digitare

clickable desktop

altrimenti con il solo clickable si effettua l'upload sul tablet

Applicazione Python in esecuzione su desktop
La stessa applicazione in esecuzione sul tablet

Come IDE si puo' usate l'editor Atom con il plugin https://atom.io/packages/atom-build-clickable
e selezionare la directory del progetto

Per compilare e lanciare la app sul tablet si preme F9. Se si preme F7 si puo' scegliere come target il desktop od il tablet.


attenzione : al momento di compilare il progetto viene effettuato il download di un docker con una immagine di Ubuntu 16.04 quindi c'e' necessita' di spazio disco e connessione di rete

Un libro di riferimento per la programmazione si trova a questo link

https://legacy.gitbook.com/book/mimecar/ubuntu-touch-programming-course/details

Geologi

  E so anche espatriare senza praticamente toccare strada asfaltata