(function (global, factory) {
    typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('@angular/common'), require('@ionic/angular'), require('@iotize/ionic')) :
    typeof define === 'function' && define.amd ? define('@iotize/ionic/gpio', ['exports', '@angular/core', '@angular/common', '@ionic/angular', '@iotize/ionic'], factory) :
    (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.iotize = global.iotize || {}, global.iotize.ionic = global.iotize.ionic || {}, global.iotize.ionic.gpio = {}), global.ng.core, global.ng.common, global.i2, global.iotize.ionic));
}(this, (function (exports, i0, i3, i2, i1) { 'use strict';

    (function (GPIO) {
        var Mode;
        (function (Mode) {
            Mode["IN"] = "IN";
            Mode["OUT"] = "OUT";
        })(Mode = GPIO.Mode || (GPIO.Mode = {}));
        var Signal;
        (function (Signal) {
            Signal["HIGH"] = "HIGH";
            Signal["LOW"] = "LOW";
        })(Signal = GPIO.Signal || (GPIO.Signal = {}));
    })(exports.GPIO || (exports.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 = {}));
    var TapGPIOController = /** @class */ (function () {
        function TapGPIOController(tap, conf) {
            this.tap = tap;
            this.conf = conf;
        }
        TapGPIOController.prototype.toggle = function () {
            if (this._state == exports.GPIO.Signal.HIGH) {
                return this.low();
            }
            else {
                return this.high();
            }
        };
        TapGPIOController.prototype.low = function () {
            var _this = this;
            return this.tap.lwm2m
                .post("/1029/" + this.conf.num + "/5", Uint8Array.from([0]))
                .then(function (response) {
                response.successful();
                _this._state = exports.GPIO.Signal.LOW;
            });
        };
        TapGPIOController.prototype.high = function () {
            var _this = this;
            // let value = 0xFF & this.conf.num;
            // let value = 0b00011101;
            var value = 1;
            return this.tap.lwm2m
                .post("/1029/" + this.conf.num + "/5", Uint8Array.from([value]))
                .then(function (response) {
                response.successful();
                _this._state = exports.GPIO.Signal.HIGH;
            });
        };
        TapGPIOController.prototype.read = function () {
            return this.tap.lwm2m.get("/1029/" + this.conf.num + "/4").then(function (response) {
                var result = response.body()[0];
                var decoded = result == 1 ? exports.GPIO.Signal.HIGH : exports.GPIO.Signal.LOW;
                console.log('READ RESULT', decoded);
                return decoded;
            });
        };
        TapGPIOController.prototype.setup = function (direction, initial) {
            var _this = this;
            if (direction === void 0) { direction = exports.GPIO.Mode.OUT; }
            if (initial === void 0) { initial = exports.GPIO.Signal.LOW; }
            // let value = 0xFF & this.conf.num;
            // let value = 0b00011101;
            var value = 1;
            switch (direction) {
                case exports.GPIO.Mode.IN:
                    return this.tap.lwm2m
                        .post("/1029/" + (this.conf.num + 10) + "/5", Uint8Array.from([value]))
                        .then(function (response) {
                        response.successful();
                        _this._mode = exports.GPIO.Mode.IN;
                    });
                case exports.GPIO.Mode.OUT:
                    return this.tap.lwm2m
                        .post("/1029/" + (this.conf.num + 10) + "/5", Uint8Array.from([0x00]))
                        .then(function (response) {
                        response.successful();
                        _this._mode = exports.GPIO.Mode.OUT;
                    });
                default:
                    throw new Error('Invalid setup');
            }
        };
        return TapGPIOController;
    }());

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

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

    var 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',
        },
    ];
    var 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.
     */

    exports.J1PinSchema = J1PinSchema;
    exports.J3PinSchema = J3PinSchema;
    exports.TapGpioComponent = TapGpioComponent;
    exports.TapGpioModule = TapGpioModule;

    Object.defineProperty(exports, '__esModule', { value: true });

})));
//# sourceMappingURL=iotize-ionic-gpio.umd.js.map
