ChatGPT Tutorial--Generate Code

ChatGPT productivity change tool

chatGPT is a natural language processing model based on artificial intelligence technology, which uses deep learning algorithms to understand and generate human language. chatGPT can be used in dialogue systems, intelligent customer service, question answering systems and other scenarios, enabling natural and smooth dialogue.

The core technology of chatGPT is based on the GPT (Generative Pre-trained Transformer) model, which is a pre-trained language model that can be trained through a large-scale corpus to learn the rules and characteristics of language. In practical applications, chatGPT can be customized through fine-tuning and other methods to adapt to different application scenarios and user needs.

The advantages of chatGPT include:

  1. Natural and smooth: chatGPT can generate natural and smooth language, making the conversation more real.
  2. Intelligent: chatGPT can understand the user's intention and context, so as to provide more intelligent answers.
  3. Customization: chatGPT can be customized through fine-tuning and other methods to adapt to different application scenarios and user needs.
  4. Efficiency: chatGPT can achieve fast response and processing and improve user experience.

提示:chatGPT生成代码秒便生产力工具,Ctrl+'C' / Ctrl+'V'变得更容易。chatGPT 链接 [www.laicj.cn](https://laicj.cn)。


1. Variable productivity process

Using chatGPT to analyze natural language and a huge knowledge base, code is required in actual use.

Created with Raphaël 2.2.0 chatGPT 描述需求 验证结果? 生产力(代码) yes no

This article uses the chatGPT tool to generate code from a web search .

1.1 Writing requirements

Operating environment: Raspberry Pi 4B, Ubuntu20.04
The array contains 4 consecutive GPIOs, use the sysfs interface to apply for the array according to the array. The
main menu includes the option to apply for GPIO and the option to exit. After selecting the option gpio, enter the submenu; the submenu includes controlling GPIO high power Level, low level and exit options, select the high level option to control GPIO high level, select the low level option to control GPIO low level, select exit to return to the main menu; the main menu can choose other options

1.2 Generate code

  • The execution environment of this article is Ubuntu20.04, select bash script
  • Describe functional requirements in detail
  • generate code

genrator

Here's the Bash code to do that:

#!/bin/bash

# 定义GPIO数组
gpio_array=(17 18 19 20)

# 申请GPIO
function request_gpio() {
    
    
  for gpio in ${gpio_array[@]}; do
    echo $gpio > /sys/class/gpio/export
  done
  echo "GPIO已申请"
}

# 控制GPIO高电平
function set_gpio_high() {
    
    
  echo "请输入要控制的GPIO编号(17-20):"
  read gpio_num
  if [[ $gpio_num -ge 17 && $gpio_num -le 20 ]]; then
    gpio_index=$(($gpio_num-17))
    echo "1" > /sys/class/gpio/gpio${gpio_array[$gpio_index]}/value
    echo "GPIO${gpio_num}已设置为高电平"
  else
    echo "输入的GPIO编号无效"
  fi
}

# 控制GPIO低电平
function set_gpio_low() {
    
    
  echo "请输入要控制的GPIO编号(17-20):"
  read gpio_num
  if [[ $gpio_num -ge 17 && $gpio_num -le 20 ]]; then
    gpio_index=$(($gpio_num-17))
    echo "0" > /sys/class/gpio/gpio${gpio_array[$gpio_index]}/value
    echo "GPIO${gpio_num}已设置为低电平"
  else
    echo "输入的GPIO编号无效"
  fi
}

# 释放GPIO
function release_gpio() {
    
    
  for gpio in ${gpio_array[@]}; do
    echo $gpio > /sys/class/gpio/unexport
  done
  echo "GPIO已释放"
}

# 主菜单
function main_menu() {
    
    
  while true; do
    echo "请选择操作:"
    echo "1. 申请GPIO"
    echo "2. 控制GPIO"
    echo "3. 释放GPIO"
    echo "4. 退出"
    read choice
    case $choice in
      1)
        request_gpio
        ;;
      2)
        gpio_menu
        ;;
      3)
        release_gpio
        ;;
      4)
        exit 0
        ;;
      *)
        echo "无效的选项"
        ;;
    esac
  done
}

# GPIO子菜单
function gpio_menu() {
    
    
  while true; do
    echo "请选择操作:"
    echo "1. 控制高电平"
    echo "2. 控制低电平"
    echo "3. 退出"
    read choice
    case $choice in
      1)
        set_gpio_high
        ;;
      2)
        set_gpio_low
        ;;
      3)
        break
        ;;
      *)
        echo "无效的选项"
        ;;
    esac
  done
}

# 运行主菜单
main_menu

Run the script on the Raspberry Pi to enter the main menu. After selecting the option to apply for GPIO, the program will automatically apply for the 4 GPIOs defined in the array. After selecting the control GPIO option, enter the submenu, you can choose to control GPIO high level, low level or exit. After selecting exit, go back to the main menu, and you can choose to release GPIO or exit the program.

1.3 Verification code

This article uses GPIO to check the schematic diagram and change it to GPIO2_A0-3, and the GPIO number needs to be modified.
GPIO number calculation formula: 32 x group number + 8 x group + io number
Take GPIO2_A1 as an example, GPIO2 group number is 2, A1 group is 0 (A is 0, B is 1, C is 2, D is 3), the pin The offset is 1
GPIO number = 32 x 2 + 8 x 0 + 1 = 65, and then calculate the others as 64 66 67.

