import { ɵɵelementStart, ɵɵtext, ɵɵelementEnd, ɵɵnextContext, ɵɵadvance, ɵɵtextInterpolate, ɵɵtextInterpolate1, ɵɵgetCurrentView, ɵɵlistener, ɵɵrestoreView, ɵɵtextInterpolate2, ɵɵproperty, ɵɵtemplate, ɵɵtemplateRefExtractor, ɵɵdirectiveInject, ɵɵdefineComponent, ɵsetClassMetadata, Component, Input, ɵɵdefineNgModule, ɵɵdefineInjector, ɵɵsetNgModuleScope, NgModule } from '@angular/core';
import { NgForOf, NgIf, CommonModule } from '@angular/common';
import { IonList, IonItem, IonLabel, IonButtons, IonButton, IonicModule } from '@ionic/angular';
import { CurrentDeviceService } from '@iotize/ionic';

var GPIO;
(function (GPIO) {
    let Mode;
    (function (Mode) {
        Mode["IN"] = "IN";
        Mode["OUT"] = "OUT";
    })(Mode = GPIO.Mode || (GPIO.Mode = {}));
    let Signal;
    (function (Signal) {
        Signal["HIGH"] = "HIGH";
        Signal["LOW"] = "LOW";
    })(Signal = GPIO.Signal || (GPIO.Signal = {}));
})(GPIO || (GPIO = {}));

var GPIO_MODE;
(function (GPIO_MODE) {
    GPIO_MODE[GPIO_MODE["INPUT_FLOATING"] = 0] = "INPUT_FLOATING";
    GPIO_MODE[GPIO_MODE["OUTPUT_OPENDRAIN"] = 1] = "OUTPUT_OPENDRAIN";
    GPIO_MODE[GPIO_MODE["OUTPUT_PUSHPULL"] = 3] = "OUTPUT_PUSHPULL";
    GPIO_MODE[GPIO_MODE["INPUT_PULLUP"] = 4] = "INPUT_PULLUP";
    GPIO_MODE[GPIO_MODE["OUTPUT_OPENDRAIN_PULLUP"] = 5] = "OUTPUT_OPENDRAIN_PULLUP";
    GPIO_MODE[GPIO_MODE["INPUT_PULLDOWN"] = 8] = "INPUT_PULLDOWN";
    GPIO_MODE[GPIO_MODE["INPUT_ANALOG_FLOATING"] = 128] = "INPUT_ANALOG_FLOATING";
    GPIO_MODE[GPIO_MODE["OUTPUT_ANALOG_PWM"] = 131] = "OUTPUT_ANALOG_PWM";
})(GPIO_MODE || (GPIO_MODE = {}));
class TapGPIOController {
    constructor(tap, conf) {
        this.tap = tap;
        this.conf = conf;
    }
    toggle() {
        if (this._state == GPIO.Signal.HIGH) {
            return this.low();
        }
        else {
            return this.high();
        }
    }
    low() {
        return this.tap.lwm2m
            .post(`/1029/${this.conf.num}/5`, Uint8Array.from([0]))
            .then((response) => {
            response.successful();
            this._state = GPIO.Signal.LOW;
        });
    }
    high() {
        // let value = 0xFF & this.conf.num;
        // let value = 0b00011101;
        const value = 1;
        return this.tap.lwm2m
            .post(`/1029/${this.conf.num}/5`, Uint8Array.from([value]))
            .then((response) => {
            response.successful();
            this._state = GPIO.Signal.HIGH;
        });
    }
    read() {
        return this.tap.lwm2m.get(`/1029/${this.conf.num}/4`).then((response) => {
            const result = response.body()[0];
            const decoded = result == 1 ? GPIO.Signal.HIGH : GPIO.Signal.LOW;
            console.log('READ RESULT', decoded);
            return decoded;
        });
    }
    setup(direction = GPIO.Mode.OUT, initial = GPIO.Signal.LOW) {
        // let value = 0xFF & this.conf.num;
        // let value = 0b00011101;
        const value = 1;
        switch (direction) {
            case GPIO.Mode.IN:
                return this.tap.lwm2m
                    .post(`/1029/${this.conf.num + 10}/5`, Uint8Array.from([value]))
                    .then((response) => {
                    response.successful();
                    this._mode = GPIO.Mode.IN;
                });
            case GPIO.Mode.OUT:
                return this.tap.lwm2m
                    .post(`/1029/${this.conf.num + 10}/5`, Uint8Array.from([0x00]))
                    .then((response) => {
                    response.successful();
                    this._mode = GPIO.Mode.OUT;
                });
            default:
                throw new Error('Invalid setup');
        }
    }
}

