[gnoduino] support custom hardware (arduino platforms)



commit 26a051f8bb34392110b557ab3914f02420e77733
Author: Lucian Langa <lucilanga gnome org>
Date:   Thu Mar 15 21:58:50 2012 +0100

    support custom hardware (arduino platforms)

 src/board.py    |   39 +++++++++++++++++++++++++++++++++++----
 src/misc.py     |    6 +++++-
 src/uploader.py |    6 ++++--
 3 files changed, 44 insertions(+), 7 deletions(-)
---
diff --git a/src/board.py b/src/board.py
index e359556..c5133ac 100644
--- a/src/board.py
+++ b/src/board.py
@@ -34,12 +34,21 @@ class Board(object):
 		self.programmers = []
 		self.defaults = []
 		try:
+			self.boards.extend(self.readCustomBoards())
+		except: pass
+		try:
 			self.boards.extend(self.readArduinoBoards())
 		except:
 			self.boards.extend(self.readGnoduinoBoards())
+		#renumber ids in case we grown with customs
+		for i in range(len(self.boards)): self.boards[i]['id'] = i+1
 		self.p = prefs.preferences()
 		if config.cur_board == -1:
-			config.cur_board = self.getBoardIdByName(self.p.getSafeValue("board", "uno")) - 1
+			"""error can occur when placing different types of hardware in sketchdir"""
+			try:
+				config.cur_board = self.getBoardIdByName(self.p.getSafeValue("board", "uno")) - 1
+			except:
+				config.cur_board = self.getBoardIdByName("uno") - 1
 
 	def readGnoduinoBoards(self):
 		boards = []
@@ -59,9 +68,12 @@ class Board(object):
 		return boards
 
 	def readArduinoBoards(self):
+		return self.readArduinoBoardsFile(misc.getArduinoFile("hardware/arduino/boards.txt"))
+
+	def readArduinoBoardsFile(self, boardsFile):
 		boards = []
 		try:
-			f = open(misc.getArduinoFile("hardware/arduino/boards.txt"))
+			f = open(boardsFile)
 			q = []
 			z = []
 			for c in f.readlines():
@@ -84,12 +96,22 @@ class Board(object):
 							w['desc'] = c[1]
 						else:
 							w[var] = c[1]
+				w['hwpath'] = boardsFile
 				w['id'] = k
 				k += 1
 				boards.append(w)
 			return boards
 		except: return None
 
+	def readCustomBoards(self):
+		p = prefs.preferences()
+		d = os.path.join(p.getValue("sketchbook.path"), "hardware")
+		try:
+			for i in os.listdir(d):
+				if os.path.exists(os.path.join(d, i, "boards.txt")):
+					return self.readArduinoBoardsFile(os.path.join(d, i, "boards.txt"))
+		except:	return None
+
 	def getBoards(self):
 		return self.boards
 
@@ -106,10 +128,14 @@ class Board(object):
 		return self.boards[id]['f_cpu']
 
 	def getPGM(self, id):
-		return self.boards[id]['protocol']
+		try:
+			return self.boards[id]['protocol']
+		except KeyError: return ""
 
 	def getPGMSpeed(self, id):
-		return self.boards[id]['speed']
+		try:
+			return self.boards[id]['speed']
+		except KeyError: return 0
 
 	def getFuseLock(self, id):
 		return self.boards[id]['lock_bits']
@@ -136,6 +162,11 @@ class Board(object):
 			return self.boards[id]['variant']
 		except KeyError: return ""
 
+	def getHardwarePath(self, id):
+		try:
+			return self.boards[id]['hwpath']
+		except KeyError: return ""
+
 	def getBootloader(self, id):
 		return self.boards[id]['file']
 
diff --git a/src/misc.py b/src/misc.py
index 264aaec..6b6d354 100644
--- a/src/misc.py
+++ b/src/misc.py
@@ -62,7 +62,11 @@ def getArduinoVariantPath():
 	b = board.Board()
 	variant = b.getVariant(b.getBoard())
 	if variant:
-		return get_path("hardware/arduino/variants/" + variant)
+		try:
+			return get_path("hardware/arduino/variants/" + variant)
+		except:
+			hwdir = os.path.dirname(b.getHardwarePath(b.getBoard()))
+			return os.path.join(hwdir, "variants", variant)
 
 def getArduinoIncludes():
 	includes = ["-I" + getArduinoPath()]
diff --git a/src/uploader.py b/src/uploader.py
index fb7d43e..20cd7e1 100644
--- a/src/uploader.py
+++ b/src/uploader.py
@@ -140,10 +140,12 @@ def upload(obj, serial, output, notify):
 	compline=[i for i in avr]
 	# avrdude wants "stk500v1" to distinguish it from stk500v2
 	protocol = b.getPGM(b.getBoard())
-	if protocol == "stk500": protocol = "stk500v1"
+	if protocol == "stk500" or protocol == "": protocol = "stk500v1"
 	compline.append("-c" + protocol)
 	compline.append("-P" + port)
-	compline.append("-b" + b.getPGMSpeed(b.getBoard()))
+	try:
+		compline.append("-b" + b.getPGMSpeed(b.getBoard()))
+	except: pass
 	compline.append("-p" + b.getBoardMCU(b.getBoard()))
 	compline.append("-Uflash:w:"+obj+".hex:i")
 	try:



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]