createImageBitmap function polyfill with ImageData

Edge browser does not contain createBitmapImage function.

if you want to use it with ImageData

you can use this function as polyfill with typescript


Window.prototype.createImageBitmap = function(imageData):Promise{

return new Promise((resolve,reject)=>{

let canvas=document.createElement('canvas');
canvas.width=imageData.width;
canvas.height=imageData.height;
let context= canvas.getContext("2d");
context.putImageData(imageData,0,0);
return resolve(canvas);

});

}

typescript extension methods

Typescript ile extension method nasıl yazılır.

let x: number = 5.5;

let rounded = Math.round(x);

böyle yazmaktan sıkıldıysanız.

let rounded = x.extRound(); 

şeklinde yazmak isterseniz.

 

extensions.ts adlı bir dosya açalım

 

 

interface Number {

    extRound: () => number;

}

Number.prototype.extRound = function(): number {

    return Math.round(this);

}

calculator.ts dosyasında söyle kullanalım

 

 

import "./extensions";

export class calculator {

 public   roundedVal(): number {
   let test = 5.5;
   return test.extRound();
 }

}

Tabiki burada extension methodlar yazarken dikkat edilmesi gereken, methodlara mutlaka unique bir isim vermeniz. aksi takdirde kullandığınız diğer library’lerden aynı isimli methodları ezme ihtimaliniz var. bu yüzden ben methodun başına ext koydum.

nodejs event-driven non-blocking I/O

nodejs muhteşem dimi ?

acayip kavramları hayatımıza getirmiş gibi gözüküyor?

öncelikli olarak ben nodejs’i seviyorum ve kullanıyorum. ama pazarlanmasını pek sevmedim o yüzden, pazarlanmasında öne çıkan şeyleri aşağıda eleştireceğim.

olaya biraz derinlemesine bakalım.

I/O

öncelikli olarak bir  var socket= createSocket(…..) açtığımızı düşünelim

işletim sistemlerinde 3 türlü I/O mekanızması vardır .

a) blocking I/O mode

int size= read(socket,…) dediğimizde socket’te data yoksa, gelinceye kadar program bekler

b) non/blocking I/O mode

int size=read(socket,…) dediğimizde method hemen geri döner, veri varsa size >0 olur yoksa size=0 olur.

c) Async I/O

read(soket, $callback) mantığı ile çalışır. yani sokete veri geldiğinde $callback methodunu çağır ile çalışır.

gördüğünüz gibi  node.js kullandığı model c modelidir.

node.js async IO yapmak için https://github.com/libuv/libuv projesini geliştirmiş . bu projenin feature kısmında zaten “epoll,kqueue …..” gibi şeyleri gördünüzde async I/O görürsünüz.

yani işletim sistemleri (linux,bsd,windows) zaten bu üç modelide çok eskiden beridir destekliyorlar. bayağı eski hemde

yoksa apache,nginx, IIS ve ismini sayamadığım müthiş performanslı çalışan web serverları yazmak mümkün olmazdı.

yani yeni bir şey yok.

Event-driven

event-driven zaten çok eski bir fikir.  “event-driven architecture” diye geçer. bu fikir üzerine inşa edilmiş bir sürü şey var.

yine yeni bir şey yok.

velhasılı kelam, node.js bir “javascript ecosystem” yaratmak için güzel bir başlangıç noktası olmuştur.  ama müthiş bir backend programlama dilimi  acaba, pek emin değilim.

yani hayatımıza yeni kavramlar falan getirmedi. hepsi zaten kullandığımız şeylerdi.

hakkınıda çok yemeyeyim, ben seviyorum nodejs’i

 

 

 

 

 

raspberrypi gpio examples with c – part5- gpio-events

lets talk about gpio  events.

bcm2835 arm peripherals document  explains that, there as some registers for detecting events. page 97-99

GPIO Rising Edge Detect Enable Registers (GPRENn)

GPIO Falling Edge Detect Enable Registers (GPRENn)

GPIO High Detect Enable Registers (GPHENn)

GPIO Low Detect Enable Registers (GPLENn)

GPIO Asynchronous Falling Edge Detect Enable Registers (GPAFENn)

GPIO Asynchronous rising Edge Detect Enable Registers (GPARENn)

these above registers are used to set which pin you want to detect.enable  pin bit 1,disable for pin bit 0.

at the end, when an event occurs, you can get it from below register related pin. if it is 1 than you got the event. writing 1 to the related pin clears event.

GPIO Event Detect Status Registers (GPEDSn)

