Posts tagged ‘Linux’

A Makefile for NXC code

# A Makefile to compile NXC code using NBC
# Copyright (C) 2008 Gary French
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#

INCLUDEDIR =../include
NBC=/usr/local/bin/nbc
NBC_FLAGS=-I=$(INCLUDEDIR)
#optional compiler optimizations
# NBC_FLAGS=-I=$(INCLUDEDIR) -Z1
# NBC_FLAGS=-I=$(INCLUDEDIR) -Z2

#this can also be explicitly set
# NXC_FILES := TheProgram.nxc
NXC_FILES := $(shell ls -1 -t -F *.nxc  2>/dev/null | grep '\.nxc')
RXE_FILES := $(NXC_FILES:.nxc=.rxe)

BYTECODE_FILES := $(NXC_FILES:.nxc=.bytecode)
LISTING_FILES := $(NXC_FILES:.nxc=.listing)
SYMTABLE_FILES := $(NXC_FILES:.nxc=.symtable)

#the targets can be called individually, or called from the "all" target
#all: compile bytecode listing symtable
all: compile

compile: $(RXE_FILES)
%.rxe: $(NXC_FILES)
	@echo "compiling $<"
	$(NBC) $(NBC_FLAGS) -O=$@ $<
	@echo

bytecode: $(BYTECODE_FILES)
%.bytecode: $(NXC_FILES)
	@echo "generating bytecode for $<"
	$(NBC) $(NBC_FLAGS) -b -nbc=$@ $<
	@echo

listing: $(LISTING_FILES)
%.listing: $(NXC_FILES)
	@echo "generating code listing for $<"
	$(NBC) $(NBC_FLAGS) -b -L=$@ $<
	@echo

symtable: $(SYMTABLE_FILES)
%.symtable: $(NXC_FILES)
	@echo "generating symbol table for $<"
	$(NBC) $(NBC_FLAGS) -b -Y=$@ $<
	@echo

clean:
	@echo "cleaning up..."
	-rm $(RXE_FILES)
	-rm $(BYTECODE_FILES)
	-rm $(LISTING_FILES)
	-rm $(SYMTABLE_FILES)
	@echo

.PHONY: clean bytecode listing symtable

Compiling and Transfering NXC Code to a NXT-Brick Under Fedora Linux

you must be able to communicate with the NXT Brick for any of the following to be applicable. there are a few “how-to”s available, including this post.

I am using the compiler available at Next Byte Codes & Not eXactly C site. This is a simple package package to “install” (simply copying the executable from the tar/gzip to /usr/local/bin did it for me), and is wonderfully documented.

I am using a modified version of a script provided by Jan-Klaas Kollhof:

#!/bin/sh
set -e

_nbcpath=/PATH/TO/NBC/EXECUTABLE/DIRECTORY

_dirname=`dirname $1`
if [ "${_dirname}" == "." ]; then
	_dirname=$(pwd)
fi
_basename=`basename $1 .nxc`

echo "compiling ${_basename}.rxe from ${_basename}.nxc..."
${_nbcpath}/nbc \
	-O="${_dirname}/${_basename}.rxe" \
	-nbc="${_dirname}/${_basename}.nbc" \
		"${_dirname}/${_basename}.nxc"

echo "sending ${_basename}.rxe to brick..."
cd "${_dirname}"
nxt_push "${_basename}.rxe"

as should be apparent, /PATH/TO/NBC/EXECUTABLE/DIRECTORY will need to be altered to suit your environment.

the “-nbc” switch will save the generated “Next Byte Code” to YOUR_CODE_FILE_NAME.nbc. the resulting file is not only interesting to poke around in, but may help when optimizing for size.

currently, I am having some issues with nxt_python, documented here.

nxt_pull, a companion program for nxt_push

while working with nxt_python and a few applications written in NXC, I found I had a need to get files from the NXT Brick with a minimum of fuss. The following python script will “pull” a single file from the NXT Brick. Wildcard behavior is undefined, and is planned for version 0.2.

example usage:

[gary@motoko]# nxt_pull log.txt

code:

#!/usr/bin/env python
#
# nxt_pull:
#   Pull a file to a LEGO Mindstorms NXT brick,
#   based on Douglas P Lau's nxt_filer and nxt_push
#   Copyright (C) 2008 Gary French
#   Version: 0.1
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

import sys
import nxt.locator
from nxt.brick import FileFinder, FileReader, FileWriter

def read_file( b, fname ):
	r = FileReader(b, fname)
	r.__enter__()
	try:
		f = open(fname, 'wb')
		print 'Pulling %s ...' % (fname),
		try:
			size = 0
			for data in r:
				f.write(data)
				size += len(data)
		finally:
			f.close()
			print 'read %d bytes' % size
	finally:
		r.__exit__(None, None, None)

