├── ADCDemo
├── .classpath
├── .project
├── .settings
│ └── org.eclipse.jdt.core.prefs
├── Android.mk
├── AndroidManifest.xml
├── build.sh
├── libs
│ └── armeabi
│ │ └── libfriendlyarm-hardware.so
├── project.properties
├── res
│ ├── drawable
│ │ └── icon.png
│ ├── layout
│ │ ├── adc_listview_item.xml
│ │ └── adc_main.xml
│ └── values
│ │ └── strings.xml
└── src
│ └── com
│ └── friendlyarm
│ ├── ADCDemo
│ ├── ADC.java
│ └── ADCDemoMainActivity.java
│ ├── AndroidSDK
│ ├── FileCtlEnum.java
│ ├── GPIOEnum.java
│ ├── HardwareControler.java
│ ├── SPI.java
│ └── SPIEnum.java
│ └── Utils
│ └── CommonFuncs.java
├── GPIO_LED_Demo
├── .classpath
├── .project
├── .settings
│ └── org.eclipse.jdt.core.prefs
├── Android.mk
├── AndroidManifest.xml
├── build.sh
├── libs
│ └── armeabi
│ │ └── libfriendlyarm-hardware.so
├── project.properties
├── res
│ ├── drawable
│ │ └── icon.png
│ ├── layout
│ │ ├── checkbox_listview_item.xml
│ │ └── gpiodemo_main.xml
│ └── values
│ │ └── strings.xml
└── src
│ └── com
│ └── friendlyarm
│ ├── AndroidSDK
│ ├── FileCtlEnum.java
│ ├── GPIOEnum.java
│ ├── HardwareControler.java
│ ├── SPI.java
│ └── SPIEnum.java
│ ├── GPIODemo
│ ├── GPIODemoMainActivity.java
│ └── GPIOPin.java
│ └── Utils
│ └── CommonFuncs.java
├── I2C_LCD1602_Demo
├── .classpath
├── .project
├── .settings
│ └── org.eclipse.jdt.core.prefs
├── Android.mk
├── AndroidManifest.xml
├── build.sh
├── libs
│ └── armeabi
│ │ └── libfriendlyarm-hardware.so
├── project.properties
├── res
│ ├── drawable-hdpi
│ │ └── icon.png
│ ├── drawable-ldpi
│ │ └── icon.png
│ ├── drawable-mdpi
│ │ └── icon.png
│ ├── layout
│ │ └── mainactivity.xml
│ └── values
│ │ └── strings.xml
└── src
│ └── com
│ └── friendlyarm
│ ├── AndroidSDK
│ ├── FileCtlEnum.java
│ ├── GPIOEnum.java
│ ├── HardwareControler.java
│ ├── SPI.java
│ └── SPIEnum.java
│ ├── LCD1602Demo
│ ├── MainActivity.java
│ └── PCF8574.java
│ └── Utils
│ └── CommonFuncs.java
├── PWMDemo
├── .classpath
├── .project
├── .settings
│ └── org.eclipse.jdt.core.prefs
├── Android.mk
├── AndroidManifest.xml
├── build.sh
├── libs
│ └── armeabi
│ │ └── libfriendlyarm-hardware.so
├── project.properties
├── res
│ ├── drawable
│ │ └── icon.png
│ ├── layout
│ │ └── pwmtestingactivity.xml
│ └── values
│ │ └── strings.xml
└── src
│ └── com
│ └── friendlyarm
│ ├── AndroidSDK
│ ├── FileCtlEnum.java
│ ├── GPIOEnum.java
│ ├── HardwareControler.java
│ ├── SPI.java
│ └── SPIEnum.java
│ ├── PWMDemo
│ └── PWMTestingActivity.java
│ └── Utils
│ ├── BorderScrollView.java
│ └── CommonFuncs.java
├── README.md
├── SDCardDemo
├── Android.mk
├── AndroidManifest.xml
├── build.sh
├── ic_launcher-web.png
├── res
│ ├── drawable-hdpi
│ │ ├── ic_action_search.png
│ │ └── ic_launcher.png
│ ├── drawable-ldpi
│ │ └── ic_launcher.png
│ ├── drawable-mdpi
│ │ ├── ic_action_search.png
│ │ └── ic_launcher.png
│ ├── drawable-xhdpi
│ │ ├── ic_action_search.png
│ │ └── ic_launcher.png
│ ├── layout
│ │ └── activity_main.xml
│ ├── menu
│ │ └── activity_main.xml
│ ├── values-v11
│ │ └── styles.xml
│ ├── values-v14
│ │ └── styles.xml
│ └── values
│ │ ├── strings.xml
│ │ └── styles.xml
└── src
│ └── com
│ └── FriendlyARM
│ └── sdcarddemo
│ └── MainActivity.java
├── SPI_OLED_Demo
├── .classpath
├── .project
├── .settings
│ └── org.eclipse.jdt.core.prefs
├── Android.mk
├── AndroidManifest.xml
├── Smart4418SDK+OLED.png
├── build.sh
├── libs
│ └── armeabi
│ │ └── libfriendlyarm-hardware.so
├── project.properties
├── res
│ ├── drawable
│ │ └── icon.png
│ ├── layout
│ │ └── mainactivity.xml
│ └── values
│ │ └── strings.xml
└── src
│ └── com
│ └── friendlyarm
│ ├── AndroidSDK
│ ├── FileCtlEnum.java
│ ├── GPIOEnum.java
│ ├── HardwareControler.java
│ ├── SPI.java
│ └── SPIEnum.java
│ ├── SPI_OLED
│ ├── MainActivity.java
│ └── OLED.java
│ └── Utils
│ └── CommonFuncs.java
├── SerialPortDemo
├── .classpath
├── AndroidManifest.xml
├── libs
│ └── armeabi
│ │ └── libfriendlyarm-hardware.so
├── res
│ ├── drawable
│ │ └── icon.png
│ ├── layout
│ │ ├── serialport_dataprocessview.xml
│ │ └── serialport_dataprocessview_landscape.xml
│ └── values
│ │ └── strings.xml
└── src
│ └── com
│ └── friendlyarm
│ ├── AndroidSDK
│ ├── FileCtlEnum.java
│ ├── GPIOEnum.java
│ ├── HardwareControler.java
│ ├── SPI.java
│ └── SPIEnum.java
│ ├── SerialPortDemo
│ └── SerialPortDataProcessView.java
│ └── Utils
│ ├── BorderScrollView.java
│ └── CommonFuncs.java
└── 友善电子Android硬件开发指南.pdf
/ADCDemo/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/ADCDemo/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | ADCDemoMainActivity
4 |
5 |
6 |
7 |
8 |
9 | com.android.ide.eclipse.adt.ResourceManagerBuilder
10 |
11 |
12 |
13 |
14 | com.android.ide.eclipse.adt.PreCompilerBuilder
15 |
16 |
17 |
18 |
19 | org.eclipse.jdt.core.javabuilder
20 |
21 |
22 |
23 |
24 | com.android.ide.eclipse.adt.ApkBuilder
25 |
26 |
27 |
28 |
29 |
30 | com.android.ide.eclipse.adt.AndroidNature
31 | org.eclipse.jdt.core.javanature
32 |
33 |
34 |
--------------------------------------------------------------------------------
/ADCDemo/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
3 | org.eclipse.jdt.core.compiler.compliance=1.6
4 | org.eclipse.jdt.core.compiler.source=1.6
5 |
--------------------------------------------------------------------------------
/ADCDemo/Android.mk:
--------------------------------------------------------------------------------
1 | LOCAL_PATH:= $(call my-dir)
2 | include $(CLEAR_VARS)
3 |
4 | LOCAL_SRC_FILES := $(call all-subdir-java-files)
5 |
6 | LOCAL_PACKAGE_NAME := ADCDemo
7 |
8 | LOCAL_CERTIFICATE := platform
9 | LOCAL_MODULE_TAGS := eng
10 |
11 | include $(BUILD_PACKAGE)
12 |
--------------------------------------------------------------------------------
/ADCDemo/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/ADCDemo/build.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | ANDROIDPATH=/opt/FriendlyARM/smart4418/android/
4 | APPNAME=ADCDemo
5 | APKNAME=${APPNAME}-r1.0.apk
6 | PRJPATH=$PWD
7 | cd $PRJPATH
8 | rm -rf bin gen
9 |
10 | cd $ANDROIDPATH
11 | rm -f $APKNAME
12 | . setenv
13 | mmm $PRJPATH -B
14 | java -jar out/host/linux-x86/framework/signapk.jar vendor/friendly-arm/nanopi2/security/platform.x509.pem vendor/friendly-arm/nanopi2/security/platform.pk8 out/target/product/nanopi2/obj/APPS/${APPNAME}_intermediates/package.apk.unsigned ${APPNAME}-unaligned.apk
15 | zipalign -v 4 ${APPNAME}-unaligned.apk $APKNAME
16 | adb install -r ${APKNAME}
17 |
--------------------------------------------------------------------------------
/ADCDemo/libs/armeabi/libfriendlyarm-hardware.so:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/friendlyarm/AndroidHardwareAccess/8084bdb9d85b9e466aea5544769ffb30d60dfda3/ADCDemo/libs/armeabi/libfriendlyarm-hardware.so
--------------------------------------------------------------------------------
/ADCDemo/project.properties:
--------------------------------------------------------------------------------
1 | # This file is automatically generated by Android Tools.
2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3 | #
4 | # This file must be checked in Version Control Systems.
5 | #
6 | # To customize properties used by the Ant build system edit
7 | # "ant.properties", and override values to adapt the script to your
8 | # project structure.
9 | #
10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
12 |
13 | # Project target.
14 | target=android-17
15 |
--------------------------------------------------------------------------------
/ADCDemo/res/drawable/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/friendlyarm/AndroidHardwareAccess/8084bdb9d85b9e466aea5544769ffb30d60dfda3/ADCDemo/res/drawable/icon.png
--------------------------------------------------------------------------------
/ADCDemo/res/layout/adc_listview_item.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
15 |
16 |
26 |
27 |
--------------------------------------------------------------------------------
/ADCDemo/res/layout/adc_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
9 |
10 |
--------------------------------------------------------------------------------
/ADCDemo/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | ADC Demo
4 | ADC Testing: /dev/adc
5 |
6 |
--------------------------------------------------------------------------------
/ADCDemo/src/com/friendlyarm/ADCDemo/ADC.java:
--------------------------------------------------------------------------------
1 | package com.friendlyarm.ADCDemo;
2 |
3 | public class ADC {
4 |
5 | int value = 0;
6 | String name = null;
7 |
8 | public ADC(int value, String name) {
9 | super();
10 | this.value = value;
11 | this.name = name;
12 | }
13 |
14 | public int getValue() {
15 | return value;
16 | }
17 |
18 | public void setValue(int value) {
19 | this.value = value;
20 | }
21 |
22 | public String getName() {
23 | return name;
24 | }
25 |
26 | public void setName(String name) {
27 | this.name = name;
28 | }
29 |
30 | }
--------------------------------------------------------------------------------
/ADCDemo/src/com/friendlyarm/ADCDemo/ADCDemoMainActivity.java:
--------------------------------------------------------------------------------
1 | package com.friendlyarm.ADCDemo;
2 | import java.util.ArrayList;
3 | import java.util.Timer;
4 | import java.util.TimerTask;
5 | import android.app.Activity;
6 | import android.content.Context;
7 | import android.os.Bundle;
8 | import android.os.Handler;
9 | import android.os.Message;
10 | import android.util.Log;
11 | import android.view.LayoutInflater;
12 | import android.view.View;
13 | import android.view.ViewGroup;
14 | import android.widget.ArrayAdapter;
15 | import android.widget.ListView;
16 | import android.widget.TextView;
17 |
18 | import com.friendlyarm.AndroidSDK.HardwareControler;
19 |
20 | public class ADCDemoMainActivity extends Activity {
21 |
22 | MyCustomAdapter dataAdapter = null;
23 |
24 | @Override
25 | public void onCreate(Bundle savedInstanceState) {
26 | super.onCreate(savedInstanceState);
27 | setContentView(R.layout.adc_main);
28 | Log.d("ADCDemo", "BoardID: " + boardType);
29 | timer.schedule(task, 0, 500);
30 | }
31 |
32 | private int boardType = HardwareControler.getBoardType();
33 | private Timer timer = new Timer();
34 | private Handler handler = new Handler() {
35 | public void handleMessage(Message msg) {
36 |
37 | // Array list of countries
38 | ArrayList adcValueList = new ArrayList();
39 |
40 | switch (msg.what) {
41 | case 1:
42 | if (boardType == HardwareControler.S3C6410_COMMON) {
43 | int[] channels = { 0, 1, 4, 5, 6, 7 };
44 | int[] adc = HardwareControler.readADCWithChannels(channels);
45 |
46 | for (int i = 0; i < 6; i++) {
47 | ADC adcObj = new ADC(adc[i], String.format("[AIN%d]",
48 | channels[i]));
49 | adcValueList.add(adcObj);
50 | }
51 |
52 | } else if (boardType == HardwareControler.S5PV210_COMMON) {
53 | int[] channels = { 0, 1, 6, 7, 8, 9 };
54 | int[] adc = HardwareControler.readADCWithChannels(channels);
55 |
56 | for (int i = 0; i < 6; i++) {
57 | ADC adcObj = new ADC(adc[i], String.format("[AIN%d]",
58 | channels[i]));
59 | adcValueList.add(adcObj);
60 | }
61 |
62 | } else if (boardType == HardwareControler.S5P4412_COMMON) {
63 | int[] channels = { 0, 1, 2, 3 };
64 | int[] adc = HardwareControler.readADCWithChannels(channels);
65 |
66 | for (int i = 0; i < 4; i++) {
67 | ADC adcObj = new ADC(adc[i], String.format("[AIN%d]",
68 | channels[i]));
69 | adcValueList.add(adcObj);
70 | }
71 |
72 | } else if (boardType == HardwareControler.Smart4418SDK) {
73 | int[] channels = { 1, 3, 4, 5, 6, 7 };
74 | int[] adc = HardwareControler.readADCWithChannels(channels);
75 |
76 | for (int i = 0; i < channels.length; i++) {
77 | ADC adcObj = new ADC(adc[i], String.format("[AIN%d]",
78 | channels[i]));
79 | adcValueList.add(adcObj);
80 | }
81 | } else {
82 | int adc = HardwareControler.readADCWithChannel(0);
83 | ADC adcObj = new ADC(adc, String.format("[AIN%d]", 0));
84 | adcValueList.add(adcObj);
85 | }
86 |
87 | dataAdapter = new MyCustomAdapter(getApplicationContext(),
88 | R.layout.adc_listview_item, adcValueList);
89 | ListView listView = (ListView) findViewById(R.id.listView1);
90 | listView.setAdapter(dataAdapter);
91 |
92 | break;
93 | }
94 | super.handleMessage(msg);
95 | }
96 | };
97 | private TimerTask task = new TimerTask() {
98 | public void run() {
99 | Message message = new Message();
100 | message.what = 1;
101 | handler.sendMessage(message);
102 | }
103 | };
104 |
105 |
106 | private class MyCustomAdapter extends ArrayAdapter {
107 |
108 | private ArrayList adcValueList;
109 |
110 | public MyCustomAdapter(Context context, int textViewResourceId,
111 | ArrayList adcValueList) {
112 | super(context, textViewResourceId, adcValueList);
113 | this.adcValueList = new ArrayList();
114 | this.adcValueList.addAll(adcValueList);
115 | }
116 |
117 | private class ViewHolder {
118 | TextView nameTextView;
119 | TextView valueTextView;
120 | }
121 |
122 | @Override
123 | public View getView(int position, View convertView, ViewGroup parent) {
124 |
125 | ViewHolder holder = null;
126 | Log.v("ConvertView", String.valueOf(position));
127 |
128 | if (convertView == null) {
129 | LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
130 | convertView = vi.inflate(R.layout.adc_listview_item, null);
131 |
132 | holder = new ViewHolder();
133 |
134 | holder.nameTextView = (TextView) convertView
135 | .findViewById(R.id.listTextView1);
136 |
137 | holder.valueTextView = (TextView) convertView
138 | .findViewById(R.id.listTextView2);
139 |
140 | convertView.setTag(holder);
141 |
142 | } else {
143 | holder = (ViewHolder) convertView.getTag();
144 | }
145 |
146 | ADC adcObj = adcValueList.get(position);
147 | if (holder.nameTextView != null) {
148 | holder.nameTextView.setText(adcObj.getName());
149 | }
150 | if (holder.valueTextView != null) {
151 | holder.valueTextView.setText(String.valueOf(adcObj.getValue()));
152 | }
153 |
154 | return convertView;
155 |
156 | }
157 |
158 | }
159 | }
--------------------------------------------------------------------------------
/ADCDemo/src/com/friendlyarm/AndroidSDK/FileCtlEnum.java:
--------------------------------------------------------------------------------
1 | package com.friendlyarm.AndroidSDK;
2 |
3 | public class FileCtlEnum {
4 | /* File Flag */
5 | public final static int O_ACCMODE = 00000003;
6 | public final static int O_RDONLY = 00000000;
7 | public final static int O_WRONLY = 00000001;
8 | public final static int O_RDWR = 00000002;
9 | public final static int O_CREAT = 00000100; /* not fcntl */
10 | public final static int O_EXCL = 00000200; /* not fcntl */
11 | public final static int O_NOCTTY = 00000400; /* not fcntl */
12 | public final static int O_TRUNC = 00001000; /* not fcntl */
13 | public final static int O_APPEND = 00002000;
14 | public final static int O_NONBLOCK = 00004000;
15 | public final static int O_DSYNC = 00010000; /* used to be O_SYNC, see below */
16 | public final static int FASYNC = 00020000; /* fcntl, for BSD compatibility */
17 | public final static int O_DIRECT = 00040000; /* direct disk access hint */
18 | public final static int O_LARGEFILE = 00100000;
19 | public final static int O_DIRECTORY = 00200000; /* must be a directory */
20 | public final static int O_NOFOLLOW = 00400000; /* don't follow links */
21 | public final static int O_NOATIME = 01000000;
22 | public final static int O_CLOEXEC = 02000000; /* set close_on_exec */
23 | }
24 |
--------------------------------------------------------------------------------
/ADCDemo/src/com/friendlyarm/AndroidSDK/GPIOEnum.java:
--------------------------------------------------------------------------------
1 | package com.friendlyarm.AndroidSDK;
2 |
3 | /*
4 | GPIO signals have paths like /sys/class/gpio/gpio42/ (for GPIO #42)
5 | and have the following read/write attributes:
6 |
7 | /sys/class/gpio/gpioN/
8 |
9 | "direction" ... reads as either "in" or "out". This value may
10 | normally be written. Writing as "out" defaults to
11 | initializing the value as low. To ensure glitch free
12 | operation, values "low" and "high" may be written to
13 | configure the GPIO as an output with that initial value.
14 |
15 | Note that this attribute *will not exist* if the kernel
16 | doesn't support changing the direction of a GPIO, or
17 | it was exported by kernel code that didn't explicitly
18 | allow userspace to reconfigure this GPIO's direction.
19 |
20 | "value" ... reads as either 0 (low) or 1 (high). If the GPIO
21 | is configured as an output, this value may be written;
22 | any nonzero value is treated as high.
23 |
24 | If the pin can be configured as interrupt-generating interrupt
25 | and if it has been configured to generate interrupts (see the
26 | description of "edge"), you can poll(2) on that file and
27 | poll(2) will return whenever the interrupt was triggered. If
28 | you use poll(2), set the events POLLPRI and POLLERR. If you
29 | use select(2), set the file descriptor in exceptfds. After
30 | poll(2) returns, either lseek(2) to the beginning of the sysfs
31 | file and read the new value or close the file and re-open it
32 | to read the value.
33 | */
34 |
35 | public class GPIOEnum {
36 | //Direction
37 | public final static int IN = 1;
38 | public final static int OUT = 2;
39 |
40 | //Value
41 | public final static int LOW = 0;
42 | public final static int HIGH = 1;
43 | }
44 |
--------------------------------------------------------------------------------
/ADCDemo/src/com/friendlyarm/AndroidSDK/HardwareControler.java:
--------------------------------------------------------------------------------
1 | package com.friendlyarm.AndroidSDK;
2 | import android.util.Log;
3 |
4 | public class HardwareControler
5 | {
6 | /* File */
7 | static public native int open(String devName, int flags);
8 | static public native int write(int fd, byte[] data);
9 | static public native int read(int fd, byte[] buf, int len);
10 | static public native int select(int fd, int sec, int usec);
11 | static public native void close(int fd);
12 | static public native int ioctlWithIntValue(int fd, int cmd, int value);
13 |
14 | /* Serial Port */
15 | static public native int openSerialPort( String devName, long baud, int dataBits, int stopBits );
16 | static public native int openSerialPortEx( String devName
17 | , long baud
18 | , int dataBits
19 | , int stopBits
20 | , String parityBit
21 | , String flowCtrl
22 | );
23 |
24 | /* LED */
25 | static public native int setLedState( int ledID, int ledState );
26 |
27 | /* PWM */
28 | static public native int PWMPlay(int frequency);
29 | static public native int PWMStop();
30 | static public native int PWMPlayEx(int gpioPin, int frequency);
31 | static public native int PWMStopEx(int gpioPin);
32 |
33 | /* ADC */
34 | static public native int readADC();
35 | static public native int readADCWithChannel(int channel);
36 | static public native int[] readADCWithChannels(int[] channels);
37 |
38 | /* I2C */
39 | static public native int setI2CSlave(int fd, int slave);
40 | static public native int setI2CTimeout(int fd, int timeout);
41 | static public native int setI2CRetries(int fd, int retries);
42 | static public native int I2CReadByteData(int fd, int pos, int wait_us);
43 | static public native int I2CReadByte(int fd, int wait_us);
44 | static public native int I2CWriteByteData(int fd, int pos, byte byteData, int wait_us);
45 | static public native int I2CWriteByte(int fd, byte byteData, int wait_us);
46 |
47 | /* Discard */ static public native int readByteFromI2C(int fd, int pos, int wait_ms);
48 | /* Discard */ static public native int writeByteToI2C(int fd, int pos, byte byteData, int wait_ms);
49 | /* Discard */ static public native int writeByteToI2C2(int fd, byte byteData, int wait_ms);
50 |
51 | /* SPI */
52 | static public native int setSPIWriteBitsPerWord( int spi_fd, int bits );
53 | static public native int setSPIReadBitsPerWord( int spi_fd, int bits );
54 | static public native int setSPIBitOrder( int spi_fd, int order);
55 | static public native int setSPIClockDivider( int spi_fd, int divider);
56 | static public native int setSPIMaxSpeed( int spi_fd, int spi_speed);
57 | static public native int setSPIDataMode( int spi_fd, int mode);
58 | static public native int SPItransferOneByte( int spi_fd, byte byteData, int spi_delay, int spi_speed, int spi_bits);
59 | static public native int SPItransferBytes(int spi_fd, byte[] writeData, byte[] readBuff, int spi_delay, int spi_speed, int spi_bits);
60 | static public native int writeBytesToSPI(int spi_fd, byte[] writeData, int spi_delay, int spi_speed, int spi_bits);
61 | static public native int readBytesFromSPI(int spi_fd, byte[] readBuff, int spi_delay, int spi_speed, int spi_bits);
62 |
63 | /* GPIO */
64 | static public native int exportGPIOPin(int pin);
65 | static public native int unexportGPIOPin(int pin);
66 | //GPIOEnum.LOW or GPIOEnum.HIGH
67 | static public native int setGPIOValue(int pin, int value);
68 | static public native int getGPIOValue(int pin);
69 | //GPIOEnum.IN or GPIOEnum.OUT
70 | static public native int setGPIODirection(int pin, int direction);
71 | static public native int getGPIODirection(int pin);
72 |
73 | /* OldInterface: for EEPROM */
74 | static public native int openI2CDevice();
75 | static public native int writeByteDataToI2C(int fd, int pos, byte byteData);
76 | static public native int readByteDataFromI2C(int fd, int pos);
77 |
78 | static public native int getBoardType();
79 | /* getBoardType return value: */
80 | public static final int S3C6410_COMMON = 6410;
81 | public static final int S5PV210_COMMON = 210;
82 | public static final int S5P4412_COMMON = 4412;
83 | public static final int S5P4418_BASE = 4418;
84 | public static final int NanoPi2 = S5P4418_BASE+0;
85 | public static final int NanoPC_T2 = S5P4418_BASE+1;
86 | public static final int NanoPi_S2 = S5P4418_BASE+2;
87 | public static final int Smart4418 = S5P4418_BASE+3;
88 | public static final int NanoPi2_Fire = S5P4418_BASE+4;
89 | public static final int NanoPi_M2 = S5P4418_BASE+5;
90 | public static final int NanoPi_M2A = S5P4418_BASE+7;
91 | public static final int Smart4418SDK = S5P4418_BASE+0x103;
92 | public static final int S5P4418_MAX = Smart4418SDK;
93 | public static final int S5P6818_BASE = 6818;
94 | public static final int NanoPC_T3 = S5P6818_BASE+1;
95 | public static final int NanoPi_S3 = S5P6818_BASE+2;
96 | public static final int Smart6818 = S5P6818_BASE+3;
97 | public static final int NanoPi_M3 = S5P6818_BASE+7;
98 | public static final int S5P6818_MAX = NanoPi_M3;
99 |
100 | static public boolean isS5P4418Board() {
101 | int boardtype = getBoardType();
102 | return boardtype>=S5P4418_BASE && boardtype<=S5P4418_MAX;
103 | }
104 |
105 | static public boolean isS5P6818Board() {
106 | int boardtype = getBoardType();
107 | return boardtype>=S5P6818_BASE && boardtype<=S5P6818_MAX;
108 | }
109 |
110 | static {
111 | try {
112 | System.loadLibrary("friendlyarm-hardware");
113 | } catch (UnsatisfiedLinkError e) {
114 | Log.d("HardwareControler", "libfriendlyarm-hardware library not found!");
115 | }
116 | }
117 | }
118 |
--------------------------------------------------------------------------------
/ADCDemo/src/com/friendlyarm/AndroidSDK/SPI.java:
--------------------------------------------------------------------------------
1 | package com.friendlyarm.AndroidSDK;
2 | import android.util.Log;
3 | import android.widget.Toast;
4 |
5 | import com.friendlyarm.AndroidSDK.HardwareControler;
6 | import com.friendlyarm.AndroidSDK.SPIEnum;
7 | import com.friendlyarm.AndroidSDK.GPIOEnum;
8 | import com.friendlyarm.AndroidSDK.FileCtlEnum;
9 |
10 | public class SPI {
11 | private static final String TAG = "com.friendlyarm.AndroidSDK.SPI";
12 | private int spi_mode = 0;
13 | private int spi_bits = 8;
14 | private int spi_delay = 0;
15 | private int spi_speed = 500000;
16 | private int spi_byte_order = SPIEnum.LSBFIRST;
17 |
18 | private static final String devName = "/dev/spidev1.0";
19 | private int spi_fd = -1;
20 |
21 | public void begin() {
22 | spi_fd = HardwareControler.open( devName, FileCtlEnum.O_RDWR );
23 | if (spi_fd >= 0) {
24 | Log.d(TAG, "open " + devName + "ok!");
25 |
26 | /* spi init */
27 | HardwareControler.setSPIWriteBitsPerWord( spi_fd, spi_bits );
28 | HardwareControler.setSPIReadBitsPerWord( spi_fd, spi_bits );
29 | } else {
30 | Log.d(TAG, "open " + devName + "failed!");
31 | spi_fd = -1;
32 | }
33 | }
34 |
35 | public void end() {
36 | if (spi_fd != -1) {
37 | HardwareControler.close(spi_fd);
38 | spi_fd = -1;
39 | }
40 | }
41 |
42 | public void setBitOrder(int order) {
43 | if (spi_fd < 0) {
44 | return ;
45 | }
46 | spi_byte_order = SPIEnum.MSBFIRST;
47 | if(spi_byte_order == SPIEnum.LSBFIRST) {
48 | spi_mode |= SPIEnum.SPI_LSB_FIRST;
49 | } else {
50 | spi_mode &= ~SPIEnum.SPI_LSB_FIRST;
51 | }
52 | HardwareControler.setSPIBitOrder( spi_fd, spi_byte_order );
53 |
54 | }
55 |
56 | public void setClockDivider(int divider) {
57 | if (spi_fd < 0) {
58 | return ;
59 | }
60 | spi_speed = 66666666/(2*(divider+1));
61 | if(spi_speed > 500000) {
62 | spi_speed = 500000;
63 | }
64 | HardwareControler.setSPIClockDivider( spi_fd, divider);
65 | }
66 |
67 | public void setDataMode(int mode) {
68 | if (spi_fd < 0) {
69 | return ;
70 | }
71 | switch(mode)
72 | {
73 | case SPIEnum.SPI_MODE0:
74 | spi_mode &= ~(SPIEnum.SPI_CPHA|SPIEnum.SPI_CPOL);
75 | break;
76 | case SPIEnum.SPI_MODE1:
77 | spi_mode &= ~(SPIEnum.SPI_CPOL);
78 | spi_mode |= (SPIEnum.SPI_CPHA);
79 | break;
80 | case SPIEnum.SPI_MODE2:
81 | spi_mode |= (SPIEnum.SPI_CPOL);
82 | spi_mode &= ~(SPIEnum.SPI_CPHA);
83 | break;
84 | case SPIEnum.SPI_MODE3:
85 | spi_mode |= (SPIEnum.SPI_CPHA|SPIEnum.SPI_CPOL);
86 | break;
87 | default:
88 | Log.e(TAG, "error data mode");
89 | }
90 |
91 | HardwareControler.setSPIDataMode( spi_fd, spi_mode );
92 | }
93 |
94 | public void setChipSelectPolarity(int cs, int active) {
95 |
96 | }
97 |
98 | public void chipSelect(int cs) {
99 |
100 | }
101 |
102 | public byte transfer(int value) {
103 | if (spi_fd < 0) {
104 | return 0;
105 | }
106 | return (byte) HardwareControler.SPItransferOneByte(spi_fd, (byte) value, spi_delay, spi_speed, spi_bits);
107 | }
108 | }
109 |
--------------------------------------------------------------------------------
/ADCDemo/src/com/friendlyarm/AndroidSDK/SPIEnum.java:
--------------------------------------------------------------------------------
1 | package com.friendlyarm.AndroidSDK;
2 | public class SPIEnum {
3 |
4 | // SPIBitOrder
5 | public final static int LSBFIRST = 0; ///< LSB First
6 | public final static int MSBFIRST = 1; ///< MSB First
7 |
8 | // SPIMode
9 | public final static int SPI_MODE0 = 0; ///< CPOL = 0, CPHA = 0
10 | public final static int SPI_MODE1 = 1; ///< CPOL = 0, CPHA = 1
11 | public final static int SPI_MODE2 = 2; ///< CPOL = 1, CPHA = 0
12 | public final static int SPI_MODE3 = 3; ///< CPOL = 1, CPHA = 1
13 |
14 |
15 | public final static int SPI_CPHA = 0x01;
16 | public final static int SPI_CPOL = 0x02;
17 | public final static int SPI_CS_HIGH = 0x04;
18 | public final static int SPI_LSB_FIRST = 0x08;
19 | public final static int SPI_3WIRE = 0x10;
20 | public final static int SPI_LOOP = 0x20;
21 | public final static int SPI_NO_CS = 0x40;
22 | public final static int SPI_READY = 0x80;
23 |
24 | // SPIClockDivider
25 | public final static int SPI_CLOCK_DIV65536 = 0; ///< 65536 = 256us = 4kHz
26 | public final static int SPI_CLOCK_DIV32768 = 32768; ///< 32768 = 126us = 8kHz
27 | public final static int SPI_CLOCK_DIV16384 = 16384; ///< 16384 = 64us = 15.625kHz
28 | public final static int SPI_CLOCK_DIV8192 = 8192; ///< 8192 = 32us = 31.25kHz
29 | public final static int SPI_CLOCK_DIV4096 = 4096; ///< 4096 = 16us = 62.5kHz
30 | public final static int SPI_CLOCK_DIV2048 = 2048; ///< 2048 = 8us = 125kHz
31 | public final static int SPI_CLOCK_DIV1024 = 1024; ///< 1024 = 4us = 250kHz
32 | public final static int SPI_CLOCK_DIV512 = 512; ///< 512 = 2us = 500kHz
33 | public final static int SPI_CLOCK_DIV256 = 256; ///< 256 = 1us = 1MHz
34 | public final static int SPI_CLOCK_DIV128 = 128; ///< 128 = 500ns = = 2MHz
35 | public final static int SPI_CLOCK_DIV64 = 64; ///< 64 = 250ns = 4MHz
36 | public final static int SPI_CLOCK_DIV32 = 32; ///< 32 = 125ns = 8MHz
37 | public final static int SPI_CLOCK_DIV16 = 16; ///< 16 = 50ns = 20MHz
38 | public final static int SPI_CLOCK_DIV8 = 8; ///< 8 = 25ns = 40MHz
39 | public final static int SPI_CLOCK_DIV4 = 4; ///< 4 = 12.5ns 80MHz
40 | public final static int SPI_CLOCK_DIV2 = 2; ///< 2 = 6.25ns = 160MHz
41 | public final static int SPI_CLOCK_DIV1 = 1; ///< 0 = 256us = 4kHz
42 | }
--------------------------------------------------------------------------------
/ADCDemo/src/com/friendlyarm/Utils/CommonFuncs.java:
--------------------------------------------------------------------------------
1 | package com.friendlyarm.Utils;
2 |
3 | import android.app.Activity;
4 | import android.app.AlertDialog;
5 | import android.widget.Toast;
6 | import android.app.Activity;
7 | import android.os.Bundle;
8 | import android.os.Handler;
9 | import android.os.Message;
10 | import android.content.Context;
11 | import android.content.DialogInterface;
12 |
13 | public class CommonFuncs {
14 | public static void showAlertDialog(Context context, String message) {
15 | AlertDialog.Builder builder = new AlertDialog.Builder(context);
16 | builder.setMessage(message)
17 | .setCancelable(false)
18 | .setPositiveButton("Close",
19 | new DialogInterface.OnClickListener() {
20 | public void onClick(DialogInterface dialog, int id) {
21 | }
22 | });
23 | AlertDialog alert = builder.create();
24 | alert.show();
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/GPIO_LED_Demo/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/GPIO_LED_Demo/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | GPIO_LED_Demo
4 |
5 |
6 |
7 |
8 |
9 | com.android.ide.eclipse.adt.ResourceManagerBuilder
10 |
11 |
12 |
13 |
14 | com.android.ide.eclipse.adt.PreCompilerBuilder
15 |
16 |
17 |
18 |
19 | org.eclipse.jdt.core.javabuilder
20 |
21 |
22 |
23 |
24 | com.android.ide.eclipse.adt.ApkBuilder
25 |
26 |
27 |
28 |
29 |
30 | com.android.ide.eclipse.adt.AndroidNature
31 | org.eclipse.jdt.core.javanature
32 |
33 |
34 |
--------------------------------------------------------------------------------
/GPIO_LED_Demo/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
3 | org.eclipse.jdt.core.compiler.compliance=1.6
4 | org.eclipse.jdt.core.compiler.source=1.6
5 |
--------------------------------------------------------------------------------
/GPIO_LED_Demo/Android.mk:
--------------------------------------------------------------------------------
1 | LOCAL_PATH:= $(call my-dir)
2 | include $(CLEAR_VARS)
3 |
4 | LOCAL_SRC_FILES := $(call all-subdir-java-files)
5 |
6 | LOCAL_PACKAGE_NAME := GPIODemo
7 |
8 | LOCAL_CERTIFICATE := platform
9 | LOCAL_MODULE_TAGS := eng
10 |
11 | include $(BUILD_PACKAGE)
12 |
--------------------------------------------------------------------------------
/GPIO_LED_Demo/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/GPIO_LED_Demo/build.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 |
4 | cd /opt/FriendlyARM/android_examples/jni-demos/GPIO_LED_Demo
5 | rm -rf gen bin
6 |
7 | cd /opt/FriendlyARM/smart4418/android/
8 | rm -f GPIODemo-r1.0.apk
9 | . setenv
10 | mmm /opt/FriendlyARM/android_examples/jni-demos/GPIO_LED_Demo
11 | java -jar out/host/linux-x86/framework/signapk.jar vendor/friendly-arm/nanopi3/security/platform.x509.pem vendor/friendly-arm/nanopi3/security/platform.pk8 /opt/FriendlyARM/smart4418/android/out/target/product/nanopi3/obj/APPS/GPIODemo_intermediates/package.apk.unsigned GPIODemo-unaligned.apk
12 | zipalign -v 4 GPIODemo-unaligned.apk GPIODemo-r1.0.apk
13 | # out/host/linux-x86/bin/zipalign -f 4 out/target/product/nanopi3/obj/APPS/GPIODemo_intermediates/package.apk.unaligned out/target/product/nanopi3/obj/APPS/GPIODemo_intermediates/package.apk.aligned
14 | adb install -r GPIODemo-r1.0.apk
15 |
--------------------------------------------------------------------------------
/GPIO_LED_Demo/libs/armeabi/libfriendlyarm-hardware.so:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/friendlyarm/AndroidHardwareAccess/8084bdb9d85b9e466aea5544769ffb30d60dfda3/GPIO_LED_Demo/libs/armeabi/libfriendlyarm-hardware.so
--------------------------------------------------------------------------------
/GPIO_LED_Demo/project.properties:
--------------------------------------------------------------------------------
1 | # This file is automatically generated by Android Tools.
2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3 | #
4 | # This file must be checked in Version Control Systems.
5 | #
6 | # To customize properties used by the Ant build system edit
7 | # "ant.properties", and override values to adapt the script to your
8 | # project structure.
9 | #
10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
12 |
13 | # Project target.
14 | target=android-17
15 |
--------------------------------------------------------------------------------
/GPIO_LED_Demo/res/drawable/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/friendlyarm/AndroidHardwareAccess/8084bdb9d85b9e466aea5544769ffb30d60dfda3/GPIO_LED_Demo/res/drawable/icon.png
--------------------------------------------------------------------------------
/GPIO_LED_Demo/res/layout/checkbox_listview_item.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/GPIO_LED_Demo/res/layout/gpiodemo_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
9 |
10 |
--------------------------------------------------------------------------------
/GPIO_LED_Demo/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | GPIO Demo
4 | GPIO Demo
5 | LED
6 |
7 |
--------------------------------------------------------------------------------
/GPIO_LED_Demo/src/com/friendlyarm/AndroidSDK/FileCtlEnum.java:
--------------------------------------------------------------------------------
1 | package com.friendlyarm.AndroidSDK;
2 |
3 | public class FileCtlEnum {
4 | /* File Flag */
5 | public final static int O_ACCMODE = 00000003;
6 | public final static int O_RDONLY = 00000000;
7 | public final static int O_WRONLY = 00000001;
8 | public final static int O_RDWR = 00000002;
9 | public final static int O_CREAT = 00000100; /* not fcntl */
10 | public final static int O_EXCL = 00000200; /* not fcntl */
11 | public final static int O_NOCTTY = 00000400; /* not fcntl */
12 | public final static int O_TRUNC = 00001000; /* not fcntl */
13 | public final static int O_APPEND = 00002000;
14 | public final static int O_NONBLOCK = 00004000;
15 | public final static int O_DSYNC = 00010000; /* used to be O_SYNC, see below */
16 | public final static int FASYNC = 00020000; /* fcntl, for BSD compatibility */
17 | public final static int O_DIRECT = 00040000; /* direct disk access hint */
18 | public final static int O_LARGEFILE = 00100000;
19 | public final static int O_DIRECTORY = 00200000; /* must be a directory */
20 | public final static int O_NOFOLLOW = 00400000; /* don't follow links */
21 | public final static int O_NOATIME = 01000000;
22 | public final static int O_CLOEXEC = 02000000; /* set close_on_exec */
23 | }
24 |
--------------------------------------------------------------------------------
/GPIO_LED_Demo/src/com/friendlyarm/AndroidSDK/GPIOEnum.java:
--------------------------------------------------------------------------------
1 | package com.friendlyarm.AndroidSDK;
2 |
3 | /*
4 | GPIO signals have paths like /sys/class/gpio/gpio42/ (for GPIO #42)
5 | and have the following read/write attributes:
6 |
7 | /sys/class/gpio/gpioN/
8 |
9 | "direction" ... reads as either "in" or "out". This value may
10 | normally be written. Writing as "out" defaults to
11 | initializing the value as low. To ensure glitch free
12 | operation, values "low" and "high" may be written to
13 | configure the GPIO as an output with that initial value.
14 |
15 | Note that this attribute *will not exist* if the kernel
16 | doesn't support changing the direction of a GPIO, or
17 | it was exported by kernel code that didn't explicitly
18 | allow userspace to reconfigure this GPIO's direction.
19 |
20 | "value" ... reads as either 0 (low) or 1 (high). If the GPIO
21 | is configured as an output, this value may be written;
22 | any nonzero value is treated as high.
23 |
24 | If the pin can be configured as interrupt-generating interrupt
25 | and if it has been configured to generate interrupts (see the
26 | description of "edge"), you can poll(2) on that file and
27 | poll(2) will return whenever the interrupt was triggered. If
28 | you use poll(2), set the events POLLPRI and POLLERR. If you
29 | use select(2), set the file descriptor in exceptfds. After
30 | poll(2) returns, either lseek(2) to the beginning of the sysfs
31 | file and read the new value or close the file and re-open it
32 | to read the value.
33 | */
34 |
35 | public class GPIOEnum {
36 | //Direction
37 | public final static int IN = 1;
38 | public final static int OUT = 2;
39 |
40 | //Value
41 | public final static int LOW = 0;
42 | public final static int HIGH = 1;
43 | }
44 |
--------------------------------------------------------------------------------
/GPIO_LED_Demo/src/com/friendlyarm/AndroidSDK/HardwareControler.java:
--------------------------------------------------------------------------------
1 | package com.friendlyarm.AndroidSDK;
2 | import android.util.Log;
3 |
4 | public class HardwareControler
5 | {
6 | /* File */
7 | static public native int open(String devName, int flags);
8 | static public native int write(int fd, byte[] data);
9 | static public native int read(int fd, byte[] buf, int len);
10 | static public native int select(int fd, int sec, int usec);
11 | static public native void close(int fd);
12 | static public native int ioctlWithIntValue(int fd, int cmd, int value);
13 |
14 | /* Serial Port */
15 | static public native int openSerialPort( String devName, long baud, int dataBits, int stopBits );
16 | static public native int openSerialPortEx( String devName
17 | , long baud
18 | , int dataBits
19 | , int stopBits
20 | , String parityBit
21 | , String flowCtrl
22 | );
23 |
24 | /* LED */
25 | static public native int setLedState( int ledID, int ledState );
26 |
27 | /* PWM */
28 | static public native int PWMPlay(int frequency);
29 | static public native int PWMStop();
30 | static public native int PWMPlayEx(int gpioPin, int frequency);
31 | static public native int PWMStopEx(int gpioPin);
32 |
33 | /* ADC */
34 | static public native int readADC();
35 | static public native int readADCWithChannel(int channel);
36 | static public native int[] readADCWithChannels(int[] channels);
37 |
38 | /* I2C */
39 | static public native int setI2CSlave(int fd, int slave);
40 | static public native int setI2CTimeout(int fd, int timeout);
41 | static public native int setI2CRetries(int fd, int retries);
42 | static public native int I2CReadByteData(int fd, int pos, int wait_us);
43 | static public native int I2CReadByte(int fd, int wait_us);
44 | static public native int I2CWriteByteData(int fd, int pos, byte byteData, int wait_us);
45 | static public native int I2CWriteByte(int fd, byte byteData, int wait_us);
46 |
47 | /* Discard */ static public native int readByteFromI2C(int fd, int pos, int wait_ms);
48 | /* Discard */ static public native int writeByteToI2C(int fd, int pos, byte byteData, int wait_ms);
49 | /* Discard */ static public native int writeByteToI2C2(int fd, byte byteData, int wait_ms);
50 |
51 | /* SPI */
52 | static public native int setSPIWriteBitsPerWord( int spi_fd, int bits );
53 | static public native int setSPIReadBitsPerWord( int spi_fd, int bits );
54 | static public native int setSPIBitOrder( int spi_fd, int order);
55 | static public native int setSPIClockDivider( int spi_fd, int divider);
56 | static public native int setSPIMaxSpeed( int spi_fd, int spi_speed);
57 | static public native int setSPIDataMode( int spi_fd, int mode);
58 | static public native int SPItransferOneByte( int spi_fd, byte byteData, int spi_delay, int spi_speed, int spi_bits);
59 | static public native int SPItransferBytes(int spi_fd, byte[] writeData, byte[] readBuff, int spi_delay, int spi_speed, int spi_bits);
60 | static public native int writeBytesToSPI(int spi_fd, byte[] writeData, int spi_delay, int spi_speed, int spi_bits);
61 | static public native int readBytesFromSPI(int spi_fd, byte[] readBuff, int spi_delay, int spi_speed, int spi_bits);
62 |
63 | /* GPIO */
64 | static public native int exportGPIOPin(int pin);
65 | static public native int unexportGPIOPin(int pin);
66 | //GPIOEnum.LOW or GPIOEnum.HIGH
67 | static public native int setGPIOValue(int pin, int value);
68 | static public native int getGPIOValue(int pin);
69 | //GPIOEnum.IN or GPIOEnum.OUT
70 | static public native int setGPIODirection(int pin, int direction);
71 | static public native int getGPIODirection(int pin);
72 |
73 | /* OldInterface: for EEPROM */
74 | static public native int openI2CDevice();
75 | static public native int writeByteDataToI2C(int fd, int pos, byte byteData);
76 | static public native int readByteDataFromI2C(int fd, int pos);
77 |
78 | static public native int getBoardType();
79 | /* getBoardType return value: */
80 | public static final int S3C6410_COMMON = 6410;
81 | public static final int S5PV210_COMMON = 210;
82 | public static final int S5P4412_COMMON = 4412;
83 | public static final int S5P4418_BASE = 4418;
84 | public static final int NanoPi2 = S5P4418_BASE+0;
85 | public static final int NanoPC_T2 = S5P4418_BASE+1;
86 | public static final int NanoPi_S2 = S5P4418_BASE+2;
87 | public static final int Smart4418 = S5P4418_BASE+3;
88 | public static final int NanoPi2_Fire = S5P4418_BASE+4;
89 | public static final int NanoPi_M2 = S5P4418_BASE+5;
90 | public static final int NanoPi_M2A = S5P4418_BASE+7;
91 | public static final int Smart4418SDK = S5P4418_BASE+0x103;
92 | public static final int S5P4418_MAX = Smart4418SDK;
93 | public static final int S5P6818_BASE = 6818;
94 | public static final int NanoPC_T3 = S5P6818_BASE+1;
95 | public static final int NanoPi_S3 = S5P6818_BASE+2;
96 | public static final int Smart6818 = S5P6818_BASE+3;
97 | public static final int NanoPi_M3 = S5P6818_BASE+7;
98 | public static final int S5P6818_MAX = NanoPi_M3;
99 |
100 | static public boolean isS5P4418Board() {
101 | int boardtype = getBoardType();
102 | return boardtype>=S5P4418_BASE && boardtype<=S5P4418_MAX;
103 | }
104 |
105 | static public boolean isS5P6818Board() {
106 | int boardtype = getBoardType();
107 | return boardtype>=S5P6818_BASE && boardtype<=S5P6818_MAX;
108 | }
109 |
110 | static {
111 | try {
112 | System.loadLibrary("friendlyarm-hardware");
113 | } catch (UnsatisfiedLinkError e) {
114 | Log.d("HardwareControler", "libfriendlyarm-hardware library not found!");
115 | }
116 | }
117 | }
118 |
--------------------------------------------------------------------------------
/GPIO_LED_Demo/src/com/friendlyarm/AndroidSDK/SPI.java:
--------------------------------------------------------------------------------
1 | package com.friendlyarm.AndroidSDK;
2 | import android.util.Log;
3 | import android.widget.Toast;
4 |
5 | import com.friendlyarm.AndroidSDK.HardwareControler;
6 | import com.friendlyarm.AndroidSDK.SPIEnum;
7 | import com.friendlyarm.AndroidSDK.GPIOEnum;
8 | import com.friendlyarm.AndroidSDK.FileCtlEnum;
9 |
10 | public class SPI {
11 | private static final String TAG = "com.friendlyarm.AndroidSDK.SPI";
12 | private int spi_mode = 0;
13 | private int spi_bits = 8;
14 | private int spi_delay = 0;
15 | private int spi_speed = 500000;
16 | private int spi_byte_order = SPIEnum.LSBFIRST;
17 |
18 | private static final String devName = "/dev/spidev1.0";
19 | private int spi_fd = -1;
20 |
21 | public void begin() {
22 | spi_fd = HardwareControler.open( devName, FileCtlEnum.O_RDWR );
23 | if (spi_fd >= 0) {
24 | Log.d(TAG, "open " + devName + "ok!");
25 |
26 | /* spi init */
27 | HardwareControler.setSPIWriteBitsPerWord( spi_fd, spi_bits );
28 | HardwareControler.setSPIReadBitsPerWord( spi_fd, spi_bits );
29 | } else {
30 | Log.d(TAG, "open " + devName + "failed!");
31 | spi_fd = -1;
32 | }
33 | }
34 |
35 | public void end() {
36 | if (spi_fd != -1) {
37 | HardwareControler.close(spi_fd);
38 | spi_fd = -1;
39 | }
40 | }
41 |
42 | public void setBitOrder(int order) {
43 | if (spi_fd < 0) {
44 | return ;
45 | }
46 | spi_byte_order = SPIEnum.MSBFIRST;
47 | if(spi_byte_order == SPIEnum.LSBFIRST) {
48 | spi_mode |= SPIEnum.SPI_LSB_FIRST;
49 | } else {
50 | spi_mode &= ~SPIEnum.SPI_LSB_FIRST;
51 | }
52 | HardwareControler.setSPIBitOrder( spi_fd, spi_byte_order );
53 |
54 | }
55 |
56 | public void setClockDivider(int divider) {
57 | if (spi_fd < 0) {
58 | return ;
59 | }
60 | spi_speed = 66666666/(2*(divider+1));
61 | if(spi_speed > 500000) {
62 | spi_speed = 500000;
63 | }
64 | HardwareControler.setSPIClockDivider( spi_fd, divider);
65 | }
66 |
67 | public void setDataMode(int mode) {
68 | if (spi_fd < 0) {
69 | return ;
70 | }
71 | switch(mode)
72 | {
73 | case SPIEnum.SPI_MODE0:
74 | spi_mode &= ~(SPIEnum.SPI_CPHA|SPIEnum.SPI_CPOL);
75 | break;
76 | case SPIEnum.SPI_MODE1:
77 | spi_mode &= ~(SPIEnum.SPI_CPOL);
78 | spi_mode |= (SPIEnum.SPI_CPHA);
79 | break;
80 | case SPIEnum.SPI_MODE2:
81 | spi_mode |= (SPIEnum.SPI_CPOL);
82 | spi_mode &= ~(SPIEnum.SPI_CPHA);
83 | break;
84 | case SPIEnum.SPI_MODE3:
85 | spi_mode |= (SPIEnum.SPI_CPHA|SPIEnum.SPI_CPOL);
86 | break;
87 | default:
88 | Log.e(TAG, "error data mode");
89 | }
90 |
91 | HardwareControler.setSPIDataMode( spi_fd, spi_mode );
92 | }
93 |
94 | public void setChipSelectPolarity(int cs, int active) {
95 |
96 | }
97 |
98 | public void chipSelect(int cs) {
99 |
100 | }
101 |
102 | public byte transfer(int value) {
103 | if (spi_fd < 0) {
104 | return 0;
105 | }
106 | return (byte) HardwareControler.SPItransferOneByte(spi_fd, (byte) value, spi_delay, spi_speed, spi_bits);
107 | }
108 | }
109 |
--------------------------------------------------------------------------------
/GPIO_LED_Demo/src/com/friendlyarm/AndroidSDK/SPIEnum.java:
--------------------------------------------------------------------------------
1 | package com.friendlyarm.AndroidSDK;
2 | public class SPIEnum {
3 |
4 | // SPIBitOrder
5 | public final static int LSBFIRST = 0; ///< LSB First
6 | public final static int MSBFIRST = 1; ///< MSB First
7 |
8 | // SPIMode
9 | public final static int SPI_MODE0 = 0; ///< CPOL = 0, CPHA = 0
10 | public final static int SPI_MODE1 = 1; ///< CPOL = 0, CPHA = 1
11 | public final static int SPI_MODE2 = 2; ///< CPOL = 1, CPHA = 0
12 | public final static int SPI_MODE3 = 3; ///< CPOL = 1, CPHA = 1
13 |
14 |
15 | public final static int SPI_CPHA = 0x01;
16 | public final static int SPI_CPOL = 0x02;
17 | public final static int SPI_CS_HIGH = 0x04;
18 | public final static int SPI_LSB_FIRST = 0x08;
19 | public final static int SPI_3WIRE = 0x10;
20 | public final static int SPI_LOOP = 0x20;
21 | public final static int SPI_NO_CS = 0x40;
22 | public final static int SPI_READY = 0x80;
23 |
24 | // SPIClockDivider
25 | public final static int SPI_CLOCK_DIV65536 = 0; ///< 65536 = 256us = 4kHz
26 | public final static int SPI_CLOCK_DIV32768 = 32768; ///< 32768 = 126us = 8kHz
27 | public final static int SPI_CLOCK_DIV16384 = 16384; ///< 16384 = 64us = 15.625kHz
28 | public final static int SPI_CLOCK_DIV8192 = 8192; ///< 8192 = 32us = 31.25kHz
29 | public final static int SPI_CLOCK_DIV4096 = 4096; ///< 4096 = 16us = 62.5kHz
30 | public final static int SPI_CLOCK_DIV2048 = 2048; ///< 2048 = 8us = 125kHz
31 | public final static int SPI_CLOCK_DIV1024 = 1024; ///< 1024 = 4us = 250kHz
32 | public final static int SPI_CLOCK_DIV512 = 512; ///< 512 = 2us = 500kHz
33 | public final static int SPI_CLOCK_DIV256 = 256; ///< 256 = 1us = 1MHz
34 | public final static int SPI_CLOCK_DIV128 = 128; ///< 128 = 500ns = = 2MHz
35 | public final static int SPI_CLOCK_DIV64 = 64; ///< 64 = 250ns = 4MHz
36 | public final static int SPI_CLOCK_DIV32 = 32; ///< 32 = 125ns = 8MHz
37 | public final static int SPI_CLOCK_DIV16 = 16; ///< 16 = 50ns = 20MHz
38 | public final static int SPI_CLOCK_DIV8 = 8; ///< 8 = 25ns = 40MHz
39 | public final static int SPI_CLOCK_DIV4 = 4; ///< 4 = 12.5ns 80MHz
40 | public final static int SPI_CLOCK_DIV2 = 2; ///< 2 = 6.25ns = 160MHz
41 | public final static int SPI_CLOCK_DIV1 = 1; ///< 0 = 256us = 4kHz
42 | }
--------------------------------------------------------------------------------
/GPIO_LED_Demo/src/com/friendlyarm/GPIODemo/GPIODemoMainActivity.java:
--------------------------------------------------------------------------------
1 | package com.friendlyarm.GPIODemo;
2 | import java.util.ArrayList;
3 | import java.util.HashMap;
4 | import java.util.Map;
5 | import java.util.Timer;
6 | import java.util.TimerTask;
7 |
8 | import android.app.Activity;
9 | import android.content.Context;
10 | import android.os.Bundle;
11 | import android.os.Handler;
12 | import android.os.Message;
13 | import android.util.Log;
14 | import android.view.LayoutInflater;
15 | import android.view.View;
16 | import android.view.ViewGroup;
17 | import android.view.View.OnClickListener;
18 | import android.widget.AdapterView;
19 | import android.widget.ArrayAdapter;
20 | import android.widget.Button;
21 | import android.widget.CheckBox;
22 | import android.widget.ListView;
23 | import android.widget.TextView;
24 | import android.widget.Toast;
25 | import android.os.SystemProperties;
26 | import android.widget.AdapterView.OnItemClickListener;
27 |
28 | import com.friendlyarm.AndroidSDK.GPIOEnum;
29 | import com.friendlyarm.AndroidSDK.HardwareControler;
30 |
31 | public class GPIODemoMainActivity extends Activity {
32 |
33 | private static final String TAG = "GPIODemo";
34 | private Timer timer = new Timer();
35 | private int step = 0;
36 | private Map demoGPIOPins = new HashMap();
37 |
38 | static int STEP_INIT_GPIO_DIRECTION = 1;
39 | static int STEP_SETALLPIN_TO_HIGH = 2;
40 | static int STEP_INIT_VIEW = 3;
41 |
42 | @Override
43 | public void onDestroy() {
44 | timer.cancel();
45 | super.onDestroy();
46 | }
47 |
48 |
49 | private Handler handler = new Handler() {
50 | public void handleMessage(Message msg) {
51 | switch (msg.what) {
52 | case 1:
53 | timer.cancel();
54 | // Generate list View from ArrayList
55 | displayListView();
56 | break;
57 | }
58 | super.handleMessage(msg);
59 | }
60 | };
61 |
62 | private TimerTask init_task = new TimerTask() {
63 | public void run() {
64 | Log.d(TAG, "init_task " + step);
65 | if (step == STEP_INIT_GPIO_DIRECTION) {
66 | for (Integer value: demoGPIOPins.values()) {
67 | if (HardwareControler.setGPIODirection(value, GPIOEnum.OUT) == 0) {
68 | } else {
69 | Log.v("TimerTask", String.format("setGPIODirection(%d) failed", value));
70 | }
71 | }
72 |
73 | step ++;
74 | } else if (step == STEP_SETALLPIN_TO_HIGH) {
75 | for (Integer value: demoGPIOPins.values()) {
76 | if (HardwareControler.setGPIOValue(value, GPIOEnum.HIGH) == 0) {
77 | } else {
78 | Log.v("TimerTask", String.format("setGPIOValue(%d) failed", value));
79 | }
80 | }
81 |
82 | step ++;
83 | } else if (step == STEP_INIT_VIEW) {
84 | Message message = new Message();
85 | message.what = 1;
86 | handler.sendMessage(message);
87 | }
88 |
89 | }
90 | };
91 | //////////////////////////////////////
92 |
93 | MyCustomAdapter dataAdapter = null;
94 |
95 | @Override
96 | public void onCreate(Bundle savedInstanceState) {
97 | super.onCreate(savedInstanceState);
98 | setContentView(R.layout.gpiodemo_main);
99 | setTitle("GPIO Demo");
100 |
101 | int boardType = HardwareControler.getBoardType();
102 | if (boardType == HardwareControler.Smart4418SDK) {
103 | demoGPIOPins.put("GPIOC9", 73);
104 | demoGPIOPins.put("GPIOC10", 74);
105 | demoGPIOPins.put("GPIOC11", 75);
106 | demoGPIOPins.put("GPIOC12", 76);
107 | } else if (boardType == HardwareControler.NanoPC_T2 || boardType == HardwareControler.NanoPC_T3) {
108 | demoGPIOPins.put("Pin17", 68);
109 | demoGPIOPins.put("Pin18", 71);
110 | demoGPIOPins.put("Pin19", 72);
111 | demoGPIOPins.put("Pin20", 88);
112 | demoGPIOPins.put("Pin21", 92);
113 | demoGPIOPins.put("Pin22", 58);
114 | } else if (SystemProperties.get("ro.build.version.release").contains("4.1.2")) {
115 | demoGPIOPins.put("LED 1", 281);
116 | demoGPIOPins.put("LED 2", 282);
117 | demoGPIOPins.put("LED 3", 283);
118 | demoGPIOPins.put("LED 4", 284);
119 | } else {
120 | demoGPIOPins.put("LED 1", 79);
121 | demoGPIOPins.put("LED 2", 80);
122 | demoGPIOPins.put("LED 3", 81);
123 | demoGPIOPins.put("LED 4", 82);
124 | }
125 |
126 | // export all pins
127 | for (Integer value: demoGPIOPins.values()) {
128 | if (HardwareControler.exportGPIOPin(value) == 0) {
129 | } else {
130 | Toast.makeText(this, String.format("exportGPIOPin(%d) failed!", value),
131 | Toast.LENGTH_SHORT).show();
132 | }
133 | }
134 |
135 | step = STEP_INIT_GPIO_DIRECTION;
136 | timer.schedule(init_task, 100, 100);
137 | }
138 |
139 | private void displayListView() {
140 | Log.d(TAG, "displayListView");
141 |
142 | ArrayList pinList = new ArrayList();
143 |
144 | for (Map.Entry entry : demoGPIOPins.entrySet()) {
145 | GPIOPin pin = new GPIOPin(entry.getValue(), entry.getKey(), false);
146 | pinList.add(pin);
147 | }
148 |
149 | dataAdapter = new MyCustomAdapter(this, R.layout.checkbox_listview_item,
150 | pinList);
151 | ListView listView = (ListView) findViewById(R.id.listView1);
152 | listView.setAdapter(dataAdapter);
153 |
154 | listView.setOnItemClickListener(new OnItemClickListener() {
155 | public void onItemClick(AdapterView> parent, View view,
156 | int position, long id) {
157 | GPIOPin pin = (GPIOPin) parent.getItemAtPosition(position);
158 | if (HardwareControler.setGPIOValue(pin.code,
159 | pin.isSelected()?GPIOEnum.LOW:GPIOEnum.HIGH) == 0) {
160 | } else {
161 | Log.v(TAG, String.format("setGPIOValue(%d) failed", pin.code));
162 | }
163 | }
164 | });
165 |
166 | }
167 |
168 | private class MyCustomAdapter extends ArrayAdapter {
169 | private ArrayList pinList;
170 | public MyCustomAdapter(Context context, int textViewResourceId,
171 | ArrayList pinList) {
172 | super(context, textViewResourceId, pinList);
173 | this.pinList = new ArrayList();
174 | this.pinList.addAll(pinList);
175 | }
176 |
177 | private class ViewHolder {
178 | CheckBox name;
179 | }
180 |
181 | @Override
182 | public View getView(int position, View convertView, ViewGroup parent) {
183 |
184 | ViewHolder holder = null;
185 | Log.v(TAG, String.valueOf(position));
186 |
187 | if (convertView == null) {
188 | LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
189 | convertView = vi.inflate(R.layout.checkbox_listview_item, null);
190 |
191 | holder = new ViewHolder();
192 | holder.name = (CheckBox) convertView
193 | .findViewById(R.id.checkBox1);
194 | convertView.setTag(holder);
195 |
196 | holder.name.setOnClickListener(new View.OnClickListener() {
197 | public void onClick(View v) {
198 | CheckBox cb = (CheckBox) v;
199 | GPIOPin pin = (GPIOPin) cb.getTag();
200 | pin.setSelected(cb.isChecked());
201 | if (HardwareControler.setGPIOValue(pin.code,
202 | pin.isSelected() ? GPIOEnum.LOW:GPIOEnum.HIGH) == 0) {
203 | } else {
204 | Log.v(TAG, String.format("setGPIOValue(%d) failed", pin.code));
205 | }
206 | }
207 | });
208 | } else {
209 | holder = (ViewHolder) convertView.getTag();
210 | }
211 |
212 | GPIOPin pin = pinList.get(position);
213 | holder.name.setText(pin.getName());
214 | holder.name.setChecked(pin.isSelected());
215 | holder.name.setTag(pin);
216 |
217 | return convertView;
218 |
219 | }
220 |
221 | }
222 | }
223 |
--------------------------------------------------------------------------------
/GPIO_LED_Demo/src/com/friendlyarm/GPIODemo/GPIOPin.java:
--------------------------------------------------------------------------------
1 | package com.friendlyarm.GPIODemo;
2 |
3 | public class GPIOPin {
4 |
5 | int code = 0;
6 | String name = null;
7 | boolean selected = false;
8 |
9 | public GPIOPin(int code, String name, boolean selected) {
10 | super();
11 | this.code = code;
12 | this.name = name;
13 | this.selected = selected;
14 | }
15 |
16 | public int getCode() {
17 | return code;
18 | }
19 | public void setCode(int code) {
20 | this.code = code;
21 | }
22 | public String getName() {
23 | return name;
24 | }
25 | public void setName(String name) {
26 | this.name = name;
27 | }
28 |
29 | public boolean isSelected() {
30 | return selected;
31 | }
32 | public void setSelected(boolean selected) {
33 | this.selected = selected;
34 | }
35 |
36 | }
--------------------------------------------------------------------------------
/GPIO_LED_Demo/src/com/friendlyarm/Utils/CommonFuncs.java:
--------------------------------------------------------------------------------
1 | package com.friendlyarm.Utils;
2 |
3 | import android.app.Activity;
4 | import android.app.AlertDialog;
5 | import android.widget.Toast;
6 | import android.app.Activity;
7 | import android.os.Bundle;
8 | import android.os.Handler;
9 | import android.os.Message;
10 | import android.content.Context;
11 | import android.content.DialogInterface;
12 |
13 | public class CommonFuncs {
14 | public static void showAlertDialog(Context context, String message) {
15 | AlertDialog.Builder builder = new AlertDialog.Builder(context);
16 | builder.setMessage(message)
17 | .setCancelable(false)
18 | .setPositiveButton("Close",
19 | new DialogInterface.OnClickListener() {
20 | public void onClick(DialogInterface dialog, int id) {
21 | }
22 | });
23 | AlertDialog alert = builder.create();
24 | alert.show();
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/I2C_LCD1602_Demo/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/I2C_LCD1602_Demo/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | LCD1602Demo
4 |
5 |
6 |
7 |
8 |
9 | com.android.ide.eclipse.adt.ResourceManagerBuilder
10 |
11 |
12 |
13 |
14 | com.android.ide.eclipse.adt.PreCompilerBuilder
15 |
16 |
17 |
18 |
19 | org.eclipse.jdt.core.javabuilder
20 |
21 |
22 |
23 |
24 | com.android.ide.eclipse.adt.ApkBuilder
25 |
26 |
27 |
28 |
29 |
30 | com.android.ide.eclipse.adt.AndroidNature
31 | org.eclipse.jdt.core.javanature
32 |
33 |
34 |
--------------------------------------------------------------------------------
/I2C_LCD1602_Demo/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
3 | org.eclipse.jdt.core.compiler.compliance=1.6
4 | org.eclipse.jdt.core.compiler.source=1.6
5 |
--------------------------------------------------------------------------------
/I2C_LCD1602_Demo/Android.mk:
--------------------------------------------------------------------------------
1 | LOCAL_PATH:= $(call my-dir)
2 | include $(CLEAR_VARS)
3 |
4 | LOCAL_SRC_FILES := $(call all-subdir-java-files)
5 |
6 | LOCAL_PACKAGE_NAME := LCD1602
7 |
8 | LOCAL_CERTIFICATE := platform
9 | LOCAL_MODULE_TAGS := eng
10 |
11 | include $(BUILD_PACKAGE)
12 |
--------------------------------------------------------------------------------
/I2C_LCD1602_Demo/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/I2C_LCD1602_Demo/build.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | ANDROIDPATH=/opt/FriendlyARM/smart4418/android/
4 | APPNAME=LCD1602
5 | APKNAME=${APPNAME}-r1.0.apk
6 | PRJPATH=$PWD
7 | cd $PRJPATH
8 | rm -rf bin gen
9 |
10 | cd $ANDROIDPATH
11 | rm -f $APKNAME
12 | . setenv
13 | mmm $PRJPATH -B
14 | java -jar out/host/linux-x86/framework/signapk.jar vendor/friendly-arm/nanopi2/security/platform.x509.pem vendor/friendly-arm/nanopi2/security/platform.pk8 out/target/product/nanopi2/obj/APPS/${APPNAME}_intermediates/package.apk.unsigned ${APPNAME}-unaligned.apk
15 | zipalign -v 4 ${APPNAME}-unaligned.apk $APKNAME
16 | adb install -r ${APKNAME}
17 |
--------------------------------------------------------------------------------
/I2C_LCD1602_Demo/libs/armeabi/libfriendlyarm-hardware.so:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/friendlyarm/AndroidHardwareAccess/8084bdb9d85b9e466aea5544769ffb30d60dfda3/I2C_LCD1602_Demo/libs/armeabi/libfriendlyarm-hardware.so
--------------------------------------------------------------------------------
/I2C_LCD1602_Demo/project.properties:
--------------------------------------------------------------------------------
1 | # This file is automatically generated by Android Tools.
2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3 | #
4 | # This file must be checked in Version Control Systems.
5 | #
6 | # To customize properties used by the Ant build system edit
7 | # "ant.properties", and override values to adapt the script to your
8 | # project structure.
9 | #
10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
12 |
13 | # Project target.
14 | target=android-17
15 |
--------------------------------------------------------------------------------
/I2C_LCD1602_Demo/res/drawable-hdpi/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/friendlyarm/AndroidHardwareAccess/8084bdb9d85b9e466aea5544769ffb30d60dfda3/I2C_LCD1602_Demo/res/drawable-hdpi/icon.png
--------------------------------------------------------------------------------
/I2C_LCD1602_Demo/res/drawable-ldpi/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/friendlyarm/AndroidHardwareAccess/8084bdb9d85b9e466aea5544769ffb30d60dfda3/I2C_LCD1602_Demo/res/drawable-ldpi/icon.png
--------------------------------------------------------------------------------
/I2C_LCD1602_Demo/res/drawable-mdpi/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/friendlyarm/AndroidHardwareAccess/8084bdb9d85b9e466aea5544769ffb30d60dfda3/I2C_LCD1602_Demo/res/drawable-mdpi/icon.png
--------------------------------------------------------------------------------
/I2C_LCD1602_Demo/res/layout/mainactivity.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
8 |
9 |
12 |
13 |
16 |
17 |
--------------------------------------------------------------------------------
/I2C_LCD1602_Demo/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | LCD1602
4 | LCD1602
5 | Apply
6 |
7 |
--------------------------------------------------------------------------------
/I2C_LCD1602_Demo/src/com/friendlyarm/AndroidSDK/FileCtlEnum.java:
--------------------------------------------------------------------------------
1 | package com.friendlyarm.AndroidSDK;
2 |
3 | public class FileCtlEnum {
4 | /* File Flag */
5 | public final static int O_ACCMODE = 00000003;
6 | public final static int O_RDONLY = 00000000;
7 | public final static int O_WRONLY = 00000001;
8 | public final static int O_RDWR = 00000002;
9 | public final static int O_CREAT = 00000100; /* not fcntl */
10 | public final static int O_EXCL = 00000200; /* not fcntl */
11 | public final static int O_NOCTTY = 00000400; /* not fcntl */
12 | public final static int O_TRUNC = 00001000; /* not fcntl */
13 | public final static int O_APPEND = 00002000;
14 | public final static int O_NONBLOCK = 00004000;
15 | public final static int O_DSYNC = 00010000; /* used to be O_SYNC, see below */
16 | public final static int FASYNC = 00020000; /* fcntl, for BSD compatibility */
17 | public final static int O_DIRECT = 00040000; /* direct disk access hint */
18 | public final static int O_LARGEFILE = 00100000;
19 | public final static int O_DIRECTORY = 00200000; /* must be a directory */
20 | public final static int O_NOFOLLOW = 00400000; /* don't follow links */
21 | public final static int O_NOATIME = 01000000;
22 | public final static int O_CLOEXEC = 02000000; /* set close_on_exec */
23 | }
24 |
--------------------------------------------------------------------------------
/I2C_LCD1602_Demo/src/com/friendlyarm/AndroidSDK/GPIOEnum.java:
--------------------------------------------------------------------------------
1 | package com.friendlyarm.AndroidSDK;
2 |
3 | /*
4 | GPIO signals have paths like /sys/class/gpio/gpio42/ (for GPIO #42)
5 | and have the following read/write attributes:
6 |
7 | /sys/class/gpio/gpioN/
8 |
9 | "direction" ... reads as either "in" or "out". This value may
10 | normally be written. Writing as "out" defaults to
11 | initializing the value as low. To ensure glitch free
12 | operation, values "low" and "high" may be written to
13 | configure the GPIO as an output with that initial value.
14 |
15 | Note that this attribute *will not exist* if the kernel
16 | doesn't support changing the direction of a GPIO, or
17 | it was exported by kernel code that didn't explicitly
18 | allow userspace to reconfigure this GPIO's direction.
19 |
20 | "value" ... reads as either 0 (low) or 1 (high). If the GPIO
21 | is configured as an output, this value may be written;
22 | any nonzero value is treated as high.
23 |
24 | If the pin can be configured as interrupt-generating interrupt
25 | and if it has been configured to generate interrupts (see the
26 | description of "edge"), you can poll(2) on that file and
27 | poll(2) will return whenever the interrupt was triggered. If
28 | you use poll(2), set the events POLLPRI and POLLERR. If you
29 | use select(2), set the file descriptor in exceptfds. After
30 | poll(2) returns, either lseek(2) to the beginning of the sysfs
31 | file and read the new value or close the file and re-open it
32 | to read the value.
33 | */
34 |
35 | public class GPIOEnum {
36 | //Direction
37 | public final static int IN = 1;
38 | public final static int OUT = 2;
39 |
40 | //Value
41 | public final static int LOW = 0;
42 | public final static int HIGH = 1;
43 | }
44 |
--------------------------------------------------------------------------------
/I2C_LCD1602_Demo/src/com/friendlyarm/AndroidSDK/HardwareControler.java:
--------------------------------------------------------------------------------
1 | package com.friendlyarm.AndroidSDK;
2 | import android.util.Log;
3 |
4 | public class HardwareControler
5 | {
6 | /* File */
7 | static public native int open(String devName, int flags);
8 | static public native int write(int fd, byte[] data);
9 | static public native int read(int fd, byte[] buf, int len);
10 | static public native int select(int fd, int sec, int usec);
11 | static public native void close(int fd);
12 | static public native int ioctlWithIntValue(int fd, int cmd, int value);
13 |
14 | /* Serial Port */
15 | static public native int openSerialPort( String devName, long baud, int dataBits, int stopBits );
16 | static public native int openSerialPortEx( String devName
17 | , long baud
18 | , int dataBits
19 | , int stopBits
20 | , String parityBit
21 | , String flowCtrl
22 | );
23 |
24 | /* LED */
25 | static public native int setLedState( int ledID, int ledState );
26 |
27 | /* PWM */
28 | static public native int PWMPlay(int frequency);
29 | static public native int PWMStop();
30 | static public native int PWMPlayEx(int gpioPin, int frequency);
31 | static public native int PWMStopEx(int gpioPin);
32 |
33 | /* ADC */
34 | static public native int readADC();
35 | static public native int readADCWithChannel(int channel);
36 | static public native int[] readADCWithChannels(int[] channels);
37 |
38 | /* I2C */
39 | static public native int setI2CSlave(int fd, int slave);
40 | static public native int setI2CTimeout(int fd, int timeout);
41 | static public native int setI2CRetries(int fd, int retries);
42 | static public native int I2CReadByteData(int fd, int pos, int wait_us);
43 | static public native int I2CReadByte(int fd, int wait_us);
44 | static public native int I2CWriteByteData(int fd, int pos, byte byteData, int wait_us);
45 | static public native int I2CWriteByte(int fd, byte byteData, int wait_us);
46 |
47 | /* Discard */ static public native int readByteFromI2C(int fd, int pos, int wait_ms);
48 | /* Discard */ static public native int writeByteToI2C(int fd, int pos, byte byteData, int wait_ms);
49 | /* Discard */ static public native int writeByteToI2C2(int fd, byte byteData, int wait_ms);
50 |
51 | /* SPI */
52 | static public native int setSPIWriteBitsPerWord( int spi_fd, int bits );
53 | static public native int setSPIReadBitsPerWord( int spi_fd, int bits );
54 | static public native int setSPIBitOrder( int spi_fd, int order);
55 | static public native int setSPIClockDivider( int spi_fd, int divider);
56 | static public native int setSPIMaxSpeed( int spi_fd, int spi_speed);
57 | static public native int setSPIDataMode( int spi_fd, int mode);
58 | static public native int SPItransferOneByte( int spi_fd, byte byteData, int spi_delay, int spi_speed, int spi_bits);
59 | static public native int SPItransferBytes(int spi_fd, byte[] writeData, byte[] readBuff, int spi_delay, int spi_speed, int spi_bits);
60 | static public native int writeBytesToSPI(int spi_fd, byte[] writeData, int spi_delay, int spi_speed, int spi_bits);
61 | static public native int readBytesFromSPI(int spi_fd, byte[] readBuff, int spi_delay, int spi_speed, int spi_bits);
62 |
63 | /* GPIO */
64 | static public native int exportGPIOPin(int pin);
65 | static public native int unexportGPIOPin(int pin);
66 | //GPIOEnum.LOW or GPIOEnum.HIGH
67 | static public native int setGPIOValue(int pin, int value);
68 | static public native int getGPIOValue(int pin);
69 | //GPIOEnum.IN or GPIOEnum.OUT
70 | static public native int setGPIODirection(int pin, int direction);
71 | static public native int getGPIODirection(int pin);
72 |
73 | /* OldInterface: for EEPROM */
74 | static public native int openI2CDevice();
75 | static public native int writeByteDataToI2C(int fd, int pos, byte byteData);
76 | static public native int readByteDataFromI2C(int fd, int pos);
77 |
78 | static public native int getBoardType();
79 | /* getBoardType return value: */
80 | public static final int S3C6410_COMMON = 6410;
81 | public static final int S5PV210_COMMON = 210;
82 | public static final int S5P4412_COMMON = 4412;
83 | public static final int S5P4418_BASE = 4418;
84 | public static final int NanoPi2 = S5P4418_BASE+0;
85 | public static final int NanoPC_T2 = S5P4418_BASE+1;
86 | public static final int NanoPi_S2 = S5P4418_BASE+2;
87 | public static final int Smart4418 = S5P4418_BASE+3;
88 | public static final int NanoPi2_Fire = S5P4418_BASE+4;
89 | public static final int NanoPi_M2 = S5P4418_BASE+5;
90 | public static final int NanoPi_M2A = S5P4418_BASE+7;
91 | public static final int Smart4418SDK = S5P4418_BASE+0x103;
92 | public static final int S5P4418_MAX = Smart4418SDK;
93 | public static final int S5P6818_BASE = 6818;
94 | public static final int NanoPC_T3 = S5P6818_BASE+1;
95 | public static final int NanoPi_S3 = S5P6818_BASE+2;
96 | public static final int Smart6818 = S5P6818_BASE+3;
97 | public static final int NanoPi_M3 = S5P6818_BASE+7;
98 | public static final int S5P6818_MAX = NanoPi_M3;
99 |
100 | static public boolean isS5P4418Board() {
101 | int boardtype = getBoardType();
102 | return boardtype>=S5P4418_BASE && boardtype<=S5P4418_MAX;
103 | }
104 |
105 | static public boolean isS5P6818Board() {
106 | int boardtype = getBoardType();
107 | return boardtype>=S5P6818_BASE && boardtype<=S5P6818_MAX;
108 | }
109 |
110 | static {
111 | try {
112 | System.loadLibrary("friendlyarm-hardware");
113 | } catch (UnsatisfiedLinkError e) {
114 | Log.d("HardwareControler", "libfriendlyarm-hardware library not found!");
115 | }
116 | }
117 | }
118 |
--------------------------------------------------------------------------------
/I2C_LCD1602_Demo/src/com/friendlyarm/AndroidSDK/SPI.java:
--------------------------------------------------------------------------------
1 | package com.friendlyarm.AndroidSDK;
2 | import android.util.Log;
3 | import android.widget.Toast;
4 |
5 | import com.friendlyarm.AndroidSDK.HardwareControler;
6 | import com.friendlyarm.AndroidSDK.SPIEnum;
7 | import com.friendlyarm.AndroidSDK.GPIOEnum;
8 | import com.friendlyarm.AndroidSDK.FileCtlEnum;
9 |
10 | public class SPI {
11 | private static final String TAG = "com.friendlyarm.AndroidSDK.SPI";
12 | private int spi_mode = 0;
13 | private int spi_bits = 8;
14 | private int spi_delay = 0;
15 | private int spi_speed = 500000;
16 | private int spi_byte_order = SPIEnum.LSBFIRST;
17 |
18 | private static final String devName = "/dev/spidev1.0";
19 | private int spi_fd = -1;
20 |
21 | public void begin() {
22 | spi_fd = HardwareControler.open( devName, FileCtlEnum.O_RDWR );
23 | if (spi_fd >= 0) {
24 | Log.d(TAG, "open " + devName + "ok!");
25 |
26 | /* spi init */
27 | HardwareControler.setSPIWriteBitsPerWord( spi_fd, spi_bits );
28 | HardwareControler.setSPIReadBitsPerWord( spi_fd, spi_bits );
29 | } else {
30 | Log.d(TAG, "open " + devName + "failed!");
31 | spi_fd = -1;
32 | }
33 | }
34 |
35 | public void end() {
36 | if (spi_fd != -1) {
37 | HardwareControler.close(spi_fd);
38 | spi_fd = -1;
39 | }
40 | }
41 |
42 | public void setBitOrder(int order) {
43 | if (spi_fd < 0) {
44 | return ;
45 | }
46 | spi_byte_order = SPIEnum.MSBFIRST;
47 | if(spi_byte_order == SPIEnum.LSBFIRST) {
48 | spi_mode |= SPIEnum.SPI_LSB_FIRST;
49 | } else {
50 | spi_mode &= ~SPIEnum.SPI_LSB_FIRST;
51 | }
52 | HardwareControler.setSPIBitOrder( spi_fd, spi_byte_order );
53 |
54 | }
55 |
56 | public void setClockDivider(int divider) {
57 | if (spi_fd < 0) {
58 | return ;
59 | }
60 | spi_speed = 66666666/(2*(divider+1));
61 | if(spi_speed > 500000) {
62 | spi_speed = 500000;
63 | }
64 | HardwareControler.setSPIClockDivider( spi_fd, divider);
65 | }
66 |
67 | public void setDataMode(int mode) {
68 | if (spi_fd < 0) {
69 | return ;
70 | }
71 | switch(mode)
72 | {
73 | case SPIEnum.SPI_MODE0:
74 | spi_mode &= ~(SPIEnum.SPI_CPHA|SPIEnum.SPI_CPOL);
75 | break;
76 | case SPIEnum.SPI_MODE1:
77 | spi_mode &= ~(SPIEnum.SPI_CPOL);
78 | spi_mode |= (SPIEnum.SPI_CPHA);
79 | break;
80 | case SPIEnum.SPI_MODE2:
81 | spi_mode |= (SPIEnum.SPI_CPOL);
82 | spi_mode &= ~(SPIEnum.SPI_CPHA);
83 | break;
84 | case SPIEnum.SPI_MODE3:
85 | spi_mode |= (SPIEnum.SPI_CPHA|SPIEnum.SPI_CPOL);
86 | break;
87 | default:
88 | Log.e(TAG, "error data mode");
89 | }
90 |
91 | HardwareControler.setSPIDataMode( spi_fd, spi_mode );
92 | }
93 |
94 | public void setChipSelectPolarity(int cs, int active) {
95 |
96 | }
97 |
98 | public void chipSelect(int cs) {
99 |
100 | }
101 |
102 | public byte transfer(int value) {
103 | if (spi_fd < 0) {
104 | return 0;
105 | }
106 | return (byte) HardwareControler.SPItransferOneByte(spi_fd, (byte) value, spi_delay, spi_speed, spi_bits);
107 | }
108 | }
109 |
--------------------------------------------------------------------------------
/I2C_LCD1602_Demo/src/com/friendlyarm/AndroidSDK/SPIEnum.java:
--------------------------------------------------------------------------------
1 | package com.friendlyarm.AndroidSDK;
2 | public class SPIEnum {
3 |
4 | // SPIBitOrder
5 | public final static int LSBFIRST = 0; ///< LSB First
6 | public final static int MSBFIRST = 1; ///< MSB First
7 |
8 | // SPIMode
9 | public final static int SPI_MODE0 = 0; ///< CPOL = 0, CPHA = 0
10 | public final static int SPI_MODE1 = 1; ///< CPOL = 0, CPHA = 1
11 | public final static int SPI_MODE2 = 2; ///< CPOL = 1, CPHA = 0
12 | public final static int SPI_MODE3 = 3; ///< CPOL = 1, CPHA = 1
13 |
14 |
15 | public final static int SPI_CPHA = 0x01;
16 | public final static int SPI_CPOL = 0x02;
17 | public final static int SPI_CS_HIGH = 0x04;
18 | public final static int SPI_LSB_FIRST = 0x08;
19 | public final static int SPI_3WIRE = 0x10;
20 | public final static int SPI_LOOP = 0x20;
21 | public final static int SPI_NO_CS = 0x40;
22 | public final static int SPI_READY = 0x80;
23 |
24 | // SPIClockDivider
25 | public final static int SPI_CLOCK_DIV65536 = 0; ///< 65536 = 256us = 4kHz
26 | public final static int SPI_CLOCK_DIV32768 = 32768; ///< 32768 = 126us = 8kHz
27 | public final static int SPI_CLOCK_DIV16384 = 16384; ///< 16384 = 64us = 15.625kHz
28 | public final static int SPI_CLOCK_DIV8192 = 8192; ///< 8192 = 32us = 31.25kHz
29 | public final static int SPI_CLOCK_DIV4096 = 4096; ///< 4096 = 16us = 62.5kHz
30 | public final static int SPI_CLOCK_DIV2048 = 2048; ///< 2048 = 8us = 125kHz
31 | public final static int SPI_CLOCK_DIV1024 = 1024; ///< 1024 = 4us = 250kHz
32 | public final static int SPI_CLOCK_DIV512 = 512; ///< 512 = 2us = 500kHz
33 | public final static int SPI_CLOCK_DIV256 = 256; ///< 256 = 1us = 1MHz
34 | public final static int SPI_CLOCK_DIV128 = 128; ///< 128 = 500ns = = 2MHz
35 | public final static int SPI_CLOCK_DIV64 = 64; ///< 64 = 250ns = 4MHz
36 | public final static int SPI_CLOCK_DIV32 = 32; ///< 32 = 125ns = 8MHz
37 | public final static int SPI_CLOCK_DIV16 = 16; ///< 16 = 50ns = 20MHz
38 | public final static int SPI_CLOCK_DIV8 = 8; ///< 8 = 25ns = 40MHz
39 | public final static int SPI_CLOCK_DIV4 = 4; ///< 4 = 12.5ns 80MHz
40 | public final static int SPI_CLOCK_DIV2 = 2; ///< 2 = 6.25ns = 160MHz
41 | public final static int SPI_CLOCK_DIV1 = 1; ///< 0 = 256us = 4kHz
42 | }
--------------------------------------------------------------------------------
/I2C_LCD1602_Demo/src/com/friendlyarm/LCD1602Demo/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.friendlyarm.LCD1602Demo;
2 |
3 | import android.app.Activity;
4 | import android.view.View;
5 | import android.view.View.OnClickListener;
6 | import android.widget.Button;
7 | import android.widget.EditText;
8 | import android.widget.TextView;
9 | import android.os.Bundle;
10 |
11 | import com.friendlyarm.AndroidSDK.HardwareControler;
12 | import com.friendlyarm.AndroidSDK.FileCtlEnum;
13 | import com.friendlyarm.LCD1602Demo.R;
14 | /* for thread */
15 | import android.os.Message;
16 | import android.os.Handler;
17 | import android.os.Looper;
18 |
19 | public class MainActivity extends Activity implements OnClickListener {
20 | EditText writeEditor;
21 | TextView statusView;
22 | private Handler messageHandler;
23 | Thread writeThread = null;
24 | int devFD = -1;
25 | final int MAX_LINE_SIZE = 16;
26 |
27 | private class MessageHandler extends Handler {
28 | public MessageHandler(Looper looper) {
29 | super(looper);
30 | }
31 |
32 | @Override
33 | public void handleMessage(Message msg) {
34 | String result = (String) msg.obj;
35 | if (result.equals("ERROR")) {
36 | statusView.setText("Status: Error");
37 | } else if (result.equals("DONE")) {
38 | statusView.setText("Status: Done");
39 | } else {
40 | statusView.setText("Status: Processing");
41 | }
42 | }
43 | }
44 |
45 | @Override
46 | public void onDestroy() {
47 | if (devFD >= 0) {
48 | HardwareControler.close(devFD);
49 | devFD = -1;
50 | }
51 | super.onDestroy();
52 | }
53 |
54 | @Override
55 | public void onCreate(Bundle savedInstanceState) {
56 | super.onCreate(savedInstanceState);
57 | setContentView(R.layout.mainactivity);
58 |
59 | writeEditor = (EditText) findViewById(R.id.writeEditor);
60 | writeEditor.setText("Don't give up and don't give in.");
61 |
62 | statusView = (TextView) findViewById(R.id.statusView);
63 | Button writeButton = (Button)findViewById(R.id.sendButton);
64 | writeButton.setOnClickListener(this);
65 |
66 | Looper looper = Looper.myLooper();
67 | messageHandler = new MessageHandler(looper);
68 |
69 | devFD = HardwareControler.open("/dev/i2c-0", FileCtlEnum.O_RDWR);
70 | if (devFD < 0) {
71 | statusView.setText("Fail to open I2C device.");
72 | writeEditor.setEnabled(false);
73 | writeButton.setEnabled(false);
74 | } else {
75 | // for LCD1602 (chip: pcf8574)
76 | if (HardwareControler.setI2CSlave(devFD, 0x27) < 0) {
77 | statusView.setText("Fail to set I2C slave.");
78 | writeEditor.setEnabled(false);
79 | writeButton.setEnabled(false);
80 | } else {
81 | statusView.setText("Status: Ready");
82 | }
83 | }
84 | }
85 |
86 | private int LCD1602Init() throws InterruptedException {
87 | Thread.sleep(0,1000*15);
88 | if (PCF8574.writeCmd4(devFD, (byte)(0x03 << 4)) == -1) {
89 | return -1;
90 | }
91 | Thread.sleep(0,100*41);
92 | if (PCF8574.writeCmd4(devFD, (byte)(0x03 << 4)) == -1) {
93 | return -1;
94 | }
95 | Thread.sleep(0,100);
96 | if (PCF8574.writeCmd4(devFD, (byte)(0x03 << 4)) == -1) {
97 | return -1;
98 | }
99 | if (PCF8574.writeCmd4(devFD, (byte)(0x02 << 4)) == -1) {
100 | return -1;
101 | }
102 | if (PCF8574.writeCmd8(devFD, (byte)(0x28)) == -1) {
103 | return -1;
104 | }
105 | if (PCF8574.writeCmd8(devFD, (byte)(0x0c)) == -1) {
106 | return -1;
107 | }
108 | Thread.sleep(0,2000);
109 | if (PCF8574.writeCmd8(devFD, (byte)(0x06)) == -1) {
110 | return -1;
111 | }
112 | if (PCF8574.writeCmd8(devFD, (byte)(0x02)) == -1) {
113 | return -1;
114 | }
115 | Thread.sleep(0,2000);
116 | return 0;
117 | }
118 |
119 | private int LCD1602DispStr(byte x, byte y, String str) throws InterruptedException
120 | {
121 | byte addr;
122 | addr = (byte)(((y + 2) * 0x40) + x);
123 |
124 | if (PCF8574.writeCmd8(devFD, addr) == -1) {
125 | return -1;
126 | }
127 | byte[] strBytes = str.getBytes();
128 | byte b;
129 |
130 | for (int i = 0; i < strBytes.length && i 0) {
142 | ret = LCD1602DispStr((byte)0, (byte)1, line2);
143 | }
144 | return ret;
145 | }
146 |
147 | private int LCD1602Clear() throws InterruptedException {
148 | if (PCF8574.writeCmd8(devFD, (byte)0x01) == -1) {
149 | return -1;
150 | }
151 | return 0;
152 | }
153 |
154 | private boolean lcd1602Inited = false;
155 | private void sendMessage(String msg) {
156 | Message message = Message.obtain();
157 | message.obj = msg;
158 | messageHandler.sendMessage(message);
159 | }
160 | private void startWriteEEPROMThread(final String displayText) {
161 | statusView.setText("Status: Processing");
162 | new Thread() {
163 | @Override
164 | public void run() {
165 | if (!lcd1602Inited) {
166 | try {
167 | if (LCD1602Init() == 0) {
168 | lcd1602Inited = true;
169 | }
170 | } catch (Exception e) {
171 | sendMessage("ERROR");
172 | return ;
173 | }
174 | }
175 |
176 | if (!lcd1602Inited) {
177 | sendMessage("ERROR");
178 | return ;
179 | }
180 |
181 | try {
182 | LCD1602Clear();
183 | Thread.sleep(100, 0);
184 |
185 | String line1 = "";
186 | String line2 = "";
187 | for (int i=0; i= MAX_LINE_SIZE) {
189 | if (line2.length() >= MAX_LINE_SIZE) {
190 | break;
191 | } else {
192 | line2 = line2 + displayText.charAt(i);
193 | }
194 | } else {
195 | line1 = line1 + displayText.charAt(i);
196 | }
197 | }
198 |
199 | if (LCD1602DispLines(line1, line2) == -1) {
200 | sendMessage("ERROR");
201 | return ;
202 | }
203 | } catch (Exception e) {
204 | sendMessage("ERROR");
205 | return ;
206 | }
207 | sendMessage("DONE");
208 | }
209 |
210 | }.start();
211 | }
212 |
213 | public void onClick(View v) {
214 | switch (v.getId()) {
215 | case R.id.sendButton:
216 | startWriteEEPROMThread(writeEditor.getText().toString());
217 | break;
218 | default:
219 | break;
220 | }
221 | }
222 |
223 | }
224 |
--------------------------------------------------------------------------------
/I2C_LCD1602_Demo/src/com/friendlyarm/LCD1602Demo/PCF8574.java:
--------------------------------------------------------------------------------
1 | package com.friendlyarm.LCD1602Demo;
2 |
3 | import com.friendlyarm.AndroidSDK.HardwareControler;
4 |
5 | public class PCF8574 {
6 | static final byte RS = 0x01;
7 | static final byte En = 0x04;
8 | static final byte BL = 0x08;
9 |
10 | static int pulseEnable(int devFD, byte data) throws InterruptedException
11 | {
12 | if (HardwareControler.I2CWriteByte(devFD, (byte)(data | En), 1) != 0) {
13 | return -1;
14 | }
15 | if (HardwareControler.I2CWriteByte(devFD, (byte)(data & ~En), 0) != 0) {
16 | return -1;
17 | }
18 | return 0;
19 | }
20 |
21 | static int writeCmd4(int devFD, byte command) throws InterruptedException
22 | {
23 | if (HardwareControler.I2CWriteByte(devFD, (byte)(command | BL), 0) != 0) {
24 | return -1;
25 | }
26 | if (pulseEnable(devFD, (byte)(command | BL)) != 0) {
27 | return -1;
28 | }
29 | return 0;
30 | }
31 |
32 | static int writeCmd8(int devFD, byte command) throws InterruptedException
33 | {
34 | byte command_H = (byte) (command & 0xf0);
35 | byte command_L = (byte) ((command << 4) & 0xf0);
36 | if (writeCmd4(devFD, command_H) == -1) {
37 | return -1;
38 | }
39 |
40 | if (writeCmd4(devFD, command_L) == -1) {
41 | return -1;
42 | }
43 | return 0;
44 | }
45 |
46 | static int writeData4(int devFD, byte data) throws InterruptedException
47 | {
48 | if (HardwareControler.I2CWriteByte(devFD, (byte)(data | RS | BL), 0) != 0) {
49 | return -1;
50 | }
51 | if (pulseEnable(devFD, (byte)(data | RS | BL)) == -1) {
52 | return -1;
53 | }
54 | return 0;
55 | }
56 |
57 | static int writeData8(int devFD, byte data) throws InterruptedException
58 | {
59 | byte data_H = (byte) (data & 0xf0);
60 | byte data_L = (byte) ((data << 4) & 0xf0);
61 | if (writeData4(devFD, data_H) == -1) {
62 | return -1;
63 | }
64 | if (writeData4(devFD, data_L) == -1) {
65 | return -1;
66 | }
67 | return 0;
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/I2C_LCD1602_Demo/src/com/friendlyarm/Utils/CommonFuncs.java:
--------------------------------------------------------------------------------
1 | package com.friendlyarm.Utils;
2 |
3 | import android.app.Activity;
4 | import android.app.AlertDialog;
5 | import android.widget.Toast;
6 | import android.app.Activity;
7 | import android.os.Bundle;
8 | import android.os.Handler;
9 | import android.os.Message;
10 | import android.content.Context;
11 | import android.content.DialogInterface;
12 |
13 | public class CommonFuncs {
14 | public static void showAlertDialog(Context context, String message) {
15 | AlertDialog.Builder builder = new AlertDialog.Builder(context);
16 | builder.setMessage(message)
17 | .setCancelable(false)
18 | .setPositiveButton("Close",
19 | new DialogInterface.OnClickListener() {
20 | public void onClick(DialogInterface dialog, int id) {
21 | }
22 | });
23 | AlertDialog alert = builder.create();
24 | alert.show();
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/PWMDemo/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/PWMDemo/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | PWMTestingActivity
4 |
5 |
6 |
7 |
8 |
9 | com.android.ide.eclipse.adt.ResourceManagerBuilder
10 |
11 |
12 |
13 |
14 | com.android.ide.eclipse.adt.PreCompilerBuilder
15 |
16 |
17 |
18 |
19 | org.eclipse.jdt.core.javabuilder
20 |
21 |
22 |
23 |
24 | com.android.ide.eclipse.adt.ApkBuilder
25 |
26 |
27 |
28 |
29 |
30 | com.android.ide.eclipse.adt.AndroidNature
31 | org.eclipse.jdt.core.javanature
32 |
33 |
34 |
--------------------------------------------------------------------------------
/PWMDemo/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
3 | org.eclipse.jdt.core.compiler.compliance=1.6
4 | org.eclipse.jdt.core.compiler.source=1.6
5 |
--------------------------------------------------------------------------------
/PWMDemo/Android.mk:
--------------------------------------------------------------------------------
1 | LOCAL_PATH:= $(call my-dir)
2 | include $(CLEAR_VARS)
3 |
4 | LOCAL_SRC_FILES := $(call all-subdir-java-files)
5 |
6 | LOCAL_PACKAGE_NAME := PWM
7 |
8 | LOCAL_CERTIFICATE := platform
9 | LOCAL_MODULE_TAGS := eng
10 |
11 | include $(BUILD_PACKAGE)
12 |
--------------------------------------------------------------------------------
/PWMDemo/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
8 |
9 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/PWMDemo/build.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | ANDROIDPATH=/opt/FriendlyARM/smart4418/android/
4 | APPNAME=PWM
5 | APKNAME=${APPNAME}-r1.0.apk
6 | PRJPATH=$PWD
7 | cd $PRJPATH
8 | rm -rf bin gen
9 |
10 | cd $ANDROIDPATH
11 | rm -f $APKNAME
12 | . setenv
13 | mmm $PRJPATH -B
14 | java -jar out/host/linux-x86/framework/signapk.jar vendor/friendly-arm/nanopi2/security/platform.x509.pem vendor/friendly-arm/nanopi2/security/platform.pk8 out/target/product/nanopi2/obj/APPS/${APPNAME}_intermediates/package.apk.unsigned ${APPNAME}-unaligned.apk
15 | zipalign -v 4 ${APPNAME}-unaligned.apk $APKNAME
16 | adb install -r ${APKNAME}
17 |
--------------------------------------------------------------------------------
/PWMDemo/libs/armeabi/libfriendlyarm-hardware.so:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/friendlyarm/AndroidHardwareAccess/8084bdb9d85b9e466aea5544769ffb30d60dfda3/PWMDemo/libs/armeabi/libfriendlyarm-hardware.so
--------------------------------------------------------------------------------
/PWMDemo/project.properties:
--------------------------------------------------------------------------------
1 | # This file is automatically generated by Android Tools.
2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3 | #
4 | # This file must be checked in Version Control Systems.
5 | #
6 | # To customize properties used by the Ant build system edit
7 | # "ant.properties", and override values to adapt the script to your
8 | # project structure.
9 | #
10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
12 |
13 | # Project target.
14 | target=android-17
15 |
--------------------------------------------------------------------------------
/PWMDemo/res/drawable/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/friendlyarm/AndroidHardwareAccess/8084bdb9d85b9e466aea5544769ffb30d60dfda3/PWMDemo/res/drawable/icon.png
--------------------------------------------------------------------------------
/PWMDemo/res/layout/pwmtestingactivity.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
7 |
10 |
11 |
13 |
14 |
18 |
19 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/PWMDemo/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | PWM Demo
4 | PWM Testing: /dev/pwm
5 | +
6 | -
7 | 1000
8 | Frequency:
9 | Start
10 | Stop
11 | Back
12 |
13 |
--------------------------------------------------------------------------------
/PWMDemo/src/com/friendlyarm/AndroidSDK/FileCtlEnum.java:
--------------------------------------------------------------------------------
1 | package com.friendlyarm.AndroidSDK;
2 |
3 | public class FileCtlEnum {
4 | /* File Flag */
5 | public final static int O_ACCMODE = 00000003;
6 | public final static int O_RDONLY = 00000000;
7 | public final static int O_WRONLY = 00000001;
8 | public final static int O_RDWR = 00000002;
9 | public final static int O_CREAT = 00000100; /* not fcntl */
10 | public final static int O_EXCL = 00000200; /* not fcntl */
11 | public final static int O_NOCTTY = 00000400; /* not fcntl */
12 | public final static int O_TRUNC = 00001000; /* not fcntl */
13 | public final static int O_APPEND = 00002000;
14 | public final static int O_NONBLOCK = 00004000;
15 | public final static int O_DSYNC = 00010000; /* used to be O_SYNC, see below */
16 | public final static int FASYNC = 00020000; /* fcntl, for BSD compatibility */
17 | public final static int O_DIRECT = 00040000; /* direct disk access hint */
18 | public final static int O_LARGEFILE = 00100000;
19 | public final static int O_DIRECTORY = 00200000; /* must be a directory */
20 | public final static int O_NOFOLLOW = 00400000; /* don't follow links */
21 | public final static int O_NOATIME = 01000000;
22 | public final static int O_CLOEXEC = 02000000; /* set close_on_exec */
23 | }
24 |
--------------------------------------------------------------------------------
/PWMDemo/src/com/friendlyarm/AndroidSDK/GPIOEnum.java:
--------------------------------------------------------------------------------
1 | package com.friendlyarm.AndroidSDK;
2 |
3 | /*
4 | GPIO signals have paths like /sys/class/gpio/gpio42/ (for GPIO #42)
5 | and have the following read/write attributes:
6 |
7 | /sys/class/gpio/gpioN/
8 |
9 | "direction" ... reads as either "in" or "out". This value may
10 | normally be written. Writing as "out" defaults to
11 | initializing the value as low. To ensure glitch free
12 | operation, values "low" and "high" may be written to
13 | configure the GPIO as an output with that initial value.
14 |
15 | Note that this attribute *will not exist* if the kernel
16 | doesn't support changing the direction of a GPIO, or
17 | it was exported by kernel code that didn't explicitly
18 | allow userspace to reconfigure this GPIO's direction.
19 |
20 | "value" ... reads as either 0 (low) or 1 (high). If the GPIO
21 | is configured as an output, this value may be written;
22 | any nonzero value is treated as high.
23 |
24 | If the pin can be configured as interrupt-generating interrupt
25 | and if it has been configured to generate interrupts (see the
26 | description of "edge"), you can poll(2) on that file and
27 | poll(2) will return whenever the interrupt was triggered. If
28 | you use poll(2), set the events POLLPRI and POLLERR. If you
29 | use select(2), set the file descriptor in exceptfds. After
30 | poll(2) returns, either lseek(2) to the beginning of the sysfs
31 | file and read the new value or close the file and re-open it
32 | to read the value.
33 | */
34 |
35 | public class GPIOEnum {
36 | //Direction
37 | public final static int IN = 1;
38 | public final static int OUT = 2;
39 |
40 | //Value
41 | public final static int LOW = 0;
42 | public final static int HIGH = 1;
43 | }
44 |
--------------------------------------------------------------------------------
/PWMDemo/src/com/friendlyarm/AndroidSDK/HardwareControler.java:
--------------------------------------------------------------------------------
1 | package com.friendlyarm.AndroidSDK;
2 | import android.util.Log;
3 |
4 | public class HardwareControler
5 | {
6 | /* File */
7 | static public native int open(String devName, int flags);
8 | static public native int write(int fd, byte[] data);
9 | static public native int read(int fd, byte[] buf, int len);
10 | static public native int select(int fd, int sec, int usec);
11 | static public native void close(int fd);
12 | static public native int ioctlWithIntValue(int fd, int cmd, int value);
13 |
14 | /* Serial Port */
15 | static public native int openSerialPort( String devName, long baud, int dataBits, int stopBits );
16 | static public native int openSerialPortEx( String devName
17 | , long baud
18 | , int dataBits
19 | , int stopBits
20 | , String parityBit
21 | , String flowCtrl
22 | );
23 |
24 | /* LED */
25 | static public native int setLedState( int ledID, int ledState );
26 |
27 | /* PWM */
28 | static public native int PWMPlay(int frequency);
29 | static public native int PWMStop();
30 | static public native int PWMPlayEx(int gpioPin, int frequency);
31 | static public native int PWMStopEx(int gpioPin);
32 |
33 | /* ADC */
34 | static public native int readADC();
35 | static public native int readADCWithChannel(int channel);
36 | static public native int[] readADCWithChannels(int[] channels);
37 |
38 | /* I2C */
39 | static public native int setI2CSlave(int fd, int slave);
40 | static public native int setI2CTimeout(int fd, int timeout);
41 | static public native int setI2CRetries(int fd, int retries);
42 | static public native int I2CReadByteData(int fd, int pos, int wait_us);
43 | static public native int I2CReadByte(int fd, int wait_us);
44 | static public native int I2CWriteByteData(int fd, int pos, byte byteData, int wait_us);
45 | static public native int I2CWriteByte(int fd, byte byteData, int wait_us);
46 |
47 | /* Discard */ static public native int readByteFromI2C(int fd, int pos, int wait_ms);
48 | /* Discard */ static public native int writeByteToI2C(int fd, int pos, byte byteData, int wait_ms);
49 | /* Discard */ static public native int writeByteToI2C2(int fd, byte byteData, int wait_ms);
50 |
51 | /* SPI */
52 | static public native int setSPIWriteBitsPerWord( int spi_fd, int bits );
53 | static public native int setSPIReadBitsPerWord( int spi_fd, int bits );
54 | static public native int setSPIBitOrder( int spi_fd, int order);
55 | static public native int setSPIClockDivider( int spi_fd, int divider);
56 | static public native int setSPIMaxSpeed( int spi_fd, int spi_speed);
57 | static public native int setSPIDataMode( int spi_fd, int mode);
58 | static public native int SPItransferOneByte( int spi_fd, byte byteData, int spi_delay, int spi_speed, int spi_bits);
59 | static public native int SPItransferBytes(int spi_fd, byte[] writeData, byte[] readBuff, int spi_delay, int spi_speed, int spi_bits);
60 | static public native int writeBytesToSPI(int spi_fd, byte[] writeData, int spi_delay, int spi_speed, int spi_bits);
61 | static public native int readBytesFromSPI(int spi_fd, byte[] readBuff, int spi_delay, int spi_speed, int spi_bits);
62 |
63 | /* GPIO */
64 | static public native int exportGPIOPin(int pin);
65 | static public native int unexportGPIOPin(int pin);
66 | //GPIOEnum.LOW or GPIOEnum.HIGH
67 | static public native int setGPIOValue(int pin, int value);
68 | static public native int getGPIOValue(int pin);
69 | //GPIOEnum.IN or GPIOEnum.OUT
70 | static public native int setGPIODirection(int pin, int direction);
71 | static public native int getGPIODirection(int pin);
72 |
73 | /* OldInterface: for EEPROM */
74 | static public native int openI2CDevice();
75 | static public native int writeByteDataToI2C(int fd, int pos, byte byteData);
76 | static public native int readByteDataFromI2C(int fd, int pos);
77 |
78 | static public native int getBoardType();
79 | /* getBoardType return value: */
80 | public static final int S3C6410_COMMON = 6410;
81 | public static final int S5PV210_COMMON = 210;
82 | public static final int S5P4412_COMMON = 4412;
83 | public static final int S5P4418_BASE = 4418;
84 | public static final int NanoPi2 = S5P4418_BASE+0;
85 | public static final int NanoPC_T2 = S5P4418_BASE+1;
86 | public static final int NanoPi_S2 = S5P4418_BASE+2;
87 | public static final int Smart4418 = S5P4418_BASE+3;
88 | public static final int NanoPi2_Fire = S5P4418_BASE+4;
89 | public static final int NanoPi_M2 = S5P4418_BASE+5;
90 | public static final int NanoPi_M2A = S5P4418_BASE+7;
91 | public static final int Smart4418SDK = S5P4418_BASE+0x103;
92 | public static final int S5P4418_MAX = Smart4418SDK;
93 | public static final int S5P6818_BASE = 6818;
94 | public static final int NanoPC_T3 = S5P6818_BASE+1;
95 | public static final int NanoPi_S3 = S5P6818_BASE+2;
96 | public static final int Smart6818 = S5P6818_BASE+3;
97 | public static final int NanoPi_M3 = S5P6818_BASE+7;
98 | public static final int S5P6818_MAX = NanoPi_M3;
99 |
100 | static public boolean isS5P4418Board() {
101 | int boardtype = getBoardType();
102 | return boardtype>=S5P4418_BASE && boardtype<=S5P4418_MAX;
103 | }
104 |
105 | static public boolean isS5P6818Board() {
106 | int boardtype = getBoardType();
107 | return boardtype>=S5P6818_BASE && boardtype<=S5P6818_MAX;
108 | }
109 |
110 | static {
111 | try {
112 | System.loadLibrary("friendlyarm-hardware");
113 | } catch (UnsatisfiedLinkError e) {
114 | Log.d("HardwareControler", "libfriendlyarm-hardware library not found!");
115 | }
116 | }
117 | }
118 |
--------------------------------------------------------------------------------
/PWMDemo/src/com/friendlyarm/AndroidSDK/SPI.java:
--------------------------------------------------------------------------------
1 | package com.friendlyarm.AndroidSDK;
2 | import android.util.Log;
3 | import android.widget.Toast;
4 |
5 | import com.friendlyarm.AndroidSDK.HardwareControler;
6 | import com.friendlyarm.AndroidSDK.SPIEnum;
7 | import com.friendlyarm.AndroidSDK.GPIOEnum;
8 | import com.friendlyarm.AndroidSDK.FileCtlEnum;
9 |
10 | public class SPI {
11 | private static final String TAG = "com.friendlyarm.AndroidSDK.SPI";
12 | private int spi_mode = 0;
13 | private int spi_bits = 8;
14 | private int spi_delay = 0;
15 | private int spi_speed = 500000;
16 | private int spi_byte_order = SPIEnum.LSBFIRST;
17 |
18 | private static final String devName = "/dev/spidev1.0";
19 | private int spi_fd = -1;
20 |
21 | public void begin() {
22 | spi_fd = HardwareControler.open( devName, FileCtlEnum.O_RDWR );
23 | if (spi_fd >= 0) {
24 | Log.d(TAG, "open " + devName + "ok!");
25 |
26 | /* spi init */
27 | HardwareControler.setSPIWriteBitsPerWord( spi_fd, spi_bits );
28 | HardwareControler.setSPIReadBitsPerWord( spi_fd, spi_bits );
29 | } else {
30 | Log.d(TAG, "open " + devName + "failed!");
31 | spi_fd = -1;
32 | }
33 | }
34 |
35 | public void end() {
36 | if (spi_fd != -1) {
37 | HardwareControler.close(spi_fd);
38 | spi_fd = -1;
39 | }
40 | }
41 |
42 | public void setBitOrder(int order) {
43 | if (spi_fd < 0) {
44 | return ;
45 | }
46 | spi_byte_order = SPIEnum.MSBFIRST;
47 | if(spi_byte_order == SPIEnum.LSBFIRST) {
48 | spi_mode |= SPIEnum.SPI_LSB_FIRST;
49 | } else {
50 | spi_mode &= ~SPIEnum.SPI_LSB_FIRST;
51 | }
52 | HardwareControler.setSPIBitOrder( spi_fd, spi_byte_order );
53 |
54 | }
55 |
56 | public void setClockDivider(int divider) {
57 | if (spi_fd < 0) {
58 | return ;
59 | }
60 | spi_speed = 66666666/(2*(divider+1));
61 | if(spi_speed > 500000) {
62 | spi_speed = 500000;
63 | }
64 | HardwareControler.setSPIClockDivider( spi_fd, divider);
65 | }
66 |
67 | public void setDataMode(int mode) {
68 | if (spi_fd < 0) {
69 | return ;
70 | }
71 | switch(mode)
72 | {
73 | case SPIEnum.SPI_MODE0:
74 | spi_mode &= ~(SPIEnum.SPI_CPHA|SPIEnum.SPI_CPOL);
75 | break;
76 | case SPIEnum.SPI_MODE1:
77 | spi_mode &= ~(SPIEnum.SPI_CPOL);
78 | spi_mode |= (SPIEnum.SPI_CPHA);
79 | break;
80 | case SPIEnum.SPI_MODE2:
81 | spi_mode |= (SPIEnum.SPI_CPOL);
82 | spi_mode &= ~(SPIEnum.SPI_CPHA);
83 | break;
84 | case SPIEnum.SPI_MODE3:
85 | spi_mode |= (SPIEnum.SPI_CPHA|SPIEnum.SPI_CPOL);
86 | break;
87 | default:
88 | Log.e(TAG, "error data mode");
89 | }
90 |
91 | HardwareControler.setSPIDataMode( spi_fd, spi_mode );
92 | }
93 |
94 | public void setChipSelectPolarity(int cs, int active) {
95 |
96 | }
97 |
98 | public void chipSelect(int cs) {
99 |
100 | }
101 |
102 | public byte transfer(int value) {
103 | if (spi_fd < 0) {
104 | return 0;
105 | }
106 | return (byte) HardwareControler.SPItransferOneByte(spi_fd, (byte) value, spi_delay, spi_speed, spi_bits);
107 | }
108 | }
109 |
--------------------------------------------------------------------------------
/PWMDemo/src/com/friendlyarm/AndroidSDK/SPIEnum.java:
--------------------------------------------------------------------------------
1 | package com.friendlyarm.AndroidSDK;
2 | public class SPIEnum {
3 |
4 | // SPIBitOrder
5 | public final static int LSBFIRST = 0; ///< LSB First
6 | public final static int MSBFIRST = 1; ///< MSB First
7 |
8 | // SPIMode
9 | public final static int SPI_MODE0 = 0; ///< CPOL = 0, CPHA = 0
10 | public final static int SPI_MODE1 = 1; ///< CPOL = 0, CPHA = 1
11 | public final static int SPI_MODE2 = 2; ///< CPOL = 1, CPHA = 0
12 | public final static int SPI_MODE3 = 3; ///< CPOL = 1, CPHA = 1
13 |
14 |
15 | public final static int SPI_CPHA = 0x01;
16 | public final static int SPI_CPOL = 0x02;
17 | public final static int SPI_CS_HIGH = 0x04;
18 | public final static int SPI_LSB_FIRST = 0x08;
19 | public final static int SPI_3WIRE = 0x10;
20 | public final static int SPI_LOOP = 0x20;
21 | public final static int SPI_NO_CS = 0x40;
22 | public final static int SPI_READY = 0x80;
23 |
24 | // SPIClockDivider
25 | public final static int SPI_CLOCK_DIV65536 = 0; ///< 65536 = 256us = 4kHz
26 | public final static int SPI_CLOCK_DIV32768 = 32768; ///< 32768 = 126us = 8kHz
27 | public final static int SPI_CLOCK_DIV16384 = 16384; ///< 16384 = 64us = 15.625kHz
28 | public final static int SPI_CLOCK_DIV8192 = 8192; ///< 8192 = 32us = 31.25kHz
29 | public final static int SPI_CLOCK_DIV4096 = 4096; ///< 4096 = 16us = 62.5kHz
30 | public final static int SPI_CLOCK_DIV2048 = 2048; ///< 2048 = 8us = 125kHz
31 | public final static int SPI_CLOCK_DIV1024 = 1024; ///< 1024 = 4us = 250kHz
32 | public final static int SPI_CLOCK_DIV512 = 512; ///< 512 = 2us = 500kHz
33 | public final static int SPI_CLOCK_DIV256 = 256; ///< 256 = 1us = 1MHz
34 | public final static int SPI_CLOCK_DIV128 = 128; ///< 128 = 500ns = = 2MHz
35 | public final static int SPI_CLOCK_DIV64 = 64; ///< 64 = 250ns = 4MHz
36 | public final static int SPI_CLOCK_DIV32 = 32; ///< 32 = 125ns = 8MHz
37 | public final static int SPI_CLOCK_DIV16 = 16; ///< 16 = 50ns = 20MHz
38 | public final static int SPI_CLOCK_DIV8 = 8; ///< 8 = 25ns = 40MHz
39 | public final static int SPI_CLOCK_DIV4 = 4; ///< 4 = 12.5ns 80MHz
40 | public final static int SPI_CLOCK_DIV2 = 2; ///< 2 = 6.25ns = 160MHz
41 | public final static int SPI_CLOCK_DIV1 = 1; ///< 0 = 256us = 4kHz
42 | }
--------------------------------------------------------------------------------
/PWMDemo/src/com/friendlyarm/PWMDemo/PWMTestingActivity.java:
--------------------------------------------------------------------------------
1 | package com.friendlyarm.PWMDemo;
2 |
3 | import com.friendlyarm.AndroidSDK.HardwareControler;
4 | import com.friendlyarm.PWMDemo.R;
5 | import com.friendlyarm.Utils.CommonFuncs;
6 |
7 | import android.app.Activity;
8 | import android.content.res.Configuration;
9 | import android.os.Bundle;
10 | import android.widget.TextView;
11 | import android.util.Log;
12 | import android.view.View;
13 | import android.widget.Button;
14 | import android.widget.EditText;
15 | import android.view.View.OnClickListener;
16 |
17 | public class PWMTestingActivity extends Activity implements OnClickListener {
18 | HardwareControler hw;
19 | EditText textEditor;
20 | Button btnSub;
21 | Button btnAdd;
22 |
23 | final int smart4418PWMGPIOPin = 97; //GPIOD1, Smart4418/NanoPC-T2/NanoPC-T3
24 |
25 | /** Called when the activity is first created. */
26 | @Override
27 | public void onCreate(Bundle savedInstanceState) {
28 | super.onCreate(savedInstanceState);
29 | setContentView(R.layout.pwmtestingactivity);
30 |
31 | btnSub = ((Button)findViewById(R.id.btnSub));
32 | btnAdd = ((Button)findViewById(R.id.btnAdd));
33 |
34 | btnSub.setOnClickListener(this);
35 | btnAdd.setOnClickListener(this);
36 |
37 | textEditor = ((EditText)findViewById(R.id.fEditor));
38 | hw = new HardwareControler();
39 | }
40 |
41 | private void playPWM()
42 | {
43 | int f;
44 | f = Integer.valueOf(textEditor.getText().toString()).intValue();
45 | int boardType = HardwareControler.getBoardType();
46 | Log.d("PWMDemo", "boardtype = " + boardType);
47 |
48 | if (HardwareControler.isS5P4418Board() || HardwareControler.isS5P6818Board()) {
49 | if (hw.PWMPlayEx(smart4418PWMGPIOPin, f) != 0) {
50 | CommonFuncs.showAlertDialog(this,"Fail to play!");
51 | }
52 | } else {
53 | if (hw.PWMPlay(f) != 0) {
54 | CommonFuncs.showAlertDialog(this,"Fail to play!");
55 | }
56 | }
57 | }
58 |
59 | private void stopPWM()
60 | {
61 | if (HardwareControler.isS5P4418Board() || HardwareControler.isS5P6818Board()) {
62 | if (hw.PWMStopEx(smart4418PWMGPIOPin) != 0) {
63 | CommonFuncs.showAlertDialog(this,"Fail to stop!");
64 | }
65 | } else {
66 | if (hw.PWMStop() != 0) {
67 | CommonFuncs.showAlertDialog(this,"Fail to stop!");
68 | }
69 | }
70 | }
71 |
72 | public void onClick(View v) {
73 | int f;
74 | switch (v.getId()) {
75 | case R.id.btnSub:
76 | f = Integer.valueOf(textEditor.getText().toString()).intValue() - 100;
77 | if (f < 1) {
78 | f = 1;
79 | }
80 | textEditor.setText( "" + f );
81 | playPWM();
82 | break;
83 | case R.id.btnAdd:
84 | f = Integer.valueOf(textEditor.getText().toString()).intValue() + 100;
85 | if (f < 1) {
86 | f = 1;
87 | }
88 | textEditor.setText( "" + f );
89 | playPWM();
90 | break;
91 | default:
92 | break;
93 | }
94 | }
95 |
96 | @Override
97 | public void onStart() {
98 | super.onStart();
99 | }
100 |
101 | @Override
102 | public void onRestart() {
103 | super.onRestart();
104 | }
105 |
106 | @Override
107 | public void onResume() {
108 | super.onResume();
109 | playPWM();
110 | }
111 |
112 | @Override
113 | public void onPause() {
114 | stopPWM();
115 | super.onPause();
116 | }
117 |
118 | @Override
119 | public void onStop() {
120 | stopPWM();
121 | super.onStop();
122 | }
123 |
124 | @Override
125 | public void onDestroy() {
126 | stopPWM();
127 | super.onDestroy();
128 | }
129 | }
130 |
--------------------------------------------------------------------------------
/PWMDemo/src/com/friendlyarm/Utils/BorderScrollView.java:
--------------------------------------------------------------------------------
1 | package com.friendlyarm.Utils;
2 |
3 | import android.content.Context;
4 | import android.graphics.Canvas;
5 | import android.graphics.Paint;
6 | import android.util.AttributeSet;
7 | import android.widget.ScrollView;
8 |
9 |
10 | public class BorderScrollView extends ScrollView
11 | {
12 | @Override
13 | protected void onDraw(Canvas canvas)
14 | {
15 | super.onDraw(canvas);
16 | Paint paint = new Paint();
17 | paint.setColor(android.graphics.Color.GRAY);
18 | canvas.drawLine(0, 0, this.getWidth() - 1, 0, paint);
19 | canvas.drawLine(0, 0, 0, this.getHeight() - 1, paint);
20 | canvas.drawLine(this.getWidth() - 1, 0, this.getWidth() - 1, this.getHeight() - 1, paint);
21 | canvas.drawLine(0, this.getHeight() - 1, this.getWidth() - 1, this.getHeight() - 1, paint);
22 | }
23 | public BorderScrollView(Context context, AttributeSet attrs)
24 | {
25 | super(context, attrs);
26 | }
27 | }
--------------------------------------------------------------------------------
/PWMDemo/src/com/friendlyarm/Utils/CommonFuncs.java:
--------------------------------------------------------------------------------
1 | package com.friendlyarm.Utils;
2 |
3 | import android.app.Activity;
4 | import android.app.AlertDialog;
5 | import android.widget.Toast;
6 | import android.app.Activity;
7 | import android.os.Bundle;
8 | import android.os.Handler;
9 | import android.os.Message;
10 | import android.content.Context;
11 | import android.content.DialogInterface;
12 |
13 | public class CommonFuncs {
14 | public static void showAlertDialog(Context context, String message) {
15 | AlertDialog.Builder builder = new AlertDialog.Builder(context);
16 | builder.setMessage(message)
17 | .setCancelable(false)
18 | .setPositiveButton("Close",
19 | new DialogInterface.OnClickListener() {
20 | public void onClick(DialogInterface dialog, int id) {
21 | }
22 | });
23 | AlertDialog alert = builder.create();
24 | alert.show();
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # AndroidHardwareAccess
2 | Examples for accessing FriendlyELEC's development board hardware on android.
3 |
4 | ## Applicable Boards & OS
5 |
6 | * NanoPC-T3/NanoPi-M3: Android 5
7 | * Smart4418 SDK/NanoPC-T2/NanoPi-M2/NanoPi S2: Android 5, Android 4.4
8 | * Tiny4412: Android 4.2, Android 5
9 | * Tiny210/Smart210/Mini210: Android 4.2
10 | * Tiny6410/Mini6410: Android 2.3
11 |
12 | ## Accessible Interfaces & Ports
13 |
14 | * Serial Port
15 | * PWM
16 | * EEPROM
17 | * ADC
18 | * LED
19 | * LCD 1602 (I2C)
20 | * OLED (SPI)
21 |
22 | I2C, SPI, GPIO are accessible.
23 |
24 | 
25 |
26 | ## Document
27 | [WiKi](http://wiki.friendlyarm.com/wiki/index.php/Android_Thing:_Android_Hardware_Programming)
28 | [Guide to API in Chinese](https://github.com/friendlyarm/AndroidHardwareAccess/blob/master/%E5%8F%8B%E5%96%84%E7%94%B5%E5%AD%90Android%E7%A1%AC%E4%BB%B6%E5%BC%80%E5%8F%91%E6%8C%87%E5%8D%97.pdf)
29 |
--------------------------------------------------------------------------------
/SDCardDemo/Android.mk:
--------------------------------------------------------------------------------
1 | LOCAL_PATH:= $(call my-dir)
2 | include $(CLEAR_VARS)
3 |
4 | LOCAL_MODULE_TAGS := eng
5 |
6 |
7 | LOCAL_SRC_FILES := \
8 | $(call all-java-files-under, src)
9 |
10 | LOCAL_PACKAGE_NAME := SDCardDemo
11 |
12 | include $(BUILD_PACKAGE)
13 |
14 | # additionally, build tests in sub-folders in a separate .apk
15 | include $(call all-makefiles-under,$(LOCAL_PATH))
16 |
--------------------------------------------------------------------------------
/SDCardDemo/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
5 |
6 |
9 |
10 |
11 |
12 |
13 |
17 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/SDCardDemo/build.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | ANDROIDPATH=/opt/FriendlyARM/smart4418/android/
4 | APPNAME=SDCardDemo
5 | APKNAME=${APPNAME}-r1.0.apk
6 | PRJPATH=$PWD
7 | cd $PRJPATH
8 | rm -rf bin gen
9 |
10 | cd $ANDROIDPATH
11 | rm -f $APKNAME
12 | . setenv
13 | mmm $PRJPATH -B
14 | java -jar out/host/linux-x86/framework/signapk.jar vendor/friendly-arm/nanopi2/security/platform.x509.pem vendor/friendly-arm/nanopi2/security/platform.pk8 out/target/product/nanopi2/obj/APPS/${APPNAME}_intermediates/package.apk.unsigned ${APPNAME}-unaligned.apk
15 | zipalign -v 4 ${APPNAME}-unaligned.apk $APKNAME
16 | adb install -r ${APKNAME}
17 |
--------------------------------------------------------------------------------
/SDCardDemo/ic_launcher-web.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/friendlyarm/AndroidHardwareAccess/8084bdb9d85b9e466aea5544769ffb30d60dfda3/SDCardDemo/ic_launcher-web.png
--------------------------------------------------------------------------------
/SDCardDemo/res/drawable-hdpi/ic_action_search.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/friendlyarm/AndroidHardwareAccess/8084bdb9d85b9e466aea5544769ffb30d60dfda3/SDCardDemo/res/drawable-hdpi/ic_action_search.png
--------------------------------------------------------------------------------
/SDCardDemo/res/drawable-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/friendlyarm/AndroidHardwareAccess/8084bdb9d85b9e466aea5544769ffb30d60dfda3/SDCardDemo/res/drawable-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/SDCardDemo/res/drawable-ldpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/friendlyarm/AndroidHardwareAccess/8084bdb9d85b9e466aea5544769ffb30d60dfda3/SDCardDemo/res/drawable-ldpi/ic_launcher.png
--------------------------------------------------------------------------------
/SDCardDemo/res/drawable-mdpi/ic_action_search.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/friendlyarm/AndroidHardwareAccess/8084bdb9d85b9e466aea5544769ffb30d60dfda3/SDCardDemo/res/drawable-mdpi/ic_action_search.png
--------------------------------------------------------------------------------
/SDCardDemo/res/drawable-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/friendlyarm/AndroidHardwareAccess/8084bdb9d85b9e466aea5544769ffb30d60dfda3/SDCardDemo/res/drawable-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/SDCardDemo/res/drawable-xhdpi/ic_action_search.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/friendlyarm/AndroidHardwareAccess/8084bdb9d85b9e466aea5544769ffb30d60dfda3/SDCardDemo/res/drawable-xhdpi/ic_action_search.png
--------------------------------------------------------------------------------
/SDCardDemo/res/drawable-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/friendlyarm/AndroidHardwareAccess/8084bdb9d85b9e466aea5544769ffb30d60dfda3/SDCardDemo/res/drawable-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/SDCardDemo/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
15 |
16 |
21 |
22 |
27 |
28 |
33 |
34 |
39 |
40 |
--------------------------------------------------------------------------------
/SDCardDemo/res/menu/activity_main.xml:
--------------------------------------------------------------------------------
1 |
7 |
--------------------------------------------------------------------------------
/SDCardDemo/res/values-v11/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/SDCardDemo/res/values-v14/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/SDCardDemo/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | SD Card Demo
4 | Settings
5 | SD Card Demo
6 |
7 | 1. Write SD File
8 | 2. Clear Screen
9 | 3. Read SD File
10 | 4. Close
11 |
12 |
--------------------------------------------------------------------------------
/SDCardDemo/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/SDCardDemo/src/com/FriendlyARM/sdcarddemo/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.FriendlyARM.sdcarddemo;
2 |
3 | import java.io.*;
4 | import android.app.Activity;
5 | import android.os.Bundle;
6 | import android.view.*;
7 | import android.view.View.OnClickListener;
8 | import android.widget.*;
9 | import android.os.Environment;
10 |
11 | public class MainActivity extends Activity {
12 | private static final String tag="SDCardDemo";
13 | EditText txtData;
14 | Button btnWriteSDFile;
15 | Button btnReadSDFile;
16 | Button btnClearScreen;
17 | Button btnClose;
18 |
19 | private static final String sdRootPath = "/sdcard";
20 | private static final String appPath = "Android/com.FriendlyARM.sdcarddemo";
21 |
22 | public boolean createDirIfNotExists(String path) {
23 | boolean ret = true;
24 | File file = new File(path);
25 | if (!file.exists()) {
26 | if (!file.mkdirs()) {
27 | Toast.makeText(getBaseContext(),
28 | "Create dir failed: "+ path,
29 | Toast.LENGTH_SHORT).show();
30 | ret = false;
31 | }
32 | }
33 | return ret;
34 | }
35 |
36 | public String getAppPathOnSDCard()
37 | {
38 | return sdRootPath + "/" + appPath;
39 | }
40 |
41 | @Override
42 | public void onCreate(Bundle savedInstanceState) {
43 | super.onCreate(savedInstanceState);
44 | setContentView(R.layout.activity_main);
45 |
46 | createDirIfNotExists(getAppPathOnSDCard());
47 | File file = new File(getAppPathOnSDCard());
48 | if (!file.exists()) {
49 | Toast.makeText(getBaseContext(),
50 | "Path not found: "+ getAppPathOnSDCard(),
51 | Toast.LENGTH_SHORT).show();
52 | }
53 |
54 | // bind GUI elements with local controls
55 | txtData = (EditText) findViewById(R.id.txtData);
56 | txtData.setHint("Enter some lines of data here, the text will write to: " +getAppPathOnSDCard() + "/mysdfile.txt");
57 |
58 | btnWriteSDFile = (Button) findViewById(R.id.btnWriteSDFile);
59 | btnWriteSDFile.setOnClickListener(new OnClickListener() {
60 | public void onClick(View v) {
61 | // write on SD card file data in the text box
62 | try {
63 | if (txtData.getText().length()==0) {
64 | Toast.makeText(getBaseContext(),
65 | "Please type something in the textbox first.",
66 | Toast.LENGTH_SHORT).show();
67 | return ;
68 | }
69 | File myFile = new File(getAppPathOnSDCard() + "/mysdfile.txt");
70 | myFile.createNewFile();
71 | FileOutputStream fOut = new FileOutputStream(myFile);
72 | OutputStreamWriter myOutWriter =
73 | new OutputStreamWriter(fOut);
74 | myOutWriter.append(txtData.getText());
75 | myOutWriter.close();
76 | fOut.close();
77 | Toast.makeText(getBaseContext(),
78 | "Done writing SD 'mysdfile.txt'",
79 | Toast.LENGTH_SHORT).show();
80 | } catch (Exception e) {
81 | Toast.makeText(getBaseContext(), e.getMessage(),
82 | Toast.LENGTH_SHORT).show();
83 | }
84 | }// onClick
85 | }); // btnWriteSDFile
86 |
87 | btnReadSDFile = (Button) findViewById(R.id.btnReadSDFile);
88 | btnReadSDFile.setOnClickListener(new OnClickListener() {
89 | public void onClick(View v) {
90 | // write on SD card file data in the text box
91 | try {
92 | File myFile = new File(getAppPathOnSDCard() + "/mysdfile.txt");
93 | FileInputStream fIn = new FileInputStream(myFile);
94 | BufferedReader myReader = new BufferedReader(
95 | new InputStreamReader(fIn));
96 | String aDataRow = "";
97 | String aBuffer = "";
98 | while ((aDataRow = myReader.readLine()) != null) {
99 | aBuffer += aDataRow + "\n";
100 | }
101 | txtData.setText(aBuffer);
102 | myReader.close();
103 | Toast.makeText(getBaseContext(),
104 | "Done reading SD 'mysdfile.txt'",
105 | Toast.LENGTH_SHORT).show();
106 | } catch (Exception e) {
107 | Toast.makeText(getBaseContext(), e.getMessage(),
108 | Toast.LENGTH_SHORT).show();
109 | }
110 | }// onClick
111 | }); // btnReadSDFile
112 |
113 | btnClearScreen = (Button) findViewById(R.id.btnClearScreen);
114 | btnClearScreen.setOnClickListener(new OnClickListener() {
115 |
116 | public void onClick(View v) {
117 | // clear text box
118 | txtData.setText("");
119 | }
120 | }); // btnClearScreen
121 |
122 | btnClose = (Button) findViewById(R.id.btnClose);
123 | btnClose.setOnClickListener(new OnClickListener() {
124 | public void onClick(View v) {
125 | // clear text box
126 | finish();
127 | }
128 | }); // btnClose
129 | }// onCreate
130 | }
131 |
--------------------------------------------------------------------------------
/SPI_OLED_Demo/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/SPI_OLED_Demo/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | SPI_OLED_Demo
4 |
5 |
6 |
7 |
8 |
9 | com.android.ide.eclipse.adt.ResourceManagerBuilder
10 |
11 |
12 |
13 |
14 | com.android.ide.eclipse.adt.PreCompilerBuilder
15 |
16 |
17 |
18 |
19 | org.eclipse.jdt.core.javabuilder
20 |
21 |
22 |
23 |
24 | com.android.ide.eclipse.adt.ApkBuilder
25 |
26 |
27 |
28 |
29 |
30 | com.android.ide.eclipse.adt.AndroidNature
31 | org.eclipse.jdt.core.javanature
32 |
33 |
34 |
--------------------------------------------------------------------------------
/SPI_OLED_Demo/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
3 | org.eclipse.jdt.core.compiler.compliance=1.6
4 | org.eclipse.jdt.core.compiler.source=1.6
5 |
--------------------------------------------------------------------------------
/SPI_OLED_Demo/Android.mk:
--------------------------------------------------------------------------------
1 | LOCAL_PATH:= $(call my-dir)
2 | include $(CLEAR_VARS)
3 |
4 | LOCAL_SRC_FILES := $(call all-subdir-java-files)
5 |
6 | LOCAL_PACKAGE_NAME := SPI-OLED
7 |
8 | LOCAL_CERTIFICATE := platform
9 | LOCAL_MODULE_TAGS := eng
10 |
11 | include $(BUILD_PACKAGE)
12 |
--------------------------------------------------------------------------------
/SPI_OLED_Demo/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
9 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/SPI_OLED_Demo/Smart4418SDK+OLED.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/friendlyarm/AndroidHardwareAccess/8084bdb9d85b9e466aea5544769ffb30d60dfda3/SPI_OLED_Demo/Smart4418SDK+OLED.png
--------------------------------------------------------------------------------
/SPI_OLED_Demo/build.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | ANDROIDPATH=/opt/FriendlyARM/smart4418/android/
4 | APPNAME=SPI-OLED
5 | APKNAME=${APPNAME}-r1.0.apk
6 | PRJPATH=$PWD
7 | cd $PRJPATH
8 | rm -rf bin gen
9 |
10 | cd $ANDROIDPATH
11 | rm -f $APKNAME
12 | . setenv
13 | mmm $PRJPATH -B
14 | java -jar out/host/linux-x86/framework/signapk.jar vendor/friendly-arm/nanopi2/security/platform.x509.pem vendor/friendly-arm/nanopi2/security/platform.pk8 out/target/product/nanopi2/obj/APPS/${APPNAME}_intermediates/package.apk.unsigned ${APPNAME}-unaligned.apk
15 | zipalign -v 4 ${APPNAME}-unaligned.apk $APKNAME
16 | adb install -r ${APKNAME}
17 |
--------------------------------------------------------------------------------
/SPI_OLED_Demo/libs/armeabi/libfriendlyarm-hardware.so:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/friendlyarm/AndroidHardwareAccess/8084bdb9d85b9e466aea5544769ffb30d60dfda3/SPI_OLED_Demo/libs/armeabi/libfriendlyarm-hardware.so
--------------------------------------------------------------------------------
/SPI_OLED_Demo/project.properties:
--------------------------------------------------------------------------------
1 | # This file is automatically generated by Android Tools.
2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3 | #
4 | # This file must be checked in Version Control Systems.
5 | #
6 | # To customize properties used by the Ant build system edit
7 | # "ant.properties", and override values to adapt the script to your
8 | # project structure.
9 | #
10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
12 |
13 | # Project target.
14 | target=android-15
15 |
--------------------------------------------------------------------------------
/SPI_OLED_Demo/res/drawable/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/friendlyarm/AndroidHardwareAccess/8084bdb9d85b9e466aea5544769ffb30d60dfda3/SPI_OLED_Demo/res/drawable/icon.png
--------------------------------------------------------------------------------
/SPI_OLED_Demo/res/layout/mainactivity.xml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
8 |
12 |
13 |
14 |
17 |
18 |
21 |
22 |
--------------------------------------------------------------------------------
/SPI_OLED_Demo/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | SPI-OLED
4 | SPI-OLED
5 | Back
6 | Clear
7 |
8 |
--------------------------------------------------------------------------------
/SPI_OLED_Demo/src/com/friendlyarm/AndroidSDK/FileCtlEnum.java:
--------------------------------------------------------------------------------
1 | package com.friendlyarm.AndroidSDK;
2 |
3 | public class FileCtlEnum {
4 | /* File Flag */
5 | public final static int O_ACCMODE = 00000003;
6 | public final static int O_RDONLY = 00000000;
7 | public final static int O_WRONLY = 00000001;
8 | public final static int O_RDWR = 00000002;
9 | public final static int O_CREAT = 00000100; /* not fcntl */
10 | public final static int O_EXCL = 00000200; /* not fcntl */
11 | public final static int O_NOCTTY = 00000400; /* not fcntl */
12 | public final static int O_TRUNC = 00001000; /* not fcntl */
13 | public final static int O_APPEND = 00002000;
14 | public final static int O_NONBLOCK = 00004000;
15 | public final static int O_DSYNC = 00010000; /* used to be O_SYNC, see below */
16 | public final static int FASYNC = 00020000; /* fcntl, for BSD compatibility */
17 | public final static int O_DIRECT = 00040000; /* direct disk access hint */
18 | public final static int O_LARGEFILE = 00100000;
19 | public final static int O_DIRECTORY = 00200000; /* must be a directory */
20 | public final static int O_NOFOLLOW = 00400000; /* don't follow links */
21 | public final static int O_NOATIME = 01000000;
22 | public final static int O_CLOEXEC = 02000000; /* set close_on_exec */
23 | }
24 |
--------------------------------------------------------------------------------
/SPI_OLED_Demo/src/com/friendlyarm/AndroidSDK/GPIOEnum.java:
--------------------------------------------------------------------------------
1 | package com.friendlyarm.AndroidSDK;
2 |
3 | /*
4 | GPIO signals have paths like /sys/class/gpio/gpio42/ (for GPIO #42)
5 | and have the following read/write attributes:
6 |
7 | /sys/class/gpio/gpioN/
8 |
9 | "direction" ... reads as either "in" or "out". This value may
10 | normally be written. Writing as "out" defaults to
11 | initializing the value as low. To ensure glitch free
12 | operation, values "low" and "high" may be written to
13 | configure the GPIO as an output with that initial value.
14 |
15 | Note that this attribute *will not exist* if the kernel
16 | doesn't support changing the direction of a GPIO, or
17 | it was exported by kernel code that didn't explicitly
18 | allow userspace to reconfigure this GPIO's direction.
19 |
20 | "value" ... reads as either 0 (low) or 1 (high). If the GPIO
21 | is configured as an output, this value may be written;
22 | any nonzero value is treated as high.
23 |
24 | If the pin can be configured as interrupt-generating interrupt
25 | and if it has been configured to generate interrupts (see the
26 | description of "edge"), you can poll(2) on that file and
27 | poll(2) will return whenever the interrupt was triggered. If
28 | you use poll(2), set the events POLLPRI and POLLERR. If you
29 | use select(2), set the file descriptor in exceptfds. After
30 | poll(2) returns, either lseek(2) to the beginning of the sysfs
31 | file and read the new value or close the file and re-open it
32 | to read the value.
33 | */
34 |
35 | public class GPIOEnum {
36 | //Direction
37 | public final static int IN = 1;
38 | public final static int OUT = 2;
39 |
40 | //Value
41 | public final static int LOW = 0;
42 | public final static int HIGH = 1;
43 | }
44 |
--------------------------------------------------------------------------------
/SPI_OLED_Demo/src/com/friendlyarm/AndroidSDK/HardwareControler.java:
--------------------------------------------------------------------------------
1 | package com.friendlyarm.AndroidSDK;
2 | import android.util.Log;
3 |
4 | public class HardwareControler
5 | {
6 | /* File */
7 | static public native int open(String devName, int flags);
8 | static public native int write(int fd, byte[] data);
9 | static public native int read(int fd, byte[] buf, int len);
10 | static public native int select(int fd, int sec, int usec);
11 | static public native void close(int fd);
12 | static public native int ioctlWithIntValue(int fd, int cmd, int value);
13 |
14 | /* Serial Port */
15 | static public native int openSerialPort( String devName, long baud, int dataBits, int stopBits );
16 | static public native int openSerialPortEx( String devName
17 | , long baud
18 | , int dataBits
19 | , int stopBits
20 | , String parityBit
21 | , String flowCtrl
22 | );
23 |
24 | /* LED */
25 | static public native int setLedState( int ledID, int ledState );
26 |
27 | /* PWM */
28 | static public native int PWMPlay(int frequency);
29 | static public native int PWMStop();
30 | static public native int PWMPlayEx(int gpioPin, int frequency);
31 | static public native int PWMStopEx(int gpioPin);
32 |
33 | /* ADC */
34 | static public native int readADC();
35 | static public native int readADCWithChannel(int channel);
36 | static public native int[] readADCWithChannels(int[] channels);
37 |
38 | /* I2C */
39 | static public native int setI2CSlave(int fd, int slave);
40 | static public native int setI2CTimeout(int fd, int timeout);
41 | static public native int setI2CRetries(int fd, int retries);
42 | static public native int I2CReadByteData(int fd, int pos, int wait_us);
43 | static public native int I2CReadByte(int fd, int wait_us);
44 | static public native int I2CWriteByteData(int fd, int pos, byte byteData, int wait_us);
45 | static public native int I2CWriteByte(int fd, byte byteData, int wait_us);
46 |
47 | /* Discard */ static public native int readByteFromI2C(int fd, int pos, int wait_ms);
48 | /* Discard */ static public native int writeByteToI2C(int fd, int pos, byte byteData, int wait_ms);
49 | /* Discard */ static public native int writeByteToI2C2(int fd, byte byteData, int wait_ms);
50 |
51 | /* SPI */
52 | static public native int setSPIWriteBitsPerWord( int spi_fd, int bits );
53 | static public native int setSPIReadBitsPerWord( int spi_fd, int bits );
54 | static public native int setSPIBitOrder( int spi_fd, int order);
55 | static public native int setSPIClockDivider( int spi_fd, int divider);
56 | static public native int setSPIMaxSpeed( int spi_fd, int spi_speed);
57 | static public native int setSPIDataMode( int spi_fd, int mode);
58 | static public native int SPItransferOneByte( int spi_fd, byte byteData, int spi_delay, int spi_speed, int spi_bits);
59 | static public native int SPItransferBytes(int spi_fd, byte[] writeData, byte[] readBuff, int spi_delay, int spi_speed, int spi_bits);
60 | static public native int writeBytesToSPI(int spi_fd, byte[] writeData, int spi_delay, int spi_speed, int spi_bits);
61 | static public native int readBytesFromSPI(int spi_fd, byte[] readBuff, int spi_delay, int spi_speed, int spi_bits);
62 |
63 | /* GPIO */
64 | static public native int exportGPIOPin(int pin);
65 | static public native int unexportGPIOPin(int pin);
66 | //GPIOEnum.LOW or GPIOEnum.HIGH
67 | static public native int setGPIOValue(int pin, int value);
68 | static public native int getGPIOValue(int pin);
69 | //GPIOEnum.IN or GPIOEnum.OUT
70 | static public native int setGPIODirection(int pin, int direction);
71 | static public native int getGPIODirection(int pin);
72 |
73 | /* OldInterface: for EEPROM */
74 | static public native int openI2CDevice();
75 | static public native int writeByteDataToI2C(int fd, int pos, byte byteData);
76 | static public native int readByteDataFromI2C(int fd, int pos);
77 |
78 | static public native int getBoardType();
79 | /* getBoardType return value: */
80 | public static final int S3C6410_COMMON = 6410;
81 | public static final int S5PV210_COMMON = 210;
82 | public static final int S5P4412_COMMON = 4412;
83 | public static final int S5P4418_BASE = 4418;
84 | public static final int NanoPi2 = S5P4418_BASE+0;
85 | public static final int NanoPC_T2 = S5P4418_BASE+1;
86 | public static final int NanoPi_S2 = S5P4418_BASE+2;
87 | public static final int Smart4418 = S5P4418_BASE+3;
88 | public static final int NanoPi2_Fire = S5P4418_BASE+4;
89 | public static final int NanoPi_M2 = S5P4418_BASE+5;
90 | public static final int NanoPi_M2A = S5P4418_BASE+7;
91 | public static final int Smart4418SDK = S5P4418_BASE+0x103;
92 | public static final int S5P4418_MAX = Smart4418SDK;
93 | public static final int S5P6818_BASE = 6818;
94 | public static final int NanoPC_T3 = S5P6818_BASE+1;
95 | public static final int NanoPi_S3 = S5P6818_BASE+2;
96 | public static final int Smart6818 = S5P6818_BASE+3;
97 | public static final int NanoPi_M3 = S5P6818_BASE+7;
98 | public static final int S5P6818_MAX = NanoPi_M3;
99 |
100 | static public boolean isS5P4418Board() {
101 | int boardtype = getBoardType();
102 | return boardtype>=S5P4418_BASE && boardtype<=S5P4418_MAX;
103 | }
104 |
105 | static public boolean isS5P6818Board() {
106 | int boardtype = getBoardType();
107 | return boardtype>=S5P6818_BASE && boardtype<=S5P6818_MAX;
108 | }
109 |
110 | static {
111 | try {
112 | System.loadLibrary("friendlyarm-hardware");
113 | } catch (UnsatisfiedLinkError e) {
114 | Log.d("HardwareControler", "libfriendlyarm-hardware library not found!");
115 | }
116 | }
117 | }
118 |
--------------------------------------------------------------------------------
/SPI_OLED_Demo/src/com/friendlyarm/AndroidSDK/SPI.java:
--------------------------------------------------------------------------------
1 | package com.friendlyarm.AndroidSDK;
2 | import android.util.Log;
3 | import android.widget.Toast;
4 |
5 | import com.friendlyarm.AndroidSDK.HardwareControler;
6 | import com.friendlyarm.AndroidSDK.SPIEnum;
7 | import com.friendlyarm.AndroidSDK.GPIOEnum;
8 | import com.friendlyarm.AndroidSDK.FileCtlEnum;
9 |
10 | public class SPI {
11 | private static final String TAG = "com.friendlyarm.AndroidSDK.SPI";
12 | private int spi_mode = 0;
13 | private int spi_bits = 8;
14 | private int spi_delay = 0;
15 | private int spi_speed = 500000;
16 | private int spi_byte_order = SPIEnum.LSBFIRST;
17 |
18 | private static final String devName = "/dev/spidev1.0";
19 | private int spi_fd = -1;
20 |
21 | public void begin() {
22 | spi_fd = HardwareControler.open( devName, FileCtlEnum.O_RDWR );
23 | if (spi_fd >= 0) {
24 | Log.d(TAG, "open " + devName + "ok!");
25 |
26 | /* spi init */
27 | HardwareControler.setSPIWriteBitsPerWord( spi_fd, spi_bits );
28 | HardwareControler.setSPIReadBitsPerWord( spi_fd, spi_bits );
29 | } else {
30 | Log.d(TAG, "open " + devName + "failed!");
31 | spi_fd = -1;
32 | }
33 | }
34 |
35 | public void end() {
36 | if (spi_fd != -1) {
37 | HardwareControler.close(spi_fd);
38 | spi_fd = -1;
39 | }
40 | }
41 |
42 | public void setBitOrder(int order) {
43 | if (spi_fd < 0) {
44 | return ;
45 | }
46 | spi_byte_order = SPIEnum.MSBFIRST;
47 | if(spi_byte_order == SPIEnum.LSBFIRST) {
48 | spi_mode |= SPIEnum.SPI_LSB_FIRST;
49 | } else {
50 | spi_mode &= ~SPIEnum.SPI_LSB_FIRST;
51 | }
52 | HardwareControler.setSPIBitOrder( spi_fd, spi_byte_order );
53 |
54 | }
55 |
56 | public void setClockDivider(int divider) {
57 | if (spi_fd < 0) {
58 | return ;
59 | }
60 | spi_speed = 66666666/(2*(divider+1));
61 | if(spi_speed > 500000) {
62 | spi_speed = 500000;
63 | }
64 | HardwareControler.setSPIClockDivider( spi_fd, divider);
65 | }
66 |
67 | public void setDataMode(int mode) {
68 | if (spi_fd < 0) {
69 | return ;
70 | }
71 | switch(mode)
72 | {
73 | case SPIEnum.SPI_MODE0:
74 | spi_mode &= ~(SPIEnum.SPI_CPHA|SPIEnum.SPI_CPOL);
75 | break;
76 | case SPIEnum.SPI_MODE1:
77 | spi_mode &= ~(SPIEnum.SPI_CPOL);
78 | spi_mode |= (SPIEnum.SPI_CPHA);
79 | break;
80 | case SPIEnum.SPI_MODE2:
81 | spi_mode |= (SPIEnum.SPI_CPOL);
82 | spi_mode &= ~(SPIEnum.SPI_CPHA);
83 | break;
84 | case SPIEnum.SPI_MODE3:
85 | spi_mode |= (SPIEnum.SPI_CPHA|SPIEnum.SPI_CPOL);
86 | break;
87 | default:
88 | Log.e(TAG, "error data mode");
89 | }
90 |
91 | HardwareControler.setSPIDataMode( spi_fd, spi_mode );
92 | }
93 |
94 | public void setChipSelectPolarity(int cs, int active) {
95 |
96 | }
97 |
98 | public void chipSelect(int cs) {
99 |
100 | }
101 |
102 | public byte transfer(int value) {
103 | if (spi_fd < 0) {
104 | return 0;
105 | }
106 | return (byte) HardwareControler.SPItransferOneByte(spi_fd, (byte) value, spi_delay, spi_speed, spi_bits);
107 | }
108 | }
109 |
--------------------------------------------------------------------------------
/SPI_OLED_Demo/src/com/friendlyarm/AndroidSDK/SPIEnum.java:
--------------------------------------------------------------------------------
1 | package com.friendlyarm.AndroidSDK;
2 | public class SPIEnum {
3 |
4 | // SPIBitOrder
5 | public final static int LSBFIRST = 0; ///< LSB First
6 | public final static int MSBFIRST = 1; ///< MSB First
7 |
8 | // SPIMode
9 | public final static int SPI_MODE0 = 0; ///< CPOL = 0, CPHA = 0
10 | public final static int SPI_MODE1 = 1; ///< CPOL = 0, CPHA = 1
11 | public final static int SPI_MODE2 = 2; ///< CPOL = 1, CPHA = 0
12 | public final static int SPI_MODE3 = 3; ///< CPOL = 1, CPHA = 1
13 |
14 |
15 | public final static int SPI_CPHA = 0x01;
16 | public final static int SPI_CPOL = 0x02;
17 | public final static int SPI_CS_HIGH = 0x04;
18 | public final static int SPI_LSB_FIRST = 0x08;
19 | public final static int SPI_3WIRE = 0x10;
20 | public final static int SPI_LOOP = 0x20;
21 | public final static int SPI_NO_CS = 0x40;
22 | public final static int SPI_READY = 0x80;
23 |
24 | // SPIClockDivider
25 | public final static int SPI_CLOCK_DIV65536 = 0; ///< 65536 = 256us = 4kHz
26 | public final static int SPI_CLOCK_DIV32768 = 32768; ///< 32768 = 126us = 8kHz
27 | public final static int SPI_CLOCK_DIV16384 = 16384; ///< 16384 = 64us = 15.625kHz
28 | public final static int SPI_CLOCK_DIV8192 = 8192; ///< 8192 = 32us = 31.25kHz
29 | public final static int SPI_CLOCK_DIV4096 = 4096; ///< 4096 = 16us = 62.5kHz
30 | public final static int SPI_CLOCK_DIV2048 = 2048; ///< 2048 = 8us = 125kHz
31 | public final static int SPI_CLOCK_DIV1024 = 1024; ///< 1024 = 4us = 250kHz
32 | public final static int SPI_CLOCK_DIV512 = 512; ///< 512 = 2us = 500kHz
33 | public final static int SPI_CLOCK_DIV256 = 256; ///< 256 = 1us = 1MHz
34 | public final static int SPI_CLOCK_DIV128 = 128; ///< 128 = 500ns = = 2MHz
35 | public final static int SPI_CLOCK_DIV64 = 64; ///< 64 = 250ns = 4MHz
36 | public final static int SPI_CLOCK_DIV32 = 32; ///< 32 = 125ns = 8MHz
37 | public final static int SPI_CLOCK_DIV16 = 16; ///< 16 = 50ns = 20MHz
38 | public final static int SPI_CLOCK_DIV8 = 8; ///< 8 = 25ns = 40MHz
39 | public final static int SPI_CLOCK_DIV4 = 4; ///< 4 = 12.5ns 80MHz
40 | public final static int SPI_CLOCK_DIV2 = 2; ///< 2 = 6.25ns = 160MHz
41 | public final static int SPI_CLOCK_DIV1 = 1; ///< 0 = 256us = 4kHz
42 | }
--------------------------------------------------------------------------------
/SPI_OLED_Demo/src/com/friendlyarm/SPI_OLED/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.friendlyarm.SPI_OLED;
2 |
3 | import android.app.Activity;
4 | import android.os.Bundle;
5 | import android.os.Handler;
6 | import android.os.Message;
7 | import android.util.Log;
8 | import android.view.View;
9 | import android.view.View.OnClickListener;
10 | import android.widget.Button;
11 | import android.widget.EditText;
12 | import android.widget.Toast;
13 |
14 | import java.util.Timer;
15 | import java.util.TimerTask;
16 |
17 | import com.friendlyarm.AndroidSDK.HardwareControler;
18 | import com.friendlyarm.SPI_OLED.R;
19 | import com.friendlyarm.SPI_OLED.OLED;
20 |
21 | public class MainActivity extends Activity implements OnClickListener {
22 | private static final String TAG = "SPILCD";
23 | private EditText toEditor = null;
24 |
25 | private final String devName = "/dev/spidev0.0"; /*For S5P4418*/
26 | //private final int gpioPin_For_DC = 75; /*GPIOC11 on Smart4418*/
27 | //private final int gpioPin_For_Reset = 74; /*GPIOC10 on Smart4418*/
28 |
29 | private final int gpioPin_For_DC = 68; /*GPIOC4 on T3, PIn17*/
30 | private final int gpioPin_For_Reset = 71; /*GPIOC7 on T3, Pin18*/
31 |
32 | OLED oled;
33 |
34 | private Timer timer = new Timer();
35 | private Timer timer2 = new Timer();
36 | private int spi_init_step = 0;
37 |
38 | @Override
39 | public void onDestroy() {
40 | timer.cancel();
41 | timer2.cancel();
42 |
43 | HardwareControler.unexportGPIOPin( gpioPin_For_DC );
44 | HardwareControler.unexportGPIOPin( gpioPin_For_Reset );
45 | oled.Deinit();
46 |
47 | super.onDestroy();
48 | }
49 |
50 | @Override
51 | public void onCreate(Bundle savedInstanceState) {
52 | super.onCreate(savedInstanceState);
53 | setContentView(R.layout.mainactivity);
54 | setTitle("SPI OLED");
55 |
56 | Button backButton = (Button)findViewById(R.id.backButton);
57 | backButton.setOnClickListener(this);
58 | ((Button)findViewById(R.id.cleanButton)).setOnClickListener(this);
59 |
60 | toEditor = (EditText)findViewById(R.id.toEditor);
61 | toEditor.setText("ABCDEFGHIJKOMNOPabcdefghijkomnop1234567890123456,./<>?;':\"[]{}*@");
62 |
63 |
64 |
65 | /* no focus when begin */
66 | toEditor.clearFocus();
67 | toEditor.setFocusable(false);
68 | toEditor.setFocusableInTouchMode(true);
69 |
70 |
71 | oled = new OLED();
72 | if (HardwareControler.exportGPIOPin( gpioPin_For_DC ) == 0
73 | && HardwareControler.exportGPIOPin( gpioPin_For_Reset ) == 0) {
74 |
75 | Log.d(TAG, "exportGPIOPin ok");
76 |
77 | /*
78 | * 1->set direction gpio
79 | * 2->set gpio value
80 | * 3->unexport
81 | * >3 quit timer
82 | */
83 |
84 | spi_init_step = 1;
85 | timer.schedule(init_task, 100, 100);
86 | } else {
87 | Toast.makeText(this,"exportGPIOPin failed!",Toast.LENGTH_SHORT).show();
88 | }
89 | }
90 |
91 | private Handler handler = new Handler() {
92 | public void handleMessage(Message msg) {
93 | switch (msg.what) {
94 | case 1:
95 | timer.cancel();
96 | break;
97 | case 2:
98 | timer2.cancel();
99 | break;
100 | }
101 | super.handleMessage(msg);
102 | }
103 | };
104 |
105 | private void quitTimer(int timerId) {
106 | Message message = new Message();
107 | message.what = timerId;
108 | handler.sendMessage(message);
109 | }
110 |
111 | private TimerTask init_task = new TimerTask() {
112 | public void run() {
113 | if (spi_init_step == 1) {
114 | if (oled.Init(devName, gpioPin_For_DC, gpioPin_For_Reset) == 0) {
115 | spi_init_step ++;
116 | } else {
117 | quitTimer(1);
118 | }
119 | } else if (spi_init_step == 2) {
120 | spi_init_step ++;
121 | quitTimer(1);
122 | timer2.schedule(flash_text_task, 1000, 1000);
123 | }
124 | }
125 | };
126 |
127 | public static String[] splitByByteSize(String content, int size){
128 | byte[] bytes = content.getBytes();
129 | int totalSize = bytes.length;
130 | int partNum = 0;
131 | if(totalSize == 0){
132 | return new String[0];
133 | }
134 | if(totalSize % size == 0){
135 | partNum = totalSize / size;
136 | }else{
137 | partNum = totalSize / size + 1;
138 | }
139 | String[] arr = new String[partNum];
140 | int arrIndex = -1;
141 | for(int i=0;i 0) {
165 | Log.e(TAG, "need display text: " + displayText);
166 | String[] lines = splitByByteSize(displayText, MAX_CHATS_PER_LINE);
167 | for (int i=0; i 30
60 | 0x00,0x70,0x48,0x08,0x08,0x08,0xF0,0x00,0x00,0x00,0x00,0x30,0x36,0x01,0x00,0x00,//? 31
61 | 0xC0,0x30,0xC8,0x28,0xE8,0x10,0xE0,0x00,0x07,0x18,0x27,0x24,0x23,0x14,0x0B,0x00,//@ 32
62 | 0x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00,0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20,//A 33
63 | 0x08,0xF8,0x88,0x88,0x88,0x70,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x11,0x0E,0x00,//B 34
64 | 0xC0,0x30,0x08,0x08,0x08,0x08,0x38,0x00,0x07,0x18,0x20,0x20,0x20,0x10,0x08,0x00,//C 35
65 | 0x08,0xF8,0x08,0x08,0x08,0x10,0xE0,0x00,0x20,0x3F,0x20,0x20,0x20,0x10,0x0F,0x00,//D 36
66 | 0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x20,0x23,0x20,0x18,0x00,//E 37
67 | 0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x00,0x03,0x00,0x00,0x00,//F 38
68 | 0xC0,0x30,0x08,0x08,0x08,0x38,0x00,0x00,0x07,0x18,0x20,0x20,0x22,0x1E,0x02,0x00,//G 39
69 | 0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x20,0x3F,0x21,0x01,0x01,0x21,0x3F,0x20,//H 40
70 | 0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//I 41
71 | 0x00,0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,0x00,//J 42
72 | 0x08,0xF8,0x88,0xC0,0x28,0x18,0x08,0x00,0x20,0x3F,0x20,0x01,0x26,0x38,0x20,0x00,//K 43
73 | 0x08,0xF8,0x08,0x00,0x00,0x00,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x20,0x30,0x00,//L 44
74 | 0x08,0xF8,0xF8,0x00,0xF8,0xF8,0x08,0x00,0x20,0x3F,0x00,0x3F,0x00,0x3F,0x20,0x00,//M 45
75 | 0x08,0xF8,0x30,0xC0,0x00,0x08,0xF8,0x08,0x20,0x3F,0x20,0x00,0x07,0x18,0x3F,0x00,//N 46
76 | 0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x10,0x20,0x20,0x20,0x10,0x0F,0x00,//O 47
77 | 0x08,0xF8,0x08,0x08,0x08,0x08,0xF0,0x00,0x20,0x3F,0x21,0x01,0x01,0x01,0x00,0x00,//P 48
78 | 0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x18,0x24,0x24,0x38,0x50,0x4F,0x00,//Q 49
79 | 0x08,0xF8,0x88,0x88,0x88,0x88,0x70,0x00,0x20,0x3F,0x20,0x00,0x03,0x0C,0x30,0x20,//R 50
80 | 0x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00,0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00,//S 51
81 | 0x18,0x08,0x08,0xF8,0x08,0x08,0x18,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//T 52
82 | 0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//U 53
83 | 0x08,0x78,0x88,0x00,0x00,0xC8,0x38,0x08,0x00,0x00,0x07,0x38,0x0E,0x01,0x00,0x00,//V 54
84 | 0xF8,0x08,0x00,0xF8,0x00,0x08,0xF8,0x00,0x03,0x3C,0x07,0x00,0x07,0x3C,0x03,0x00,//W 55
85 | 0x08,0x18,0x68,0x80,0x80,0x68,0x18,0x08,0x20,0x30,0x2C,0x03,0x03,0x2C,0x30,0x20,//X 56
86 | 0x08,0x38,0xC8,0x00,0xC8,0x38,0x08,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//Y 57
87 | 0x10,0x08,0x08,0x08,0xC8,0x38,0x08,0x00,0x20,0x38,0x26,0x21,0x20,0x20,0x18,0x00,//Z 58
88 | 0x00,0x00,0x00,0xFE,0x02,0x02,0x02,0x00,0x00,0x00,0x00,0x7F,0x40,0x40,0x40,0x00,//[ 59
89 | 0x00,0x0C,0x30,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x06,0x38,0xC0,0x00,//\ 60
90 | 0x00,0x02,0x02,0x02,0xFE,0x00,0x00,0x00,0x00,0x40,0x40,0x40,0x7F,0x00,0x00,0x00,//] 61
91 | 0x00,0x00,0x04,0x02,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//^ 62
92 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,//_ 63
93 | 0x00,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//` 64
94 | 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x19,0x24,0x22,0x22,0x22,0x3F,0x20,//a 65
95 | 0x08,0xF8,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x3F,0x11,0x20,0x20,0x11,0x0E,0x00,//b 66
96 | 0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x0E,0x11,0x20,0x20,0x20,0x11,0x00,//c 67
97 | 0x00,0x00,0x00,0x80,0x80,0x88,0xF8,0x00,0x00,0x0E,0x11,0x20,0x20,0x10,0x3F,0x20,//d 68
98 | 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00,//e 69
99 | 0x00,0x80,0x80,0xF0,0x88,0x88,0x88,0x18,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//f 70
100 | 0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x6B,0x94,0x94,0x94,0x93,0x60,0x00,//g 71
101 | 0x08,0xF8,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//h 72
102 | 0x00,0x80,0x98,0x98,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//i 73
103 | 0x00,0x00,0x00,0x80,0x98,0x98,0x00,0x00,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,//j 74
104 | 0x08,0xF8,0x00,0x00,0x80,0x80,0x80,0x00,0x20,0x3F,0x24,0x02,0x2D,0x30,0x20,0x00,//k 75
105 | 0x00,0x08,0x08,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//l 76
106 | 0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x20,0x3F,0x20,0x00,0x3F,0x20,0x00,0x3F,//m 77
107 | 0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//n 78
108 | 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//o 79
109 | 0x80,0x80,0x00,0x80,0x80,0x00,0x00,0x00,0x80,0xFF,0xA1,0x20,0x20,0x11,0x0E,0x00,//p 80
110 | 0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x0E,0x11,0x20,0x20,0xA0,0xFF,0x80,//q 81
111 | 0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x20,0x20,0x3F,0x21,0x20,0x00,0x01,0x00,//r 82
112 | 0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x33,0x24,0x24,0x24,0x24,0x19,0x00,//s 83
113 | 0x00,0x80,0x80,0xE0,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x1F,0x20,0x20,0x00,0x00,//t 84
114 | 0x80,0x80,0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x1F,0x20,0x20,0x20,0x10,0x3F,0x20,//u 85
115 | 0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x00,0x01,0x0E,0x30,0x08,0x06,0x01,0x00,//v 86
116 | 0x80,0x80,0x00,0x80,0x00,0x80,0x80,0x80,0x0F,0x30,0x0C,0x03,0x0C,0x30,0x0F,0x00,//w 87
117 | 0x00,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x31,0x2E,0x0E,0x31,0x20,0x00,//x 88
118 | 0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x80,0x81,0x8E,0x70,0x18,0x06,0x01,0x00,//y 89
119 | 0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x21,0x30,0x2C,0x22,0x21,0x30,0x00,//z 90
120 | 0x00,0x00,0x00,0x00,0x80,0x7C,0x02,0x02,0x00,0x00,0x00,0x00,0x00,0x3F,0x40,0x40,//{ 91
121 | 0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,//| 92
122 | 0x00,0x02,0x02,0x7C,0x80,0x00,0x00,0x00,0x00,0x40,0x40,0x3F,0x00,0x00,0x00,0x00,//} 93
123 | 0x00,0x06,0x01,0x01,0x02,0x02,0x04,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//~ 94
124 | };
125 |
126 | // write 8 * 8 bitmap
127 | int OLEDWriteData(byte data)
128 | {
129 | int ret = 0;
130 | final int pin = gpioPin_For_DC;
131 |
132 | if (HardwareControler.setGPIODirection(pin, GPIOEnum.OUT) == -1) {
133 | ret = -1;
134 | }
135 | if (HardwareControler.setGPIOValue(pin, GPIOEnum.HIGH) == -1) {
136 | ret = -1;
137 | }
138 | if (HardwareControler.SPItransferOneByte(devFD, data, spiDelay, spiSpeed, spiBits) == -1) {
139 | ret = -1;
140 | }
141 | return ret;
142 | }
143 |
144 | int OLEDWriteCmd(byte cmd)
145 | {
146 | int ret = 0;
147 | final int pin = gpioPin_For_DC;
148 |
149 | if (HardwareControler.setGPIODirection(pin, GPIOEnum.OUT) == -1) {
150 | ret = -1;
151 | }
152 | if (HardwareControler.setGPIOValue(pin, GPIOEnum.LOW) == -1) {
153 | ret = -1;
154 | }
155 |
156 | if (HardwareControler.SPItransferOneByte(devFD, cmd, spiDelay, spiSpeed, spiBits) == -1) {
157 | ret = -1;
158 | }
159 | return ret;
160 | }
161 |
162 | int OLEDSetPos(int x, int y)
163 | {
164 | int ret = 0;
165 |
166 | y = y/8 + y%8;
167 | if (OLEDWriteCmd((byte)(0xb0+y)) == -1) {
168 | ret = -1;
169 | }
170 |
171 | if (OLEDWriteCmd((byte)((x&0xf0)>>4|0x10)) == -1) {
172 | ret = -1;
173 | }
174 | if (OLEDWriteCmd((byte)((x&0x0f)|0x01)) == -1) {
175 | ret = -1;
176 | }
177 | return ret;
178 | }
179 |
180 | int OLEDReset(int pin) throws InterruptedException
181 | {
182 | int ret = -1;
183 |
184 | if ((ret = HardwareControler.setGPIODirection(pin, GPIOEnum.OUT)) == -1) {
185 | return -1;
186 | }
187 | if ((ret = HardwareControler.setGPIOValue(pin, GPIOEnum.LOW)) == -1) {
188 | return -1;
189 | }
190 | Thread.sleep(0, 100);
191 | if ((ret = HardwareControler.setGPIOValue(pin, GPIOEnum.HIGH)) == -1) {
192 | return -1;
193 | }
194 | return ret;
195 | }
196 |
197 | int OLEDSPIInit()
198 | {
199 | if (HardwareControler.setSPIDataMode(devFD, spiMode) != 0) {
200 | return -1;
201 | }
202 |
203 | if (HardwareControler.setSPIWriteBitsPerWord(devFD, spiBits) != 0) {
204 | return -1;
205 | }
206 | if (HardwareControler.setSPIMaxSpeed(devFD, spiSpeed) == -1 ) {
207 | return -1;
208 | }
209 | return 0;
210 | }
211 |
212 | void Deinit() {
213 | if (devFD >= 0) {
214 | HardwareControler.close(devFD);
215 | devFD = -1;
216 | }
217 | }
218 |
219 | int Init(String spiDevPath, int cmdDatPin, int resetPin)
220 | {
221 | gpioPin_For_Reset = resetPin;
222 | gpioPin_For_DC = cmdDatPin;
223 |
224 | devFD = HardwareControler.open( spiDevPath, FileCtlEnum.O_RDWR );
225 | if (devFD < 0) {
226 | Log.e(TAG, "Fail to open SPI device: " + spiDevPath);
227 | return -1;
228 | }
229 | Log.d(TAG, "Open " + spiDevPath + " Ok.");
230 | if (OLEDSPIInit() == -1) {
231 | Deinit();
232 | Log.e(TAG, "Fail to init SPI device OLED");
233 | return -1;
234 | }
235 | try {
236 | if (OLEDReset(gpioPin_For_Reset) == -1) {
237 | Deinit();
238 | Log.e(TAG, "Fail to reset OLED");
239 | return -1;
240 | }
241 | } catch (Exception e) {
242 | Log.e(TAG, "Fail to reset OLED");
243 | return -1;
244 | }
245 | if (OLEDWriteCmd((byte)0xae) == -1) {//--turn off oled panel
246 | Deinit();
247 | Log.e(TAG, "OLED write cmd failed");
248 | return -1;
249 | }
250 |
251 | if (OLEDWriteCmd((byte)0xae) == -1) {//--turn off oled panel
252 | Deinit();
253 | Log.e(TAG, "OLED write cmd failed");
254 | return -1;
255 | }
256 |
257 |
258 | if (OLEDWriteCmd((byte)0x00) == -1) {//---set low column address
259 | Deinit();
260 | Log.e(TAG, "OLED write cmd failed");
261 | return -1;
262 | }
263 |
264 |
265 | if (OLEDWriteCmd((byte)0x10) == -1) {//---set high column address
266 | Deinit();
267 | Log.e(TAG, "OLED write cmd failed");
268 | return -1;
269 | }
270 |
271 |
272 | if (OLEDWriteCmd((byte)0x40) == -1) {//--set start line address Set Mapping RAM Display Start Line (0x00~0x3F)
273 | Deinit();
274 | Log.e(TAG, "OLED write cmd failed");
275 | return -1;
276 | }
277 |
278 | if (OLEDWriteCmd((byte)0x81) == -1) {//--set contrast control register
279 | Deinit();
280 | Log.e(TAG, "OLED write cmd failed");
281 | return -1;
282 | }
283 |
284 | if (OLEDWriteCmd((byte)0xcf) == -1) { // Set SEG Output Current Brightness
285 | Deinit();
286 | Log.e(TAG, "OLED write cmd failed");
287 | return -1;
288 | }
289 |
290 |
291 | if (OLEDWriteCmd((byte)0xa1) == -1) {//--Set SEG/Column Mapping 0xa0左右反置 0xa1正常
292 | Deinit();
293 | Log.e(TAG, "OLED write cmd failed");
294 | return -1;
295 | }
296 |
297 |
298 | if (OLEDWriteCmd((byte)0xc8) == -1) {//Set COM/Row Scan Direction 0xc0上下反置 0xc8正常
299 | Deinit();
300 | Log.e(TAG, "OLED write cmd failed");
301 | return -1;
302 | }
303 |
304 |
305 | if (OLEDWriteCmd((byte)0xa6) == -1) {//--set normal display
306 | Deinit();
307 | Log.e(TAG, "OLED write cmd failed");
308 | return -1;
309 | }
310 |
311 |
312 | if (OLEDWriteCmd((byte)0xa8) == -1) {//--set multiplex ratio(1 to 64)
313 | Deinit();
314 | Log.e(TAG, "OLED write cmd failed");
315 | return -1;
316 | }
317 |
318 |
319 | if (OLEDWriteCmd((byte)0x3f) == -1) {//--1/64 duty
320 | Deinit();
321 | Log.e(TAG, "OLED write cmd failed");
322 | return -1;
323 | }
324 |
325 |
326 | if (OLEDWriteCmd((byte)0xd3) == -1) {//-set display offset Shift Mapping RAM Counter (0x00~0x3F)
327 | Deinit();
328 | Log.e(TAG, "OLED write cmd failed");
329 | return -1;
330 | }
331 |
332 |
333 | if (OLEDWriteCmd((byte)0x00) == -1) {//-not offset
334 | Deinit();
335 | Log.e(TAG, "OLED write cmd failed");
336 | return -1;
337 | }
338 |
339 |
340 | if (OLEDWriteCmd((byte)0xd5) == -1) {//--set display clock divide ratio/oscillator frequency
341 | Deinit();
342 | Log.e(TAG, "OLED write cmd failed");
343 | return -1;
344 | }
345 |
346 |
347 | if (OLEDWriteCmd((byte)0x80) == -1) {//--set divide ratio, Set Clock as 100 Frames/Sec
348 | Deinit();
349 | Log.e(TAG, "OLED write cmd failed");
350 | return -1;
351 | }
352 |
353 |
354 | if (OLEDWriteCmd((byte)0xd9) == -1) {//--set pre-charge period
355 | Deinit();
356 | Log.e(TAG, "OLED write cmd failed");
357 | return -1;
358 | }
359 |
360 |
361 | if (OLEDWriteCmd((byte)0xf1) == -1) {//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
362 | Deinit();
363 | Log.e(TAG, "OLED write cmd failed");
364 | return -1;
365 | }
366 |
367 |
368 | if (OLEDWriteCmd((byte)0xda) == -1) {//--set com pins hardware configuration
369 | Deinit();
370 | Log.e(TAG, "OLED write cmd failed");
371 | return -1;
372 | }
373 |
374 |
375 | if (OLEDWriteCmd((byte)0x12) == -1) {
376 | Deinit();
377 | Log.e(TAG, "OLED write cmd failed");
378 | return -1;
379 | }
380 |
381 |
382 | if (OLEDWriteCmd((byte)0xdb) == -1) {//--set vcomh
383 | Deinit();
384 | Log.e(TAG, "OLED write cmd failed");
385 | return -1;
386 | }
387 |
388 |
389 | if (OLEDWriteCmd((byte)0x40) == -1) {//Set VCOM Deselect Level
390 | Deinit();
391 | Log.e(TAG, "OLED write cmd failed");
392 | return -1;
393 | }
394 |
395 |
396 | if (OLEDWriteCmd((byte)0x20) == -1) {//-Set Page Addressing Mode (0x00/0x01/0x02)
397 | Deinit();
398 | Log.e(TAG, "OLED write cmd failed");
399 | return -1;
400 | }
401 |
402 |
403 | if (OLEDWriteCmd((byte)0x02) == -1) {//
404 | Deinit();
405 | Log.e(TAG, "OLED write cmd failed");
406 | return -1;
407 | }
408 |
409 |
410 | if (OLEDWriteCmd((byte)0x8d) == -1) {//--set Charge Pump enable/disable
411 | Deinit();
412 | Log.e(TAG, "OLED write cmd failed");
413 | return -1;
414 | }
415 |
416 |
417 | if (OLEDWriteCmd((byte)0x14) == -1){//--set(0x10) disable
418 | Deinit();
419 | Log.e(TAG, "OLED write cmd failed");
420 | return -1;
421 | }
422 |
423 | if (OLEDWriteCmd((byte)0xa4) == -1) {// Disable Entire Display On (0xa4/0xa5)
424 | Deinit();
425 | Log.e(TAG, "OLED write cmd failed");
426 | return -1;
427 | }
428 |
429 |
430 | if (OLEDWriteCmd((byte)0xa6) == -1) {// Disable Inverse Display On (0xa6/a7)
431 | Deinit();
432 | Log.e(TAG, "OLED write cmd failed");
433 | return -1;
434 | }
435 |
436 |
437 | if (OLEDWriteCmd((byte)0xaf) == -1) {//--turn on oled panel
438 | Deinit();
439 | Log.e(TAG, "OLED write cmd failed");
440 | return -1;
441 | }
442 |
443 |
444 | if (OLEDSetPos(0, 0) == -1) {
445 | Deinit();
446 | Log.e(TAG, "OLEDSetPos failed");
447 | return -1;
448 | }
449 |
450 | return 0;
451 | }
452 |
453 | int OLEDDisp8x16Char(int x, int y, byte ch)
454 | {
455 | int i = 0;
456 | final int index = ch-32;
457 |
458 | if (x<0 || x>(OLED_WIDTH-8) || y<0 || y>(OLED_HEIGHT-16)) {
459 | Log.e(TAG, "Unsupported OLED coordinate");
460 | return -1;
461 | }
462 |
463 | OLEDSetPos(x, y);
464 | for (i=0; i<8; i++) {
465 | OLEDWriteData((byte)OLEDCharMap[index*16+i]);
466 | }
467 |
468 | OLEDSetPos(x, y+8);
469 | for (i=0; i<8; i++)
470 | OLEDWriteData((byte)OLEDCharMap[index*16+i+8]);
471 |
472 | return 0;
473 | }
474 |
475 | int OLEDDisp8x16Str(int x, int y, byte[] strBytes)
476 | {
477 | int xx = x;
478 | for (int i = 0; i < strBytes.length; i++) {
479 | if (OLEDDisp8x16Char(xx, y, strBytes[i]) == -1) {
480 | Log.e(TAG, "Fail to write 8x16 Char to OLED");
481 | return -1;
482 | }
483 | xx=xx+8;
484 | }
485 | return 0;
486 | }
487 |
488 | int OLEDCleanScreen()
489 | {
490 | int x,y;
491 | final byte data = 0x00;
492 | int ret = 0;
493 | for (y=0; y<8; y++) {
494 | OLEDWriteCmd((byte)(0xb0+y));
495 | OLEDWriteCmd((byte)(0x00));
496 | OLEDWriteCmd((byte)(0x10));
497 | for (x=0; x
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/SerialPortDemo/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
8 |
9 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/SerialPortDemo/libs/armeabi/libfriendlyarm-hardware.so:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/friendlyarm/AndroidHardwareAccess/8084bdb9d85b9e466aea5544769ffb30d60dfda3/SerialPortDemo/libs/armeabi/libfriendlyarm-hardware.so
--------------------------------------------------------------------------------
/SerialPortDemo/res/drawable/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/friendlyarm/AndroidHardwareAccess/8084bdb9d85b9e466aea5544769ffb30d60dfda3/SerialPortDemo/res/drawable/icon.png
--------------------------------------------------------------------------------
/SerialPortDemo/res/layout/serialport_dataprocessview.xml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
9 |
10 |
11 |
15 |
16 |
17 |
20 |
21 |
23 |
24 |
27 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/SerialPortDemo/res/layout/serialport_dataprocessview_landscape.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
7 |
8 |
10 |
11 |
15 |
20 |
21 |
22 |
24 |
25 |
26 |
29 |
30 |
31 |
32 |
34 |
35 |
37 |
38 |
41 |
45 |
46 |
47 |
--------------------------------------------------------------------------------
/SerialPortDemo/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | SerialPort Demo
4 | Serial Port Assistant
5 | Back
6 | Send
7 | Serial Port Assistant
8 | Data received:
9 | Text to Send:
10 |
11 |
--------------------------------------------------------------------------------
/SerialPortDemo/src/com/friendlyarm/AndroidSDK/FileCtlEnum.java:
--------------------------------------------------------------------------------
1 | package com.friendlyarm.AndroidSDK;
2 |
3 | public class FileCtlEnum {
4 | /* File Flag */
5 | public final static int O_ACCMODE = 00000003;
6 | public final static int O_RDONLY = 00000000;
7 | public final static int O_WRONLY = 00000001;
8 | public final static int O_RDWR = 00000002;
9 | public final static int O_CREAT = 00000100; /* not fcntl */
10 | public final static int O_EXCL = 00000200; /* not fcntl */
11 | public final static int O_NOCTTY = 00000400; /* not fcntl */
12 | public final static int O_TRUNC = 00001000; /* not fcntl */
13 | public final static int O_APPEND = 00002000;
14 | public final static int O_NONBLOCK = 00004000;
15 | public final static int O_DSYNC = 00010000; /* used to be O_SYNC, see below */
16 | public final static int FASYNC = 00020000; /* fcntl, for BSD compatibility */
17 | public final static int O_DIRECT = 00040000; /* direct disk access hint */
18 | public final static int O_LARGEFILE = 00100000;
19 | public final static int O_DIRECTORY = 00200000; /* must be a directory */
20 | public final static int O_NOFOLLOW = 00400000; /* don't follow links */
21 | public final static int O_NOATIME = 01000000;
22 | public final static int O_CLOEXEC = 02000000; /* set close_on_exec */
23 | }
24 |
--------------------------------------------------------------------------------
/SerialPortDemo/src/com/friendlyarm/AndroidSDK/GPIOEnum.java:
--------------------------------------------------------------------------------
1 | package com.friendlyarm.AndroidSDK;
2 |
3 | /*
4 | GPIO signals have paths like /sys/class/gpio/gpio42/ (for GPIO #42)
5 | and have the following read/write attributes:
6 |
7 | /sys/class/gpio/gpioN/
8 |
9 | "direction" ... reads as either "in" or "out". This value may
10 | normally be written. Writing as "out" defaults to
11 | initializing the value as low. To ensure glitch free
12 | operation, values "low" and "high" may be written to
13 | configure the GPIO as an output with that initial value.
14 |
15 | Note that this attribute *will not exist* if the kernel
16 | doesn't support changing the direction of a GPIO, or
17 | it was exported by kernel code that didn't explicitly
18 | allow userspace to reconfigure this GPIO's direction.
19 |
20 | "value" ... reads as either 0 (low) or 1 (high). If the GPIO
21 | is configured as an output, this value may be written;
22 | any nonzero value is treated as high.
23 |
24 | If the pin can be configured as interrupt-generating interrupt
25 | and if it has been configured to generate interrupts (see the
26 | description of "edge"), you can poll(2) on that file and
27 | poll(2) will return whenever the interrupt was triggered. If
28 | you use poll(2), set the events POLLPRI and POLLERR. If you
29 | use select(2), set the file descriptor in exceptfds. After
30 | poll(2) returns, either lseek(2) to the beginning of the sysfs
31 | file and read the new value or close the file and re-open it
32 | to read the value.
33 | */
34 |
35 | public class GPIOEnum {
36 | //Direction
37 | public final static int IN = 1;
38 | public final static int OUT = 2;
39 |
40 | //Value
41 | public final static int LOW = 0;
42 | public final static int HIGH = 1;
43 | }
44 |
--------------------------------------------------------------------------------
/SerialPortDemo/src/com/friendlyarm/AndroidSDK/HardwareControler.java:
--------------------------------------------------------------------------------
1 | package com.friendlyarm.AndroidSDK;
2 | import android.util.Log;
3 |
4 | public class HardwareControler
5 | {
6 | /* File */
7 | static public native int open(String devName, int flags);
8 | static public native int write(int fd, byte[] data);
9 | static public native int read(int fd, byte[] buf, int len);
10 | static public native int select(int fd, int sec, int usec);
11 | static public native void close(int fd);
12 | static public native int ioctlWithIntValue(int fd, int cmd, int value);
13 |
14 | /* Serial Port */
15 | static public native int openSerialPort( String devName, long baud, int dataBits, int stopBits );
16 | static public native int openSerialPortEx( String devName
17 | , long baud
18 | , int dataBits
19 | , int stopBits
20 | , String parityBit
21 | , String flowCtrl
22 | );
23 |
24 | /* LED */
25 | static public native int setLedState( int ledID, int ledState );
26 |
27 | /* PWM */
28 | static public native int PWMPlay(int frequency);
29 | static public native int PWMStop();
30 | static public native int PWMPlayEx(int gpioPin, int frequency);
31 | static public native int PWMStopEx(int gpioPin);
32 |
33 | /* ADC */
34 | static public native int readADC();
35 | static public native int readADCWithChannel(int channel);
36 | static public native int[] readADCWithChannels(int[] channels);
37 |
38 | /* I2C */
39 | static public native int setI2CSlave(int fd, int slave);
40 | static public native int setI2CTimeout(int fd, int timeout);
41 | static public native int setI2CRetries(int fd, int retries);
42 | static public native int I2CReadByteData(int fd, int pos, int wait_us);
43 | static public native int I2CReadByte(int fd, int wait_us);
44 | static public native int I2CWriteByteData(int fd, int pos, byte byteData, int wait_us);
45 | static public native int I2CWriteByte(int fd, byte byteData, int wait_us);
46 |
47 | /* Discard */ static public native int readByteFromI2C(int fd, int pos, int wait_ms);
48 | /* Discard */ static public native int writeByteToI2C(int fd, int pos, byte byteData, int wait_ms);
49 | /* Discard */ static public native int writeByteToI2C2(int fd, byte byteData, int wait_ms);
50 |
51 | /* SPI */
52 | static public native int setSPIWriteBitsPerWord( int spi_fd, int bits );
53 | static public native int setSPIReadBitsPerWord( int spi_fd, int bits );
54 | static public native int setSPIBitOrder( int spi_fd, int order);
55 | static public native int setSPIClockDivider( int spi_fd, int divider);
56 | static public native int setSPIMaxSpeed( int spi_fd, int spi_speed);
57 | static public native int setSPIDataMode( int spi_fd, int mode);
58 | static public native int SPItransferOneByte( int spi_fd, byte byteData, int spi_delay, int spi_speed, int spi_bits);
59 | static public native int SPItransferBytes(int spi_fd, byte[] writeData, byte[] readBuff, int spi_delay, int spi_speed, int spi_bits);
60 | static public native int writeBytesToSPI(int spi_fd, byte[] writeData, int spi_delay, int spi_speed, int spi_bits);
61 | static public native int readBytesFromSPI(int spi_fd, byte[] readBuff, int spi_delay, int spi_speed, int spi_bits);
62 |
63 | /* GPIO */
64 | static public native int exportGPIOPin(int pin);
65 | static public native int unexportGPIOPin(int pin);
66 | //GPIOEnum.LOW or GPIOEnum.HIGH
67 | static public native int setGPIOValue(int pin, int value);
68 | static public native int getGPIOValue(int pin);
69 | //GPIOEnum.IN or GPIOEnum.OUT
70 | static public native int setGPIODirection(int pin, int direction);
71 | static public native int getGPIODirection(int pin);
72 |
73 | /* OldInterface: for EEPROM */
74 | static public native int openI2CDevice();
75 | static public native int writeByteDataToI2C(int fd, int pos, byte byteData);
76 | static public native int readByteDataFromI2C(int fd, int pos);
77 |
78 | static public native int getBoardType();
79 | /* getBoardType return value: */
80 | public static final int S3C6410_COMMON = 6410;
81 | public static final int S5PV210_COMMON = 210;
82 | public static final int S5P4412_COMMON = 4412;
83 | public static final int S5P4418_BASE = 4418;
84 | public static final int NanoPi2 = S5P4418_BASE+0;
85 | public static final int NanoPC_T2 = S5P4418_BASE+1;
86 | public static final int NanoPi_S2 = S5P4418_BASE+2;
87 | public static final int Smart4418 = S5P4418_BASE+3;
88 | public static final int NanoPi2_Fire = S5P4418_BASE+4;
89 | public static final int NanoPi_M2 = S5P4418_BASE+5;
90 | public static final int NanoPi_M2A = S5P4418_BASE+7;
91 | public static final int Smart4418SDK = S5P4418_BASE+0x103;
92 | public static final int S5P4418_MAX = Smart4418SDK;
93 | public static final int S5P6818_BASE = 6818;
94 | public static final int NanoPC_T3 = S5P6818_BASE+1;
95 | public static final int NanoPi_S3 = S5P6818_BASE+2;
96 | public static final int Smart6818 = S5P6818_BASE+3;
97 | public static final int NanoPi_M3 = S5P6818_BASE+7;
98 | public static final int S5P6818_MAX = NanoPi_M3;
99 |
100 | static public boolean isS5P4418Board() {
101 | int boardtype = getBoardType();
102 | return boardtype>=S5P4418_BASE && boardtype<=S5P4418_MAX;
103 | }
104 |
105 | static public boolean isS5P6818Board() {
106 | int boardtype = getBoardType();
107 | return boardtype>=S5P6818_BASE && boardtype<=S5P6818_MAX;
108 | }
109 |
110 | static {
111 | try {
112 | System.loadLibrary("friendlyarm-hardware");
113 | } catch (UnsatisfiedLinkError e) {
114 | Log.d("HardwareControler", "libfriendlyarm-hardware library not found!");
115 | }
116 | }
117 | }
118 |
--------------------------------------------------------------------------------
/SerialPortDemo/src/com/friendlyarm/AndroidSDK/SPI.java:
--------------------------------------------------------------------------------
1 | package com.friendlyarm.AndroidSDK;
2 | import android.util.Log;
3 | import android.widget.Toast;
4 |
5 | import com.friendlyarm.AndroidSDK.HardwareControler;
6 | import com.friendlyarm.AndroidSDK.SPIEnum;
7 | import com.friendlyarm.AndroidSDK.GPIOEnum;
8 | import com.friendlyarm.AndroidSDK.FileCtlEnum;
9 |
10 | public class SPI {
11 | private static final String TAG = "com.friendlyarm.AndroidSDK.SPI";
12 | private int spi_mode = 0;
13 | private int spi_bits = 8;
14 | private int spi_delay = 0;
15 | private int spi_speed = 500000;
16 | private int spi_byte_order = SPIEnum.LSBFIRST;
17 |
18 | private static final String devName = "/dev/spidev1.0";
19 | private int spi_fd = -1;
20 |
21 | public void begin() {
22 | spi_fd = HardwareControler.open( devName, FileCtlEnum.O_RDWR );
23 | if (spi_fd >= 0) {
24 | Log.d(TAG, "open " + devName + "ok!");
25 |
26 | /* spi init */
27 | HardwareControler.setSPIWriteBitsPerWord( spi_fd, spi_bits );
28 | HardwareControler.setSPIReadBitsPerWord( spi_fd, spi_bits );
29 | } else {
30 | Log.d(TAG, "open " + devName + "failed!");
31 | spi_fd = -1;
32 | }
33 | }
34 |
35 | public void end() {
36 | if (spi_fd != -1) {
37 | HardwareControler.close(spi_fd);
38 | spi_fd = -1;
39 | }
40 | }
41 |
42 | public void setBitOrder(int order) {
43 | if (spi_fd < 0) {
44 | return ;
45 | }
46 | spi_byte_order = SPIEnum.MSBFIRST;
47 | if(spi_byte_order == SPIEnum.LSBFIRST) {
48 | spi_mode |= SPIEnum.SPI_LSB_FIRST;
49 | } else {
50 | spi_mode &= ~SPIEnum.SPI_LSB_FIRST;
51 | }
52 | HardwareControler.setSPIBitOrder( spi_fd, spi_byte_order );
53 |
54 | }
55 |
56 | public void setClockDivider(int divider) {
57 | if (spi_fd < 0) {
58 | return ;
59 | }
60 | spi_speed = 66666666/(2*(divider+1));
61 | if(spi_speed > 500000) {
62 | spi_speed = 500000;
63 | }
64 | HardwareControler.setSPIClockDivider( spi_fd, divider);
65 | }
66 |
67 | public void setDataMode(int mode) {
68 | if (spi_fd < 0) {
69 | return ;
70 | }
71 | switch(mode)
72 | {
73 | case SPIEnum.SPI_MODE0:
74 | spi_mode &= ~(SPIEnum.SPI_CPHA|SPIEnum.SPI_CPOL);
75 | break;
76 | case SPIEnum.SPI_MODE1:
77 | spi_mode &= ~(SPIEnum.SPI_CPOL);
78 | spi_mode |= (SPIEnum.SPI_CPHA);
79 | break;
80 | case SPIEnum.SPI_MODE2:
81 | spi_mode |= (SPIEnum.SPI_CPOL);
82 | spi_mode &= ~(SPIEnum.SPI_CPHA);
83 | break;
84 | case SPIEnum.SPI_MODE3:
85 | spi_mode |= (SPIEnum.SPI_CPHA|SPIEnum.SPI_CPOL);
86 | break;
87 | default:
88 | Log.e(TAG, "error data mode");
89 | }
90 |
91 | HardwareControler.setSPIDataMode( spi_fd, spi_mode );
92 | }
93 |
94 | public void setChipSelectPolarity(int cs, int active) {
95 |
96 | }
97 |
98 | public void chipSelect(int cs) {
99 |
100 | }
101 |
102 | public byte transfer(int value) {
103 | if (spi_fd < 0) {
104 | return 0;
105 | }
106 | return (byte) HardwareControler.SPItransferOneByte(spi_fd, (byte) value, spi_delay, spi_speed, spi_bits);
107 | }
108 | }
109 |
--------------------------------------------------------------------------------
/SerialPortDemo/src/com/friendlyarm/AndroidSDK/SPIEnum.java:
--------------------------------------------------------------------------------
1 | package com.friendlyarm.AndroidSDK;
2 | public class SPIEnum {
3 |
4 | // SPIBitOrder
5 | public final static int LSBFIRST = 0; ///< LSB First
6 | public final static int MSBFIRST = 1; ///< MSB First
7 |
8 | // SPIMode
9 | public final static int SPI_MODE0 = 0; ///< CPOL = 0, CPHA = 0
10 | public final static int SPI_MODE1 = 1; ///< CPOL = 0, CPHA = 1
11 | public final static int SPI_MODE2 = 2; ///< CPOL = 1, CPHA = 0
12 | public final static int SPI_MODE3 = 3; ///< CPOL = 1, CPHA = 1
13 |
14 |
15 | public final static int SPI_CPHA = 0x01;
16 | public final static int SPI_CPOL = 0x02;
17 | public final static int SPI_CS_HIGH = 0x04;
18 | public final static int SPI_LSB_FIRST = 0x08;
19 | public final static int SPI_3WIRE = 0x10;
20 | public final static int SPI_LOOP = 0x20;
21 | public final static int SPI_NO_CS = 0x40;
22 | public final static int SPI_READY = 0x80;
23 |
24 | // SPIClockDivider
25 | public final static int SPI_CLOCK_DIV65536 = 0; ///< 65536 = 256us = 4kHz
26 | public final static int SPI_CLOCK_DIV32768 = 32768; ///< 32768 = 126us = 8kHz
27 | public final static int SPI_CLOCK_DIV16384 = 16384; ///< 16384 = 64us = 15.625kHz
28 | public final static int SPI_CLOCK_DIV8192 = 8192; ///< 8192 = 32us = 31.25kHz
29 | public final static int SPI_CLOCK_DIV4096 = 4096; ///< 4096 = 16us = 62.5kHz
30 | public final static int SPI_CLOCK_DIV2048 = 2048; ///< 2048 = 8us = 125kHz
31 | public final static int SPI_CLOCK_DIV1024 = 1024; ///< 1024 = 4us = 250kHz
32 | public final static int SPI_CLOCK_DIV512 = 512; ///< 512 = 2us = 500kHz
33 | public final static int SPI_CLOCK_DIV256 = 256; ///< 256 = 1us = 1MHz
34 | public final static int SPI_CLOCK_DIV128 = 128; ///< 128 = 500ns = = 2MHz
35 | public final static int SPI_CLOCK_DIV64 = 64; ///< 64 = 250ns = 4MHz
36 | public final static int SPI_CLOCK_DIV32 = 32; ///< 32 = 125ns = 8MHz
37 | public final static int SPI_CLOCK_DIV16 = 16; ///< 16 = 50ns = 20MHz
38 | public final static int SPI_CLOCK_DIV8 = 8; ///< 8 = 25ns = 40MHz
39 | public final static int SPI_CLOCK_DIV4 = 4; ///< 4 = 12.5ns 80MHz
40 | public final static int SPI_CLOCK_DIV2 = 2; ///< 2 = 6.25ns = 160MHz
41 | public final static int SPI_CLOCK_DIV1 = 1; ///< 0 = 256us = 4kHz
42 | }
--------------------------------------------------------------------------------
/SerialPortDemo/src/com/friendlyarm/SerialPortDemo/SerialPortDataProcessView.java:
--------------------------------------------------------------------------------
1 | package com.friendlyarm.SerialPortDemo;
2 |
3 | import android.app.Activity;
4 | import android.content.res.Configuration;
5 | import android.os.Bundle;
6 | import android.view.View;
7 | import android.view.View.OnClickListener;
8 | import android.widget.Button;
9 | import android.widget.EditText;
10 | import android.widget.ScrollView;
11 | import android.widget.TextView;
12 | import android.util.Log;
13 | import android.text.Html;
14 | import android.widget.Toast;
15 | import java.util.Timer;
16 | import java.util.TimerTask;
17 |
18 | import com.friendlyarm.AndroidSDK.HardwareControler;
19 | import com.friendlyarm.SerialPortDemo.R;
20 |
21 | import android.app.Activity;
22 | import android.os.Bundle;
23 | import android.os.Handler;
24 | import android.os.Message;
25 | import android.content.Context;
26 | import android.content.Intent;
27 |
28 | public class SerialPortDataProcessView extends Activity implements OnClickListener {
29 | private static final String TAG = "SerialPort";
30 | private TextView fromTextView = null;
31 | private EditText toEditor = null;
32 | private final int MAXLINES = 200;
33 | private StringBuilder remoteData = new StringBuilder(256 * MAXLINES);
34 |
35 | private String devName = "/dev/ttyAMA2";
36 | private int speed = 115200;
37 | private int dataBits = 8;
38 | private int stopBits = 1;
39 | private int devfd = -1;
40 |
41 | @Override
42 | public void onDestroy() {
43 | timer.cancel();
44 | if (devfd != -1) {
45 | HardwareControler.close(devfd);
46 | devfd = -1;
47 | }
48 | super.onDestroy();
49 | }
50 |
51 | @Override
52 | public void onCreate(Bundle savedInstanceState) {
53 | super.onCreate(savedInstanceState);
54 | if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
55 | setContentView(R.layout.serialport_dataprocessview_landscape);
56 | } else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
57 | setContentView(R.layout.serialport_dataprocessview);
58 | }
59 |
60 | String winTitle = devName + "," + speed + "," + dataBits + "," + stopBits;
61 | setTitle(winTitle);
62 |
63 | ((Button)findViewById(R.id.sendButton)).setOnClickListener(this);
64 |
65 | fromTextView = (TextView)findViewById(R.id.fromTextView);
66 | toEditor = (EditText)findViewById(R.id.toEditor);
67 |
68 | /* no focus when begin */
69 | toEditor.clearFocus();
70 | toEditor.setFocusable(false);
71 | toEditor.setFocusableInTouchMode(true);
72 |
73 | devfd = HardwareControler.openSerialPort( devName, speed, dataBits, stopBits );
74 | if (devfd >= 0) {
75 | timer.schedule(task, 0, 500);
76 | } else {
77 | devfd = -1;
78 | fromTextView.append("Fail to open " + devName + "!");
79 | }
80 | }
81 |
82 | private final int BUFSIZE = 512;
83 | private byte[] buf = new byte[BUFSIZE];
84 | private Timer timer = new Timer();
85 | private Handler handler = new Handler() {
86 | public void handleMessage(Message msg) {
87 | switch (msg.what) {
88 | case 1:
89 | if (HardwareControler.select(devfd, 0, 0) == 1) {
90 | int retSize = HardwareControler.read(devfd, buf, BUFSIZE);
91 | if (retSize > 0) {
92 | String str = new String(buf, 0, retSize);
93 | remoteData.append(str);
94 |
95 | //Log.d(TAG, "#### LineCount: " + fromTextView.getLineCount() + ", remoteData.length()=" + remoteData.length());
96 | if (fromTextView.getLineCount() > MAXLINES) {
97 | int nLineCount = fromTextView.getLineCount();
98 | int i = 0;
99 | for (i = 0; i < remoteData.length(); i++) {
100 | if (remoteData.charAt(i) == '\n') {
101 | nLineCount--;
102 |
103 | if (nLineCount <= MAXLINES) {
104 | break;
105 | }
106 | }
107 | }
108 | remoteData.delete(0, i);
109 | //Log.d(TAG, "#### remoteData.delete(0, " + i + ")");
110 | fromTextView.setText(remoteData.toString());
111 | } else {
112 | fromTextView.append(str);
113 | }
114 |
115 | ((ScrollView)findViewById(R.id.scroolView)).fullScroll(View.FOCUS_DOWN);
116 | }
117 | }
118 | break;
119 | }
120 | super.handleMessage(msg);
121 | }
122 | };
123 | private TimerTask task = new TimerTask() {
124 | public void run() {
125 | Message message = new Message();
126 | message.what = 1;
127 | handler.sendMessage(message);
128 | }
129 | };
130 |
131 | public void onClick(View v)
132 | {
133 | switch (v.getId()) {
134 | case R.id.sendButton:
135 | String str = toEditor.getText().toString();
136 | if (str.length() > 0) {
137 | if (str.charAt(str.length()-1) != '\n') {
138 | str = str + "\n";
139 | }
140 | int ret = HardwareControler.write(devfd, str.getBytes());
141 | if (ret > 0) {
142 | toEditor.setText("");
143 |
144 | str = ">>> " + str;
145 | if (remoteData.length() > 0) {
146 | if (remoteData.charAt(remoteData.length()-1) != '\n') {
147 | remoteData.append('\n');
148 | fromTextView.append("\n");
149 | }
150 | }
151 | remoteData.append(str);
152 | fromTextView.append(str);
153 |
154 | ((ScrollView)findViewById(R.id.scroolView)).fullScroll(View.FOCUS_DOWN);
155 | } else {
156 | Toast.makeText(this,"Fail to send!",Toast.LENGTH_SHORT).show();
157 | }
158 | }
159 |
160 | break;
161 | }
162 | }
163 | }
164 |
--------------------------------------------------------------------------------
/SerialPortDemo/src/com/friendlyarm/Utils/BorderScrollView.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/friendlyarm/AndroidHardwareAccess/8084bdb9d85b9e466aea5544769ffb30d60dfda3/SerialPortDemo/src/com/friendlyarm/Utils/BorderScrollView.java
--------------------------------------------------------------------------------
/SerialPortDemo/src/com/friendlyarm/Utils/CommonFuncs.java:
--------------------------------------------------------------------------------
1 | package com.friendlyarm.Utils;
2 |
3 | import android.app.Activity;
4 | import android.app.AlertDialog;
5 | import android.widget.Toast;
6 | import android.app.Activity;
7 | import android.os.Bundle;
8 | import android.os.Handler;
9 | import android.os.Message;
10 | import android.content.Context;
11 | import android.content.DialogInterface;
12 |
13 | public class CommonFuncs {
14 | public static void showAlertDialog(Context context, String message) {
15 | AlertDialog.Builder builder = new AlertDialog.Builder(context);
16 | builder.setMessage(message)
17 | .setCancelable(false)
18 | .setPositiveButton("Close",
19 | new DialogInterface.OnClickListener() {
20 | public void onClick(DialogInterface dialog, int id) {
21 | }
22 | });
23 | AlertDialog alert = builder.create();
24 | alert.show();
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/友善电子Android硬件开发指南.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/friendlyarm/AndroidHardwareAccess/8084bdb9d85b9e466aea5544769ffb30d60dfda3/友善电子Android硬件开发指南.pdf
--------------------------------------------------------------------------------