pk2cmd-gui

a gui wrapper for pk2cmd


With the release of Microchip's MPLABX linux users now have access to a full blown IDE for programming microchips. However, when MPLABX was first released it did not support PICKit2 and also for my andon LED circuits and the odd experimental project it was overkill. My programs are never large or complex so geany does me as an editor and pk2cmd allows me to program the chips I use.

I might not use pk2cmd for a month or two and can never remember the commands so I put together a script for programming chips that uses yad to provide a gui interface.

PIC chip with transparent background.

I have pk2cmd-gui installed in /usr/local/bin and PK2DeviceFile.dat in /usr/local/share/pk2/. In order to run pk2cmd as a normal user I have saved the following lines in a text file as /etc/udev/rules.d/50-pic-programmer.rules.

#50-pic-programmer.rules
# pickit2
SUBSYSTEMS=="usb", ATTRS{idVendor}=="04d8", ATTRS{idProduct}=="0033", GROUP="users", MODE="0666"

Pk2cmd-gui requires gputils to assemble programs and yad to provide a gui interface. The microchip image on the top right has a transparent background. I keep it in /usr/local/share/icons and use it as an icon for my desktop launcher. Pk2cmd-gui also uses the image as a window icon and will look for it in that directory.

The bottom image on the top right is a link to a gallery that will take you through the basics of using pk2cmd-gui.

I find this script very useful and have set it up for my needs. However it can be easily enough modified. Note that I have provided a menu selection for opening an editor and opening a terminal. I use geany and xfce4-terminal. Change the variables at the top of the script to suit. If there is something else you want to add it shouldn't be to difficult to sort out how to do it.

!/bin/bash 
################################################################################
# pk2cmd-gui-0.2 rm20140228
# /usr/local/bin/pk2cmd-gui
# A script providing linux gui access to a PICKit2 
# Works in Slackware and Salix
# Requires pk2cmd to access PICKit2
# Reguires yad for gui interface
# Requires gputils to assemble .asm programs
# Requires $ICON_PATH/pic10f200-trans.png 
#
# To access PICKit2 as a normal user I use the following 3 lines as a udev rule
# saved as 50-pic-programmer.rules in /etc/udev/rules.d. The first two lines are
# comments and not required. 
#
#	#50-pic-programmer.rules
#	# pickit2
#	SUBSYSTEMS=="usb", ATTRS{idVendor}=="04d8", ATTRS{idProduct}=="0033", GROUP="users", MODE="0666"
#
# Nominate a terminal application to be called by script. Change to suit your need.
# I use Xfce desktop. yours may be different
TERMINAL_AP=/usr/bin/xfce4-terminal
# Nominate an editor
EDITOR_AP=/usr/bin/geany
# Path window icon used by yad
ICON_PATH=/usr/local/share/icons
#
# Begin script #################################################################

first_run()		# Check for ~/.pk2cmd-gui directory, if not there create it.
	{
		test -d /home/$USER_NAME/.pk2cmd-gui
		if [[ $? -eq 1 ]]; then
			mkdir /home/$USER_NAME/.pk2cmd-gui
		fi
	}

get_device()	# Copy device to temp storage ~/.pk2smd-gui/last_device.txt
	{	
		test -f /home/$USER_NAME/.pk2cmd-gui/previous_device.txt
		if [[ $? -eq 0 ]]; then
			previous_device="$(cat /home/$USER_NAME/.pk2cmd-gui/previous_device.txt)"
		fi
		
		TARGET_DEVICE=$(yad --width "150" \
			--window-icon=$ICON_PATH/pic10f200-trans.png \
			--title  "Nominate Target Device" \
			--entry \
			--text "Enter or change the target device name.\nUse upper case letters e.g. PIC12F509.\nHit \"OK\" to open the options list or \"Close\" to exit " \
			--entry-text="$previous_device" \
			--button="gtk-close:1" --button="gtk-ok:0" )
		 
		if [[ $? -eq 1 ]];then
			break
		fi
		 
		echo "$TARGET_DEVICE" > /home/$USER_NAME/.pk2cmd-gui/previous_device.txt
	}
	
	
get_working_dir()	# Set the initial working directory
	{
		WORKING_DIR="$(yad --file --width "700" --height "500" \
		--title "pk2cmd-gui" --window-icon "$ICON_PATH/pic10f200-trans.png" \
		--text " Select your working directory")"
		
		echo -e "cd $WORKING_DIR"
		cd $WORKING_DIR
	}