#!/bin/bash^M
^M
# 定义GPIO数组^M
gpio_array=(64 65 66 67)^M
^M
# 申请GPIO^M
function request_gpio() {
    
    ^M
  for gpio in ${gpio_array[@]}; do^M
    echo $gpio > /sys/class/gpio/export^M
  done^M
  echo "GPIO已申请"^M
}^M
  1. Verify code in Windows environment
    Right-click --> Text document --> Rename --> test.sh --> Paste the code just copied
    --> Import to Raspberry Pi --> Convert test.sh in dos format to unix Documents under
    –> Increase Execution Permissions –> Verify Execution Results
dos2unix test.sh
chmod +x test.sh
sh test.sh
  1. Verify code in Linux environment
    Edit test.sh --> Paste the code you just copied --> Add execution permission --> Verify the execution result
gedit test.sh
# 粘贴后保存
chmod +x test.sh

Execute the open test. This article uses the Linux environment, which can be directly edited and executed directly.

. ./test.sh
arm@Ubuntu20:~$ ./test.sh 
请选择操作:
1. 申请GPIO
2. 控制GPIO
3. 释放GPIO
4. 退出
1
./test.sh: line 9: /sys/class/gpio/export: Permission denied
./test.sh: line 9: /sys/class/gpio/export: Permission denied
./test.sh: line 9: /sys/class/gpio/export: Permission denied
./test.sh: line 9: /sys/class/gpio/export: Permission denied
GPIO已申请
请选择操作:
1. 申请GPIO
2. 控制GPIO
3. 释放GPIO
4. 退出

Verify that the execution results are not expected, compare the requirements description with the generated results. If the description is found to be wrong, modify the description and regenerate it, and Xiti will do it again.

#!/bin/bash

# 定义GPIO数组
gpio_array=(64 65 66 67)

# 导出GPIO
for gpio in ${gpio_array[@]}; do
    echo $gpio > /sys/class/gpio/export
done

# 设置GPIO为输出模式
for gpio in ${gpio_array[@]}; do
    echo out > /sys/class/gpio/gpio$gpio/direction
done

# 定义菜单选项
menu_options=("GPIO ${gpio_array[0]}" "GPIO ${gpio_array[1]}" "GPIO ${gpio_array[2]}" "GPIO ${gpio_array[3]}" "退出")

# 定义控制选项
control_options=("高电平" "低电平" "退出")

# 定义函数:控制GPIO电平
function control_gpio() {
    
    
    gpio=$1
    control=$2
    if [ $control == "高电平" ]; then
        echo 1 > /sys/class/gpio/gpio$gpio/value
    elif [ $control == "低电平" ]; then
        echo 0 > /sys/class/gpio/gpio$gpio/value
    fi
}

# 主循环
while true; do
    # 显示菜单
    echo "请选择要控制的GPIO:"
    for i in ${!menu_options[@]}; do
        echo "$i. ${menu_options[$i]}"
    done

    # 读取用户输入
    read -p "请输入选项编号:" choice

    # 处理用户输入
    if [ $choice -ge 0 ] && [ $choice -lt ${#menu_options[@]} ]; then
        if [ $choice -eq $((${#menu_options[@]} - 1)) ]; then
            # 退出
            break
        else
            # 进入控制子菜单
            gpio=${gpio_array[$choice]}
            while true; do
                # 显示控制子菜单
                echo "请选择要进行的操作:"
                for i in ${!control_options[@]}; do
                    echo "$i. ${control_options[$i]}"
                done

                # 读取用户输入
                read -p "请输入选项编号:" control_choice

                # 处理用户输入
                if [ $control_choice -ge 0 ] && [ $control_choice -lt ${#control_options[@]} ]; then
                    if [ $control_choice -eq $((${#control_options[@]} - 1)) ]; then
                        # 退出控制子菜单
                        break
                    else
                        # 控制GPIO电平
                        control=${control_options[$control_choice]}
                        control_gpio $gpio $control
                    fi
                else
                    echo "无效的选项编号,请重新输入!"
                fi
            done
        fi
    else
        echo "无效的选项编号,请重新输入!"
    fi
done

# 取消导出GPIO
for gpio in ${gpio_array[@]}; do
    echo $gpio > /sys/class/gpio/unexport
done

The script uses the sysfs interface to control GPIO, first exports the GPIO to be used and sets it to output mode, then enters the main loop, displays the menu and reads user input, and enters the control submenu or exits the program according to the user's choice. In the control submenu, control options are displayed and user input is read, and the high and low levels of the corresponding GPIO are controlled according to the user's selection. Finally unexport GPIO.

If the requirements are still not met after the test, let's make another one.

Tips

  1. Editing a text document under Windows is a dos environment, which will increase at the end of each line ^M, and an error will be reported during execution; it must be manually cleared before execution.
  2. When executing Linux, confirm the execution file path
    . + /path/file is the absolute path
    sh + path/file is the relative path

Summarize

Technology has changed life.

Guess you like

Origin blog.csdn.net/weixin_35723192/article/details/130623284