if __name__ == '__main__':
	sock = nxt.locator.find_one_brick()
	#sock = nxt.locator.find_one_brick(host='00:16:53:XX:XX:XX')
	if sock:
		b = sock.connect()
		read_file( b, sys.argv[1] )
		sock.close()

Error: “bluetooth.BluetoothError: (115, ‘Operation now in progress’)” while using nxt_python

currently, to avoid a “bluetooth.BluetoothError: (115, ‘Operation now in progress’)” error, I have to:

  1. turn the Brick’s Bluetooth off
  2. turn off the Brick
  3. restart Bluetooth services
  4. turn on the Brick
  5. turn the Brick’s Bluetooth on

it acts as if something is not cleaning up after itself properly.
I’m sure I’ll figure that one out, but I have more pressing concerns at the moment. if anyone has some insight on this issue I’d appreciate the comment.

current workaround:

  • use “hcitool scan” to find the MAC
  • modify nxt_push line 49 from:
    sock = nxt.locator.find_one_brick()

    to:

    sock = nxt.locator.find_one_brick(host='00:16:53:XX:XX:XX')

as one might suspect, this workaround is applicable in any other similar situation.

Setting up Bluetooth communication with a NXT Brick under Fedora linux

it is vital that the following python packages are installed:

to test PyBlueZ, the following can be used (code by Albert Huang <albert@csail.mit.edu>)

#!/usr/bin/env python
import bluetooth
print "performing inquiry..."
nearby_devices = bluetooth.discover_devices(lookup_names = True)
print "found %d devices" % len(nearby_devices)
for name, addr in nearby_devices:
    print "  %s - %s" % (addr, name)

as I do not care for gnome, my default desktop is KDE,
what follows is my procedure for pairing with a NXT Brick.

[root@motoko]# /usr/bin/bluetooth-applet &

a Bluetooth icon will appear in your system tray,

right-click the Bluetooth icon and click “Preferences” from the popup.

  • set the radio button under “Mode of operation” to be “Visible and connectable for other devices”
    (even if it ought to be “connectible”)
  • click “Close”

on the NXT Brick, from the root menu,

  • select “Bluetooth” and press the square button.
  • select “Search” and press the square button.
  • after it has finished searching, select the name that matches your computer and press the square button.
  • select “[1]” and press the square button.
  • wait for it to connect.
  • enter passkey, or press the square button for the “1234″ default.
  • Bluetooth icon will begin to blink, click it and enter the passkey in the resulting dialog.
  • right-click the Bluetooth icon and click “Preferences” from the popup.
    • select the NXT Brick in the “Bonded devices” box.
    • Click “Set Trusted”
    • click “Close”

the connection can now be tested with the following python code:

#!/usr/bin/env python
import nxt.locator
sock = nxt.locator.find_one_brick()
if sock:
	brick = sock.connect()
	name, host, signal_strength, user_flash = brick.get_device_info()
	print 'NXT brick name: %s' % name
	print 'Host address: %s' % host
	print 'Bluetooth signal strength: %s' % signal_strength
	print 'Free user flash: %s' % user_flash
	sock.close()

Extracting RPM Package Contents

firstly, there is a package available called “alien” that will convert to/from packages for nearly any distro, I use this on machines that it can be installed on.

however, there are times that using standard tools is faster and simpler,

piping the output of “rpm2cpio” (extract cpio archive from RPM Package Manager (RPM) package.) into “cpio” (copy files to and from archives) will extract the RPM package into the current directory.

rpm2cpio package.rpm | cpio -dimv

where:
-d = make directories, -i = “in” mode, -m = keep date/time stamps, -v = verbose.

Search and Replace Text

Find and replace all instances of a string in all files in current (and any subs) directory.

find ./ -name “*” | xargs perl -pi -e ’s/SEARCH/REPLACE/g’

Using Pure Bash to Parse Strings

while looking through my snippets for a script to parse a simple “several fields to a line, one record to a line” type of data set, I realized I was probably over engineering the solution, the following is the result of a few whiles playing with a few ideas:
 
 

