import * as i0 from '@angular/core';
import { Component, Input, NgModule } from '@angular/core';
import * as i3 from '@angular/common';
import { CommonModule } from '@angular/common';
import * as i2 from '@ionic/angular';
import { IonicModule } from '@ionic/angular';
import * as i1 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');
        }
    }
}

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 */ /** @nocollapse */ TapGpioComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.2.2", ngImport: i0, type: TapGpioComponent, deps: [{ token: i1.CurrentDeviceService }], target: i0.ɵɵFactoryTarget.Component });
/** @nocollapse */ /** @nocollapse */ TapGpioComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "12.0.0", version: "13.2.2", type: TapGpioComponent, selector: "tap-gpio", inputs: { schema: "schema", lines: "lines" }, ngImport: i0, template: "<ion-list [lines]=\"lines\">\n  <ion-item *ngFor=\"let config of pins\">\n    <ng-template [ngIf]=\"config.pin.type == 'GROUND'\" #ground>\n      <span slot=\"start\">{{ config.num }}</span>\n      <ion-label>\n        <span slot=\"start\">{{ config.pin.type }}</span>\n      </ion-label>\n    </ng-template>\n    <ng-template [ngIf]=\"config.pin.type == 'POWER'\" #power>\n      <span slot=\"start\">{{ config.num }}</span>\n      <span slot=\"start\">{{ config.pin.type }}</span>\n      <ion-label>\n        <span slot=\"start\">{{ config.power }}V</span>\n      </ion-label>\n    </ng-template>\n    <ng-template [ngIf]=\"config.pin.type == 'DISABLED'\" #disabledpin>\n      <span slot=\"start\">{{ config.num }}</span>\n      <ion-label>\n        <span slot=\"start\">{{ config.pin.type }}</span>\n      </ion-label>\n    </ng-template>\n    <ng-template [ngIf]=\"config.pin.type == 'GPIO'\" #gpio>\n      <span slot=\"start\">{{ config.num }}</span>\n      <span slot=\"start\">{{ config.pin.type }} {{ config.pin.num }}</span>\n      <ion-label>\n        {{ config.title }}\n      </ion-label>\n      <ion-buttons slot=\"end\">\n        <ion-button\n          [color]=\"config.controller._mode == 'OUT' ? 'primary' : ''\"\n          (click)=\"config.controller.setup('OUT')\"\n          >OUT</ion-button\n        >\n        <ion-button (click)=\"config.controller.read()\">READ</ion-button>\n      </ion-buttons>\n      <ion-buttons slot=\"end\">\n        <ion-button\n          [color]=\"config.controller._mode == 'IN' ? 'primary' : ''\"\n          (click)=\"config.controller.setup('IN')\"\n          >IN</ion-button\n        >\n        <!-- <ion-toggle (click)=\"config.controller.toggle()\"></ion-toggle> -->\n        <ion-button\n          [color]=\"config.controller._state == 'LOW' ? 'primary' : ''\"\n          (click)=\"config.controller.low()\"\n          >LOW</ion-button\n        >\n        <ion-button\n          [color]=\"config.controller._state == 'HIGH' ? 'primary' : ''\"\n          (click)=\"config.controller.high()\"\n          >HIGH</ion-button\n        >\n      </ion-buttons>\n    </ng-template>\n  </ion-item>\n</ion-list>\n", styles: [""], components: [{ type: i2.IonList, selector: "ion-list", inputs: ["inset", "lines", "mode"] }, { type: i2.IonItem, selector: "ion-item", inputs: ["button", "color", "counter", "detail", "detailIcon", "disabled", "download", "fill", "href", "lines", "mode", "rel", "routerAnimation", "routerDirection", "shape", "target", "type"] }, { type: i2.IonLabel, selector: "ion-label", inputs: ["color", "mode", "position"] }, { type: i2.IonButtons, selector: "ion-buttons", inputs: ["collapse"] }, { type: i2.IonButton, selector: "ion-button", inputs: ["buttonType", "color", "disabled", "download", "expand", "fill", "href", "mode", "rel", "routerAnimation", "routerDirection", "shape", "size", "strong", "target", "type"] }], directives: [{ type: i3.NgForOf, selector: "[ngFor][ngForOf]", inputs: ["ngForOf", "ngForTrackBy", "ngForTemplate"] }, { type: i3.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.2.2", ngImport: i0, type: TapGpioComponent, decorators: [{
            type: Component,
            args: [{ selector: 'tap-gpio', template: "<ion-list [lines]=\"lines\">\n  <ion-item *ngFor=\"let config of pins\">\n    <ng-template [ngIf]=\"config.pin.type == 'GROUND'\" #ground>\n      <span slot=\"start\">{{ config.num }}</span>\n      <ion-label>\n        <span slot=\"start\">{{ config.pin.type }}</span>\n      </ion-label>\n    </ng-template>\n    <ng-template [ngIf]=\"config.pin.type == 'POWER'\" #power>\n      <span slot=\"start\">{{ config.num }}</span>\n      <span slot=\"start\">{{ config.pin.type }}</span>\n      <ion-label>\n        <span slot=\"start\">{{ config.power }}V</span>\n      </ion-label>\n    </ng-template>\n    <ng-template [ngIf]=\"config.pin.type == 'DISABLED'\" #disabledpin>\n      <span slot=\"start\">{{ config.num }}</span>\n      <ion-label>\n        <span slot=\"start\">{{ config.pin.type }}</span>\n      </ion-label>\n    </ng-template>\n    <ng-template [ngIf]=\"config.pin.type == 'GPIO'\" #gpio>\n      <span slot=\"start\">{{ config.num }}</span>\n      <span slot=\"start\">{{ config.pin.type }} {{ config.pin.num }}</span>\n      <ion-label>\n        {{ config.title }}\n      </ion-label>\n      <ion-buttons slot=\"end\">\n        <ion-button\n          [color]=\"config.controller._mode == 'OUT' ? 'primary' : ''\"\n          (click)=\"config.controller.setup('OUT')\"\n          >OUT</ion-button\n        >\n        <ion-button (click)=\"config.controller.read()\">READ</ion-button>\n      </ion-buttons>\n      <ion-buttons slot=\"end\">\n        <ion-button\n          [color]=\"config.controller._mode == 'IN' ? 'primary' : ''\"\n          (click)=\"config.controller.setup('IN')\"\n          >IN</ion-button\n        >\n        <!-- <ion-toggle (click)=\"config.controller.toggle()\"></ion-toggle> -->\n        <ion-button\n          [color]=\"config.controller._state == 'LOW' ? 'primary' : ''\"\n          (click)=\"config.controller.low()\"\n          >LOW</ion-button\n        >\n        <ion-button\n          [color]=\"config.controller._state == 'HIGH' ? 'primary' : ''\"\n          (click)=\"config.controller.high()\"\n          >HIGH</ion-button\n        >\n      </ion-buttons>\n    </ng-template>\n  </ion-item>\n</ion-list>\n", styles: [""] }]
        }], ctorParameters: function () { return [{ type: i1.CurrentDeviceService }]; }, propDecorators: { schema: [{
                type: Input
            }], lines: [{
                type: Input
            }] } });

class TapGpioModule {
}
/** @nocollapse */ /** @nocollapse */ TapGpioModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.2.2", ngImport: i0, type: TapGpioModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
/** @nocollapse */ /** @nocollapse */ TapGpioModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "13.2.2", ngImport: i0, type: TapGpioModule, declarations: [TapGpioComponent], imports: [CommonModule, IonicModule], exports: [TapGpioComponent] });
/** @nocollapse */ /** @nocollapse */ TapGpioModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "13.2.2", ngImport: i0, type: TapGpioModule, imports: [[CommonModule, IonicModule]] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.2.2", ngImport: i0, type: TapGpioModule, decorators: [{
            type: NgModule,
            args: [{
                    declarations: [TapGpioComponent],
                    exports: [TapGpioComponent],
                    imports: [CommonModule, IonicModule],
                }]
        }] });

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.mjs.map
