henkoegema
Posts: 13
Joined: Thu Oct 17, 2013 3:26 pm

Python and C++

Wed Apr 19, 2017 3:04 pm

For an Arduino project I made a C++ file like this:

Code: Select all

void setup()
	display a fixed text on a 20x4 LCD display

void main()
	detect motion from a sensor
How do I go forward to do the same in Python (for Raspberry pi) ?

So, the text on the display should only be setup once. The text doesn't change.
The motion detection should be in a while() loop.

(If needed I can post my C++ program for clarification)

User avatar
PeterO
Posts: 5829
Joined: Sun Jul 22, 2012 4:14 pm

Re: Python and C++

Wed Apr 19, 2017 3:45 pm

You start by learning Python ! We're here to help but we won't do your work or homework for you.

https://www.learnpython.org/

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

henkoegema
Posts: 13
Joined: Thu Oct 17, 2013 3:26 pm

Re: Python and C++

Wed Apr 19, 2017 7:33 pm

supra wrote:
henkoegema wrote:For an Arduino project I made a C++ file like this:

Code: Select all

void setup()
	display a fixed text on a 20x4 LCD display

void main()
	detect motion from a sensor
How do I go forward to do the same in Python (for Raspberry pi) ?

So, the text on the display should only be setup once. The text doesn't change.
The motion detection should be in a while() loop.

(If needed I can post my C++ program for clarification)
For python, you don't needed setup. The python will take a care of it
You do did:

Code: Select all

def main():
   display a fixed text on a 20x4 LCD display 
    
   detect motion from a sensor
Thanks very much for this answer. :)

User avatar
paddyg
Posts: 2529
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Python and C++

Wed Apr 19, 2017 10:00 pm

the standard arduino

Code: Select all

bool dmpReady;
setup() {
  dmpReady = false;
}
loop() {
  dmpReady = !dmpReady;
}
in python is

Code: Select all

dmpReady = False
while True:
  dmpReady = not dmpReady
Last edited by paddyg on Thu Apr 20, 2017 9:10 am, edited 1 time in total.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

henkoegema
Posts: 13
Joined: Thu Oct 17, 2013 3:26 pm

Re: Python and C++

Thu Apr 20, 2017 9:06 am

paddyg wrote:the standard arduino

Code: Select all

bool dmpReady;
setup() {
  dmpReady = false;
}
loop() {
  dmpReady = !dmpReady;
}
in python is

Code: Select all

dmpReady = False
while True:
  dmpReady = not dmpReady
Thanks, I got it working ! :D

hippy
Posts: 7459
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Python and C++

Thu Apr 20, 2017 10:22 am

Code: Select all

void setup()
	display a fixed text on a 20x4 LCD display

void main()
	detect motion from a sensor
To convert to Python I would go for a literal translation ...

Code: Select all

def setup():
	display a fixed text on a 20x4 LCD display

def main():
	detect motion from a sensor

setup()
main()

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: Python and C++

Thu Apr 20, 2017 11:08 am

hippy wrote:
To convert to Python I would go for a literal translation ...

Code: Select all

def setup():
	display a fixed text on a 20x4 LCD display

def main():
	detect motion from a sensor

setup()
main()
Or more accurately to replicate the behavior of the main loop

Code: Select all

def setup():
	display a fixed text on a 20x4 LCD display

def main():
	detect motion from a sensor

setup()
while True:
    main()
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

User avatar
paddyg
Posts: 2529
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Python and C++

Thu Apr 20, 2017 5:19 pm

Yes. I was (slightly disingenuously) trying to make python look as easy to read and write as possible! There's a bit of an issue when you introduce setup() and main() as the arduino default variable scope is global but has to be explicit in python unless you just want to use the variable without changing it in which case you don't need to be explicit. so

Code: Select all

dmpReady = None

def setup():
  dmpReady = False

def main():
  dmpReady = not dmpReady

setup()
while True:
  main()  
Will complain that dmpReady is referenced before assignment.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

hippy
Posts: 7459
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Python and C++

Thu Apr 20, 2017 7:05 pm

scotty101 wrote:Or more accurately to replicate the behavior of the main loop ...

Code: Select all

setup()
while True:
    main()
Thanks. Not familiar with 'Arduino C', but I thought using "main()" required the while within it which I thought the OP was adding. so, one or the other ...

Code: Select all

def setup():
  one-off stuff

def main():
  while True:
     repeated stuff

setup()
main()

Code: Select all

def setup():
  one-off stuff

def loop():
   repeated stuff

setup()
while True:
  loop()

klintkrossa
Posts: 81
Joined: Tue Nov 10, 2015 3:06 pm

Re: Python and C++

Fri Apr 21, 2017 4:22 pm

Hello,
I have two thoughts that may help, I just did a little with it and found that I now nothing about C.

Code: Select all

import ctypes
https://docs.python.org/3/extending/extending.html

I hope that helps.
Thanks
This is not like any other bulletin boards that I have been on. Been flamed on other BB's so bad I was afraid to ask.

All my Raspberry Pi's are like the Hessian artilleryman of Sleepy Hollow.

Return to “Python”