function TapGpioComponent_ion_item_1_ng_template_1_Template(rf, ctx) { if (rf & 1) {
    ɵɵelementStart(0, "span", 7);
    ɵɵtext(1);
    ɵɵelementEnd();
    ɵɵelementStart(2, "ion-label");
    ɵɵelementStart(3, "span", 7);
    ɵɵtext(4);
    ɵɵelementEnd();
    ɵɵelementEnd();
} if (rf & 2) {
    const config_r1 = ɵɵnextContext().$implicit;
    ɵɵadvance(1);
    ɵɵtextInterpolate(config_r1.num);
    ɵɵadvance(3);
    ɵɵtextInterpolate(config_r1.pin.type);
} }
function TapGpioComponent_ion_item_1_ng_template_3_Template(rf, ctx) { if (rf & 1) {
    ɵɵelementStart(0, "span", 7);
    ɵɵtext(1);
    ɵɵelementEnd();
    ɵɵelementStart(2, "span", 7);
    ɵɵtext(3);
    ɵɵelementEnd();
    ɵɵelementStart(4, "ion-label");
    ɵɵelementStart(5, "span", 7);
    ɵɵtext(6);
    ɵɵelementEnd();
    ɵɵelementEnd();
} if (rf & 2) {
    const config_r1 = ɵɵnextContext().$implicit;
    ɵɵadvance(1);
    ɵɵtextInterpolate(config_r1.num);
    ɵɵadvance(2);
    ɵɵtextInterpolate(config_r1.pin.type);
    ɵɵadvance(3);
    ɵɵtextInterpolate1("", config_r1.power, "V");
} }
function TapGpioComponent_ion_item_1_ng_template_5_Template(rf, ctx) { if (rf & 1) {
    ɵɵelementStart(0, "span", 7);
    ɵɵtext(1);
    ɵɵelementEnd();
    ɵɵelementStart(2, "ion-label");
    ɵɵelementStart(3, "span", 7);
    ɵɵtext(4);
    ɵɵelementEnd();
    ɵɵelementEnd();
} if (rf & 2) {
    const config_r1 = ɵɵnextContext().$implicit;
    ɵɵadvance(1);
    ɵɵtextInterpolate(config_r1.num);
    ɵɵadvance(3);
    ɵɵtextInterpolate(config_r1.pin.type);
} }
function TapGpioComponent_ion_item_1_ng_template_7_Template(rf, ctx) { if (rf & 1) {
    const _r15 = ɵɵgetCurrentView();
    ɵɵelementStart(0, "span", 7);
    ɵɵtext(1);
    ɵɵelementEnd();
    ɵɵelementStart(2, "span", 7);
    ɵɵtext(3);
    ɵɵelementEnd();
    ɵɵelementStart(4, "ion-label");
    ɵɵtext(5);
    ɵɵelementEnd();
    ɵɵelementStart(6, "ion-buttons", 8);
    ɵɵelementStart(7, "ion-button", 9);
    ɵɵlistener("click", function TapGpioComponent_ion_item_1_ng_template_7_Template_ion_button_click_7_listener() { ɵɵrestoreView(_r15); const config_r1 = ɵɵnextContext().$implicit; return config_r1.controller.setup("OUT"); });
    ɵɵtext(8, "OUT");
    ɵɵelementEnd();
    ɵɵelementStart(9, "ion-button", 10);
    ɵɵlistener("click", function TapGpioComponent_ion_item_1_ng_template_7_Template_ion_button_click_9_listener() { ɵɵrestoreView(_r15); const config_r1 = ɵɵnextContext().$implicit; return config_r1.controller.read(); });
    ɵɵtext(10, "READ");
    ɵɵelementEnd();
    ɵɵelementEnd();
    ɵɵelementStart(11, "ion-buttons", 8);
    ɵɵelementStart(12, "ion-button", 9);
    ɵɵlistener("click", function TapGpioComponent_ion_item_1_ng_template_7_Template_ion_button_click_12_listener() { ɵɵrestoreView(_r15); const config_r1 = ɵɵnextContext().$implicit; return config_r1.controller.setup("IN"); });
    ɵɵtext(13, "IN");
    ɵɵelementEnd();
    ɵɵelementStart(14, "ion-button", 9);
    ɵɵlistener("click", function TapGpioComponent_ion_item_1_ng_template_7_Template_ion_button_click_14_listener() { ɵɵrestoreView(_r15); const config_r1 = ɵɵnextContext().$implicit; return config_r1.controller.low(); });
    ɵɵtext(15, "LOW");
    ɵɵelementEnd();
    ɵɵelementStart(16, "ion-button", 9);
    ɵɵlistener("click", function TapGpioComponent_ion_item_1_ng_template_7_Template_ion_button_click_16_listener() { ɵɵrestoreView(_r15); const config_r1 = ɵɵnextContext().$implicit; return config_r1.controller.high(); });
    ɵɵtext(17, "HIGH");
    ɵɵelementEnd();
    ɵɵelementEnd();
} if (rf & 2) {
    const config_r1 = ɵɵnextContext().$implicit;
    ɵɵadvance(1);
    ɵɵtextInterpolate(config_r1.num);
    ɵɵadvance(2);
    ɵɵtextInterpolate2("", config_r1.pin.type, " ", config_r1.pin.num, "");
    ɵɵadvance(2);
    ɵɵtextInterpolate1(" ", config_r1.title, " ");
    ɵɵadvance(2);
    ɵɵproperty("color", config_r1.controller._mode == "OUT" ? "primary" : "");
    ɵɵadvance(5);
    ɵɵproperty("color", config_r1.controller._mode == "IN" ? "primary" : "");
    ɵɵadvance(2);
    ɵɵproperty("color", config_r1.controller._state == "LOW" ? "primary" : "");
    ɵɵadvance(2);
    ɵɵproperty("color", config_r1.controller._state == "HIGH" ? "primary" : "");
} }
function TapGpioComponent_ion_item_1_Template(rf, ctx) { if (rf & 1) {
    ɵɵelementStart(0, "ion-item");
    ɵɵtemplate(1, TapGpioComponent_ion_item_1_ng_template_1_Template, 5, 2, "ng-template", 2, 3, ɵɵtemplateRefExtractor);
    ɵɵtemplate(3, TapGpioComponent_ion_item_1_ng_template_3_Template, 7, 3, "ng-template", 2, 4, ɵɵtemplateRefExtractor);
    ɵɵtemplate(5, TapGpioComponent_ion_item_1_ng_template_5_Template, 5, 2, "ng-template", 2, 5, ɵɵtemplateRefExtractor);
    ɵɵtemplate(7, TapGpioComponent_ion_item_1_ng_template_7_Template, 18, 8, "ng-template", 2, 6, ɵɵtemplateRefExtractor);
    ɵɵelementEnd();
} if (rf & 2) {
    const config_r1 = ctx.$implicit;
    ɵɵadvance(1);
    ɵɵproperty("ngIf", config_r1.pin.type == "GROUND");
    ɵɵadvance(2);
    ɵɵproperty("ngIf", config_r1.pin.type == "POWER");
    ɵɵadvance(2);
    ɵɵproperty("ngIf", config_r1.pin.type == "DISABLED");
    ɵɵadvance(2);
    ɵɵproperty("ngIf", config_r1.pin.type == "GPIO");
} }
class TapGpioComponent {
    constructor(tapService) {
        this.tapService = tapService;
        this.schema = [];
        this.lines = 'full';
        this.pins = this.schema.map((entry) => {
            if (entry.pin.type === 'GPIO') {
                entry.controller = new TapGPIOController(this.tap, entry.pin);
            }
            return entry;
        });
    }
    get tap() {
        return this.tapService.tap;
    }
}
/** @nocollapse */ TapGpioComponent.ɵfac = function TapGpioComponent_Factory(t) { return new (t || TapGpioComponent)(ɵɵdirectiveInject(CurrentDeviceService)); };
/** @nocollapse */ TapGpioComponent.ɵcmp = ɵɵdefineComponent({ type: TapGpioComponent, selectors: [["tap-gpio"]], inputs: { schema: "schema", lines: "lines" }, decls: 2, vars: 2, consts: [[3, "lines"], [4, "ngFor", "ngForOf"], [3, "ngIf"], ["ground", ""], ["power", ""], ["disabledpin", ""], ["gpio", ""], ["slot", "start"], ["slot", "end"], [3, "color", "click"], [3, "click"]], template: function TapGpioComponent_Template(rf, ctx) { if (rf & 1) {
        ɵɵelementStart(0, "ion-list", 0);
        ɵɵtemplate(1, TapGpioComponent_ion_item_1_Template, 9, 4, "ion-item", 1);
        ɵɵelementEnd();
    } if (rf & 2) {
        ɵɵproperty("lines", ctx.lines);
        ɵɵadvance(1);
        ɵɵproperty("ngForOf", ctx.pins);
    } }, directives: [IonList, NgForOf, IonItem, NgIf, IonLabel, IonButtons, IonButton], styles: [""] });
