I am pretty new to python and need to output values read in by an SPI to a text file. I have a function to create the text file, and one to read the SPI but I am not sure how to get these two to interact with each other. I am familiar with the concept of "friends" in OOP but not how python treats separate functions in terms of how to create visibility. What I need is to pass the returned value of my SPI function to the create text function so that it can be stored.
def timeStamp(fname, fmt = '%Y%m%d--%H%M%S_{fname}'):
return datetime.datetime.now().strftime(fmt).format(fname = fname)
with open (timeStamp('SPI_Inputs'), 'w') as output:
output.write('this needs to be adc output')
def readadc(adcnum):
if adcnum > 7 or adcnum < 0:
return -1
r = spi.xfer2([1,8 + adcnum << 4,0])
adcout = ((r[1] & 3) << 8) + r[2]
return adcout #this needs to be put into text file
while True:
ADC_Val = int(round(readadc(0)/10.24))
print "Input = ", ADC_Val
count = count +1
time.sleep (0.2)
timeStamp(readadc))
I think that what you're trying to do is the following:
def timeStamp(fname, fmt = '%Y%m%d--%H%M%S_{fname}'):
return datetime.datetime.now().strftime(fmt).format(fname = fname)
def write_to_file(str):
with open (timeStamp('SPI_Inputs'), 'w') as output:
output.write(str)
def readadc(adcnum):
if adcnum > 7 or adcnum < 0:
return -1
r = spi.xfer2([1,8 + adcnum << 4,0])
adcout = ((r[1] & 3) << 8) + r[2]
return adcout #this needs to be put into text file
while True:
ADC_Val = int(round(readadc(0)/10.24))
write_to_file(ADC_Val)
print "Input = ", ADC_Val
time.sleep (0.2)
print timeStamp(readadc))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.