select_options()	# Main selection menu.
	{
		TARGET_DEVICE=$(cat /home/$USER_NAME/.pk2cmd-gui/previous_device.txt)
		test -z $TARGET_DEVICE
 		if [[ $? -eq 0 ]] 
		then
			first_line_1=$(echo "You have not nominated a target device.")
		else
			TARGET_DEVICE=$(cat /home/rick/.pk2cmd-gui/previous_device.txt)
			first_line_1=$(echo "The target device is $TARGET_DEVICE.")
		fi
		
		
		
		first_line_2="The working directory is $WORKING_DIR."
		
		
			
		get_options=$(yad  --width "600" --height "425" \
			--title "pk2cmd Options" --window-icon "$ICON_PATH/pic10f200-trans.png" \
			--text "$first_line_1\n$first_line_2\nWhat do you want to do?" \
			--list --checklist --column " " \
			--button="gtk-ok:0"  \
			--column "	Option" --column " " \
			FALSE "Target Device" "Nominate a target device" \
			FALSE "Working Directory" "Set working directory path" \
			FALSE "Power Up" "Power device from USB" \
			FALSE "Power Down" "Remove USB power source" \
			FALSE "Assemble Code" "Assemble program and create hex file " \
			FALSE "Program Device" "Write program to device." \
			FALSE "Read Program" "Copy program from device and save as a file." \
			FALSE "Edit File" "Open a file in an editor" \
			FALSE "Open Terminal" " " \
			FALSE "Help" "List pk2cmd options" \
			FALSE "List Supported Devices" " " \
			FALSE "List Error Codes" " " \
			FALSE "Quit" " ")
			 
		if [ $? -eq 1 ];then
			break
		fi
		
		options=$(echo "$get_options" | awk -F "|" '{ print $2 }')
		
	}
	
assemble_code()		# Assemble code from existing .asm file.
	{
		asm_file="$(yad --file --width "700" --height "500" \
		--title "pk2cmd-gui" --window-icon "$ICON_PATH/pic10f200-trans.png" \
		--text " Select the file you want to asemble")"
		
		if [[ $asm_file != *.asm ]]
			then
			yad --title "pk2cmd-gui" \
				--window-icon "$ICON_PATH/pic10f200-trans.png" \
				--button=gtk-close \
				--text "You did not select file with an .asm extension"
		elif [[ $asm_file == *.asm ]]
			then
				asm_dir=$(dirname $asm_file)
			base_name=$(basename -s .asm $asm_file)
			time_stamp=$(date +%y%m%d%H%M)
			mv $asm_dir/$base_name $asm_dir/$base_name-$time_stamp
			mkdir $asm_dir/$base_name
		fi
			
		gpasm -c $asm_dir/demo.asm -o $asm_dir/$base_name/$base_name > /home/$USER_NAME/.pk2cmd-gui/stdout.txt
		echo -e "\n\n" >> /home/$USER_NAME/.pk2cmd-gui/stdout.txt
		gplink $asm_dir/$base_name/$base_name.o -o $asm_dir/$base_name/$base_name >> /home/$USER_NAME/.pk2cmd-gui/stdout.txt
			
		yad --title "pk2cmd-gui" --width "500" \
			--window-icon "$ICON_PATH/pic10f200-trans.png" \
			--button=gtk-close \
			--text "`cat /home/$USER_NAME/.pk2cmd-gui/stdout.txt`"
	}
	
program_device()	# Program a microchip with an existing .hex file.
	{
	file_name=$(yad --file --width "700" --height "500" \
		--title "pk2cmd-gui" --window-icon "$ICON_PATH/pic10f200-trans.png" \
		--text " Select the hex file you want to load.")
		
		if [[ ! -d "$file_name" ]]
			then
				pk2cmd -p$TARGET_DEVICE -m -f$file_name | sed 's/^.*PICkit/PICkit/' > /home/$USER_NAME/.pk2cmd-gui/stdout.txt
				yad --title "pk2cmd-gui" --width "200" \
				--window-icon "$ICON_PATH/pic10f200-trans.png" \
				--button=gtk-close \
				--text "`cat /home/$USER_NAME/.pk2cmd-gui/stdout.txt`"
			else
				yad --title "pk2cmd-gui" \
				--window-icon "$ICON_PATH/pic10f200-trans.png" \
				--button=gtk-close \
				--text "You did not select a file"
		fi
	}