Note: default raspian kernels have gpio driver enabled, and this gpio driver catch’s interrupts.when event occurs also it generates interrupts, before you can see event from register, interrupt handler reads event and clears register. if you want to detect event, you must disable  interrupts, or driver.

you can use my compiled kernel. kernel.img.tar.gz

prepare your circuit as below

gpio event circuit

gpio event circuit

 

and here is the code event_pull.c

have a nice day.

 

next article we will talk about raspberry pi mini uart

 

 

 

 

 

 

 

 

 

 

raspberrypi gpio examples with c – part4- pull up/down

hi, again.

when you put a pin to mode INPUT.  you can set pull up/down registers.

because when you booted raspberry pi ,all gpio’s have default PULL UP or PULL DOWN registers. previus article sample is working because when we put pin to INPUT, it’is LOW. if you use another pin that is HIGH it will not work.

look at below picture. it is taken from bcm2835 sheet page 102

raspberry pi pull up/down

raspberry pi pull up/down

now changing pull up/down

in document page 91, I have taken this address’s

0x 7E20 0094  GPIO Pin Pull-up/down Enable 32

0x 7E20 0098 GPIO Pin Pull-up/down Enable Clock 0

0x 7E20 009C GPPUDCLK1 GPIO Pin Pull-up/down Enable Clock 1

 
#define PERIPHERALS_GPIO_PULL_PHYSICAL (0x94/sizeof(int))
#define PERIPHERALS_GPIO_PULL_CLOCK_PHYSICAL (0x98/sizeof(int))

as you see we defined two address, why ?

because we will set PULL value to PERIPHERALS_GPIO_PULL_PHYSICAL address

and pin value to PERIPHERALS_GPIO_PULL_CLOCK_PHYSICAL address.

but procedure of changing pull value is more than this.

from document:

1. Write to GPPUD to set the required control signal (i.e. Pull-up or Pull-Down or neither
to remove the current Pull-up/down)
2. Wait 150 cycles – this provides the required set-up time for the control signal
3. Write to GPPUDCLK0/1 to clock the control signal into the GPIO pads you wish to
modify – NOTE only the pads which receive a clock will be modified, all others will
retain their previous state.
4. Wait 150 cycles – this provides the required hold time for the control signal
5. Write to GPPUD to remove the control signal
6. Write to GPPUDCLK0/1 to remove the clock

lets start:

write correct bit values to (gpio+PERIPHERALS_GPIO_PULL_PHYSICAL)

correct values:

00 = Off – disable pull-up/down
01 = Enable Pull Down control
10 = Enable Pull Up control

then

wait 5 microseconds

then

set correct pin bit to 1 at (gpio+PERIPHERALS_GPIO_PULL_CLOCK_PHYSICAL) adress

then

wait 5 microsecond

then

write zero to (gpio+PERIPHERALS_GPIO_PULL_PHYSICAL) address

then

wait 5 microsecond

then

write (gpio+PERIPHERALS_GPIO_PULL_CLOCK_PHYSICAL) adress

 

below is the algorithm.

now create a template board above. and test it

neededs:

2 cables (yellow is output, and orange is input)

1 10K resistor

1 push button

pullboard

as you see, orange cable is at 7 pin, which is HIGH when rebooted raspberry.

if you disable

setPull(inputpin,EM_PULL_DOWN);

code it will now work as expected.

reboot your raspberry before testing. when you change a pin PULL_DOWN, until you change again it will be PULL_DOWN.

here is test code pull.c

gcc -o test pull.c

 

 

 

 

 

 

 

 

 

 

raspberrypi gpio examples with c – part3- reading input gpio

I will explain in this article , reading from input gpio

if you did not read previous articles, some definitions can become to you undefined.

first reading from gpio

gpio pin levels address are below

0x 7E20 0034 GPLEV0 GPIO Pin Level 0 32 R
0x 7E20 0038 GPLEV1 GPIO Pin Level 1 32 R

we have  32 bit+32 bit =64 bit registers. as you know bcm cpu has 54 pins. these 64 bits are enough for us.

#define PIN_LEVEL_START  (13)

calculation of address 0x34/(sizeof(int)=13

int value= *(gpio+PIN_LEVEL_START+PIN_WRITE_POSITION(pin));

we get all 32 bits, now we will lookat, pin bit

int pin= 1 << PIN_WRITE_SHIFT(pin)

if( value & pin) return HIGH else return LOW

now we will create a breadboard test environment;

neededs

1 push button

1 count 10k resistor

2 cables.

attach them as below

yellow cable is output, orange cable is input

read

and the code is here read.c