Demo display and hardware construction - light
This is a demo of operating the LED to make it flash. We will introduce the basic methods of operating the hardware through this demo. Let's see the final effect first:
[external chain picture transfer failed. The source station may have anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-sgqd42lg-1630555470016)( https://user-gold-cdn.xitu.io/2018/6/19/16417f4b402aeb0e?imageslim )]
Hardware construction steps:
- A resistor and an LED lamp are connected in series through the bread board and connected to the positive pole of the bread board
- The positive pole of the bread board is connected to a GPIO bus port of raspberry pie
- The other pin of LED is connected to the GROUND pin of raspberry pie through jumper. My connection diagram is attached:
[external chain picture transfer failed. The source station may have anti-theft chain mechanism. It is recommended to save the picture and upload it directly (IMG nazxfcpo-1630555470018)( https://user-gold-cdn.xitu.io/2018/6/19/16417f4b5520f554?imageView2/0/w/1280/h/960/ignore -error/1)]
So far, the construction of the hardware module is completed.
For more intuitive reference, please refer to the following official pictures:
[external chain picture transfer failed. The source station may have anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-gtadivgy-1630555470020)( https://user-gold-cdn.xitu.io/2018/6/19/16417f4b588fd212?imageView2/0/w/1280/h/960/ignore -error/1)]
It should be emphasized here that Ground is the Ground wire, which is used to simulate the zero voltage line. The GPIO bus port can choose different voltages according to the applied resistance and LED. I choose the port with voltage of 3.3v named BCM2
light code analysis
public class LightActivity extends Activity { Handler mHandler; PeripheralManager mPeripheralManager; Gpio mLightGpio; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_light); mHandler = new Handler(); mPeripheralManager = PeripheralManager.getInstance(); try { mLightGpio = mPeripheralManager.openGpio("BCM2"); mLightGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW); mHandler.post(mBlinkRunnable); } catch (IOException e) { e.printStackTrace(); } } private Runnable mBlinkRunnable = new Runnable() { @Override public void run() { try { if (mLightGpio == null) return; mLightGpio.setValue(!mLightGpio.getValue()); mHandler.postDelayed(mBlinkRunnable, 1000); } catch (IOException e) { } } }; @Override protected void onDestroy() { super.onDestroy(); if (mLightGpio != null) { try { mLightGpio.close(); mLightGpio = null; } catch (IOException e) { e.printStackTrace(); } } } }
The code is not much, so it's all posted. Let's analyze the code below.
mPeripheralManager=PeripheralManager.getInstance(); mLightGpio= mPeripheralManager.openGpio("BCM2");
We call the openGpio method after the PeripheralManager singleton, and the incoming parameter is the name corresponding to the GPIO port, so we get an instance mLightGpio of the port control object Gpio.
mLightGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_HIGH);
This sentence code has two functions: 1 Set the level direction as the output direction; 2 set the initial level to high and activate immediately. After this code is executed, the LED will become normally on. We can also achieve the same effect as the above sentence through the following three lines of code:
//Set the level direction as the output direction, set the initial level as the low level and activate immediately mLightGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW); //Set the active state to high mLightGpio.setActiveType(Gpio.ACTIVE_HIGH) //Activate mLightGpio.setValue(true);
In mBlinkRunnable, through mlightgpio SetValue (! Mlightgpio. Getvalue()) to cycle the activation state of the level to realize the flashing of the LED. At this point, the LED can blink.
Finally, let's take a look at the differences between the ats project and the standard Android. There are two main differences:
- The ats project permission does not need the user's dynamic authorization, but can be directly named in the manifest, such as the permission required to access the GPIO bus port and register the user driver mentioned below
<uses-permission android:name="com.google.android.things.permission.USE_PERIPHERAL_IO"/> <uses-permission android:name="com.google.android.things.permission.MANAGE_INPUT_DRIVERS" />
- After the ats project is deployed to the hardware, you can set its accidental shutdown and restart and startup self startup, so as to ensure the high availability state of the Internet of things device and prevent the device from being unavailable due to program crash. For specific steps, you only need to add the following intent filter to the portal Activity:
<intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.HOME"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter>
User driver
The so-called user driver is that ats allows you to convert the electrical signal of the corresponding hardware into system events. For example, the click event of the button is registered as the keyboard key event of the system, and the electrical signal of the temperature sensor is registered as the existing sensor event of the system. In this way, each component can easily use the standard framework api to operate the hardware.
Take a chestnut
The content shown in this example is to register the electrical signal of the click event of the button as the key event of the keyboard, so that the button becomes a keyboard, which can be clicked and pressed for a long time. After registering as a system event, it can be used in various components of the application. Demonstration effect:
[external chain picture transfer failed. The source station may have anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-wvrlstq3-1630555470021)( https://user-gold-cdn.xitu.io/2018/6/19/16417f4b80ab008e?imageslim )]
Hardware installation drawing:
[external chain picture transfer failed. The source station may have anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-g8rupslc-1630555470023)( https://user-gold-cdn.xitu.io/2018/6/19/16417f4b7887acc7?imageView2/0/w/1280/h/960/ignore -error/1)]
Look at the code below:
package com.androfarmer.button; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log; import android.view.KeyEvent; import com.google.android.things.pio.Gpio; import com.google.android.things.pio.GpioCallback; import com.google.android.things.pio.PeripheralManager; import com.google.android.things.userdriver.UserDriverManager; import com.google.android.things.userdriver.input.InputDriver; import com.google.android.things.userdriver.input.InputDriverEvent; import java.io.IOException; public class KeyCodeDriverService extends Service { private InputDriver mDriver; private Gpio mButtonGpio; private static final int KEY_CODE = KeyEvent.KEYCODE_A; @Override public void onCreate() { super.onCreate(); //Create an input driver and set the basic information of the driver mDriver = new InputDriver.Builder() .setName("Button2Keyboard") .setSupportedKeys(new int[]{KEY_CODE}) .build(); // Register the driver created above through UserDriverManager UserDriverManager manager = UserDriverManager.getInstance(); manager.registerInputDriver(mDriver); PeripheralManager peripheralManager = PeripheralManager.getInstance(); try { mButtonGpio = peripheralManager.openGpio("BCM21"); //Set level direction as input mButtonGpio.setDirection(Gpio.DIRECTION_IN); //Set activation type mButtonGpio.setActiveType(Gpio.ACTIVE_LOW); //Set the monitoring event as: level interrupt change event, GPIO EDGE_ BOTH //It means that the level interrupt from low to high and from high to low will trigger the callback mButtonGpio.setEdgeTriggerType(Gpio.EDGE_BOTH); //Monitor for setting level change mButtonGpio.registerGpioCallback(new GpioCallback() { @Override public boolean onGpioEdge(Gpio gpio) { try { Log.d("-------------button",gpio.getValue()+""); boolean pressed=gpio.getValue(); InputDriverEvent event = new InputDriverEvent(); event.setKeyPressed(KEY_CODE, pressed); mDriver.emit(event); } catch (IOException e) { e.printStackTrace(); } //Returning true means listening all the time, and false means listening once return true; } }); } catch (IOException e) { e.printStackTrace(); } } @Override public void onDestroy() { super.onDestroy(); //Contact registration UserDriverManager manager = UserDriverManager.getInstance(); manager.unregisterInputDriver(mDriver); //Close the gpio port try { mButtonGpio.close(); mButtonGpio = null; } catch (IOException e) { e.printStackTrace(); } } @Override public IBinder onBind(Intent intent) { return null; } }
The comments of the code have been written in great detail, but there is no more explanation. Here is a brief introduction to the next steps
- Creating basic information objects through InputDriver
- Register drivers through UserDriverManager
- The PeripheralManager handles the electrical signals of specific peripheral hardware
- The inputdrivereevent event is emitted through the emit method of InputDriver, so that each component of the system can respond to this event
- Don't forget to unregister and close the port when it's no longer in use
User driven type
It is mainly divided into four categories:
- Location drive
- Input user input event driven
- Sensor drive
- LoWPAN
Further description of user driven
Many scenes of ats can be like developing app s, which is easy for students who have done android development, but we may hear the concept of user driven for the first time. The design of ats is modular. An ats hardware board will only contain basic hardware modules, such as network module, cpu, memory, etc. After we get the basic board running the ats system, if we want to develop into a specific Internet of things product, we may need to connect peripheral sensors to realize specific functions: such as temperature sensor, smoke alarm, etc.
Learning sharing
① "Complete analysis of Android interview questions" PDF Full HD version + ② compressed package of "Android interview knowledge system" learning mind map
>](https://codechina.csdn.net/m0_60958482/android_p7)**
① "Complete analysis of Android interview questions" PDF Full HD version + ② compressed package of "Android interview knowledge system" learning mind map
[external chain picture transferring... (img-4dgNzX02-1630555470024)]
[external chain picture transferring... (img-MEvxBbyP-1630555470025)]
[external chain picture transferring... (img-Isn0EXzm-1630555470026)]