#!/bin/bash
ITEMS='field 1;field 2;field 3;field 4;field 5'
LEN=${#ITEMS}
while [ "$LEN" -gt "0" ]; do
	echo -------------
	echo ${LEN}
	echo "post:"${ITEMS}
	#get the shortest match from front of the source string
	# by matching the longest from the back of the source string
	THIS_FIELD=${ITEMS%%;*}
	#remove the match from the source string
	ITEMS=${ITEMS:$((${#THIS_FIELD}+1))}
	#calc the length of the source string
	LEN=${#ITEMS}
	#show results
	echo "anno:"${ITEMS}
	echo ${THIS_FIELD}
done

will output:

————-
39
post:field 1;field 2;field 3;field 4;field 5
anno:field 2;field 3;field 4;field 5
field 1
————-
31
post:field 2;field 3;field 4;field 5
anno:field 3;field 4;field 5
field 2
————-
23
post:field 3;field 4;field 5
anno:field 4;field 5
field 3
————-
15
post:field 4;field 5
anno:field 5
field 4
————-
7
post:field 5
anno:
field 5

 
 
the idea can be taken further by using an array:

#!/bin/bash
#declare the array
declare -a FIELDS
ITEMS='field 1;field 2;field 3;field 4;field 5'
LEN=${#ITEMS}
while [ "$LEN" -gt "0" ]; do
	echo -------------
	echo ${LEN}
	echo "post:"${ITEMS}
	#get the shortest match from front of the source string
	# by matching the longest from the back of the source string
	THIS_FIELD=${ITEMS%%;*}
	#remove the match from the source string
	ITEMS=${ITEMS:$((${#THIS_FIELD}+1))}
	#calc the length of the source string
	LEN=${#ITEMS}
	#store result in the array
	FIELDS[ (${#FIELDS[*]}) ]=${THIS_FIELD}
	#show results
	echo "anno:"${ITEMS}
	echo ${THIS_FIELD}
	echo ${FIELDS[ (${#FIELDS[*]}-1) ]}
done

echo -------------
#show the array count
echo ${#FIELDS[*]}
#show the array content
echo ${FIELDS[*]}

will output:

————-
39
post:field 1;field 2;field 3;field 4;field 5
anno:field 2;field 3;field 4;field 5
field 1
field 1
————-
31
post:field 2;field 3;field 4;field 5
anno:field 3;field 4;field 5
field 2
field 2
————-
23
post:field 3;field 4;field 5
anno:field 4;field 5
field 3
field 3
————-
15
post:field 4;field 5
anno:field 5
field 4
field 4
————-
7
post:field 5
anno:
field 5
field 5
————-
5
field 1 field 2 field 3 field 4 field 5

 
 
the final script fragment without the comments::

#!/bin/bash
ITEMS='field 1;field 2;field 3;field 4;field 5'
declare -a FIELDS
LEN=${#ITEMS}
while [ "$LEN" -gt "0" ]; do
	THIS_FIELD=${ITEMS%%;*}
	ITEMS=${ITEMS:$((${#THIS_FIELD}+1))}
	LEN=${#ITEMS}
	FIELDS[ (${#FIELDS[*]}) ]=${THIS_FIELD}
done

COUNT=0
while [ "$COUNT" -lt ${#FIELDS[*]} ]; do
	echo ${FIELDS[$COUNT]}
	COUNT=$(( ${COUNT}+1 ))
done

obviously you would want to change the “ITEMS” variable and do something meaningful in the second loop.

this was posted in the hope that it will be of some use to someone.

GhostScript and Margins

I had a postscript document that simply did not want to come out correctly, the offsets were such that the page content was going well off the visible area.

the following vile hack is one solution:

cat <<EOF>offsetfix.ps
%!PS-Adobe-2.0
<<
	/PageOffset [ 16 -52]
	/Margins [0 0]
	/.HWMargins [0 0 0 0]
>>
setpagedevice
EOF

gs -q -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pdfwrite \
	-dOptimize=true \
	-sOutputFile=output.pdf \
	offsetfix.ps input.ps

granted, one could cat the snippit in offsetfix.ps with the original document, but as I’m not sure what else may be using it, I’d rather not.

this also has the advantage of being a bit more portable, albeit rather ugly when compared to the original:

ps2pdf input.ps output.pdf

File Modifications

Change File Extention

echo “`echo TESTFILENAME.EXTENTION | perl -pe ’s/\.[^.]+$//’`.NEWEXTENTION”

User Modifications

Add a user for server/system procs

useradd -g GROUP -c “USERNAME” -s /sbin/nologin USERNAME

Directory Tree Modifications

Change the owner of just the dirs in the current tree

find . -type d -exec chown USER:GROUP {} \;

Change the owner of just the files in the current tree

find . -type f -exec chown USER:GROUP {} \;

Change the permissions of just the dirs in the current tree

find . -type d -exec chmod 0775 {} \;

Change the permissions of just the files in the current tree

find . -type f -exec chmod 0775 {} \;

Change the permissions of files and dirs in the current tree

find . -type d -exec chmod 0775 {} \; ; \
find . -type f -exec chmod 0664 {} \;

  • Categories

  • Need Code Written?

  • Need a Coding Job?

  • Tags

  • Archives