Skip to content

Commit

Permalink
Merge pull request #8 from brainelectronics/feature/add-support-for-p…
Browse files Browse the repository at this point in the history
…osition-functions

Add support for position functions
  • Loading branch information
brainelectronics authored Jul 29, 2022
2 parents e3aa7d0 + 2651641 commit ec2d5c1
Show file tree
Hide file tree
Showing 11 changed files with 174 additions and 16 deletions.
19 changes: 18 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@ r"^\#\# \[\d{1,}[.]\d{1,}[.]\d{1,}\] \- \d{4}\-\d{2}-\d{2}$"
-->

## Released
## [0.6.0] - 2022-07-29
### Added
- Support `Get_place_xcen`, `Set_place_xcen`, `Get_place_ycen` and
`Set_place_ycen` by new class `CommonPositionMixin` in
[`common`](nextion/common.py)

### Changed
- Inherit from `CommonPositionMixin` in
- Button
- Dual state button
- Number
- Text

### Fixed
- Remove unused imports in nextion elements

## [0.5.0] - 2022-07-29
### Added
- Support `Get_background_color_bco` and `Set_background_color_bco` by new
Expand Down Expand Up @@ -92,8 +108,9 @@ r"^\#\# \[\d{1,}[.]\d{1,}[.]\d{1,}\] \- \d{4}\-\d{2}-\d{2}$"
- [Example HMI file](examples/everything.HMI) to be used for all examples

<!-- Links -->
[Unreleased]: https://github.com/brainelectronics/micropython-nextion/compare/0.5.0...develop
[Unreleased]: https://github.com/brainelectronics/micropython-nextion/compare/0.6.0...develop

[0.6.0]: https://github.com/brainelectronics/micropython-nextion/tree/0.6.0
[0.5.0]: https://github.com/brainelectronics/micropython-nextion/tree/0.5.0
[0.4.0]: https://github.com/brainelectronics/micropython-nextion/tree/0.4.0
[0.3.0]: https://github.com/brainelectronics/micropython-nextion/tree/0.3.0
Expand Down
22 changes: 22 additions & 0 deletions examples/button/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
button_text = "btn txt"
background_color_value = 63488 # red
font_color_value = 31 # blue
x_offset = 20
y_offset = 20

# request the text of button "b0"
print('Requesting button "{}" text ...'.format(b0.name))
Expand Down Expand Up @@ -116,6 +118,26 @@
if response != font_color_value:
print('WARNING: GET value did not match SET value')

time.sleep(1)

# request the x/y position of button "b0" again
print('Requesting x/y position of button "{}" ...'.format(b0.name))
x_position = b0.Get_place_xcen()
y_position = b0.Get_place_ycen()
print('Position of button "{}" is: "x={}", "y={}"'.
format(b0.name, x_position, y_position))
print()

x_position += x_offset
y_position += y_offset

# modify the x/y position of button "b0"
print('Set x/y position of button "{}" to "x={}", "y={}"'.
format(b0.name, x_position, y_position))
b0.Set_place_xcen(x_position)
b0.Set_place_ycen(y_position)
print()

print('Returning to REPL in 5 seconds')

# wait for 5 more seconds to safely finish the may still running threads
Expand Down
24 changes: 23 additions & 1 deletion examples/dual_button/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
# new values for dual state button
button_text = "btn txt"
button_value = 1 # 1 is active (green), 0 is inactive (grey)
font_color_value = 31 # blue
font_color_value = 31 # blue
x_offset = 20
y_offset = 20

# request the text of dual state button "bt0"
print('Requesting dual state button "{}" text ...'.format(bt0.name))
Expand Down Expand Up @@ -108,6 +110,26 @@
if response != font_color_value:
print('WARNING: GET value did not match SET value')

time.sleep(1)

# request the x/y position of dual state button "bt0" again
print('Requesting x/y position of dual state button "{}" ...'.format(bt0.name))
x_position = bt0.Get_place_xcen()
y_position = bt0.Get_place_ycen()
print('Position of dual state button "{}" is: "x={}", "y={}"'.
format(bt0.name, x_position, y_position))
print()

