Skip to content

Commit

Permalink
Introduces mrubyc-test
Browse files Browse the repository at this point in the history
  • Loading branch information
hasumikin committed Sep 24, 2021
1 parent 90dec3d commit 3fcc072
Show file tree
Hide file tree
Showing 22 changed files with 164 additions and 38 deletions.
15 changes: 12 additions & 3 deletions .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,20 @@ jobs:
BUNDLE_GEMFILE: ./src/ruby/Gemfile
steps:
- uses: actions/checkout@v2
with:
submodules: recursive
- uses: ruby/setup-ruby@v1
with:
ruby-version: 3.0.1
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
- run: |
BUNDLE_GEMFILE=Gemfile bundle exec steep check
BUNDLE_GEMFILE=Gemfile bundle exec mrubyc-test
- id: steep
run: BUNDLE_GEMFILE=Gemfile bundle exec steep check
working-directory: src/ruby
#- name: Setup tmate session
# uses: mxschmitt/action-tmate@v3
- id: picorbc
run: make host_production_libc
working-directory: lib/picoruby
- id: test
run: BUNDLE_GEMFILE=Gemfile CFLAGS=-DMAX_SYMBOLS_COUNT=600 bundle exec mrubyc-test
working-directory: src/ruby
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change log

## 0.9.4 in 2021/09/24
### Improvement
- Nou you can use "Right side flipped split" keyboard (eg. [Zinc](https://www.sho-k.co.uk/tech/735.html)). See [pull/22](https://github.com/picoruby/prk_firmware/pull/22)

## 0.9.3 in 2021/09/17
### Improvement
- Abbreviated keynames things like `KC_ENT` for `KC_ENTER` can be used in `keymap.rb`. See [bc23e52](https://github.com/picoruby/prk_firmware/commit/bc23e52f51c2899ce5309643f0ab89606a9b469d)
Expand Down
33 changes: 21 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,50 +59,59 @@ set(RBC ${CMAKE_CURRENT_SOURCE_DIR}/lib/picoruby/build/bin/host-production/alloc

add_custom_target(steep
COMMAND bundle exec steep -h || bundle install
COMMAND ${CMAKE_COMMAND} -E env PROJECT_BINARY_DIR=${PROJECT_BINARY_DIR} bundle exec steep check
COMMAND ${CMAKE_COMMAND} -E env PROJECT_BINARY_DIR=${PROJECT_BINARY_DIR} rake steep
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/src/ruby
)

add_custom_target(mrubyc-test
COMMAND bundle exec mrubyc-test -h || bundle install
COMMAND ${CMAKE_COMMAND} -E env PROJECT_BINARY_DIR=${PROJECT_BINARY_DIR} bundle exec mrubyc-test -p ${RBC}
COMMAND bundle exec mrubyc-test version || bundle install
COMMAND ${CMAKE_COMMAND} -E env PROJECT_BINARY_DIR=${PROJECT_BINARY_DIR} rake test
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/src/ruby
)

if(PRK_NO_MSC)
add_custom_target(keymap
COMMAND ${RBC} -Bkeymap ${PROJECT_BINARY_DIR}/../keymap.rb
COMMAND mv ${PROJECT_BINARY_DIR}/../keymap.c ./
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/src/ruby/lib
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/src/ruby/app/
)
else()
add_custom_target(keymap)
endif()

add_custom_target(ruby
add_custom_target(models
COMMAND ${RBC} -Bcore core.rb
COMMAND ${RBC} -Bkeyboard keyboard.rb
COMMAND ${RBC} -Brgb rgb.rb
COMMAND ${RBC} -Brotary_encoder rotary_encoder.rb
COMMAND ${RBC} -Btud tud.rb
COMMAND ${RBC} -Bbuffer buffer.rb
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/src/ruby/app/models
)

add_custom_target(tasks
COMMAND ${RBC} -Busb_task usb_task.rb
COMMAND ${RBC} -Brgb_task rgb_task.rb
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/src/ruby/lib
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/src/ruby/app/tasks
)

add_custom_target(sandbox
COMMAND ${RBC} -Bbuffer buffer.rb
COMMAND ${RBC} -Bsandbox sandbox.rb
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/lib/picoruby/cli/ruby
add_custom_target(ruby
)

#add_custom_target(sandbox
# COMMAND ${RBC} -Bsandbox sandbox.rb
# WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/lib/picoruby/cli/ruby
#)

add_dependencies(ruby
models
tasks
steep
mrubyc-test
)
add_dependencies(${PROJECT}
picoruby
ruby
sandbox
# sandbox
)
if(PRK_NO_MSC)
add_dependencies(${PROJECT}
Expand Down
20 changes: 10 additions & 10 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@

/* ruby */
/* models */
#include "ruby/lib/core.c"
#include "ruby/lib/keyboard.c"
#include "ruby/lib/rotary_encoder.c"
#include "ruby/lib/rgb.c"
#include "../lib/picoruby/cli/ruby/buffer.c"
#include "ruby/app/models/core.c"
#include "ruby/app/models/keyboard.c"
#include "ruby/app/models/rotary_encoder.c"
#include "ruby/app/models/rgb.c"
#include "ruby/app/models/buffer.c"
/* tasks */
#include "ruby/lib/tud.c"
#include "ruby/lib/rgb_task.c"
#include "ruby/app/tasks/usb_task.c"
#include "ruby/app/tasks/rgb_task.c"
#ifdef PRK_NO_MSC
#include "ruby/lib/keymap.c"
#include "ruby/app/keymap.c"
#endif

void
Expand Down Expand Up @@ -164,7 +164,7 @@ int main() {
#endif
CDC_INIT();
GPIO_INIT();
TUD_INIT();
USB_INIT();
UART_INIT();
WS2812_INIT();
ROTARY_ENCODER_INIT();
Expand All @@ -174,7 +174,7 @@ int main() {
mrbc_load_model(buffer);
mrbc_load_model(rotary_encoder);
mrbc_load_model(keyboard);
mrbc_create_task(tud, 0);
mrbc_create_task(usb_task, 0);
mrbc_create_task(rgb_task, 0);
create_sandbox();
mrbc_define_method(0, mrbc_class_object, "autoreload_ready?", c_autoreload_ready_q);
Expand Down
2 changes: 1 addition & 1 deletion src/ruby/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ source 'https://rubygems.org'
gem 'steep'
gem 'typeprof'
gem "rbs", "1.7.0.beta.2"
gem "mrubyc-test", "0.7.0.pre.rc0"
gem "mrubyc-test", "0.7.0.pre.rc1"
4 changes: 2 additions & 2 deletions src/ruby/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ GEM
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
minitest (5.14.4)
mrubyc-test (0.7.0.pre.rc0)
mrubyc-test (0.7.0.pre.rc1)
activesupport (~> 6.0)
rufo (~> 0.12)
thor (~> 1.0)
Expand Down Expand Up @@ -53,7 +53,7 @@ PLATFORMS
x86_64-linux

DEPENDENCIES
mrubyc-test (= 0.7.0.pre.rc0)
mrubyc-test (= 0.7.0.pre.rc1)
rbs (= 1.7.0.beta.2)
steep
typeprof
Expand Down
2 changes: 1 addition & 1 deletion src/ruby/Mrubycfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
test_dir: test
test_tmp_dir: test/tmp
mrubyc_src_dir: ../../lib/picoruby/src/mrubyc/src
mruby_lib_dir: lib
mruby_lib_dir: app
mrbc_path: ../../../../lib/picoruby/build/bin/host-production/alloc_libc/picorbc # Relative path from test/tmp/
2 changes: 1 addition & 1 deletion src/ruby/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ namespace :steep do
end

task :test do
exec %q(bundle exec mrubyc-test)
exec %q(CFLAGS=-DMAX_SYMBOLS_COUNT=600 bundle exec mrubyc-test)
end
2 changes: 1 addition & 1 deletion src/ruby/Steepfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
target :lib do
signature "sig"

check "lib"
check "app"
# PROJECT_BINARY_DIR is configured in CMakeLists.txt
check "../../#{(ENV['PROJECT_BINARY_DIR'] || 'keyboards/**/build').split('/')[-3..-2].join('/')}/keymap.rb"
end
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 8 additions & 0 deletions src/ruby/lib/keyboard.rb → src/ruby/app/models/keyboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,14 @@ class Keyboard
XXXXXXX: :KC_NO,
}
letter = nil
end

#
# Keyboard class have to be defined twice to avoid "too big operand" error of mrbc
# It seems `b` operand of OP_LOADSYM will be more than 0xFFFF if the class is united
#

class Keyboard

STANDARD_SPLIT = :standard_split
RIGHT_SIDE_FLIPPED_SPLIT = :right_side_flipped_split
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/ruby/sig/buffer.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Buffer
@cursor: Hash[Symbol, Integer]
@lines: Array[String]

def initialize: (String prompt) -> void
def initialize: (?String prompt) -> void
def cursor: () -> Hash[Symbol, Integer]
def clear: () -> void
def dump: () -> void
Expand Down
4 changes: 4 additions & 0 deletions src/ruby/sig/keyboard.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
$mutex: bool

# Classes
class Object
end

class Keyboard
GPIO_OUT: Integer
GPIO_IN: Integer
Expand All @@ -14,6 +17,7 @@ class Keyboard
KEYCODE_SFT: Hash[Symbol, Integer]
LETTER: Array[String | Symbol | nil]
KC_ALIASES: Hash[Symbol, Symbol]

STANDARD_SPLIT: Symbol
RIGHT_SIDE_FLIPPED_SPLIT: Symbol

Expand Down
77 changes: 75 additions & 2 deletions src/ruby/test/keyboard_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,80 @@
class KeyboardTest < MrubycTestCase
# TODO: improve mock functionality of mrubyc-test
def define_mocks
kbd = Keyboard.new
mock(kbd).gpio_init(1)
mock(kbd).gpio_set_dir(2)
mock(kbd).gpio_put(2)
mock(kbd).gpio_pull_up(1)
end
def setup
@kbd = Keyboard.new
@kbd.init_pins([1,2], [3,4])
end

##### Tests from here

description "default layer"
def default_layer
kbd = Keyboard.new
assert_equal :default, kbd.layer
assert_equal :default, @kbd.layer
end

description ":KC_ENT should be equal to :KC_ENTER(0x28)"
def kc_aliases_1
assert_equal (0x28)*(-1), @kbd.find_keycode_index(:KC_ENT)
end

description "split_style should be :standard_split or :right_side_flipped_split"
def split_stype_case
assert_equal :standard_split, @kbd.split_style
@kbd.split_style = :right_side_flipped_split
assert_equal :right_side_flipped_split, @kbd.split_style
@kbd.split_style = :this_is_invalid
assert_equal :standard_split, @kbd.split_style
end

description "should return col_index as it is"
def standard_calculate_col_position_case
@kbd.split = true
@kbd.split_style = :standard_split
@kbd.add_layer :default, %i(KC_A KC_B KC_C KC_D KC_E KC_F KC_G KC_H)
assert_equal 3, @kbd.calculate_col_position(3)
end

description "should return calculated col_index"
def flipped_calculate_col_position_case
@kbd.split = true
@kbd.split_style = :right_side_flipped_split
@kbd.add_layer :default, %i(KC_A KC_B KC_C KC_D KC_E KC_F KC_G KC_H)
assert_equal 2, @kbd.calculate_col_position(3)
end

description "raise"
def raise_layer_case
@kbd.add_layer :default, %i(KC_A)
@kbd.add_layer :raise, %i(KC_B)
@kbd.add_layer :lower, %i(KC_C)
@kbd.raise_layer
assert_equal :raise, @kbd.instance_variable_get("@locked_layer")
@kbd.raise_layer
assert_equal :lower, @kbd.instance_variable_get("@locked_layer")
end

description "lower"
def lower_layer
@kbd.add_layer :default, %i(KC_A)
@kbd.add_layer :raise, %i(KC_B)
@kbd.add_layer :lower, %i(KC_C)
@kbd.lower_layer
assert_equal :lower, @kbd.instance_variable_get("@locked_layer")
@kbd.lower_layer
assert_equal :raise, @kbd.instance_variable_get("@locked_layer")
end

description "@ruby_mode should switch"
def ruby_mode
assert_not_equal true, @kbd.instance_variable_get("@ruby_mode")
@kbd.ruby
assert_equal true, @kbd.instance_variable_get("@ruby_mode")
end
end
25 changes: 22 additions & 3 deletions src/ruby/test/tmp/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,34 @@
#define MEMORY_SIZE (1024*640)-1
static uint8_t my_memory_pool[MEMORY_SIZE];

int exit_code;
int exit_code = 0;

static void c_exit(mrb_vm *vm, mrb_value *v, int argc){
static void c_exit(struct VM *vm, mrbc_value v[], int argc)
{
exit_code = GET_INT_ARG(1);
}

//================================================================
/*! Object#instance_variable_get
*/
static void c_instance_variable_get(struct VM *vm, mrbc_value v[], int argc)
{
mrbc_kv_handle *kvh = &v[0].instance->ivar;
const char *name = (const char *)GET_STRING_ARG(1);
for(int i = 0; i < kvh->n_stored; i++) {
if (strncmp(&name[1], symid_to_str(kvh->data[i].sym_id), strlen(name) - 1) == 0) {
SET_RETURN(kvh->data[i].value);
return;
}
}
SET_NIL_RETURN();
}

//================================================================
/*! DEBUG PRINT
*/
static void c_debugprint(mrb_vm *vm, mrb_value *v, int argc){
static void c_debugprint(struct VM *vm, mrbc_value v[], int argc)
{
console_putchar('\n');
for( int i = 0; i < 79; i++ ) { console_putchar('='); }
console_putchar('\n');
Expand All @@ -34,6 +52,7 @@ int main(void) {
mrbc_init(my_memory_pool, MEMORY_SIZE);
mrbc_define_method(0, mrbc_class_object, "debugprint", c_debugprint);
mrbc_define_method(0, mrbc_class_object, "exit", c_exit);
mrbc_define_method(0, mrbc_class_object, "instance_variable_get", c_instance_variable_get);
mrbc_create_task( models, 0 );
mrbc_create_task( test, 0 );
mrbc_run();
Expand Down
2 changes: 1 addition & 1 deletion src/usb_descriptors.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void c_tud_task(mrb_vm *vm, mrb_value *v, int argc);
void c_report_hid(mrb_vm *vm, mrb_value *v, int argc);
void c_tud_mounted_q(mrb_vm *vm, mrb_value *v, int argc);

#define TUD_INIT() do { \
#define USB_INIT() do { \
mrbc_define_method(0, mrbc_class_object, "tud_task", c_tud_task); \
mrbc_define_method(0, mrbc_class_object, "tud_mounted?", c_tud_mounted_q); \
mrbc_class *mrbc_class_Keyboard = mrbc_define_class(0, "Keyboard", mrbc_class_object); \
Expand Down

0 comments on commit 3fcc072

Please sign in to comment.