66#include < M5Unified.h>
77#include " gob_unifiedButton.hpp"
88
9+ namespace
10+ {
11+ constexpr char labelA[] = " BtnA" ;
12+ constexpr char labelB[] = " BtnB" ;
13+ constexpr char labelC[] = " BtnC" ;
14+ constexpr const char * label_table[] = { labelA, labelB, labelC };
15+ constexpr int32_t olClr = lgfx::color565(64 ,64 ,64 );
16+ }
917namespace goblib
1018{
1119// class UnifiedButton
1220void UnifiedButton::begin (LovyanGFX* gfx, const appearance_t app)
1321{
14- _board = M5.getBoard ();
1522 assert (gfx);
1623 _gfx = gfx;
1724 _appearance = app;
1825 _dirty = true ;
1926 _font = gfx->getFont ();
20- createButtons (_appearance);
27+ _enable = M5.getBoard () == m5::board_t ::board_M5StackCoreS3 && M5.Touch .isEnabled ();
28+ _rotation = _gfx->getRotation ();
29+ if (_enable) { create_buttons (_appearance); }
2130}
2231
23- void UnifiedButton::createButtons (const appearance_t app)
32+ void UnifiedButton::create_buttons (const appearance_t app)
2433{
2534 int32_t w{_gfx->width () / 3 };
2635 int32_t h{32 };
@@ -41,19 +50,21 @@ void UnifiedButton::createButtons(const appearance_t app)
4150 top = h/2 ;
4251 break ;
4352 }
53+ for (uint_fast8_t i = 0 ; i < 3 ; ++i)
54+ {
55+ _btns[i].initButton (_gfx, left + w * i + w / 2 , top, w, h, olClr, TFT_DARKGRAY, TFT_BLACK,label_table[i]);
56+ }
57+ }
4458
45- constexpr int32_t olClr = lgfx::color565 (64 ,64 ,64 );
46-
47- _btns[0 ].initButton (_gfx, left + w * 0 + w / 2 , top, w, h, olClr, TFT_DARKGRAY, TFT_BLACK, " BtnA" );
48- _btns[1 ].initButton (_gfx, left + w * 1 + w / 2 , top, w, h, olClr, TFT_DARKGRAY, TFT_BLACK, " BtnB" );
49- _btns[2 ].initButton (_gfx, left + w * 2 + w / 2 , top, w, h, olClr, TFT_DARKGRAY, TFT_BLACK, " BtnC" );
50-
51- // M5_LOGI("[gob] change appearance_t:%02xH", app);
59+ void UnifiedButton::setRotation (const uint_fast8_t rot)
60+ {
61+ _rotation = rot & 0x07 ; // Valid values are [0...7]
62+ create_buttons (_appearance);
5263}
5364
5465void UnifiedButton::update ()
5566{
56- if (_board != m5:: board_t ::board_M5StackCoreS3 ) { return ; }
67+ if (!_enable ) { return ; }
5768
5869 // Processes buttons in the same way as they are processed in Core2
5970 auto ms = m5gfx::millis ();
@@ -72,15 +83,31 @@ void UnifiedButton::update()
7283 if (M5.BtnC .isPressed ()) { btn_bits |= 1 << 2 ; }
7384 if (btn_bits || !(det.state & m5::touch_state_t ::mask_moving))
7485 {
86+ // Correct coordinates to match rotation.
87+ auto x = raw.x ;
88+ auto y = raw.y ;
89+ const uint32_t rot = _gfx->getRotation ();
90+ const auto wid = _gfx->width ();
91+ const auto hgt = _gfx->height ();
92+
93+ if (!(rot & 1 )) { std::swap (x, y); }
94+ const auto rot4 = rot ^ ((((rot >> 2 ) ^ 1 ) - 1 ) & 0x07 ); // 0-3 => 0-3, 4-7 => 3-0
95+ switch (rot4)
96+ {
97+ case 0 : x = wid - x - 1 ; break ;
98+ case 2 : y = hgt - y - 1 ; break ;
99+ case 3 : x = wid - x - 1 ; y = hgt - y - 1 ; break ;
100+ default : break ;
101+ }
102+
75103 for (int i=0 ; i<3 ; ++i)
76104 {
77- btn_bits |= (_btns[i].contains (raw. x , raw. y ) << i);
105+ btn_bits |= (_btns[i].contains (x, y) << i);
78106 }
79107 _press_bits = btn_bits;
80108 }
81109 }
82110 }
83-
84111 // Set status to M5.BtnX
85112 M5.BtnA .setRawState (ms, btn_bits & 1 );
86113 M5.BtnB .setRawState (ms, btn_bits & 2 );
@@ -91,15 +118,13 @@ void UnifiedButton::update()
91118
92119void UnifiedButton::draw (const bool force)
93120{
94- if (_board != m5::board_t ::board_M5StackCoreS3) { return ; }
95-
96- if (!(_appearance & appearance_t ::transparent_bottom) && _show && (_dirty || force))
121+ if (_enable && !(_appearance & appearance_t ::transparent_bottom) && _show && (_dirty || force))
97122 {
98123 auto gfont = _gfx->getFont ();
99124 _gfx->setFont (_font);
100125
101126 _dirty = false ;
102- for (int i=0 ; i< 3 ; ++i)
127+ for (int i=0 ; i < 3 ; ++i)
103128 {
104129 _btns[i].drawButton (_press_bits & (1 <<i));
105130 }
0 commit comments