(function () { (typeof ngDevMode === "undefined" || ngDevMode) && ɵsetClassMetadata(TapGpioComponent, [{
        type: Component,
        args: [{
                selector: 'tap-gpio',
                templateUrl: './tap-gpio.component.html',
                styleUrls: ['./tap-gpio.component.scss'],
            }]
    }], function () { return [{ type: CurrentDeviceService }]; }, { schema: [{
            type: Input
        }], lines: [{
            type: Input
        }] }); })();

class TapGpioModule {
}
/** @nocollapse */ TapGpioModule.ɵfac = function TapGpioModule_Factory(t) { return new (t || TapGpioModule)(); };
/** @nocollapse */ TapGpioModule.ɵmod = ɵɵdefineNgModule({ type: TapGpioModule });
/** @nocollapse */ TapGpioModule.ɵinj = ɵɵdefineInjector({ imports: [[CommonModule, IonicModule]] });
(function () { (typeof ngJitMode === "undefined" || ngJitMode) && ɵɵsetNgModuleScope(TapGpioModule, { declarations: [TapGpioComponent], imports: [CommonModule, IonicModule], exports: [TapGpioComponent] }); })();
(function () { (typeof ngDevMode === "undefined" || ngDevMode) && ɵsetClassMetadata(TapGpioModule, [{
        type: NgModule,
        args: [{
                declarations: [TapGpioComponent],
                exports: [TapGpioComponent],
                imports: [CommonModule, IonicModule],
            }]
    }], null, null); })();

