This is an importable file that i have been working on.
It has no specific purpose other than to add some features to python to make it easier.
I just add functions when i need them.
It is also designed to make python more friendly to users of other languages (Eg, arduino IDE (Delay(time in milliseconds)))
I hope that someone can make use of at least half of these functions.
This is supposed to be imported using
PS, if anyone has a solution to stop chipTalk(port,baudrate,Data_out,timer) from reinitialising the arduino everytime it is called, I would be very thankful.
It has no specific purpose other than to add some features to python to make it easier.
I just add functions when i need them.
It is also designed to make python more friendly to users of other languages (Eg, arduino IDE (Delay(time in milliseconds)))
I hope that someone can make use of at least half of these functions.
This is supposed to be imported using
From module_name import *
import serial,time,textwrap,smtplib,random,os,math def chipReset(port,baudrate): try: serial.Serial(port, baudrate) return 1 except: return 0 def chipTalk(port,baudrate,Data_out,timer): try: ser = serial.Serial(port, baudrate) delay(timer) ser.write(Data_out) return 1 except: return 0 def chipRead(): try: complete = "" info = ser.read() if (info != "EOL"): if (info != "0"): complete = complete + info; else: return complete else: return complete if ((ser.available())==False): return complete chipRead() except: return 0 def writeToFile(Filename,Data): try: file = open(Filename,'w') file.write(Data) file.close return 1 except: return 0 def ReadFromFile(Filename): try: file = open(Filename,'r') data = file.read() file.close return data except: return 0 def appendFile(append,Filename): try: file = open(Filename,'r') data = file.read() file.close data = data + append file = open(Filename,'w') file.write(data) file.close return 1 except: return 0 def deleteFile(FileName): try: os.remove(FileName) return 1 except: return 0 def isEven(number): if (number % 2 == 0): return True else: return False def isPrime(x): return all(x % i for i in range(2, x)) def wait(x): time.sleep(x) return def pause(x): x = x / 1000 time.sleep(x) return def delay(x): time.sleep(x/1000) return def send_email(GMAIL_USERNAME,GMAIL_PASSWORD,SUBJECT,TEXT): try: smtpserver = smtplib.SMTP("smtp.gmail.com",587) smtpserver.ehlo() smtpserver.starttls() smtpserver.ehlo() smtpserver.login(GMAIL_USERNAME, GMAIL_PASSWORD) header = '\tTo:' + GMAIL_USERNAME + '\n\t' + 'From: ' + GMAIL_USERNAME header = header + '\n\t' + 'Subject:' + SUBJECT + '\t\n' msg = header + '\t\n' + TEXT + ' \n\n' smtpserver.sendmail(GMAIL_USERNAME, GMAIL_USERNAME, msg) smtpserver.close() return 1 except: return 0 def randomFloat(range1,range2): result = random.uniform(range1.range2) return result def randomChoice(selection): result = random.choice(selection) return result def randomSample(selection,amount): result = random.sample(selection,amount) return result def randomNormalVariate(mean, sdev): result = random.normalvariate(mean, sdev) return resul def TrigUnknownHypotenuse(sideA,sideB): result = math.sqrt((sideA * sideA) + (sideB * sideB)) return result def TrigKnownHypotenuse(side,hyp): result = math.sqrt((hyp * hyp) - (side * side)) return result def perimeter(sideA,sideB): result = (2 * sideA) + (2 * sideB) return result def area(sideA,sideB): result = sideA * sideB return result def fibonacciSet(x): result = [] def fib(): a, b = 0, 1 while True: # First iteration: yield a # yield 0 to start with and then a, b = b, a + b # a will now be 1, and b will also be 1, (0 + 1) for index, fibonacci_number in enumerate(fib()): result.append(fibonacci_number) if index == x: break return result def fibonacciSingular(x): result = 0 def fib(): a, b = 0, 1 while True: # First iteration: yield a # yield 0 to start with and then a, b = b, a + b # a will now be 1, and b will also be 1, (0 + 1) for index, fibonacci_number in enumerate(fib()): if index == x: result = fibonacci_number break return result def password(password,clear,alreadySet): message = "" password2 = password file = open("Password.EncodedSafe","r") if (file.read(1) == "T"): alreadySet = "T" + file.read(3) else: alreadySet = "F" + file.read(4) unit = file.read() file.close() if (alreadySet == "True"): if(password == unit): if(clear == True): alreadySet = "False" file = open("Password.EncodedSafe","w") file.write("") file.close() return "3 - Password Cleared" else: return "1 - Correct" else: return "4 - Incorrect" else: unit = password alreadySet = "True" file = open("Password.EncodedSafe","w") message = alreadySet + unit file.write(message) file.close() return "2 - Password set" if(password2 == unit): if(clear == True): alreadySet = "False" file = open("Password.EncodedSafe","w") file.write("") file.close() return "3 - Password Cleared" else: return "4 - Incorrect" def CelsiusToFahrenheit(x): x = x * (9/5) + 32 return x def FahrenheitToCelsius(x): x = (x - 32) * (5/9) return x def CelsiusToKelvin(x): x = x + 273.15 return x def KelvinToCelsius(x): x = x - 273.15 return x def FahrenheitToKelvin(x): x = (FahrenheitToCelsius(x)) + 273.15 return x def KelvinToFahrenheit(x): x = (FahrenheitToCelsius(x)) - 273.15 return x def cosD(x,decimalPlaces): x = math.cos(math.radians(x)) x = round(x,decimalPlaces) return x def sinD(x,decimalPlaces): x = math.sin(math.radians(x)) x = round(x,decimalPlaces) return x def tanD(x,decimalPlaces): x = math.tan(math.radians(x)) x = round(x,decimalPlaces) return x def keyCont(): random_Unused_Variable = input() returnIf you have any other suggestions for functions, please comment as i am certain that there are some crucial ones that i have missed out.
PS, if anyone has a solution to stop chipTalk(port,baudrate,Data_out,timer) from reinitialising the arduino everytime it is called, I would be very thankful.