x_position += x_offset
y_position += y_offset

# modify the x/y position of dual state button "bt0"
print('Set x/y position of dual state button "{}" to "x={}", "y={}"'.
format(bt0.name, x_position, y_position))
bt0.Set_place_xcen(x_position)
bt0.Set_place_ycen(y_position)
print()

print('Returning to REPL in 5 seconds')

# wait for 5 more seconds to safely finish the may still running threads
Expand Down
22 changes: 22 additions & 0 deletions examples/number/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
number_value = randint(1, 100)
background_color_value = 63488 # red
font_color_value = 31 # blue
x_offset = 20
y_offset = 20

# request the value of number "n0"
print('Requesting number "{}" value ...'.format(n0.name))
Expand Down Expand Up @@ -117,6 +119,26 @@
if response != font_color_value:
print('WARNING: GET value did not match SET value')

time.sleep(1)

# request the x/y position of number "n0" again
print('Requesting x/y position of number "{}" ...'.format(n0.name))
x_position = n0.Get_place_xcen()
y_position = n0.Get_place_ycen()
print('Position of number "{}" is: "x={}", "y={}"'.
format(n0.name, x_position, y_position))
print()

x_position += x_offset
y_position += y_offset

# modify the x/y position of number "n0"
print('Set x/y position of number "{}" to "x={}", "y={}"'.
format(n0.name, x_position, y_position))
n0.Set_place_xcen(x_position)
n0.Set_place_ycen(y_position)
print()

print('Returning to REPL in 5 seconds')

# wait for 5 more seconds to safely finish the may still running threads
Expand Down
22 changes: 22 additions & 0 deletions examples/text/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
textfield_text = "other txt"
background_color_value = 63488 # red
font_color_value = 31 # blue
x_offset = 20
y_offset = 20

# request the text of textfield "t0"
print('Requesting textfield "{}" text ...'.format(t0.name))
Expand Down Expand Up @@ -116,6 +118,26 @@
if response != font_color_value:
print('WARNING: GET value did not match SET value')

time.sleep(1)

# request the x/y position of textfield "t0" again
print('Requesting x/y position of textfield "{}" ...'.format(t0.name))
x_position = t0.Get_place_xcen()
y_position = t0.Get_place_ycen()
print('Position of textfield "{}" is: "x={}", "y={}"'.
format(t0.name, x_position, y_position))
print()

x_position += x_offset
y_position += y_offset

# modify the x/y position of textfield "t0"
print('Set x/y position of textfield "{}" to "x={}", "y={}"'.
format(t0.name, x_position, y_position))
t0.Set_place_xcen(x_position)
t0.Set_place_ycen(y_position)
print()

print('Returning to REPL in 5 seconds')

# wait for 5 more seconds to safely finish the may still running threads
Expand Down
59 changes: 59 additions & 0 deletions nextion/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,65 @@ def setFont(self, number: int) -> bool:
return self._nh.recvRetCommandFinished()


class CommonPositionMixin(object):
"""docstring for CommonPositionMixin"""
def Get_place_xcen(self) -> int:
"""
Get xcen attribute of component
:returns: The x position
:rtype: int
"""
cmd = "get {}.xcen".format(self.name)
self._nh.sendCommand(cmd)
sleep(0.1) # necessary, data might not be available otherwise
return self._nh.recvRetNumber()

def Set_place_xcen(self, number: int) -> bool:
"""
Get xcen attribute of component
:param number: The new x position
:type number: int
:returns: True on success, false otherwise
:rtype: bool
"""
cmd = "{}.xcen={}".format(self.name, number)
self._nh.sendCommand(cmd)
cmd = "ref {}".format(self.name)
self._nh.sendCommand(cmd)
return self._nh.recvRetCommandFinished()

def Get_place_ycen(self) -> int:
"""
Get ycen attribute of component
:returns: The y position
:rtype: int
"""
cmd = "get {}.ycen".format(self.name)
self._nh.sendCommand(cmd)
sleep(0.1) # necessary, data might not be available otherwise
return self._nh.recvRetNumber()

