Close

Step 3: Firmware

A project log for ESP32-S3 Digital Combination Lock Based SquareLine

In this article, I'm going to make a digital combination lock, using Squareline_studio and MaTouch ESP32-S3 Rotary IPS Display with Touch 2.

makerfabsMakerfabs 12/13/2023 at 05:480 Comments

The MaTouch ESP32 S3 2.1 Rotary TFT with Touch is not compatible with the TFT_eSPI library, so I use the GFX_Library_of_Arduino library instead of the driver.

void my_touchpad_read( lv_indev_drv_t * indev_driver, lv_indev_data_t * data )
{
   int touchX = 0, touchY = 0;

    if (read_touch(&touchX, &touchY) == 1)
    {
        data->state = LV_INDEV_STATE_PR;

        data->point.x = (uint16_t)touchX;
        data->point.y = (uint16_t)touchY;
    }
    else
    {
        data->state = LV_INDEV_STATE_REL;
    }
}

To establish a connection between the rotary encoder values and the values of the semi-circle and the labels, some intermediate processing is required.

attachInterrupt(ENCODER_CLK, encoder_irq, CHANGE);

void encoder_irq()
{
    State = digitalRead(ENCODER_CLK);
    if (State != old_State)
    {
        if (digitalRead(ENCODER_DT) == State)
        {
            counter++;
        }
        else
        {
            counter--;
        }
    }
    old_State = State; // the first position was changed
}

void cal_encoder()
{
    USBSerial.print("Position: ");
    USBSerial.println(counter);

    target_count = (counter % 20) / 2;
    if (target_count < 0)
    {
        target_count = (counter % 20) / 2 + 10;
    }

    if (target_count > arc_coun)
        arc_coun++;
    else if (target_count < arc_coun)
        arc_coun--;

    USBSerial.print("arc_Position: ");
    USBSerial.println(arc_coun);
}

lv_arc_set_value(ui_Arc1, (int)arc_coun);

if (label_flag == 1)
    {
        _ui_arc_set_text_value(ui_Label1, ui_Arc1, "", "");
        if (arc_coun == 2)
        {
            if (digitalRead(BUTTON_PIN) == 0)
            {
                USBSerial.println("Button Press");
                while (digitalRead(BUTTON_PIN) == 0) 
                {
                    delay(50);
                }
                label_flag = 2;
            }
        }
    }


    if (label_flag == 2)
    {
        _ui_arc_set_text_value(ui_Label2, ui_Arc1, "", "");
        if (arc_coun == 4)
        {
            if (digitalRead(BUTTON_PIN) == 0)
            {
                USBSerial.println("Button Press");
                while (digitalRead(BUTTON_PIN) == 0) 
                {
                    delay(50);
                }
                label_flag = 3;
            }
        }
    }
    if (label_flag == 3)
    {
        _ui_arc_set_text_value(ui_Label3, ui_Arc1, "", "");
        if (arc_coun == 6)
        {
            if (digitalRead(BUTTON_PIN) == 0)
            {
                USBSerial.println("Button Press");
                while (digitalRead(BUTTON_PIN) == 0) 
                {
                    delay(50);
                }
                label_flag = 4;
            }
        }
    }
    if (label_flag == 4)
    {
        _ui_arc_set_text_value(ui_Label4, ui_Arc1, "", "");
        if (arc_coun == 8)
        {
            if (digitalRead(BUTTON_PIN) == 0)
            {
                USBSerial.println("Button Press");
                while (digitalRead(BUTTON_PIN) == 0) 
                {
                    delay(50);
                }


                label_flag = 0;
                _ui_state_modify(ui_ImgButton2, LV_STATE_DISABLED, _UI_MODIFY_STATE_REMOVE);
            }
        }
    }
void unlock()
{
    USBSerial.println("Unlock...");
    digitalWrite(RX_PIN, HIGH);
    digitalWrite(TX_PIN, LOW);
}

The raw code.

How the code work.

Discussions