Skip to content

Commit

Permalink
Merge pull request #105 from picoruby/suppress_RGB_random_flicker
Browse files Browse the repository at this point in the history
Suppress RGB random flicker
  • Loading branch information
hasumikin authored Jun 10, 2022
2 parents cd269ea + 51710a1 commit a7cc030
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 39 deletions.
85 changes: 55 additions & 30 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ int autoreload_state; /* from msc_disk.h */
#define NODE_BOX_SIZE 50
#endif

mrbc_tcb *tcb_keymap;
static mrbc_tcb *tcb_keymap;

#define KEYMAP_PREFIX "puts;" /* Somehow scapegoat... */
#define KEYMAP_PREFIX_SIZE (sizeof(KEYMAP_PREFIX) - 1)
Expand Down Expand Up @@ -310,6 +310,7 @@ typedef struct picogems {
void (*initializer)(void);
const uint8_t *mrb;
const uint8_t *task;
mrbc_tcb *tcb;
bool required;
} picogems;

Expand All @@ -326,44 +327,67 @@ init_RGB(void)
}

picogems gems[] = {
{"keyboard", NULL, keyboard, NULL, false},
{"debounce", NULL, debounce, NULL, false},
{"rotary_encoder", init_RotaryEncoder, rotary_encoder, NULL, false},
{"rgb", init_RGB, rgb, rgb_task, false},
{"via", NULL, via, NULL, false},
{NULL, NULL, NULL, NULL, false}
{"keyboard", NULL, keyboard, NULL, NULL, false},
{"debounce", NULL, debounce, NULL, NULL, false},
{"rotary_encoder", init_RotaryEncoder, rotary_encoder, NULL, NULL, false},
{"rgb", init_RGB, rgb, rgb_task, NULL, false},
{"via", NULL, via, NULL, NULL, false},
{NULL, NULL, NULL, NULL, NULL, false}
};

void
c_require(mrb_vm *vm, mrb_value *v, int argc)
int
gem_index(const char *name)
{
char *name = GET_STRING_ARG(1);
bool result = false;
if (!name) return;
int i;
if (!name) return -1;
for (int i = 0; ; i++) {
if (gems[i].name == NULL) {
console_printf("cannot load such mrb -- %s\n (LoadError)", name);
break;
console_printf("cannot find such gem -- %s\n (LoadError)", name);
return -1;
} else if (strcmp(name, gems[i].name) == 0) {
if (!gems[i].required) {
if (gems[i].initializer) gems[i].initializer();
if (mrbc_load_model(gems[i].mrb)) {
if (gems[i].task) {
if (mrbc_create_task(gems[i].task, 0) != NULL) {
console_printf("failed to create task\n (LoadError)", name);
result = false; /* ToDo: Exception */
}
}
gems[i].required = true;
} else {
console_printf("failed to load mrb -- %s\n (LoadError)", name);
result = false; /* ToDo: Exception */
}
}
return i;
break;
}
}
if (result) { SET_TRUE_RETURN(); } else { SET_FALSE_RETURN(); }
}

void
c_resume_task(mrb_vm *vm, mrb_value *v, int argc)
{
int i = gem_index(GET_STRING_ARG(1));
if (i < 0) {
SET_FALSE_RETURN();
} else {
mrbc_resume_task(gems[i].tcb);
SET_TRUE_RETURN();
}
}

void
c_require(mrb_vm *vm, mrb_value *v, int argc)
{
const char *name = GET_STRING_ARG(1);
int i = gem_index(name);
SET_FALSE_RETURN();
if (i < 0) return;
if (gems[i].required) return;
if (gems[i].initializer) gems[i].initializer();
if (mrbc_load_model(gems[i].mrb)) {
if (gems[i].task) {
gems[i].tcb = mrbc_create_task(gems[i].task, 0);
if (gems[i].tcb == NULL) {
console_printf("failed to create task\n (LoadError)", name);
/* ToDo: Exception */
} else {
mrbc_suspend_task(gems[i].tcb);
}
}
gems[i].required = true;
SET_TRUE_RETURN();
} else {
console_printf("failed to load mrb -- %s\n (LoadError)", name);
/* ToDo: Exception */
}
}

int loglevel;
Expand Down Expand Up @@ -408,6 +432,7 @@ int main() {
mrbc_define_method(0, mrbc_class_object, "reload_keymap", c_reload_keymap);
mrbc_define_method(0, mrbc_class_object, "suspend_keymap", c_suspend_keymap);
mrbc_define_method(0, mrbc_class_object, "resume_keymap", c_resume_keymap);
mrbc_define_method(0, mrbc_class_object, "resume_task", c_resume_task);
#endif
autoreload_state = AUTORELOAD_READY;
mrbc_run();
Expand Down
1 change: 1 addition & 0 deletions src/ruby/app/models/keyboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,7 @@ def start!
end

@via.start! if @via
resume_task("rgb") if $rgb

@keycodes = Array.new
prev_layer = :default
Expand Down
8 changes: 6 additions & 2 deletions src/ruby/app/models/rgb.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class RGB
def initialize(pin, underglow_size, backlight_size, is_rgbw = false)
puts "Initializing RGB."
@offed = true
turn_off
@fifo = Array.new
# TODO: @underglow_size, @backlight_size
@pixel_size = underglow_size + backlight_size
Expand Down Expand Up @@ -47,10 +47,11 @@ def hsv2rgb(h, s, v)
EFFECTS = %i|swirl rainbow_mood breath nokogiri static|

def effect=(name)
@offed = true
turn_off
@effect = name
init_values
reset_pixel
turn_on
end

def reset_pixel
Expand All @@ -68,6 +69,9 @@ def reset_pixel
when :breathing
puts "[WARN] :breathing is deprecated. Use :rainbow_mood instead"
end
end

def turn_on
@offed = false
end

Expand Down
9 changes: 3 additions & 6 deletions src/ruby/app/tasks/rgb_task.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
while !$mutex
relinquish
end

puts "RGB task started."

puts "Starting rgb task ..."
3.times { sleep 1 }

while true
unless $rgb
sleep 1
sleep 10
else
$rgb.show
end
Expand Down
1 change: 1 addition & 0 deletions src/ruby/sig/object.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Object
def autoreload_ready?: () -> bool
def suspend_keymap: () -> void
def resume_keymap: () -> void
def resume_task: (String) -> bool
def reload_keymap: () -> void
def report_hid : (Integer, String) -> void
def raw_hid_report_received?: -> bool
Expand Down
1 change: 1 addition & 0 deletions src/ruby/sig/rgb.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class RGB
def initialize: (Integer pin, Integer underglow_size, Integer backlight_size, ?bool is_rgbw) -> void
def init_values: () -> void
def turn_off: () -> void
def turn_on: () -> void
def toggle: () -> void
def effect=: (Symbol) -> void
def hue=: (Integer) -> void
Expand Down
2 changes: 1 addition & 1 deletion src/ws2812.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ static uint sm = 0;

#define MAX_PIXEL_SIZE 150

static int32_t pixels[MAX_PIXEL_SIZE + 1];
static int32_t pixels[MAX_PIXEL_SIZE + 1] = {};
static int32_t dma_ws2812_grb_pixels[MAX_PIXEL_SIZE];
static int dma_ws2812_channel;
static uint8_t dma_ws2812_last_append_index = 0;
Expand Down

0 comments on commit a7cc030

Please sign in to comment.