save_hex()	# Copy program from microchip and save it to a file. Do not change format of the eighth line in this function
	{
	start_path=$(pwd)
	file_name=$(yad --window-icon=$ICON_PATH/pic10f200-trans.png \
		--title  "Hex file destination" \
		--entry \
		--text "Your pwd is $start_path.\n\nEnter your backup hex file name. \
Provide a path to save in a different directory, e.g. mypath/backup.hex\n" \
		--button="gtk-cancel:1" --button="gtk-ok:0" )
		 
		if [[ -z "$file_name" ]]
			then
				yad --title "pk2cmd-gui" \
				--window-icon "$ICON_PATH/pic10f200-trans.png" \
				--button=gtk-close \
				--text "You did not provide a filename"
			else
				pk2cmd -p$TARGET_DEVICE -gf$file_name | sed 's/^.*Read/Read/' > /home/$USER_NAME/.pk2cmd-gui/stdout.txt
				yad --title "pk2cmd-gui" --width "200" \
				--window-icon "$ICON_PATH/pic10f200-trans.png" \
				--button=gtk-close \
				--text "`cat /home/$USER_NAME/.pk2cmd-gui/stdout.txt`"
		fi
	}
	
open_terminal()		# Open terminal named in variable $TERMINAL_AP.
	{
		$TERMINAL_AP
	}

edit_file()		# Open a file in the editor named in the variable $EDITOR_AP.
	{
		file_name=$(yad --file --width "700" --height "500" \
			--title "pk2cmd-gui" --window-icon "$ICON_PATH/pic10f200-trans.png" \
			--text " Select the file you want to edit.")
		
		$EDITOR_AP $file_name
			
	}
	
# Begin main
first_run 

while true; do
	USER_NAME=$(whoami)
	WORKING_DIR=$(pwd)
	get_device 
	select_options
	
	case $options in
		"Working Directory")
			get_working_dir
			;;
		"Power Up")
			pk2cmd -p$TARGET_DEVICE -r -t > /home/$USER_NAME/.pk2cmd-gui/stdout.txt
		
			yad --title "pk2cmd-gui" --width "200" \
			--window-icon "$ICON_PATH/pic10f200-trans.png" \
			--button=gtk-close \
			--text "`cat /home/$USER_NAME/.pk2cmd-gui/stdout.txt`"
			#break
			;;
		"Power Down")
			pk2cmd -p$TARGET_DEVICE -w > /home/$USER_NAME/.pk2cmd-gui/stdout.txt
			yad --title "pk2cmd-gui" --width "200" \
			--window-icon "$ICON_PATH/pic10f200-trans.png" \
			--button=gtk-close \
			--text "`cat /home/$USER_NAME/.pk2cmd-gui/stdout.txt`"
			#break
			;;			
		"Assemble Code")
			assemble_code
			;;			
		"Program Device")
			program_device
			;;		
		"Read Program")
			save_hex			
			;;
		"Edit File")
			edit_file
			;;
		"Open Terminal")
			open_terminal
			;;
		"Help")
			pk2cmd | sed '/^Operation/d' | \
			yad --title "pk2cmd-gui" \
			--window-icon "$ICON_PATH/pic10f200-trans.png" \
			--width "600" --height "650" \
			--list --column "Pk2cmd help and information"  \
			--button=gtk-close 
			;;
		"List Supported Devices")
			pk2cmd -?p | sed '/^Operation/d' | \
			yad --title "pk2cmd-gui" \
			--window-icon "$ICON_PATH/pic10f200-trans.png" \
			--width "550" --height "500" \
			--list --column "Pk2cmd supported devices" \
			--button=gtk-close 
			;;	
		"List Error Codes")
			pk2cmd -?e | sed '/^Operation/d' | \
			yad --title "pk2cmd-gui" \
			--window-icon "$ICON_PATH/pic10f200-trans.png" \
			--width "550" --height "500" \
			--list --column "Pk2cmd error codes" \
			--button=gtk-close \
			--button=gtk-exit
			;;	
		"Quit")
			break
			;;
	esac
done

# End of script


What's up?