/*
 *
 *  $Id: GettingStarted_Yocto_Display_ePaper.java 75123 2026-07-06 09:14:28Z seb $
 *
 *  An example that shows how to use a  Yocto-Display-ePaper
 *
 *  You can find more information on our web site:
 *   Yocto-Display-ePaper documentation:
 *      https://www.yoctopuce.com/EN/products/yocto-display-epaper/doc.html
 *   Android API Reference:
 *      https://www.yoctopuce.com/EN/doc/reference/yoctolib-android-EN.html
 *
 */

package com.yoctopuce.doc_examples;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

import com.yoctopuce.YoctoAPI.YAPI;
import com.yoctopuce.YoctoAPI.YAPI_Exception;
import com.yoctopuce.YoctoAPI.YDisplay;
import com.yoctopuce.YoctoAPI.YDisplayLayer;

import java.util.Random;

public class GettingStarted_Yocto_Display_ePaper extends Activity implements OnItemSelectedListener {


    private YDisplay display = null;
    private ArrayAdapter<String> aa;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gettingstarted_yocto_display_epaper);
        Spinner my_spin = (Spinner) findViewById(R.id.spinner1);
        my_spin.setOnItemSelectedListener(this);
        aa = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
        aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        my_spin.setAdapter(aa);
    }

    @Override
    protected void onStart() {
        super.onStart();
        aa.clear();
        try {
            YAPI.EnableUSBHost(this);
            YAPI.RegisterHub("usb");
            YDisplay d = YDisplay.FirstDisplay();
            while (d != null) {
                String hwid = d.get_hardwareId();
                aa.add(hwid);
                d = d.nextDisplay();
            }
        } catch (YAPI_Exception e) {
            e.printStackTrace();
        }
        aa.notifyDataSetChanged();
    }

    @Override
    protected void onStop() {
        super.onStop();
        YAPI.FreeAPI();
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        String hwid = parent.getItemAtPosition(pos).toString();
        display = YDisplay.FindDisplay(hwid);
        updateDisplay(null);
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
    }

    public void updateDisplay(View view) {
        if (display == null)
            return;

        EditText message = (EditText) findViewById(R.id.editText1);
        // clean up
        try {
            int[] colors = {0xFFFFFF, 0x000000, 0xFF0000, 0xFFFF00};
            String paneltype = display.get_displayPanel();
            if (paneltype.equals("NOT_SET")) {
                System.out.println("Use the virtual to Configure the panel first");
                YAPI.Sleep(3000);
                System.exit(1);
            }

            // retrieve the display size
            int w = display.get_displayWidth();
            int h = display.get_displayHeight();
            int middleX = (int) (w / 2);
            int middleY = (int) (h / 2);
            System.out.printf("Using device %s (panel: %s  %d x %d pixels)\n", display.get_serialNumber(), paneltype, w, h);

            // retrieve the first layer
            YDisplayLayer l0 = display.get_displayLayer(0);
            l0.selectFont("medium.yfm");
            display.triggerRefresh(); // make sure next refresh will be a full one
            Random rnd = new Random();
            // prevent refreshing for 2 sec
            display.postponeRefresh(2000);
            l0.clear();
            // draw a few circle
            for (int i = 0; i < 15; i++) {
                int cx = rnd.nextInt(w);
                int cy = rnd.nextInt(h);
                int r = (int) (h / 20) + rnd.nextInt((int) (h / 10));
                l0.selectFillColor(colors[rnd.nextInt(4)]);
                l0.drawDisc(cx, cy, r);
                l0.drawCircle(cx, cy, r);
            }
            // draw a rectangle with panel type in it
            l0.selectFillColor(0xffffff);
            l0.drawBar(middleX - 75, middleY - 10, middleX + 75, middleY + 12);
            l0.drawRect(middleX - 75, middleY - 10, middleX + 75, middleY + 12);
            l0.drawText(middleX, middleY, YDisplayLayer.ALIGN.CENTER, paneltype);
            display.triggerRefresh(); // display is allowed to refresh  again
            YAPI.Sleep(1000);
        } catch (YAPI_Exception e) {
            e.printStackTrace();
        }

    }


}
