Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ target_sources(${NAME} INTERFACE
${CMAKE_CURRENT_LIST_DIR}/src/widgets/text_widget.cpp
${CMAKE_CURRENT_LIST_DIR}/src/widgets/qrcode_widget.cpp
${CMAKE_CURRENT_LIST_DIR}/src/drivers/button.cpp
# ${CMAKE_CURRENT_LIST_DIR}/src/drivers/led.cpp
${CMAKE_CURRENT_LIST_DIR}/src/drivers/ir.cpp
${CMAKE_CURRENT_LIST_DIR}/src/drivers/led.cpp
${CMAKE_CURRENT_LIST_DIR}/src/drivers/rp2040/sio_pin.cpp
${CMAKE_CURRENT_LIST_DIR}/src/drivers/rp2040/gpio_pin.cpp
)
Expand All @@ -51,4 +52,5 @@ include(drivers/button/button)
target_link_libraries(${NAME} INTERFACE
pico_display
pico_explorer
hardware_gpio
)
1 change: 1 addition & 0 deletions include/drivers/button.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class MyButton : public RP2040::SioPin{

struct ButtonEvent{
ButtonEventType type;
absolute_time_t event_time;
MyButton *button;
};

Expand Down
24 changes: 20 additions & 4 deletions include/drivers/ir.hpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
#pragma once

#include <cstdint>
#include "drivers/rp2040/sio_pin.hpp"
#include "pico/time.h"
#include "queue.hpp"

namespace IR
{
class LED{

class LED : public RP2040::SioPin{
public:
LED(uint pin);
~LED(){}
protected:
// void init() override;

};
class Detector{
class Detector : public RP2040::SioPin{
public:
Detector(uint pin);
~Detector(){}
// void init() override;
protected:
void onInterrupt(uint32_t flags) override;
private:
absolute_time_t prev_time;
long prev_time_diff_us;
bool prev_state;
bool state;
};
} // namespace IR
13 changes: 13 additions & 0 deletions include/drivers/rp2040/pwm_pin.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include "gpio_pin.hpp"

namespace RP2040
{
class PwmPin : public GpioPin{
public:
PwmPin(uint pin, Polarity pol) : GpioPin{pin, pol}{
setFunc(GPIO_FUNC_PWM);
}
};
} // namespace RP2040
20 changes: 19 additions & 1 deletion include/gui/drawable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,26 @@ namespace gui
const Size2& getSize() const { return m_box.size; }
const Size2& getRequiredSize() const { return m_requiredSize; }
bool canGrow() const { return m_grow; }
bool canGrowH() const { return m_growH; }
bool canGrowV() const { return m_growV; }
void setGrow(bool grow) {
if(m_grow == grow)
return;
m_grow = grow;
requiresRelayout();
}
void setGrowH(bool grow) {
if(m_growH == grow)
return;
m_growH = grow;
requiresRelayout();
}
void setGrowV(bool grow) {
if(m_growV == grow)
return;
m_growV = grow;
requiresRelayout();
}

void setPos(const Point2 &pos) { setBox({pos, m_box.size}); }
void setSize(const Size2 &size) { setBox({m_box.pos, size}); }
Expand Down Expand Up @@ -51,7 +65,10 @@ namespace gui
* returns false on success
* returns true if size requirements changed!
*/
virtual bool relayout(){ return false; };
virtual bool relayout(){
printf("default relayout (%p)\n", this);
return false;
};
void redrawDone() { m_needsRedrawing = false; m_childNeedsRedrawing = false;}
void relayoutDone() {
m_needsRelayout = false;
Expand Down Expand Up @@ -107,6 +124,7 @@ namespace gui
Rectangle m_box{};
Size2 m_requiredSize{};
bool m_grow = false;
bool m_growH = false, m_growV = false;

};
} // namespace gui
10 changes: 6 additions & 4 deletions include/gui/view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,19 @@ namespace gui
int start_timer(long timeout_us);

View *getParentView() const {
return static_cast<View*>(getParent());
// return static_cast<View*>(getParent());
return parentView;
}
void setParentView(View *v) {
this->setParent(v);
// this->setParent(v);
parentView = v;
}
View *getChildView() const {return childView; }
void setChildView(View* view) {
printf("[VIEW] (%p)->setChildView(%p)\n", this, view);
childView = view;
if(childView)
childView->setParent(this);
childView->setParentView(this);
}

void closeView();
Expand All @@ -60,7 +62,7 @@ namespace gui
static int64_t timer_handler(alarm_id_t, void *data);


View *childView;
View *childView, *parentView;
Widget *childWidgets = nullptr;
Color m_backgroundColor;

Expand Down
9 changes: 9 additions & 0 deletions include/gui/view_controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ class ViewController;
}
#include "gui/view.hpp"
#include "gui/common.hpp"
#include "gui/widgets/divider_widget.hpp"
#include "gui/widgets/spacer_widget.hpp"
#include "gui/widgets/text_widget.hpp"

namespace gui{

Expand All @@ -29,5 +32,11 @@ namespace gui{
gui::Graphics graphics;
View *current_view = nullptr;
View *next_view = nullptr;
struct{
VDividerWidget div;
HDividerWidget bar;
SpacerWidget sp;
TextWidget txt;
}ui;
};
} // namespace gui
29 changes: 22 additions & 7 deletions include/gui/widgets/divider_widget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@

#include "gui/widgets/widget.hpp"

#include <algorithm>

namespace gui
{
/* abscract */ class DividerWidget : public Widget{
public:
DividerWidget(Widget *parent = nullptr, Rectangle r = {}) : Widget(parent, r){
setGrow(true);
}
uint getSpacing() const { return m_spacing; }
uint getGap() const { return m_separator + 2*m_spacing; }
uint getSeparator() const { return m_separator; }
void setSpacing(uint s) { m_spacing = s; }
void setSeparator(uint s) { m_separator = s; }
int getSpacing() const { return m_spacing; }
int getGap() const { return m_separator + 2*m_spacing; }
int getSeparator() const { return m_separator; }
void setSpacing(int s) { m_spacing = s; }
void setSeparator(int s) { m_separator = s; }


size_t addWidget(Widget *w){
Expand Down Expand Up @@ -47,12 +49,17 @@ namespace gui


protected:
bool relayout() override = 0;
// virtual Point2 convert(Point2) = 0;
// virtual Point2 convert(Size2) = 0;
virtual Size2 accumulate(Size2, Size2) = 0;
virtual bool growable(const Drawable &) = 0;
virtual Size2 excess(Size2) = 0;
virtual Size2 childSize(Size2 off, Size2 add, const Drawable&d) = 0;
Widget *m_children = nullptr;
private:
uint m_spacing = 1;
uint m_separator = 1;
int m_spacing = 1;
int m_separator = 1;
};
class HDividerWidget : public DividerWidget{
public:
Expand All @@ -61,6 +68,10 @@ namespace gui
protected:
void redraw(Graphics&, bool) override;
bool relayout() override;
Size2 accumulate(Size2 a, Size2 b) override { return {a.w+b.w, std::max(a.h,b.h)}; }
bool growable(const Drawable&d) override { return d.canGrowH(); }
Size2 excess(Size2 s) override { return {std::max(getSize().w - s.w, 0), 0}; }
Size2 childSize(Size2 off, Size2 add, const Drawable&d) override { return {std::min(getSize().w - off.w, d.getRequiredSize().w+add.w), getSize().h}; }
private:
};

Expand All @@ -71,6 +82,10 @@ namespace gui
protected:
void redraw(Graphics&, bool) override;
bool relayout() override;
Size2 accumulate(Size2 a, Size2 b) override { return {std::max(a.w,b.w), a.h+b.h}; }
bool growable(const Drawable&d) override { return d.canGrowV(); }
Size2 excess(Size2 s) override { return {0, std::max(getSize().h - s.h, 0)}; }
Size2 childSize(Size2 off, Size2 add, const Drawable&d) override { return {getSize().w, std::min(getSize().h - off.h, d.getRequiredSize().h+add.h)}; }
private:
};
} // namespace gui
2 changes: 2 additions & 0 deletions include/gui/widgets/list_widget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ namespace gui{
return i;
}

// bool handleScrollInput(InputEvent ev);

protected:
void redraw(Graphics&, bool = false) override;
bool relayout() override;
Expand Down
6 changes: 6 additions & 0 deletions include/gui/widgets/spacer_widget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

namespace gui
{
class SpacerWidget : public Widget{
protected:
void redraw(Graphics&, bool) override {
printf("SpacerWidget::redraw()\n");
}
};
class HSpacerWidget : public Widget{};
class VSpacerWidget : public Widget{};

Expand Down
1 change: 1 addition & 0 deletions include/gui/widgets/text_widget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace gui
void setFontSize(int scale);
void setFontColor(gui::Color c);
void setFont(const gui::Font*);
void setScroll(bool);
// void setHighlight(bool h) { m_isHighlighted = h; }
void redraw(Graphics&, bool = false);
protected:
Expand Down
11 changes: 6 additions & 5 deletions src/drivers/button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,21 @@ int64_t MyButton::button_alarm_callback(alarm_id_t alarm_id, void* env){
bool val = button->raw();
bool changed = val != button->last_state;
button->last_state = val;
absolute_time_t time = get_absolute_time();
if(changed){
if(val){
//pressed
DEBUG_PRINTF("Pressed\n");
event_queue.try_add(ButtonEvent{ButtonEventType::Pressed, button});
event_queue.try_add(ButtonEvent{ButtonEventType::Pressed, time, button});
DEBUG_PRINTF("Alarm rescheduled for 1s\n");
return button->hold_time*1000;
}else{
//released
DEBUG_PRINTF("Released\n");
event_queue.try_add(ButtonEvent{ButtonEventType::Released, button});
event_queue.try_add(ButtonEvent{ButtonEventType::Released, time, button});
if(!button->held){
DEBUG_PRINTF("Clicked\n");
event_queue.try_add(ButtonEvent{ButtonEventType::Clicked, button});
event_queue.try_add(ButtonEvent{ButtonEventType::Clicked, time, button});
}
}
button->held = false;
Expand All @@ -64,11 +65,11 @@ int64_t MyButton::button_alarm_callback(alarm_id_t alarm_id, void* env){
if(!button->held){
DEBUG_PRINTF("Held\n");
button->held = true;
event_queue.try_add(ButtonEvent{ButtonEventType::Held, button});
event_queue.try_add(ButtonEvent{ButtonEventType::Held, time, button});
}
if(button->repeat_time){
DEBUG_PRINTF("Repeat Clicked\n");
event_queue.try_add(ButtonEvent{ButtonEventType::RepeatClicked, button});
event_queue.try_add(ButtonEvent{ButtonEventType::RepeatClicked, time, button});
DEBUG_PRINTF("Alarm rescheduled for 0.2s\n");
return button->repeat_time*1000;
}
Expand Down
43 changes: 43 additions & 0 deletions src/drivers/ir.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "drivers/ir.hpp"

#include "pico/stdio.h"
#include "pico/printf.h"

using namespace IR;
using namespace RP2040;

LED::LED(uint pin) : RP2040::SioPin{pin, Polarity::ACTIVE_HIGH}{
setDirection(GPIO_OUT);
}

#define BIT_PERIOD_US 400

Detector::Detector(uint pin) : SioPin{pin}{

}

void Detector::onInterrupt(uint32_t flags){
// bool

bool new_state = raw();
if(new_state == state)
return;
//calulate timing and therefore num bits
absolute_time_t time = get_absolute_time();
int time_diff_us = absolute_time_diff_us(prev_time, time);
printf("%c %d,", '0'+state, time_diff_us);

// time_diff_us += (BIT_PERIOD_US* 0.5);
// int num_bits = time_diff_us/BIT_PERIOD_US;
// if(num_bits < 10){
// for(int i = 0; i < num_bits; i++)
// printf("%c", '0'+state);
// }else{
// printf("\n");
// }

prev_time = time;
prev_state = state;
state = new_state;
prev_time_diff_us = time_diff_us;
}
7 changes: 7 additions & 0 deletions src/drivers/led.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "drivers/led.hpp"


LED::LED(uint pin, Polarity p) : RP2040::PwmPin{pin, p}{
setDirection(GPIO_OUT);

}
Loading