RubyConf Taiwan 2023

The Rise of Microcontroller Ruby

#rubyconftw 2023

The Rise of Microcontroller Ruby

In this presentation, we will embark on a journey of live coding to bring microcontrollers to life. All you need is (at least) two "Raspberry Pi Pico W" boards, the same number of USB cables, and a laptop. Witness the realization of PicoRuby, a production-ready Ruby implementation for microcontrollers, as we delve into the world of innovation.

RubyConf Taiwan 2023

00:00:28.960 Hello, Ruby! Please join me in welcoming our speaker.
00:00:58.920 Hitoshi Hasumi.
00:01:12.439 Hello! Oh, do you hear me?
00:01:26.079 Okay, uh, hello! You're very lucky.
00:01:31.400 You chose my talk. I think there are a lot of talks that are difficult today, right? You look tired. My talk is very easy, so be relaxed.
00:01:40.040 So let's start! Oh, I think I still have one preparation. Wait a minute.
00:02:28.840 Hello! The title of my talk is "P Ruby Episode Four: The Rise of Microcontroller Ruby." Actually, this is episode four.
00:02:35.400 Former talks included episode two, "Attack of the Rake," and episode three, "Revenge of the Standard Interface." This year, a beginner's complete guide to microcontroller programming with Ruby can be found on the internet, please check it later.
00:02:48.319 And episode one is "Hidden Story." It's not published yet.
00:03:03.799 Let me introduce myself. My name is Hitoshi Hasumi. Hasumi is my family name, and Hitoshi is my first name. So you can call me just Hoshi or Hasumi, or whatever is still okay.
00:03:18.040 It's my internet name on GitHub and Twitter. I am the creator of P Ruby, which I will talk about today.
00:03:30.360 I also created PK firmware, which is keyboard firmware written in Ruby. Actually, this keyboard runs with Ruby now.
00:03:43.799 I am a committer on the series IRB, and at the end of this year, 10 days from now, RCC will be shipped with a new IRB that has been greatly improved.
00:03:50.000 So please take a look at the new IRB with R3.3. Actually, here’s another IRB maintainer out there. Please ask her, not me, about IRB.
00:04:04.439 I am a two-time first prize winner of the Ruby Award. I was also a finalist for the Ruby Prize two years ago because of P Ruby.
00:04:12.040 Can I ask something? If possible, please turn off your Bluetooth because I'll show you a Bluetooth application during a demonstration.
00:04:18.799 The number of the Bluetooth channel is, I think, 12. It will become very congested easily, so just turn off your Bluetooth for my talk. Let's begin with microcontroller programming with Ruby.
00:05:06.759 Connect your Raspberry Pi Pico W and laptop while pressing the boot select button. You'll find the RP2 drive in the file manager. Connect and download the "RP2.uf2" file from GitHub or release. It's an executable file out there.
00:05:38.199 Then just drag it into the RP2 drive. You can find a screen that looks like this. Let me try. Here comes P Ruby! This is the first screen. This is the hardest thing about my talk: every computing resource is in the Raspberry Pi Pico.
00:06:30.360 This screen was created by the Raspberry Pi Pico, not my laptop. Is it okay? Then this looks kind of unique, like a shell. You can type some command like 'L'. There's no file, 'PWD.' Oh, now I mean at home. 'LS' shows things like 'beep.' There are some executables. Maybe we can use 'date' command.
00:07:07.600 Yeah, you can see the current time. The Raspberry Pi Pico doesn't have a battery to keep the current time of a real-time clock, so every time you start it, the time should revert back to the epoch time. But you can process the current time using a battery if you want.
00:08:02.000 This means Ruby itself running on a microcontroller. Furthermore, Ruby can be compiled into VM code on the fly. You have to understand this! This means there is a small footprint Ruby that passes on the microcontroller.
00:08:59.440 By the way, RP2 stands for Ruby own Raspberry Pi. That's why I named it RP2. I would love to move such kind of robot with Ruby in the future, but not for today.
00:09:46.320 This is today's setup: two Raspberry Pi Pico W here, a character display, and some FTDI USB TTL serial converter cable. This is the setup for today.
00:10:13.840 About the Raspberry Pi Pico, there are two types: one with Wi-Fi and one without. The type with Wi-Fi has BLE (Bluetooth Low Energy) and Wi-Fi features. Today, I'll show you the BLE feature.
00:11:08.480 This is the very beginning of microcontroller programming. GPIO, let me do it. CYW43 is a wireless module.
00:12:11.440 Oh, I should have required 'CYW43' and 'CYW.' If I write 'gp.WR1,' what’s going to happen? Guess what is going to happen! Yes, it turns on the light. To turn it off, write 'GP.0.' You can do things like that.
00:13:09.440 You can incrementally try out the GPIO and see how it works. Now, let’s make an LED.
00:14:01.920 This will look like this. Actually, let me use a has method. How does the 'own method' look like? 'GP.1' - if you want the LED to turn on, then 'LED#on.' Really? What about though? Are you a copilot and one more thing, invert it.
00:15:40.640 If you achieve this, please pray for me. This is a kind of VI editor. How can I save this file and exit the editor? How can I say 'wq'?
00:16:29.040 I require 'LED = LED.new.' If I hit 'LED.invert,' it turns off.
00:17:18.000 Yes, right! And if I...
00:17:48.319 Oh yes, something’s wrong. But don't mind, let’s go to the next example.
00:18:49.440 GPIO is the most basic peripheral of microcontrollers. These three peripherals are more complex, including serial communication protocols, I2C, SPI, and UART.
00:19:23.760 Writing an LED class means creating a kind of device driver. The LED class and GPIO class provide basic low-level function.
00:20:56.800 The same can be done with the LED class. Just like that! It was a piece of cake.
00:21:54.720 But as we will see from now, wrapping SPI and I2C is much more valuable than LED.
00:22:35.600 This is very long to type for me, so I will cheat a little.
00:23:34.800 Oops! Some things are going wrong.
00:23:54.720 Oh, it doesn't work today. Oh, I'm sorry. I made a mistake. This should be the one, because I2C is...
00:24:36.400 These are just initialization procedures. Don't mind the details.
00:25:03.440 I'm ready! Who sees the camera?
00:25:29.680 Something went wrong - wait a minute, we have enough time.
00:26:30.160 Okay, the connection was loose.
00:26:56.000 Just, I mean, a kind of mistake but let's go and move on.
00:27:02.000 This is very long to type, right? Maybe because I made a mistake, so this should not work.
00:27:46.400 But it's okay! It seems we have a better one.
00:28:07.520 This already has the LCD RB.
00:28:25.840 The LCD class looks like this. It's not so complicated, right? But you can already be familiar with the reset method that I showed you just before.
00:29:21.920 The usage of the LCD class looks like this: 'LCD.new.' See? Initialize it, then just 'PR hello.' You can see... 'LCD' with a break line.
00:30:07.680 Let's see if this works.
00:30:55.520 In this way, we can write our own device driver incrementally with IRB. This is a kind of cool experience, right? This is microcontroller programming!
00:31:36.520 You don't need to write C; I've already done a lot of C for you, so you should just write Ruby to handle the hardware.
00:32:25.760 Unfortunately, we don't have enough time, so I will skip the SPI class that uses the thermometer. I will show you the final project.
00:33:40.000 Today, I copied the main.rb. If the main.rb file exists, that file will run at startup. So just reset the microcontroller to start the app.
00:34:18.000 You don’t need shell command anymore. Detach the cable and just connect it again.
00:34:51.360 But it doesn't work.
00:35:18.880 Sorry, I made another mistake; this should be 'broadcast.'
00:35:40.000 This is now battery-driven, a standalone IoT product! If I switch this on...
00:36:48.560 The live demonstration is very difficult!
00:37:30.560 This is now reporting the temperature of the room through Bluetooth!
00:38:49.680 Observer devices are working to catch the radio waves from this device. If everything goes correctly, this will show the temperature data.
00:39:43.520 In this way, we can create real Bluetooth applications with Ruby.
00:40:07.680 What I wanted to talk about today is a Ruby implementation targeting microcontrollers and building device drivers for general I/O like GPIO, TCP, and PWM.
00:40:35.480 They are ready, and you can incrementally write your device drivers and applications with P Ruby. The BLE is also ready but still unstable for real applications.
00:41:28.240 So, you can make an IoT system with Ruby. That's all for today! Please scan this QR code and visit my repository at GitHub.com/P/P-Ruby.
00:42:25.440 Thank you all! Okay, thank you, Hitoshi, for your informative sharing. Now, we have no time for a Q&A session, but if anyone has questions, please raise your hand.
00:43:02.920 Now let's end this meeting. This meeting is adjourned.
00:43:12.920 Before you go, if you want to attend the AAL party, please raise your hand. About 80% of people. The official party will commence at 6:00 p.m. at the P College office.
00:43:27.680 The topic of the panel discussion will be 'The Future of Engineers' Happiness in the Age of AI.' We welcome everyone to attend and remember to bring your ticket.
00:43:46.880 Lastly, check in at the reception tomorrow to receive lunch boxes. If you find any lost items, please bring them to reception. Thank you!