Jump to content

How Do I Write Code to Have a GPIO Command Repeat At a Specific Frequency


Korben_Dallas

Recommended Posts

Hello all!

First of all, apologies for being such a noob but I am very eager to get started and there may be some ambiguous language here as a result.

I am getting started in Armbian Linux Buster on the Pine A64+ and for my first project I'd like to program a 7 segment display (four digits) to display descending numbers from 1000.  As you may know, 7-segment displays with more than one digit require the display to operate at a certain frequency in order to display multiple digits as a result of the common anode/cathode configuration.  I have already played around with a simple LED and have gotten it to light up in armbian linux buster on the A64 (by defining the pin as output etc. etc.) but that's all I've accomplished so far.  I have looked everywhere for the answer but this OS has much less information available than the pi or arduino (I have done several projects with arduino on an ESP8266).  How do I tell a command to repeat in any sort of time domain?  Are there any simple classes for learning how to code in armbian linux?  I would love to be pointed towards some resources but I have had a hard time really understanding ARM and how it differs/relates to languages like Python and C++.  I have learned some C++ and have a grasp on arduino code (same thing i guess.)  Is it possible to run an arduino script through linux and out of my A64?  ANY advice would be greatly appreciated and especially so with example code.  I wrote a basic list of steps for what I want the code to do below, but I don't know how to correctly do this.

 

define the four anode pins as inputs and label them D1,D2,D3,D4

define the seven cathodes and label as A through G

 

define the numbers (0-9) as they pertain to A-G so they can be called on rather than rewriting the pins needed every time

for example, the number 1 oon any digit (D1-D4) will be B and C pulled low and the required digit, say D4, pulled high.

 

This I am confident I can do but now theres the part I don't understand

 

for the number 1000, D1 will be a one and D2. D3 ,D4 will be zeroes 

in order to do this I need to momentarily flash the one on D1, then the zero on D2, then the next zero on D3 and then the next zero on D4 at a continuous rate (faster than 24 Hz)

so the flickering isn't noticeable to the human eye, and THIS is my question; how do I set this rate/frequency/period at which to loop the command and also how do I instruct the 

program to go from 1000 down to 1 at one second per full four digit number.  (counting down at 1 Hz, but each individual digit is flickering at, say, 100 Hz)

HOW TO DEFINE A FREQUENCY FOR INSTRUCTIONS EXITING THE GPIO PINS IS MY MAIN QUESTION.

 

Thank you to those who made it this far, sorry about my grammar.

 

I hope you are all staying positive and testing negative!

 

- Korben.

 

Link to comment
Share on other sites

What you need to understand is that linux is an operating system and armbian a distribution whereas your arduino-ide or nodemcu are development programs.

 

So :

- you should put that sort of question on a development forum

- you should learn the basics of operating system before trying to develop on your pine64 board

 

In practice, you should perhaps learn Python, which has functions like time.sleep() to add delays in a loop. But what you also need to understand is that it is more easy to handle GPIO on microcontrollers like Arduino than on an OS like linux, because you can do a loop in your arduino but you need to let your computer handle other tasks in an OS. 

 

You can do what you want in a lot of way, so there is no magic answer (*). You could perhaps learn the shell (bash) and try to handle gpio in bash, in order to better understand what your board is and it can do, the difference between programming with an event loop (that does your "sketch") and multitasking in processes.

 

Magic answer : python <your program> or bash <your program> and consult development forum for what you put in your program.

Link to comment
Share on other sites

@lanefu and @arox, thank you so much! I appreciate the fast replies and apologize for any clutter.  These are exactly the answers I was looking for and what I spent the most time trying to understand.  I will look at those libraries and get a better grasp of what Linux really is.  I am almost done with the Codecademy course (the free one) on Python 2 and so far it is wonderful and easy to use, i would love to run my python scripts in Linux if I can get them in there.  I don't want to break any rules here and I will move this discussion to development, but I had a few more questions: 

 

Will ALL code types work within Linux Armbian Buster (for example, python, C++, Ruby) with the right libraries?  I was mistaken that Linux was a language/IDE rather than a full blown OS (the bare-bones UX of Armbian Buster is new to me).  So from what I've gathered my next step is learning the basic Linux commands, seeing what I can accomplish with shell (bash) and learning how to implement programs written in Python or another language.  Is this a fair assessment or am I still misunderstanding the use of Linux?

 

From the bottom of my heart thank you for your time and thought, I really tried to find info but my global misunderstanding of Linux caused me to use the wrong terminology.  I can kill the thread or move it once I know where to place it, I know how messy things can get.

 

- Korben

Link to comment
Share on other sites

You will find every interpreter or compiler you may want for linux and most of them are probably already installed on your machine.

 

Just be aware :

- python2 and python3 coexistence may cause you some trouble because python2 is deprecated (no more supported) but was the default and used by many scripts. 

- when you become ambitious, you will need to download libraries and armbian is based on debian which is a long term release that may not propose the ultimate version.

 

So you can begin to develop as soon as you know some basic shell commands to edit and launch programs. But you will need to improve your skills in linux in order to manage libraries or debug you programs.

 

The arduino-ide is an all-in-one product that make "cut and paste" easy, compile, insert your app in a mini system with a boot-loader and an event loop, and upload everything on the board. With linux you are alone with your app : you must provide the loop, you must indicate what librairies you are using and where are the include files, you must find (and master) a good editor, you work directly on the target computer and don't **posses** the system and the boot-loader - so you will soon discover that you need to know how to kill a process (ctrl-C and "kill" command) and manage access rights (chmod +x <your program>).

 

Good luck ! You are on the right way as soon as you understand that "cut and paste" is not a good approach for doing things.

Link to comment
Share on other sites

@arox Thank you for following up, I've gotten Python (2) up and running and I'm working on getting .py files into the right places which is proving to be confusing.  I am wondering if I should switch to Python 3 so it doesn't bite me in the future.  Is the only syntax difference the parenthesis needed with strings, or is is recommended to use the most recent version to avoid any unlearning?  I'm sure Python 2 is still widely used, just curious.  So far this is the most fun I've had on a computer in ages, the level of control is exiting but also scary. 

 

Per @sgjava's recommendation I've been looking into java and installed it, I'm starting the codecademy Java course as I've just finished Python 2 (free).  I am not sure how to get things from github onto my SBC yet, should I use the get function with a download link?  I could use my flashdrive and move the file to sys I think, I'm not fully confident I can do that though.  I have yet to grasp the file system controls and what constitutes the Linux version of a ".dll".  Also, thank you for putting that code out there for us, once I manage to get it onto my SBC I'm going to try using this as a solution, along with a similar python script so I can learn both methods.

 

I suppose I could use a simple 555 timer and set the rate and duty cycle with the circuit plugged into a gpio input, which I have the resources to do (but not the coding knowledge), but my main goal here is to learn as much as I can about Linux, Python, Java and other languages that I can use to interface with physical systems later on.  So far I have also set up the ufw firewall thing, connected to my laptop and phone via SSH (wow it is much nicer controlling things from a laptop) and set up a large flash-drive as memory for libraries and other files (I only have a 4 Gb sd card and I don't know the full restrictions of using this type of memory).  Overall I am slowly moving forward.  All of your input has helped considerably, as support for this corner of programming seems to be sparse. 

 

Muchas Gracias, Danke Schon and Merci Beaucoup for your time everybody, I have a lot of work to do today and hopefully I will return with a success story.

 

- Korben

Link to comment
Share on other sites

9 minutes ago, Korben_Dallas said:

@arox Thank you for following up, I've gotten Python (2) up and running and I'm working on getting .py files into the right places which is proving to be confusing.  I am wondering if I should switch to Python 3 so it doesn't bite me in the future.  Is the only syntax difference the parenthesis needed with strings, or is is recommended to use the most recent version to avoid any unlearning?  I'm sure Python 2 is still widely used, just curious.  So far this is the most fun I've had on a computer in ages, the level of control is exiting but also scary. 

 

 

I don't like Python and don't use it much (and just python2). (Python2/Python3 coexistence is one of the reason). The **major** problem is that you will be forced some day to use Python3 because developers don't want to maintain Python2 and so you will fail in some silly library problems.

 

https://www.python.org/doc/sunset-python-2/

 

"As of January 1st, 2020 no new bug reports, fixes, or changes will be made to Python 2, and Python 2 is no longer supported. A few changes were made between ..."

 

I switched my default python interpreter because python2 does not handle proxy server as python3 and I couldn't install some software without rewriting the installer scripts (because of **latest** virtualenv mess). So be sure it will bite you at some time.

 

But you need to manage your priorities : if you want to use some gpio libraries or example for python2 and don't find python3 version, go on with it (as I do). Writing your own code is fun, debug other's code is a big problem !

 

And in your priorities you should place the basic usage of "apt" on top ... before even dreaming of copying files to the system.

Link to comment
Share on other sites

On 10/28/2020 at 5:03 AM, Korben_Dallas said:

I'd like to program a 7 segment display (four digits)

 

Personally, I would not drive a 4 digits 7 segments display directly from SBC GPIOs, I would rather use a small MCU like Atmel328 or STM32F103 which will do all the scanning easily and would attach it to SBC using I2C bus for example ...

Link to comment
Share on other sites

6 hours ago, Korben_Dallas said:

Per @sgjava's recommendation I've been looking into java and installed it, I'm starting the codecademy Java course as I've just finished Python 2 (free).  I am not sure how to get things from github onto my SBC yet, should I use the get function with a download link?  I could use my flashdrive and move the file to sys I think, I'm not fully confident I can do that though.  I have yet to grasp the file system controls and what constitutes the Linux version of a ".dll".  Also, thank you for putting that code out there for us, once I manage to get it onto my SBC I'm going to try using this as a solution, along with a similar python script so I can learn both methods.

 

I don't know how you are intend to use your pine64 board. I'm guessing you use window and connect via ssh. It's not the more efficient way to develop programs for the board, because you cannot use a graphic editor directly on the board. You could use tools like vnc or an Xserver (better but much more difficult) in order to display a desktop or windows from your linux board onto Windows, but if your goal is to learn programming and not system administration, your best choice would be to connect a screen and keyboard on the pine64 and install a nice text graphical editor (like : sudo apt install geany). 

 

When you work on linux, you rarely need to install shared libraries (.so files). You can install all the tools you need with the package manager and the command "sudo apt get" (about 50000 packages). You can download github  software with "git clone url" (sudo apt install git - if not already installed). When you login on your board, you are in your own directory (=folder) and it is also where file transfer programs will put the files. (The "sudo" command I mentioned above is needed to acquire rights for installing or breaking anything on the system). So you should forget about other files that the ones you write or download with "git" in your own directory, until you understand what you really need. ("sys" is not a real storage directory on linux)

 

If you transfer files between Windows and linux, be aware that text files like python script are made of lines separated by a "newline" (=linefeed) character on linux and a sequence newline/cariage-return on windows. That is why you would have better to edit your files directly with an editor on linux, and not transfer files with flashdrive, or network file transfer. 

Link to comment
Share on other sites

@Korben_DallasYou can use https://github.com/vsergeev/python-periphery if you want to use pure Python. I'd definitely stick with Python 3 and skip Python 2. I modeled my code for Java after the *-periphery stuff, so once you learn one you can do JVM languages (Java, Groovy, Scala, etc.), C, Python and Lua using a similar API. This probably gives you the most language choices out there for device IO stuff using a unified API. Please note python-periphery is a lot slower than the C or my Java equivalent. You'll find Python in general is slow, so it depends on what you are doing. Also using kernel device IO you code will port to most boards Armbian supports and many other OS distributions. In my mind you want cross OS, cross board and cross language compatibility.

 

As far as using a GUI IDE there's some hacks you can use. Under Ubuntu desktop (my dev PC) I just open a file window and use Ctrl+L and type ssh://board_ip to browse the dev board's file system. Obviously you can open files in Eclipse, NetBeans, etc. and dev source directly on the board. Just use a command shell to compile/run on the command line. Honestly though I just do all my dev on my PC and copy/compile the code on the target platform. There are some IDE solutions that will allow a seamless dev environment, but they are usually language and board specific. 

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...

Important Information

Terms of Use - Privacy Policy - Guidelines