RSS

Category Archives: dev

git + dropbox = gitbox!

Let say you’d like to sync all your projects between different pcs you own. Surely you can put every project on GitHub but this is not good for closed source projects (not if you’re using their free plan).

You can put the entire project on DropBox, but it will fill your space with .obj, .dll, .tmp, /node_modules/, … stuff you really NOT need.

The trick is to keep just a bare repository into DropBox: a well formed gitignore file will prevent you to sync object files.
So I create a GitBox folder into my DropBox folder, then:

cd "c:\DropBox\GitBox"
mkdir MyAwesomeProject
cd MyAwesomeProject
git init --bare

cd "c:\MyDevPath\MyAwesomeProject"
git init
git remote add gitbox "c:\DropBox\GitBox\MyAwesomeProject"
git add *
git status
git commit -m "1st commit"
git push --set-upstream gitbox master

Advertisements
 
Leave a comment

Posted by on 2018/04/22 in dev

 

Tags: ,

Inspect a list of filenames (js)

This shortcut demonstrates the power of some ES6 features.

Given a list of filenames, a map function is applied onto the array to generate (another) array of objects. Arrow functions and parentheses (to return JSON objects) are the trick used to add properties “on the fly”.
The resulting object will have the name, path and stats information resolved.

https://bitbucket.org/snippets/pirraste/8eG956/filesniffjs

 
Leave a comment

Posted by on 2018/04/20 in dev

 

Tags: , ,

SELECT latestversion FROM versionedRows: a comparative approach

Because there’s no “the best way to do things” but only the best way to do THIS thing (to read with Yoda voice).

 
Leave a comment

Posted by on 2017/09/07 in dev

 

Tags: , ,

GZip a file in memory (csharp)

It is a thing I ALWAYS forget: what’s the right piping sequence? How avoid errors? ..and so on…

Here’s the snippet

 
Leave a comment

Posted by on 2017/08/23 in dev

 

Tags: , ,

POST a file, read a JSON response

Here’s a trick to send a file to a server AND read a response from it, in the same context.
I used it with a Node.js server (Express + busboy) and it worked like a charm.

When using HTML forms, you can submit stuff to a server but you can’t read a response from it.
If you need some kind of “send data” → (do something server side) → “read response” it can be painful.

I wrote a workaround to manage this kind of situation. It is pretty simple:
– prepare a form as usual
– block the original event
– build an ajax request as a POST method

Here’s the code

 
Leave a comment

Posted by on 2017/07/14 in dev, Uncategorized

 

Tags: , ,

Promises rejection (..but with style)

I find handy define this kind of function in my code:

function if_error(reject, err) {
  if (err) {
    console.error(err);
    reject(err);
  }
}

Now when I’ll need to handle a rejection, my code will be

something.can('go-wrong', (err) => {
  if_error(reject, err);
  ...

It’s something between pure functional programming and very readable code.

 
Leave a comment

Posted by on 2017/05/16 in dev

 

Tags: ,

Arduino colors!

Last night I was playing with a Neopixel Ring controlled by a Wemos Mini, when I decided to attach a potentiometer to manually handle the colors.
Now, when you read from an analog source you’ll get a value in an interval, like [0, 1024]. The problem with this kind of value is that it is a single number that needs to be mapped in three r,g,b values (ring.setPixelColor(n, red, green, blue);)!

After trying a good way to map [0,1024] => {[0,255], [0,255], [0,255]}, I remembered there are MANY ways to define colors: RGB, CMYC, HSV, …Wait! HSV? It’s perfect!
It’s like handling polar coordinates where you have an angle that describes the Hue and it is a number between 0 and 360. The other parameters are two vectors and describe the Saturation and the Brightness. These values can be set constant to 1, in my case.

First let’s map the analog value to a Hue value:

int val = analogRead(A0); // 0 - 1024
float h = 360.0 * ((float)val / (float)1024); // 0 - 360

then use this function to convert. Note that r,g and b are by ref.

/**
* HSV to RGB color conversion
* The hue value H runs from 0 to 360 degrees.
* The saturation S is the degree of strength or purity and is from 0 to 1. Purity is how much white is added to the color, so S=1 makes the purest color (no white).
* Brightness V also ranges from 0 to 1, where 0 is the black.
* R,G,B are between 0 and 255.
* http://www.cs.rit.edu/~ncs/color/t_convert.html
*/
void HSVtoRGB(float h, float s, float v, float *r, float *g, float *b)
{
    int i;
    float f, p, q, t;

    if (s == 0) {
        // achromatic (grey)
        *r = *g = *b = v;
    }
    else {
        h /= 60;      // sector 0 to 5
        i = floor(h);
        f = h - i;      // factorial part of h
        p = v * ( 1 - s );
        q = v * ( 1 - s * f );
        t = v * ( 1 - s * ( 1 - f ) );

        switch( i ) {
            case 0:
                *r = v;
                *g = t;
                *b = p;
                break;
            case 1:
                *r = q;
                *g = v;
                *b = p;
                break;
            case 2:
                *r = p;
                *g = v;
                *b = t;
                break;
            case 3:
                *r = p;
                *g = q;
                *b = v;
                break;
            case 4:
                *r = t;
                *g = p;
                *b = v;
                break;
            default:    // case 5:
                *r = v;
                *g = p;
                *b = q;
                break;
        }
        
        *r *= 255;
        *g *= 255;
        *b *= 255;
    }
}
 
Leave a comment

Posted by on 2017/03/24 in dev

 

Tags: ,