def Set_place_ycen(self, number: int) -> bool:
"""
Get ycen attribute of component
:param number: The new y position
:type number: int
:returns: True on success, false otherwise
:rtype: bool
"""
cmd = "{}.ycen={}".format(self.name, number)
self._nh.sendCommand(cmd)
cmd = "ref {}".format(self.name)
self._nh.sendCommand(cmd)
return self._nh.recvRetCommandFinished()


class CommonTextMixin(object):
"""docstring for CommonTextMixin"""
def getText(self) -> str:
Expand Down
4 changes: 2 additions & 2 deletions nextion/nextion_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
"""

# custom packages
from .common import Common, CommonBackgroundColorMixin, CommonFontMixin, CommonTextMixin
from .common import Common, CommonBackgroundColorMixin, CommonFontMixin, CommonPositionMixin, CommonTextMixin


class NexButtonError(Exception):
"""Base class for exceptions in this module."""
pass


class NexButton(Common, CommonBackgroundColorMixin, CommonFontMixin, CommonTextMixin):
class NexButton(Common, CommonBackgroundColorMixin, CommonFontMixin, CommonPositionMixin, CommonTextMixin):
"""docstring for NexButton"""
def __init__(self, nh, pid: int, cid: int, name: str) -> None:
"""
Expand Down
7 changes: 2 additions & 5 deletions nextion/nextion_dual_state_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,16 @@
Functions to interact with a Nextion dual state Button element
"""

# system packages
from time import sleep

# custom packages
from .common import Common, CommonFontMixin, CommonTextMixin, CommonValueMixin
from .common import Common, CommonFontMixin, CommonPositionMixin, CommonTextMixin, CommonValueMixin


class NexDSButtonError(Exception):
"""Base class for exceptions in this module."""
pass


class NexDSButton(Common, CommonFontMixin, CommonTextMixin, CommonValueMixin):
class NexDSButton(Common, CommonFontMixin, CommonPositionMixin, CommonTextMixin, CommonValueMixin):
"""docstring for NexDSButton"""
def __init__(self, nh, pid: int, cid: int, name: str) -> None:
"""
Expand Down
4 changes: 2 additions & 2 deletions nextion/nextion_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
"""

# custom packages
from .common import Common, CommonBackgroundColorMixin, CommonFontMixin, CommonValueMixin
from .common import Common, CommonBackgroundColorMixin, CommonFontMixin, CommonPositionMixin, CommonValueMixin


class NexNumberError(Exception):
"""Base class for exceptions in this module."""
pass


class NexNumber(Common, CommonBackgroundColorMixin, CommonFontMixin, CommonValueMixin):
class NexNumber(Common, CommonBackgroundColorMixin, CommonFontMixin, CommonPositionMixin, CommonValueMixin):
"""docstring for NexNumber"""
def __init__(self, nh, pid: int, cid: int, name: str) -> None:
"""
Expand Down
4 changes: 2 additions & 2 deletions nextion/nextion_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
"""

# custom packages
from .common import Common, CommonBackgroundColorMixin, CommonFontMixin, CommonTextMixin
from .common import Common, CommonBackgroundColorMixin, CommonFontMixin, CommonPositionMixin, CommonTextMixin


class NexTextError(Exception):
"""Base class for exceptions in this module."""
pass


class NexText(Common, CommonBackgroundColorMixin, CommonFontMixin, CommonTextMixin):
class NexText(Common, CommonBackgroundColorMixin, CommonFontMixin, CommonPositionMixin, CommonTextMixin):
"""docstring for NexText"""
def __init__(self, nh, pid: int, cid: int, name: str) -> None:
"""
Expand Down
3 changes: 0 additions & 3 deletions nextion/nextion_waveform.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
Functions to interact with a Nextion Waveform element
"""

# system packages
from time import sleep

# custom packages
from .common import Common, CommonBackgroundColorMixin

Expand Down

0 comments on commit ec2d5c1

Please sign in to comment.