The original link: www.shuaihuajun.com/article/ruf…
I still remember that in the summer of 2016 when I graduated from college, I bought the Arduino development board. Due to my lack of familiarity with some circuit concepts and the programming language used for Arduino development, I temporarily put my learning of Arduino on hold. In early October 2016, I began to work in the Institute of Information and Industry, Chinese Academy of Sciences. I had a lot of spare time and had a lot of time for research and learning, so I started Ruff development board.
Ruff is based on JavaScript, so it’s much easier for a front-end engineer to learn Ruff. The Development programming language of Arduino is realized on the basis of C/C++. The development environment of Arduino is very similar to that of Processing, which is based on Java, a type-snatchlanguage.
From my layman’s experience with Ruff and Arduino, Ruff is not as good as Arduino in terms of ecology and hardware.
With a recent revival of interest in hardware programming, the Ruff board is back out, So Easy! Ruff’s official website and community have not been updated for a long time. Record a Ruff use experience, everything must leave a trace.
First of all, download ruff SDK from ruff. IO, I use Apple system, because I don’t like the one-click installation method of.dmg installation package, I always feel that the installation is not clear, so download the compressed installation package in.zip format. Unzip the package and put it in the home directory with my NodeJS package. Modify the configuration of environment variable configuration file. Bash_profile. Add the decompressed bin directory to the environment variable and run source. bash_profile to make the configuration environment variable take effect.
Using the echo $PATH command, you can see that ruff’s bin directory is already in the environment variable. The bin directory of ruff has only two executable files, one is ruff and the other is rap. The rap command is more commonly used. So far, I do not know what ruff command is used for.
Here are the steps I think you should take to complete a hardware program with Ruff.
Creative idea
The creative step is very important. If you don’t have ideas and practical plans and expectations of what you want to achieve, it’s impossible to solve the problem step by step because you don’t know what you’re doing.
Because now is the beginning, the purpose is very simple, is through the button press and release, in the terminal output some characters.
Create a project
Create the project directory -> Enter the project -> Initialize the project. After a wave of output from the terminal, the project prototype is completed:
rap --version
ruff -v
mkdir hello-ruff
cd hello-ruff
rap init
Copy the code
Add the driver
A peripheral is an external device, either a sensor or an electronic component. I need a button. I got a big button from Ruff’s box. There are three pins in the button element, so I need three wires for all of them. This is the general idea that there are as many pins in an electronic component as there are wires, but some components seem to be different.
Find the name of the device from the package of the big button. The name of the button I prepared is CK002, and all uppercase cases are treated as lowercase cases. At this time, remember to CD to the project directory, then rap device add ck002 command to query available drivers, use ⬆️⬇️ to select the latest driver (generally, the arrow will stay in the latest driver by default), in general, only need to confirm ✅.
Visual layout
Ruff provides a visual layout interface. Run the rap Layout — Visual command and Ruff will automatically open your browser to a visual layout interface like the one shown in the following image.
When you put the mouse on a wire, you can select the wire, which shows the connection mode more clearly. I am sometimes dazzled by the use of many electronic components.
Write human-computer interaction logic program
The following code should be familiar to front-end engineers. In the code, the element representing the button is represented in a jquery-like way, such as $(‘#ck002’) for the added button, which can be found as the id of the button in the app.json file at the root of the project.
Write logic, press the button red LED light up, release the button red LED light off, the LED light mentioned here is Ruff development board with LED light, of course, Ruff development kit also has LED light, but here only used a button element.
'use strict'
$.ready(function (error) {
if (error) {
console.log(error)
return
}
$('#ck002').on('push'.function(){
console.log('Button pushed.');
$('#led-r').turnOn()
})
$('#ck002').on('release'.function(){
console.log('Button released.');
$('#led-r').turnOff()
})
})
$.end(function () {$('#led-r').turnOff()
})
Copy the code
The deployment of
The hardware connection is completed, the software is written, and the program is deployed to the development board. Run the rap deploy-s command, and the terminal displays information about the deployment process. If no error is displayed, the program is successfully deployed.
I also need to open a new terminal interface for output when a button is pressed and released, like communication between a development board and a computer. Run rap log on the new terminal, indicating that the development board is successfully connected. Press and release the button continuously at this time, the terminal will output continuously, and the program wants to achieve the same.