const J1PinSchema = [
    {
        num: 1,
        pin: {
            power: 3.3,
            type: 'POWER',
        },
        title: 'Vcc',
    },
    {
        num: 2,
        pin: {
            num: 4,
            type: 'GPIO',
        },
        title: 'SP3PIO/SWDIO (TMS)',
    },
    {
        num: 3,
        pin: {
            type: 'GROUND',
        },
        title: 'Gnd',
    },
    {
        num: 4,
        pin: {
            num: 2,
            type: 'GPIO',
        },
        title: 'S3PCLK/SWDCLK (TCK)',
    },
    {
        num: 5,
        pin: {
            type: 'GROUND',
        },
        title: 'Gnd',
    },
    {
        num: 6,
        pin: {
            num: 3,
            type: 'GPIO',
        },
        title: 'SWO (TDO)',
    },
    {
        num: 7,
        pin: {
            type: 'DISABLED',
        },
        title: 'Not connected',
    },
    {
        num: 8,
        pin: {
            num: 4,
            type: 'GPIO',
        },
        title: 'TDI',
    },
    {
        num: 9,
        pin: {
            type: 'GROUND',
        },
        title: 'Gnd',
    },
    {
        num: 10,
        pin: {
            num: 0,
            type: 'GPIO',
        },
        title: 'RST',
    },
];
const J3PinSchema = [
    {
        num: 1,
        pin: {
            power: 3.3,
            type: 'POWER',
        },
        title: 'Vcc',
    },
    {
        num: 2,
        pin: {
            type: 'GROUND',
        },
        title: 'Gnd',
    },
    {
        num: 2,
        pin: {
            num: 4,
            type: 'GPIO',
        },
        title: 'SP3PIO/SWDIO (TMS)',
    },
    {
        num: 4,
        pin: {
            num: 2,
            type: 'GPIO',
        },
        title: 'S3PCLK/SWDCLK (TCK)',
    },
    {
        num: 10,
        pin: {
            num: 0,
            type: 'GPIO',
        },
        title: 'RST',
    },
];

/**
 * Generated bundle index. Do not edit.
 */

export { GPIO, J1PinSchema, J3PinSchema, TapGpioComponent, TapGpioModule };
//# sourceMappingURL=iotize-ionic-gpio.js.map
