Skip to content

Instantly share code, notes, and snippets.

@dansku
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save dansku/9474455 to your computer and use it in GitHub Desktop.

Select an option

Save dansku/9474455 to your computer and use it in GitHub Desktop.
#Python Parser
import os
import sys
import re
#-----[ Config ]-------------------------------------
baseFile = 'base_main_Rate8.cfg'
outputFile = 'configure_parser.c'
cFunctionName = 'configure_AEC'
#----------------------------------------------------
def createList(filename):
f = open(filename, 'r')
wList = []
subList = []
start = 0
k=0
for i in f:
if i[0] == "w":
if start==1:
#here we save the subList to the last write
wList[k-1].append(subList)
#Clear list
subList = []
start=0
wLine = i.split() #Separate by space
wList.append(wLine[1:])
k=k+1
else:
if i[0] == ">":
start = 1
rLine = i.split()
subList.append(rLine[1])
return wList
#Create The Magic List
l = createList(baseFile)
outFile = open(outputFile, "w")
print >> outFile, 'static int '+cFunctionName+'(int fd)'
print >> outFile, '{'
for x in l:
addr = x[0]
reg = x[1]
val = x[2]
buf = x[3:]
if not x[3:]: #This will create the write function with only one data
print >> outFile, ' data[0] = 0x'+str(val)
print >> outFile, ' i2c_write(fd, 0x'+addr+', 0x'+reg+', data, '+str(k+1)+');'
else:
if buf:
print >> outFile, ' data[0] = 0x'+str(val)
for z in buf:
k=0
for i in z:
print >> outFile,' data['+str(k+1)+'] = 0x'+str(z[k])
k=k+1
k=k-1
print >> outFile, ' i2c_write(fd, 0x'+addr+', 0x'+reg+', data, '+str(k+2)+');'
k=0
print >> outFile, '}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment