Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

7 seg displays #485

Closed
martinohanlon opened this issue Oct 17, 2016 · 13 comments
Closed

7 seg displays #485

martinohanlon opened this issue Oct 17, 2016 · 13 comments

Comments

@martinohanlon
Copy link
Contributor

Im going to be using a 7 seg display in an upcoming project, so I thought it was time it was added to gpiozero, can I get some comments on my thinking so far:

  • class name of 7SegmentDisplay
  • extending LEDCollection
  • overwriting value to take natural values of numbers and letters to display on the 7 seg display i.e. 7seg.value = "3" would display a 3, likewise 7seg.value = "a" would display A.

Next step would be to create a multi digit 7 segment display. The way these work (generally) is that in addition to the 8 pins for the display they have additional pins to state which digit should be turned on. To make each digit show a different number you would use plexing to flash individual digits to give the appearance of a multi digit display.

@lurch
Copy link
Contributor

lurch commented Oct 17, 2016

See also #357
I haven't got round to looking through the sample code posted there myself yet.

@martinohanlon
Copy link
Contributor Author

Thanks, I did a search for 'seg' on the issues too and didnt find any.

@bennuttall
Copy link
Member

7SegmentDisplay is not a valid class name in Python, and 7seg is not a valid object name :P

I'm not sure about seg.value = "a" because .value should be boolean or float to be compatible with the rest of the library.

Other than that, sounds good. Do various 7seg displays tend to work the same way?

@lurch
Copy link
Contributor

lurch commented Oct 17, 2016

I'm not sure about seg.value = "a" because .value should be boolean or float to be compatible with the rest of the library.

Yeah, that was my gut reaction too - glad to see I wasn't alone ;-)
But it then begs the question, how do we map the LEDs illuminated on a 7-segment display (or 8-segment if it also contains a decimal-point) to a single .value between 0 and 1 (or -1 and +1)? There doesn't seem to be any obvious solution...
The only thing I can think of is to treat the 8-segments as elements of a bit-field, and than divide by 256.0 to get a floating-point value (so all segments off would be 0.0; all segments on would be 1.0; and segments 'a' and 'c' turned on would be ((2**0 + 2**2) / 256.0) = ((1 + 4) / 256.0) = 0.01953125). So we could then do seg2.source = seg1.values, and it'd work as expected, provided seg1 and seg2 were initialised with their segments in the same order.

But to maintain ease-of-use, we'd also want some kind of .character property, where you could do seg.character = 3 (with the int automatically being converted to str) or seg.character = "A".
Or perhaps seg.number = 3 and seg.hexnumber = 0xa? shrug

P.S. Another approach would be to treat SevenSegmentDisplay as a CompositeOutputDevice subclass, and have .value be a tuple of bools (so my earlier segments a and c example would be (False, False, False, False, False, True, False, True)). double shrug

@martinohanlon
Copy link
Contributor Author

I'm not sure about seg.value = "a" because .value should be boolean or float to be compatible with the rest of the library.

Yeah I thought it was a bit weird, one of the reasons I asked for comment. Less weird would be an 8 bit field, represented as 0 - 1.

It should definitely have a 'friendly' method for showing a number/character how about .display?

@bennuttall
Copy link
Member

.display would work.

It might make sense to have .value as an n-tuple, like (I assume) LEDCollection already is.

@martinohanlon
Copy link
Contributor Author

Thought about it a bit:

Inheriting LEDCollection will bring CompositeOutputDevice and value will be a tuple of booleans.

A couple of methods:

  • display(char)
  • decimal_point(bool)

which would obviously set the tuple in the .value property

@lurch
Copy link
Contributor

lurch commented Oct 17, 2016

Just having had another look at the docs, I wonder if it would make more sense to just inherit from LEDBoard, rather than LEDCollection? And then add the display and decimal_point methods that you mention. Being a geeky programmer type, I'd love there to be a hexdisplay function too ;-)

I'll try to have a more in-depth look at #357 this evening, while these ideas are fresh in my mind.

@dglaude
Copy link
Contributor

dglaude commented Oct 17, 2016

I vote for hex too.

On 17 Oct 2016 17:10, "Andrew Scheller" notifications@github.com wrote:

Just having had another look at the docs
http://gpiozero.readthedocs.io/en/v1.3.1/api_boards.html#ledboard, I
wonder if it would make more sense to just inherit from LEDBoard, rather
than LEDCollection? And then add the display and decimal_point methods
that you mention. Being a geeky programmer type, I'd love there to be a
hexdisplay function too ;-)

I'll try to have a more in-depth look at #357
#357 this evening,
while these ideas are fresh in my mind.


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#485 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/ASiRnOxKFhG3yehJrxRNx-Bpuy-FKZw4ks5q04__gaJpZM4KYjMj
.

@martinohanlon
Copy link
Contributor Author

First stab in the dark at SevenSegmentDisplay:

It needs a display_hex function.

Some thoughts:

  • Should pins be passed in as a, b, c, d, e, f, g and constructed into a
  • Are layouts associated to the class the 'right' thing to do?
  • Common anode / cathode is supported by the active_high property, this could be changed to common_cathode to make it more descriptive
from gpiozero import LEDCollection, LEDBoard
from time import sleep

class SevenSegmentDisplay(LEDBoard):
    layouts = {"1": (False, True, True, False, False, False, False),
        "2": (True, True, False, True, True, False, True),
        "3": (True, True, True, True, False, False, True),
        "4": (False, True, True, False, False, True, True),
        "5": (True, False, True, True, False, True, True),
        "6": (True, False, True, True, True, True, True),
        "7": (True, True, True, False, False, False, False),
        "8": (True, True, True, True, True, True, True),
        "9": (True, True, True, True, False, True, True),
        "0": (True, True, True, True, True, True, False),
        " ": (False, False, False, False, False, False, False),
        "A": (True, True, True, False, True, True, True),
        "B": (True, True, True, True, True, True, True),
        "C": (True, False, False, True, True, True, False),
        "D": (False, True, True, True, True, False, True),
        "E": (True, False, False, True, True, True, True),
        "F": (True, False, False, False, True, True, True),
        "G": (True, False, True, True, True, True, False),
        "H": (False, True, True, False, True, True, True)}

    def __init__(self, *pins, **kwargs):
        # 7 segment displays must have 8 pins
        if len(pins) < 7 or len(pins) > 8:
            raise ValueError('7 segment display must have 7 or 8 pins')
        # Don't allow 7 segments to contain collections
        for pin in pins:
            assert not isinstance(pin, LEDCollection)
        pwm = kwargs.pop('pwm', False)
        active_high = kwargs.pop('active_high', True)
        super(SevenSegmentDisplay, self).__init__(*pins, pwm=pwm, active_high=active_high)

    def display(self, char):
        char = char.upper()
        if char in SevenSegmentDisplay.layouts.keys():
            layout = SevenSegmentDisplay.layouts[char]
            for led in range(0,7):
                self[led].value = layout[led]

    def decimal_point(self, on):
        # does the 7seg display have a decimal point (i.e pin 8)
        if len(self) > 7:
            self[7].value = on

@lurch
Copy link
Contributor

lurch commented Oct 18, 2016

Looking good...

Some comments: (apologies if there's too much here!)

  • I guess "must have 8 pins" was supposed to be "must have 7 or 8 pins"
  • Your "8" layout is identical to "B" - IMHO you should use the 'standard' hex-letters https://en.wikipedia.org/wiki/Seven-segment_display#Displaying_letters
  • I guess you could add other letters like "-"
  • GpioZero code generally seems to prefer single-quotes to double-quotes for creating strings
  • Personally I'd insert a newline after the first opening bracket of the layouts dict
  • Perhaps the constructor should check for left-over kwargs, like LEDBarGraph does https://github.com/RPi-Distro/python-gpiozero/blob/master/gpiozero/boards.py#L498
  • IMHO the display method should throw an exception if you try to show an invalid char, which is easiest to do if you just remove the if char in SevenSegmentDisplay.layouts.keys(): line
  • I wonder if the display method should clear the decimal point? Perhaps display could have a clear_decimal_point=True parameter?
  • Perhaps the value-setter should be over-ridden, so that value can always be set from a 7-tuple or an 8-tuple, regardless of whether this 7-segment has a dp or not (with the dp being ignored as necessary) ? This woud make it easier to chain source and values.
  • With the previous change in place, I believe for led in range(0,7): self[led].value = layout[led] could then be replaced by self.value = layout
  • Your example code doesn't need the from time import sleep ;-)
  • range(0,7) is the same as range(7) ;-)
  • if x in somedict.keys(): is the same as if x in somedict:
  • It might be worth upper-casing LAYOUTS to make it clear it's a constant (unless we decide to allow the user to add their own layouts, in which case it should remain lower-case)
  • If you changed char = char.upper() to char = str(char).upper() then it'd also work with ints
  • After converting char to a str, you should check that it has a len of 1, and throw a ValueError otherwise
  • Maybe decimal_point could be made a (gettable and settable) property?

Common anode / cathode is supported by the active_high property, this could be changed to common_cathode to make it more descriptive

For consistency, it should remain as active_high. You could always enhance the active_high docstring to make it clearer, as RGBLED does.

It needs a display_hex function.

That's easy:

    def display_hex(self, hexnumber):
        self.display(hex(hexnumber)[2:])

Should pins be passed in as a, b, c, d, e, f, g...

There's part of me that still wonders if that's the best approach (see my comments in #357 (comment) ) as that way it'd create a namedtuple, so you could do sevenseg.b.on() in addition to sevenseg[1].on()

@martinohanlon
Copy link
Contributor Author

@lurch Thanks for the comments, they are really useful.

@martinohanlon
Copy link
Contributor Author

Closing issue, PR #488 submitted.

@bennuttall bennuttall removed this from In progress in Roadmap Mar 28, 2017
crs-k pushed a commit to crs-k/pwnagotchi-scoreboard that referenced this issue Feb 17, 2024
Bumps [gpiozero](https://github.com/gpiozero/gpiozero) from 1.5.1 to
2.0.1.
<details>
<summary>Changelog</summary>
<p><em>Sourced from <a
href="https://github.com/gpiozero/gpiozero/blob/master/docs/changelog.rst">gpiozero's
changelog</a>.</em></p>
<blockquote>
<h1>Release 2.0.1 (2024-02-15)</h1>
<ul>
<li>Fixed Python 3.12 compatibility, and clarify that 3.9 is the lowest
supported
version in our CI configuration
(<code>[#1113](https://github.com/gpiozero/gpiozero/issues/1113)</code>_)</li>
</ul>
<p>.. _<a
href="https://redirect.github.com/gpiozero/gpiozero/issues/1113">#1113</a>:
<a
href="https://redirect.github.com/gpiozero/gpiozero/issues/1113">gpiozero/gpiozero#1113</a></p>
<h1>Release 2.0 (2023-09-12)</h1>
<ul>
<li>Removed Python 2.x support; many thanks to Fangchen Li for a
substantial
amount of work on this!
(<code>[#799](https://github.com/gpiozero/gpiozero/issues/799)</code>_
<code>[#896](https://github.com/gpiozero/gpiozero/issues/896)</code>_)</li>
<li>Removed RPIO pin implementation</li>
<li>Made :class:<code>gpiozero.pins.lgpio.LGPIOFactory</code> the
default factory; the
former default,
:class:<code>gpiozero.pins.rpigpio.RPiGPIOFactory</code>, is now the
second place preference</li>
<li>Added :doc:<code>compat</code> chapter</li>
<li>Added :program:<code>pintest</code> utility</li>
<li>Added Raspberry Pi 5 board data</li>
</ul>
<p>.. _<a
href="https://redirect.github.com/gpiozero/gpiozero/issues/799">#799</a>:
<a
href="https://redirect.github.com/gpiozero/gpiozero/issues/799">gpiozero/gpiozero#799</a>
.. _<a
href="https://redirect.github.com/gpiozero/gpiozero/issues/896">#896</a>:
<a
href="https://redirect.github.com/gpiozero/gpiozero/issues/896">gpiozero/gpiozero#896</a></p>
<h1>Release 1.6.2 (2021-03-18)</h1>
<ul>
<li>Correct docs referring to 1.6.0 as the last version supporting
Python 2</li>
</ul>
<p>.. warning::</p>
<pre><code>This is the last release to support Python 2
</code></pre>
<h1>Release 1.6.1 (2021-03-17)</h1>
<ul>
<li>Fix missing font files for 7-segment displays</li>
</ul>
<h1>Release 1.6.0 (2021-03-14)</h1>
<ul>
<li>Added :class:<code>RotaryEncoder</code> class (thanks to Paulo
Mateus)
(<code>[#482](https://github.com/gpiozero/gpiozero/issues/482)</code><em>,
<code>[#928](https://github.com/gpiozero/gpiozero/issues/928)</code></em>)</li>
<li>Added support for multi-segment character displays with
:class:<code>LEDCharDisplay</code> and
:class:<code>LEDMultiCharDisplay</code> along with &quot;font&quot;
support using :class:<code>LEDCharFont</code> (thanks to Martin
O'Hanlon)
(<code>[#357](https://github.com/gpiozero/gpiozero/issues/357)</code><em>,

<code>[#485](https://github.com/gpiozero/gpiozero/issues/485)</code></em>,
<code>[#488](https://github.com/gpiozero/gpiozero/issues/488)</code><em>,
<code>[#493](https://github.com/gpiozero/gpiozero/issues/493)</code></em>,
<code>[#930](https://github.com/gpiozero/gpiozero/issues/930)</code>_)</li>
<li>Added :class:<code>Pibrella</code> class (thanks to Carl Monk)
(<code>[#773](https://github.com/gpiozero/gpiozero/issues/773)</code><em>,
<code>[#798](https://github.com/gpiozero/gpiozero/issues/798)</code></em>)</li>
<li>Added :class:<code>TrafficpHat</code> class (thanks to Ryan
Walmsley)
(<code>[#845](https://github.com/gpiozero/gpiozero/issues/845)</code><em>,
<code>[#846](https://github.com/gpiozero/gpiozero/issues/846)</code></em>)</li>
</ul>
<!-- raw HTML omitted -->
</blockquote>
<p>... (truncated)</p>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a
href="https://github.com/gpiozero/gpiozero/commit/a13848bd9701844c139709750cfa038c59b2ab5f"><code>a13848b</code></a>
Run copyrights script for release</li>
<li><a
href="https://github.com/gpiozero/gpiozero/commit/3ee415cbfd5de07324696ceadd22ded92c48a517"><code>3ee415c</code></a>
Bump revision and add changelog for release</li>
<li><a
href="https://github.com/gpiozero/gpiozero/commit/ebf8874bd71d353ebb5f83dcb1fdbc6be58b454d"><code>ebf8874</code></a>
Fix <a
href="https://redirect.github.com/gpiozero/gpiozero/issues/1113">#1113</a></li>
<li><a
href="https://github.com/gpiozero/gpiozero/commit/fa114311048c052ea1b6810c235cc0f858720f36"><code>fa11431</code></a>
Set 3.9 as base version, fix 3.12 compatibility</li>
<li><a
href="https://github.com/gpiozero/gpiozero/commit/981e3f70b00853fd011baf8cd1cb17f241e4ab65"><code>981e3f7</code></a>
Merge pull request <a
href="https://redirect.github.com/gpiozero/gpiozero/issues/1104">#1104</a>
from lurch/patch-2</li>
<li><a
href="https://github.com/gpiozero/gpiozero/commit/0f09a5a1d0c3b0055cdfe52de3009deea30f8654"><code>0f09a5a</code></a>
Small docs typo</li>
<li><a
href="https://github.com/gpiozero/gpiozero/commit/e59c55c77bdd635a566ff31815e93a51d54907e8"><code>e59c55c</code></a>
Workaround rtd's ancient flag</li>
<li><a
href="https://github.com/gpiozero/gpiozero/commit/025ba5bc2f0b7a0936afc792345a0615586f6b61"><code>025ba5b</code></a>
Would help if I specified the right requirement...</li>
<li><a
href="https://github.com/gpiozero/gpiozero/commit/cba887aa60d3c844ea7e36f8c4d170fd9073d87c"><code>cba887a</code></a>
RTD builds &quot;old&quot; projects with sphinx 1.8</li>
<li><a
href="https://github.com/gpiozero/gpiozero/commit/390caf61ba73af65f39395551c25674631eca7d0"><code>390caf6</code></a>
The root doc default changed to 'contents'</li>
<li>Additional commits viewable in <a
href="https://github.com/gpiozero/gpiozero/compare/v1.5.1...v2.0.1">compare
view</a></li>
</ul>
</details>
<br />


[![Dependabot compatibility
score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=gpiozero&package-manager=pip&previous-version=1.5.1&new-version=2.0.1)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself. You can also trigger a rebase manually by commenting
`@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after
your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge
and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating
it. You can achieve the same result by closing it manually
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop
Dependabot creating any more for this major version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop
Dependabot creating any more for this minor version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop
Dependabot creating any more for this dependency (unless you reopen the
PR or upgrade to it yourself)


</details>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants