Visualizzazione post con etichetta UBPorts. Mostra tutti i post
Visualizzazione post con etichetta UBPorts. 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)




Geologi

  E so anche espatriare senza praticamente toccare strada asfaltata