Developer

IOP JavaScript Code

MAX_TEMP=65;
MAX_CURR=16;
MIN_CURR=0.05;

var st={alarm:0,pot:0.4,led:0.25,en:16};
try{st=Iop.store();}
catch(e){st.alarm=0x10;}

var led=Io.Wg(1,false);
led.freq(1000);

var r0=Io.Pin(2,'o');
var r1=Io.Pin(4,'o');

//Mains Detect
var md=Io.Tc(0,'f',100);
md.on('t',function(){
    Sensor.suspend();
    led.duty(0);
    if(r0.t){
        Iop.store(st);
    }
    r0.t=false;
});
md.on('r',function(){
    led.duty(st.led);
    Sensor.resume();
    setTimeout(function(){
        r0.t=true;
    },1000);
});

//Load Detect
var pot=Sensor.Pot(0);
pot.value(st.pot);

var ld=Io.Tc(1,'f',100);
ld.on('r',function(){
    if(st.en & 0x10 && r0.t) {
        on();
    }
});

function on() {
    if(st.alarm || curr.trip()) return;
    r0.pulse(5);
    st.op=true;
    if(st.en & 0x40) {
        st.start=t.config(st.timer);
    }
}

function off() {
    r0.t=false;
    r1.pulse(5);
    st.op=false;
    setTimeout(function(){
        r0.t=true;
    },1000);
}

var t=Timer();
t.on('t',on);
t.on('r',off);

//Sensor Bus
var temp=Sensor.Temp(0);
temp.on('c',function(){
    if(temp.value()>MAX_TEMP) {
        off();
        st.alarm|=0x01;
    }
});
var light=Sensor.Light(0);
light.on('t',off);
light.on('r',on);
var curr=Sensor.Current(0);
curr.on('c',function(){
    var c=curr.value();
    if(c>MAX_CURR) {
        off();
        st.alarm|=0x04;
    }
    else if(st.en & 0x20 && c<MIN_CURR && r0.t){
        off();
    }
    led.mod(c?1/c:0);
});
curr.on('t',function(){
    off();
    st.alarm|=0x08;
});
curr.on('g',function(){
    off();
    st.alarm|=0x02;
});

var svc=Ble.addService('12341234-0000-2000-8000-004235434352');

var cmdChar=svc.addChar('88888888-0000-2000-8000-004235434352','rwv',3,true);
cmdChar.on('w',function(data){
    var cmd=data.charCodeAt(0);
    var d = data.charCodeAt(1);
    switch(cmd) {
        case 0x01:
            st.duty=data.charCodeAt(2);
            if(d & 0x01) on();
            else off();
            break;
        case 0x04:
            st.led=d/256;
            led.duty(st.led);
            break;
        case 0x10:
            st.alarm=0;
            break;
        case 0x20:
            off();
            st.pot=pot.scan(0.1);
            break;
        case 0x40:
            Iop.store(st);
            break;
    }
});

var configChar=svc.addChar('88888889-0000-2000-8000-004235434352','rwv',9,true);
configChar.on('w',function(data){
    st.en=data.charCodeAt(0);
    st.config=data;
    Sensor.config(data);
});
configChar.write(st.config);

var timerChar=svc.addChar('88888890-0000-2000-8000-004235434352','rwv',20,true);
timerChar.on('w',function(data){
    st.timer=data;
    st.start=t.config(data);
});

var adv={
    md: {
        'ci': 888,
        'data' : ''
    }
};
setInterval(function(){
    var a=[];
    a[0]=2;
    a[1]=1;
    a[2]=md.state() | (ld.state() << 1) | (st.op << 2) | (Ble.connected() << 3)
        | (t.type()?0x10:0x0) | (t.state() << 5);
    a[3]=st.alarm;
    a[4]=st.en;
    a[5]=st.duty;
    a[6]=st.led*256;
    adv.md.data=String.fromCharCode.apply(this,a)+Sensor.read();
    Ble.advData('g',0,false,adv);
    timerChar.write(t.config());
    Iop.sleep();
},1000);

Sensor.config(st.config);
t.config(st.timer, st.start);

Physical Sensor Interface

Pin Name Description
1 VLD0 3.3V OUT 3.3V output for sensors
2 GND Ground
3 IO_ADC0 General IO, A/D Converter
4 IO_ADC1 General IO, A/D Converter
5 IO_DAC0 General IO, D/A Converter
6 IO_ADC3 General IO, A/D Converter
7 SPI_MISO SPI data in
8 SPI_MOSI SPI data out
9 SPI_SCK SPI clock
10 SPI_CS SPI chip select
11 TWI_SDA TWI/I2C data
12 TWI_SCL TWI/I2C clock
13 UART_TX Serial port transmit
14 UART_RX Serial port receive
15 POWER_IN 3-5 V power supply
16 GND Ground
17 PDI_DATA Programming and debug interface
18 3.3V MCU Programming and debug interface
19 RESET/PDI_CLK Programming and debug interface
20 GND Ground