├── .gitignore ├── Makefile ├── demo ├── Makefile ├── matrix-0.96_128x64_oled │ ├── .gitignore │ ├── Makefile │ └── Matrix-oled.c ├── matrix-3_axis_digital_accelerometer │ ├── .gitignore │ ├── Makefile │ └── Matrix-accelerometer.c ├── matrix-3_axis_digital_compass │ ├── .gitignore │ ├── Makefile │ └── Matrix-compass.c ├── matrix-analog_to_digital_converter │ ├── .gitignore │ ├── Makefile │ └── Matrix-adc.c ├── matrix-compact_kit │ ├── .gitignore │ ├── Makefile │ └── Matrix-compact_kit.c ├── matrix-fbtft │ ├── .gitignore │ ├── Makefile │ └── Matrix-fbtft.c ├── matrix-gpio_int │ ├── .gitignore │ ├── Makefile │ └── Matrix-gpio_int.c ├── matrix-gpio_out │ ├── .gitignore │ ├── Makefile │ └── Matrix-gpio_out.c ├── matrix-i2c_lcd1602 │ ├── .gitignore │ ├── Makefile │ └── Matrix-lcd1602.c ├── matrix-i2c_lcd1602_keypad │ ├── .gitignore │ ├── Makefile │ └── Matrix-lcd1602_keypad.c ├── matrix-ir_receiver │ ├── .gitignore │ ├── Makefile │ └── Matrix-ir_receiver.c ├── matrix-joystick │ ├── .gitignore │ ├── Makefile │ └── Matrix-joystick.c ├── matrix-lcdtest │ ├── .gitignore │ ├── Makefile │ └── Matrix-lcdtest.c ├── matrix-pressure_and_temperature_sensor │ ├── .gitignore │ ├── Makefile │ └── Matrix-pressure_temp.c ├── matrix-pwm │ ├── .gitignore │ ├── Makefile │ └── Matrix-pwm.c ├── matrix-rgb_led │ ├── .gitignore │ ├── Makefile │ └── Matrix-rgb_led.c ├── matrix-rotary_encoder │ ├── .gitignore │ ├── Makefile │ └── Matrix-rotary_encoder.c ├── matrix-rtc │ ├── .gitignore │ ├── Makefile │ └── Matrix-rtc.c ├── matrix-temperature_and_humidity_sensor │ ├── .gitignore │ ├── Makefile │ └── Matrix-temp_humidity.c ├── matrix-temperature_sensor │ ├── .gitignore │ ├── Makefile │ └── Matrix-temp_sensor.c ├── matrix-ultrasonic_ranger │ ├── .gitignore │ ├── Makefile │ └── Matrix-ultrasonic_ranger.c └── nanopi-status │ ├── .gitignore │ ├── bg.png │ ├── build.sh │ ├── display-debian.sh │ ├── main.cpp │ ├── main.qrc │ ├── mainwidget.cpp │ ├── mainwidget.h │ ├── qte.tgz │ ├── run.sh │ ├── util.cpp │ └── util.h └── lib ├── Makefile ├── adxl34x.c ├── bmp180.c ├── common.c ├── common.h ├── config.c ├── config.h ├── filectl.c ├── gpio.c ├── gpio_sensor.c ├── hmc5883.c ├── i2c.c ├── iio.c ├── includes ├── i2c-dev.h ├── libfahw-GPIOSensor.h ├── libfahw-adxl34x.h ├── libfahw-bmp180.h ├── libfahw-encoder.h ├── libfahw-filectl.h ├── libfahw-gpio.h ├── libfahw-hmc5883.h ├── libfahw-i2c.h ├── libfahw-iio.h ├── libfahw-mcp23017.h ├── libfahw-oled.h ├── libfahw-pcf8574.h ├── libfahw-pcf8591.h ├── libfahw-pwm.h ├── libfahw-spi.h ├── libfahw-w1.h ├── libfahw.h ├── spi_enum.h └── spidev.h ├── led.c ├── mcp23017.c ├── oled.c ├── pcf8574.c ├── pcf8591.c ├── pwm.c ├── rotary_encoder.c ├── spi.c └── w1.c /.gitignore: -------------------------------------------------------------------------------- 1 | # 2 | # NOTE! Don't add files that are generated in specific 3 | # subdirectories here. Add them in the ".gitignore" file 4 | # in that subdirectory instead. 5 | # 6 | # NOTE! Please use 'git ls-files -i --exclude-standard' 7 | # command after changing this file, to see if there are 8 | # any tracked files which get ignored after the change. 9 | # 10 | # Normal rules 11 | # 12 | 13 | *.o 14 | *.o.* 15 | libfahw.so 16 | libfahw.a 17 | /lib/.deps 18 | 19 | # source insight 20 | /si 21 | 22 | # eclipse 23 | .project 24 | .cproject 25 | 26 | # gedit 27 | *~ 28 | 29 | /install/* 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | CROSS_COMPILE ?= 2 | 3 | AS = $(CROSS_COMPILE)as 4 | LD = $(CROSS_COMPILE)ld 5 | CC = $(CROSS_COMPILE)gcc 6 | AR = $(CROSS_COMPILE)ar 7 | NM = $(CROSS_COMPILE)nm 8 | STRIP = $(CROSS_COMPILE)strip 9 | OBJCOPY = $(CROSS_COMPILE)objcopy 10 | OBJDUMP = $(CROSS_COMPILE)objdump 11 | RANLIB = $(CROSS_COMPILE)ranlib 12 | 13 | export AS LD CC AR NM STRIP OBJCOPY OBJDUMP RANLIB CROSS_COMPILE 14 | 15 | SUBDIRS = lib demo 16 | TARGET = subdirs 17 | PHONY += $(TARGET) $(SUBDIRS) %.clean 18 | 19 | all: $(TARGET) 20 | 21 | subdirs: $(SUBDIRS) 22 | 23 | $(SUBDIRS): 24 | $(MAKE) -C $@ 25 | 26 | %.clean: 27 | @(cd $(patsubst %.clean, %, $@) && $(MAKE) clean) 28 | 29 | clean distclean: $(patsubst %, %.clean, $(SUBDIRS)) 30 | 31 | install: 32 | make -C ./demo install 33 | make -C ./lib install 34 | 35 | .PHONY: $(PHONY) install clean distclean 36 | -------------------------------------------------------------------------------- /demo/Makefile: -------------------------------------------------------------------------------- 1 | SUBDIRS = $(wildcard matrix*) 2 | TARGET = subdirs 3 | PHONY += $(TARGET) $(SUBDIRS) %.clean 4 | 5 | all: $(TARGET) 6 | 7 | subdirs: $(SUBDIRS) 8 | 9 | $(SUBDIRS): 10 | $(MAKE) -C $@ 11 | 12 | %.clean: 13 | @(cd $(patsubst %.clean, %, $@) && $(MAKE) clean) 14 | 15 | clean distclean: $(patsubst %, %.clean, $(SUBDIRS)) 16 | 17 | install: 18 | mkdir -p $(INSTALLDIR)/usr/bin 19 | for demo_folder in $(wildcard matrix*); do \ 20 | cd $$demo_folder; \ 21 | install --mode=755 matrix* /usr/bin; \ 22 | cd ..;\ 23 | done; 24 | 25 | .PHONY: $(PHONY) install clean distclean 26 | -------------------------------------------------------------------------------- /demo/matrix-0.96_128x64_oled/.gitignore: -------------------------------------------------------------------------------- 1 | /matrix-oled 2 | -------------------------------------------------------------------------------- /demo/matrix-0.96_128x64_oled/Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | 3 | TARGET = $(patsubst Matrix-%.c,matrix-%, $(wildcard *.c)) 4 | SRC = $(wildcard *.c) 5 | SRC += $(wildcard *.h) 6 | 7 | $(TARGET):$(SRC) 8 | $(CC) -o $@ -L ../../lib/ -I ../../lib/includes/ $^ -lfahw -lm -Wall 9 | 10 | .PHONY: clean 11 | clean: 12 | rm -f $(TARGET) 13 | -------------------------------------------------------------------------------- /demo/matrix-0.96_128x64_oled/Matrix-oled.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "libfahw.h" 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | int devFD; 7 | 8 | if (boardInit() < 0) { 9 | printf("Fail to init board\n"); 10 | return -1; 11 | } 12 | 13 | if ((devFD = OLEDInit(GPIO_PIN(7), GPIO_PIN(11))) == -1) { 14 | printf("Fail to init OLED\n"); 15 | return -1; 16 | } 17 | OLEDCleanScreen(devFD); 18 | // Char bitmap: 8x16 19 | OLEDDisp8x16Str(devFD, 0, 0, "ABCDEFGHIJKLMN"); 20 | OLEDDisp8x16Str(devFD, 0, 16, "123456789"); 21 | OLEDDeInit(devFD); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /demo/matrix-3_axis_digital_accelerometer/.gitignore: -------------------------------------------------------------------------------- 1 | /matrix-accelerometer 2 | -------------------------------------------------------------------------------- /demo/matrix-3_axis_digital_accelerometer/Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | 3 | TARGET = $(patsubst Matrix-%.c,matrix-%, $(wildcard *.c)) 4 | SRC = $(wildcard *.c) 5 | SRC += $(wildcard *.h) 6 | 7 | $(TARGET):$(SRC) 8 | $(CC) -o $@ -L ../../lib/ -I ../../lib/includes/ $^ -lfahw -lm -Wall 9 | 10 | .PHONY: clean 11 | clean: 12 | rm -f $(TARGET) 13 | -------------------------------------------------------------------------------- /demo/matrix-3_axis_digital_accelerometer/Matrix-accelerometer.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "libfahw.h" 5 | 6 | #define BUF_SIZE 32 7 | 8 | int main(int argc, char ** argv) 9 | { 10 | char position[BUF_SIZE]; 11 | int board; 12 | 13 | if ((board = boardInit()) < 0) { 14 | printf("Fail to init board\n"); 15 | return -1; 16 | } 17 | 18 | system("modprobe adxl34x"); 19 | system("modprobe adxl34x-i2c"); 20 | memset(position, 0, BUF_SIZE); 21 | if (adxl34xRead(position) > 0) { 22 | printf("The position is %s", position); 23 | } else { 24 | printf("Fail to get position\n"); 25 | } 26 | system("rmmod adxl34x-i2c"); 27 | system("rmmod adxl34x"); 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /demo/matrix-3_axis_digital_compass/.gitignore: -------------------------------------------------------------------------------- 1 | /matrix-compass 2 | -------------------------------------------------------------------------------- /demo/matrix-3_axis_digital_compass/Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | 3 | TARGET = $(patsubst Matrix-%.c,matrix-%, $(wildcard *.c)) 4 | SRC = $(wildcard *.c) 5 | SRC += $(wildcard *.h) 6 | 7 | $(TARGET):$(SRC) 8 | $(CC) -o $@ -L ../../lib/ -I ../../lib/includes/ $^ -lfahw -lm -Wall 9 | 10 | .PHONY: clean 11 | clean: 12 | rm -f $(TARGET) 13 | -------------------------------------------------------------------------------- /demo/matrix-3_axis_digital_compass/Matrix-compass.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "libfahw.h" 4 | 5 | int main(int argc, char ** argv) 6 | { 7 | int devFD; 8 | double angle; 9 | int i2cDev = 0; 10 | 11 | if (boardInit() < 0) { 12 | printf("Fail to init board\n"); 13 | return -1; 14 | } 15 | 16 | if (argc == 2) 17 | i2cDev = atoi(argv[1]); 18 | if ((devFD = hmc5883Init(i2cDev)) == -1) { 19 | printf("Fail to init hmc5883\n"); 20 | return -1; 21 | } 22 | if ((angle = hmc5883Read(devFD)) != -1) { 23 | printf("The angle is %.1f\n", angle); 24 | } else { 25 | printf("Fail to read hmc5883\n"); 26 | } 27 | hmc5883DeInit(devFD); 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /demo/matrix-analog_to_digital_converter/.gitignore: -------------------------------------------------------------------------------- 1 | /matrix-adc 2 | -------------------------------------------------------------------------------- /demo/matrix-analog_to_digital_converter/Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | 3 | TARGET = $(patsubst Matrix-%.c,matrix-%, $(wildcard *.c)) 4 | SRC = $(wildcard *.c) 5 | SRC += $(wildcard *.h) 6 | 7 | $(TARGET):$(SRC) 8 | $(CC) -o $@ -L ../../lib/ -I ../../lib/includes/ $^ -lfahw -lm -Wall 9 | 10 | .PHONY: clean 11 | clean: 12 | rm -f $(TARGET) 13 | -------------------------------------------------------------------------------- /demo/matrix-analog_to_digital_converter/Matrix-adc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "libfahw.h" 7 | 8 | #define BUF_SIZE (32) 9 | #define ADC_READ_TIMES (1) 10 | #define DRIVER_MODULE "pcf8591" 11 | 12 | void intHandler(int signNum) 13 | { 14 | if (signNum == SIGINT) { 15 | printf("Clean up\n"); 16 | system("rmmod "DRIVER_MODULE); 17 | } 18 | exit(0); 19 | } 20 | 21 | int main(int argc, char ** argv) 22 | { 23 | int i = 0; 24 | int value = 0; 25 | int channel = 0; 26 | 27 | if (boardInit() < 0) { 28 | printf("Fail to init board\n"); 29 | return -1; 30 | } 31 | 32 | if (argc == 2) 33 | channel = atoi(argv[1]); 34 | system("modprobe "DRIVER_MODULE); 35 | signal(SIGINT, intHandler); 36 | for (i=0; i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "libfahw.h" 7 | 8 | #define LED_BLINK_TIMES (2) 9 | #define LED_NUM (4) 10 | int testLED(int board) 11 | { 12 | int i, j, ret; 13 | int pin[LED_NUM]={22, 31, 37, 35}; 14 | ret = 0; 15 | 16 | if (board == BOARD_NANOPI_2) 17 | pin[0] = 33; 18 | for (i=0; i 0) { 129 | printf("The temperature is %.3f C\n", atoi(temperature)/1000.0); 130 | } else { 131 | printf("Fail to get temperature\n"); 132 | } 133 | system("rmmod "TEMP_GPIO_DRIVER_MODULE); 134 | system("rmmod "TEMP_DRIVER_MODULE); 135 | 136 | return 0; 137 | } 138 | 139 | #define PWM_DRIVER_MODULE "matrix_pwm" 140 | static int pwm; 141 | void PWMIntHandler(int signNum) 142 | { 143 | if (signNum == SIGINT) { 144 | printf("Clean up\n"); 145 | PWMStop(pwm); 146 | system("rmmod "PWM_DRIVER_MODULE); 147 | } 148 | exit(0); 149 | } 150 | int testPWM(int board) 151 | { 152 | int Hz, duty; 153 | 154 | Hz = 1000; 155 | duty = 500; 156 | if (board == BOARD_NANOPI_M2 || board == BOARD_NANOPI_2_FIRE || board == BOARD_NANOPI_2) 157 | pwm = 0; 158 | else 159 | return -1; 160 | system("modprobe "PWM_DRIVER_MODULE); 161 | signal(SIGINT, PWMIntHandler); 162 | printf("Pwm start\n"); 163 | if (PWMPlay(pwm, Hz, duty) == -1) { 164 | printf("Fail to output PWM\n"); 165 | } 166 | sleep(1); 167 | PWMStop(pwm); 168 | system("rmmod "PWM_DRIVER_MODULE); 169 | printf("Pwm stop\n"); 170 | 171 | return 0; 172 | } 173 | 174 | #define GPIO_IR_DEV "/dev/input/gpio_ir_recv" 175 | #define IR_EVENT_TIMES (6) 176 | #define IR_DRIVER_MODULE "matrix_ir_recv" 177 | static int irFD; 178 | void IRIntHandler(int signNum) 179 | { 180 | if (signNum == SIGINT) { 181 | printf("Clean up\n"); 182 | closeHW(irFD); 183 | system("rmmod "IR_DRIVER_MODULE); 184 | } 185 | exit(0); 186 | } 187 | int testIR() 188 | { 189 | int i, j; 190 | int retSize = -1; 191 | char *devName = GPIO_IR_DEV; 192 | int pin = GPIO_PIN(32); 193 | char modStr[BUF_SIZE]; 194 | struct input_event evKey; 195 | 196 | sprintf(modStr, "modprobe %s gpio=%d", IR_DRIVER_MODULE, pintoGPIO(pin)); 197 | system(modStr); 198 | signal(SIGINT, IRIntHandler); 199 | sleep(1); 200 | irFD = openHW(devName, O_RDWR); 201 | if (irFD < 0) { 202 | printf("Fail to open GPIO IR device\n"); 203 | goto err; 204 | } 205 | printf("Press the IR remoter\n"); 206 | for (i=0; i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #define RGB(r,g,b) (((r << 11) & 0xF800) | ((g << 6) & 0x07E0) | (( b & 0x1F))) 16 | 17 | static int xres, yres, bpp; 18 | static unsigned char *fbaddr; 19 | 20 | 21 | /* Draw point (x, y) with specific RGB888 color (r,g,b) */ 22 | static void lcd_point_rgb(int x, int y, int r, int g, int b) 23 | { 24 | unsigned int v = 0; 25 | unsigned char *p = fbaddr; 26 | 27 | if ((x < 0 || x >= xres) || (y < 0 || y >= yres)) { 28 | /* out of LCD display area */ 29 | return; 30 | } 31 | 32 | switch (bpp) { 33 | case 16: 34 | v |= ((b >> 3) & 0x1f); 35 | v |= ((g >> 2) & 0x3f) << 5; 36 | v |= ((r >> 3) & 0x1f) << 11; 37 | 38 | p += ((y * xres) + x) * 2; 39 | *p++ = v; 40 | *p++ = v >> 8; 41 | break; 42 | 43 | case 18: 44 | v |= ((b >> 2) & 0x3f); 45 | v |= ((g >> 2) & 0x3f) << 6; 46 | v |= ((r >> 2) & 0x3f) << 12; 47 | 48 | p += ((y * xres) + x) * 3; 49 | *p++ = v; 50 | *p++ = v >> 8; 51 | *p++ = v >> 16; 52 | break; 53 | 54 | case 24: 55 | p += ((y * xres) + x) * 3; 56 | *p++ = b; 57 | *p++ = g; 58 | *p++ = r; 59 | break; 60 | 61 | case 32: 62 | p += ((y * xres) + x) * 4; 63 | *p++ = b; 64 | *p++ = g; 65 | *p++ = r; 66 | break; 67 | } 68 | } 69 | 70 | /* Draw line (x0, y0)~(x1, y1) with specific RGB888 color (r,g,b) */ 71 | static void lcd_drawline(int x0, int y0, int x1, int y1, int r, int g, int b) 72 | { 73 | int dx, dy, dm, dn, m, n, k, u, v, l, sum; 74 | 75 | dx = x1 - x0; 76 | dy = y1 - y0; 77 | dm = 1; 78 | dn = 1; 79 | 80 | if (dx < 0) { 81 | dx = -dx; 82 | dm = -1; 83 | } 84 | if (dy < 0) { 85 | dy = -dy; 86 | dn = -1; 87 | } 88 | 89 | m = dx; 90 | n = dy; 91 | k = 1; 92 | u = x0; 93 | v = y0; 94 | 95 | if (dx < dy) { 96 | m = dy; 97 | n = dx; 98 | k = dm; 99 | dm = dn; 100 | dn = k; 101 | k = 0; 102 | u = y0; 103 | v = x0; 104 | } 105 | 106 | l = 0; 107 | sum = m; 108 | 109 | lcd_point_rgb(x0, y0, r, g, b); 110 | 111 | while (sum != 0) { 112 | sum = sum - 1; 113 | l = l + n; 114 | u = u + dm; 115 | if (l >= m) { 116 | v = v + dn; 117 | } 118 | l = l % m; 119 | 120 | if (k == 1) { 121 | lcd_point_rgb(u, v, r, g, b); 122 | } else { 123 | lcd_point_rgb(v, u, r, g, b); 124 | } 125 | } 126 | } 127 | 128 | static int set_color(int bpp, unsigned int color) 129 | { 130 | int i; 131 | 132 | if (bpp == 16) { 133 | ushort *p = (ushort *) fbaddr; 134 | for (i = 0; i < (xres * yres); i++) { 135 | *p++ = color; 136 | } 137 | } else if (bpp == 32) { 138 | unsigned int *p = (unsigned int *) fbaddr; 139 | for (i = 0; i < (xres * yres); i++) { 140 | *p++ = color; 141 | } 142 | } else { 143 | printf("Unsupported BPP %d\n", bpp); 144 | } 145 | 146 | return 0; 147 | } 148 | 149 | 150 | int main(int argc, char *argv[]) 151 | { 152 | char *devname; 153 | struct fb_fix_screeninfo fix; 154 | struct fb_var_screeninfo var; 155 | int fb; 156 | int fbsize; 157 | int color; 158 | 159 | if ((devname = getenv("MATRIX_FB")) == NULL) { 160 | devname = "/dev/fb0"; 161 | } 162 | printf("FB device:%s\n", devname); 163 | if ((fb = open(devname, O_RDWR)) < 0) { 164 | printf("Open FB '%s' failed: %d, %s\n", devname, errno, strerror(errno)); 165 | return -1; 166 | } 167 | 168 | memset(&var, 0, sizeof(struct fb_var_screeninfo)); 169 | if (ioctl(fb, FBIOGET_VSCREENINFO, &var)) { 170 | printf("Get var screeninfo failed, %d\n", errno); 171 | return -1; 172 | } 173 | 174 | /* initialize global vars */ 175 | xres = var.xres; 176 | yres = var.yres; 177 | bpp = var.bits_per_pixel; 178 | 179 | printf("Current setting(s):\n"); 180 | printf(" X-res, Y-res ........ %4d, %4d\n", var.xres, var.yres); 181 | printf(" bpp ................. %4d\n", bpp); 182 | printf(" pixclock ............ %4d\n", var.pixclock); 183 | printf(" left, right ........ %4d, %4d\n", var.left_margin, var.right_margin); 184 | printf(" upper, lower ........ %4d, %4d\n", var.upper_margin, var.lower_margin); 185 | printf(" hsync, vsync ........ %4d, %4d\n", var.hsync_len, var.vsync_len); 186 | printf("\n"); 187 | 188 | memset( &fix, 0, sizeof(struct fb_fix_screeninfo) ); 189 | if (ioctl(fb, FBIOGET_FSCREENINFO, &fix)) { 190 | printf("Get fix screeninfo failed, %d\n", errno); 191 | return -1; 192 | } 193 | 194 | fbsize = fix.smem_len; 195 | fbaddr = (unsigned char *)mmap(0, fbsize, PROT_READ | (1 * PROT_WRITE), MAP_SHARED, fb, 0); 196 | 197 | if (fbaddr == MAP_FAILED) { 198 | printf("Cannot map Frame Buffer, fbsize is %u, errno = %d\n", fbsize, errno); 199 | return -1; 200 | } 201 | 202 | if (argc == 6) { // draw boarder 203 | int x0, y0, x1, y1, color; 204 | int r, g, b; 205 | 206 | x0 = atoi(argv[1]); 207 | y0 = atoi(argv[2]); 208 | x1 = atoi(argv[3]); 209 | y1 = atoi(argv[4]); 210 | color = strtol(argv[5], NULL, 0); 211 | 212 | r = (color >> 16) & 0xff; 213 | g = (color >> 8) & 0xff; 214 | b = (color >> 0) & 0xff; 215 | lcd_drawline(x0, y0, x1, y1, r, g, b); 216 | printf("Line (%d, %d) --> (%d, %d), RGB(%02x, %02x, %02x)\n", x0, y0, x1, y1, r, g, b); 217 | } else if (argc == 2) { // draw pure color 218 | color = strtoul(argv[1], 0, 0); 219 | set_color(bpp, color); 220 | } else { 221 | switch(bpp) { 222 | case 16: 223 | // blue, bit[0-4] 224 | set_color(bpp, 0x1f); 225 | printf("outputting blue\n"); 226 | sleep(1); 227 | // green, bit[5-10] 228 | set_color(bpp, 0x7e0); 229 | printf("outputting green\n"); 230 | sleep(1); 231 | // red, bit[11-15] 232 | set_color(bpp, 0x1f800); 233 | printf("outputting red\n"); 234 | sleep(1); 235 | break; 236 | case 32: 237 | // blue, bit[0-7] 238 | set_color(bpp, 0xff); 239 | printf("outputting blue\n"); 240 | sleep(1); 241 | // green, bit[8-15] 242 | set_color(bpp, 0xff00); 243 | printf("outputting green\n"); 244 | sleep(1); 245 | // red, bit[16-23] 246 | set_color(bpp, 0xff0000); 247 | printf("outputting red\n"); 248 | sleep(1); 249 | break; 250 | default: 251 | printf("unsupported bpp\n"); 252 | break; 253 | } 254 | } 255 | 256 | close(fb); 257 | munmap(fbaddr, fbsize); 258 | 259 | return 0; 260 | } 261 | 262 | -------------------------------------------------------------------------------- /demo/matrix-gpio_int/.gitignore: -------------------------------------------------------------------------------- 1 | /matrix-gpio_int 2 | -------------------------------------------------------------------------------- /demo/matrix-gpio_int/Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | 3 | TARGET = $(patsubst Matrix-%.c,matrix-%, $(wildcard *.c)) 4 | SRC = $(wildcard *.c) 5 | SRC += $(wildcard *.h) 6 | 7 | $(TARGET):$(SRC) 8 | $(CC) -o $@ -L ../../lib/ -I ../../lib/includes/ $^ -lfahw -lm -Wall 9 | 10 | .PHONY: clean 11 | clean: 12 | rm -f $(TARGET) 13 | -------------------------------------------------------------------------------- /demo/matrix-gpio_int/Matrix-gpio_int.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "libfahw.h" 5 | 6 | #define DRIVER_MODULE "matrix_gpio_int" 7 | 8 | static struct sensor dev[] = { 9 | { 10 | GPIO_PIN(7), 11 | IRQ_TYPE_EDGE_FALLING, 12 | } 13 | }; 14 | int devFD = -1; 15 | 16 | void intHandler(int signNum) 17 | { 18 | if (signNum == SIGINT) { 19 | printf("Clean up\n"); 20 | sensorDeinit(devFD); 21 | system("rmmod "DRIVER_MODULE); 22 | } 23 | exit(0); 24 | } 25 | 26 | int main(int argc, char ** argv) 27 | { 28 | int i, board; 29 | int retSize = -1; 30 | char value[ARRAY_SIZE(dev)]; 31 | 32 | if ((board = boardInit()) < 0) { 33 | printf("Fail to init board\n"); 34 | return -1; 35 | } 36 | 37 | if (argc == 2) 38 | dev[0].pin = atoi(argv[1]); 39 | system("modprobe "DRIVER_MODULE); 40 | signal(SIGINT, intHandler); 41 | if (board == BOARD_NANOPC_T2 || board == BOARD_NANOPC_T3) 42 | dev[0].pin = GPIO_PIN(15); 43 | printf("Use GPIO_PIN(%d)\n", dev[0].pin); 44 | if ((devFD =sensorInit(dev, ARRAY_SIZE(dev))) == -1) { 45 | printf("Fail to init sensor\n"); 46 | goto err; 47 | } 48 | printf("Waiting event...\n"); 49 | if ((retSize = sensorRead(devFD, value, ARRAY_SIZE(dev))) == -1) { 50 | printf("Fail to read sensors\n"); 51 | } 52 | if (retSize > 0) { 53 | i = 0; 54 | for (i=0; i 2 | #include 3 | #include 4 | #include "libfahw.h" 5 | 6 | #define STATUS_CHANGE_TIMES (5) 7 | 8 | int main(int argc, char ** argv) 9 | { 10 | int pin = GPIO_PIN(7); 11 | int i, value, board; 12 | int ret = -1; 13 | 14 | if ((board = boardInit()) < 0) { 15 | printf("Fail to init board\n"); 16 | return -1; 17 | } 18 | if (board == BOARD_NANOPC_T2 || board == BOARD_NANOPC_T3) 19 | pin = GPIO_PIN(15); 20 | 21 | if (argc == 2) 22 | pin = GPIO_PIN(atoi(argv[1])); 23 | if ((ret = exportGPIOPin(pin)) == -1) { 24 | printf("exportGPIOPin(%d) failed\n", pin); 25 | } 26 | if ((ret = setGPIODirection(pin, GPIO_OUT)) == -1) { 27 | printf("setGPIODirection(%d) failed\n", pin); 28 | } 29 | for (i = 0; i < STATUS_CHANGE_TIMES; i++) { 30 | if (i % 2) { 31 | value = GPIO_HIGH; 32 | } else { 33 | value = GPIO_LOW; 34 | } 35 | if ((ret = setGPIOValue(pin, value)) > 0) { 36 | printf("%d: GPIO_PIN(%d) value is %d\n", i+1, pin, value); 37 | } else { 38 | printf("setGPIOValue(%d) failed\n", pin); 39 | } 40 | sleep(1); 41 | } 42 | unexportGPIOPin(pin); 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /demo/matrix-i2c_lcd1602/.gitignore: -------------------------------------------------------------------------------- 1 | /matrix-lcd1602 2 | -------------------------------------------------------------------------------- /demo/matrix-i2c_lcd1602/Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | 3 | TARGET = $(patsubst Matrix-%.c,matrix-%, $(wildcard *.c)) 4 | SRC = $(wildcard *.c) 5 | SRC += $(wildcard *.h) 6 | 7 | $(TARGET):$(SRC) 8 | $(CC) -o $@ -L ../../lib/ -I ../../lib/includes/ $^ -lfahw -lm -Wall 9 | 10 | .PHONY: clean 11 | clean: 12 | rm -f $(TARGET) 13 | -------------------------------------------------------------------------------- /demo/matrix-i2c_lcd1602/Matrix-lcd1602.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include "libfahw.h" 8 | 9 | int main(int argc, char ** argv) 10 | { 11 | int devFD, board; 12 | int i2cDev = 0; 13 | 14 | if ((board = boardInit()) < 0) { 15 | printf("Fail to init board\n"); 16 | return -1; 17 | } 18 | 19 | if (argc == 2) 20 | i2cDev = atoi(argv[1]); 21 | if ((devFD = LCD1602Init(i2cDev)) == -1) { 22 | printf("Fail to init LCD1602\n"); 23 | return -1; 24 | } 25 | if (LCD1602Clear(devFD) == -1) { 26 | printf("Fail to Clear\n"); 27 | } 28 | printf("clearing LCD1602\n"); 29 | sleep(1); 30 | if (LCD1602DispLines(devFD, " B&G Char LCD", "--by FriendlyARM") == -1) { 31 | printf("Fail to Display String\n"); 32 | } 33 | printf("displaying LCD1602\n"); 34 | LCD1602DeInit(devFD); 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /demo/matrix-i2c_lcd1602_keypad/.gitignore: -------------------------------------------------------------------------------- 1 | /matrix-lcd1602_keypad 2 | -------------------------------------------------------------------------------- /demo/matrix-i2c_lcd1602_keypad/Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | 3 | TARGET = $(patsubst Matrix-%.c,matrix-%, $(wildcard *.c)) 4 | SRC = $(wildcard *.c) 5 | SRC += $(wildcard *.h) 6 | 7 | $(TARGET):$(SRC) 8 | $(CC) -o $@ -L ../../lib/ -I ../../lib/includes/ $^ -lfahw -lm -Wall 9 | 10 | .PHONY: clean 11 | clean: 12 | rm -f $(TARGET) 13 | -------------------------------------------------------------------------------- /demo/matrix-i2c_lcd1602_keypad/Matrix-lcd1602_keypad.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include "libfahw.h" 17 | 18 | #define TIME_STR_BUFSIZE 32 19 | 20 | int showIP(int devFD, char *netDev) 21 | { 22 | int socketFD; 23 | struct sockaddr_in *sin; 24 | struct ifreq ifr; 25 | socketFD = socket(AF_INET, SOCK_DGRAM, 0); 26 | if(socketFD == -1) { 27 | printf("socket error\n"); 28 | return -1; 29 | } 30 | strcpy(ifr.ifr_name, netDev); 31 | if(ioctl(socketFD, SIOCGIFADDR, &ifr) < 0) { 32 | return -1; 33 | } else { 34 | sin = (struct sockaddr_in *)&(ifr.ifr_addr); 35 | LCD1602KeyDispStr(devFD, 0, 1, inet_ntoa(sin->sin_addr)); 36 | } 37 | return 0; 38 | } 39 | 40 | int main(int argc, char ** argv) 41 | { 42 | int devFD, board; 43 | int keyValue = 0; 44 | int lastKeyValue = -1; 45 | int showDefault = 1; 46 | int needClear = 1; 47 | time_t lt; 48 | char curTime[TIME_STR_BUFSIZE]; 49 | char preTime[TIME_STR_BUFSIZE]; 50 | int i2cDev = 0; 51 | 52 | if ((board = boardInit()) < 0) { 53 | printf("Fail to init board\n"); 54 | return -1; 55 | } 56 | 57 | if (argc == 2) 58 | i2cDev = atoi(argv[1]); 59 | if ((devFD = LCD1602KeyInit(i2cDev)) == -1) { 60 | printf("Fail to init LCD1602\n"); 61 | return -1; 62 | } 63 | LCD1602KeyClear(devFD); 64 | printf("waiting key press...\n"); 65 | while (1) { 66 | keyValue = LCD1602GetKey(devFD); 67 | keyValue &= 0x1f; 68 | if (keyValue != lastKeyValue) { 69 | lastKeyValue = keyValue; 70 | } else if (showDefault != 1){ 71 | usleep(1000); 72 | continue; 73 | } 74 | switch (keyValue) { 75 | // F1 76 | case 0x1e: 77 | showDefault = 0; 78 | LCD1602KeyClear(devFD); 79 | LCD1602KeyDispStr(devFD, 0, 0, "#F1-IP address"); 80 | if (showIP(devFD, "eth0")) { 81 | if (showIP(devFD, "wlan0")) { 82 | if (showIP(devFD, "usb0")) { 83 | showIP(devFD, "lo"); 84 | } 85 | } 86 | } 87 | break; 88 | // F2 89 | case 0x1d: 90 | showDefault = 0; 91 | LCD1602KeyClear(devFD); 92 | LCD1602KeyDispStr(devFD, 0, 0, "#F2-Your favor"); 93 | LCD1602KeyDispStr(devFD, 0, 1, "Come add it"); 94 | break; 95 | // F3 96 | case 0x1b: 97 | showDefault = 0; 98 | LCD1602KeyClear(devFD); 99 | LCD1602KeyDispStr(devFD, 0, 0, "#F3-Your idea"); 100 | LCD1602KeyDispStr(devFD, 0, 1, "Come show it"); 101 | break; 102 | // F4 103 | case 0x17: 104 | showDefault = 0; 105 | LCD1602KeyClear(devFD); 106 | LCD1602KeyDispStr(devFD, 0, 0, "#F4-About"); 107 | LCD1602KeyDispStr(devFD, 0, 1, "by FriendlyARM"); 108 | break; 109 | // F5 110 | case 0xf: 111 | showDefault = 1; 112 | break; 113 | } 114 | if (showDefault == 1) { 115 | if (needClear) { 116 | LCD1602KeyClear(devFD); 117 | LCD1602KeyDispStr(devFD, 0, 0, "#Default"); 118 | needClear = 0; 119 | } 120 | memset(curTime, 0, TIME_STR_BUFSIZE); 121 | lt = time(NULL); 122 | strncpy(curTime, ctime(<) + 11, 8); 123 | if(strcmp(curTime, preTime)) { 124 | printf("time:%s\n", curTime); 125 | LCD1602KeyDispStr(devFD, 0, 1, curTime); 126 | } 127 | memset(preTime, 0, TIME_STR_BUFSIZE); 128 | strcpy(preTime, curTime); 129 | } else { 130 | needClear = 1; 131 | usleep(1000); 132 | } 133 | } 134 | printf("quit reading key press\n"); 135 | LCD1602KeyDeInit(devFD); 136 | return 0; 137 | } 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | -------------------------------------------------------------------------------- /demo/matrix-ir_receiver/.gitignore: -------------------------------------------------------------------------------- 1 | /matrix-ir_receiver 2 | -------------------------------------------------------------------------------- /demo/matrix-ir_receiver/Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | 3 | TARGET = $(patsubst Matrix-%.c,matrix-%, $(wildcard *.c)) 4 | SRC = $(wildcard *.c) 5 | SRC += $(wildcard *.h) 6 | 7 | $(TARGET):$(SRC) 8 | $(CC) -o $@ -L ../../lib/ -I ../../lib/includes/ $^ -lfahw -lm -Wall 9 | 10 | .PHONY: clean 11 | clean: 12 | rm -f $(TARGET) 13 | -------------------------------------------------------------------------------- /demo/matrix-ir_receiver/Matrix-ir_receiver.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "libfahw.h" 7 | 8 | #define BUF_SIZE (64) 9 | #define GPIO_IR_DEV "/dev/input/gpio_ir_recv" 10 | #define IR_EVENT_TIMES (6) 11 | #define DRIVER_MODULE "matrix_ir_recv" 12 | static int irFD; 13 | void IRIntHandler(int signNum) 14 | { 15 | if (signNum == SIGINT) { 16 | printf("Clean up\n"); 17 | closeHW(irFD); 18 | system("rmmod "DRIVER_MODULE); 19 | } 20 | exit(0); 21 | } 22 | 23 | int main(int argc, char ** argv) 24 | { 25 | int board, i, j; 26 | int retSize = -1; 27 | char *devName = GPIO_IR_DEV; 28 | int pin = GPIO_PIN(7); 29 | char modStr[BUF_SIZE]; 30 | struct input_event evKey; 31 | 32 | if ((board = boardInit()) < 0) { 33 | printf("Fail to init board\n"); 34 | return -1; 35 | } 36 | if (board == BOARD_NANOPC_T2 || board == BOARD_NANOPC_T3) 37 | pin = GPIO_PIN(15); 38 | 39 | sprintf(modStr, "modprobe %s gpio=%d", DRIVER_MODULE, pintoGPIO(pin)); 40 | system(modStr); 41 | signal(SIGINT, IRIntHandler); 42 | sleep(1); 43 | irFD = openHW(devName, O_RDWR); 44 | if (irFD < 0) { 45 | printf("Fail to open GPIO IR device\n"); 46 | goto err; 47 | } 48 | printf("Press the IR remoter\n"); 49 | for (i=0; i 2 | #include 3 | #include 4 | #include 5 | #include "libfahw.h" 6 | 7 | #define PS2_READ_TIMES (1000) 8 | #define DRIVER_MODULE "pcf8591" 9 | 10 | void intHandler(int signNum) 11 | { 12 | if (signNum == SIGINT) { 13 | printf("Clean up\n"); 14 | system("rmmod "DRIVER_MODULE); 15 | } 16 | exit(0); 17 | } 18 | 19 | int main(int argc, char ** argv) 20 | { 21 | int i = 0; 22 | int x, y, board; 23 | 24 | x = y = 0; 25 | if ((board = boardInit()) < 0) { 26 | printf("Fail to init board\n"); 27 | return -1; 28 | } 29 | 30 | system("modprobe "DRIVER_MODULE); 31 | signal(SIGINT, intHandler); 32 | for (i=0; i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #define RGB(r,g,b) (((r << 11) & 0xF800) | ((g << 6) & 0x07E0) | (( b & 0x1F))) 16 | 17 | static int xres, yres, bpp; 18 | static unsigned char *fbaddr; 19 | 20 | 21 | /* Draw point (x, y) with specific RGB888 color (r,g,b) */ 22 | static void lcd_point_rgb(int x, int y, int r, int g, int b) 23 | { 24 | unsigned int v = 0; 25 | unsigned char *p = fbaddr; 26 | 27 | if ((x < 0 || x >= xres) || (y < 0 || y >= yres)) { 28 | /* out of LCD display area */ 29 | return; 30 | } 31 | 32 | switch (bpp) { 33 | case 16: 34 | v |= ((b >> 3) & 0x1f); 35 | v |= ((g >> 2) & 0x3f) << 5; 36 | v |= ((r >> 3) & 0x1f) << 11; 37 | 38 | p += ((y * xres) + x) * 2; 39 | *p++ = v; 40 | *p++ = v >> 8; 41 | break; 42 | 43 | case 18: 44 | v |= ((b >> 2) & 0x3f); 45 | v |= ((g >> 2) & 0x3f) << 6; 46 | v |= ((r >> 2) & 0x3f) << 12; 47 | 48 | p += ((y * xres) + x) * 3; 49 | *p++ = v; 50 | *p++ = v >> 8; 51 | *p++ = v >> 16; 52 | break; 53 | 54 | case 24: 55 | p += ((y * xres) + x) * 3; 56 | *p++ = b; 57 | *p++ = g; 58 | *p++ = r; 59 | break; 60 | 61 | case 32: 62 | p += ((y * xres) + x) * 4; 63 | *p++ = b; 64 | *p++ = g; 65 | *p++ = r; 66 | break; 67 | } 68 | } 69 | 70 | /* Draw line (x0, y0)~(x1, y1) with specific RGB888 color (r,g,b) */ 71 | static void lcd_drawline(int x0, int y0, int x1, int y1, int r, int g, int b) 72 | { 73 | int dx, dy, dm, dn, m, n, k, u, v, l, sum; 74 | 75 | dx = x1 - x0; 76 | dy = y1 - y0; 77 | dm = 1; 78 | dn = 1; 79 | 80 | if (dx < 0) { 81 | dx = -dx; 82 | dm = -1; 83 | } 84 | if (dy < 0) { 85 | dy = -dy; 86 | dn = -1; 87 | } 88 | 89 | m = dx; 90 | n = dy; 91 | k = 1; 92 | u = x0; 93 | v = y0; 94 | 95 | if (dx < dy) { 96 | m = dy; 97 | n = dx; 98 | k = dm; 99 | dm = dn; 100 | dn = k; 101 | k = 0; 102 | u = y0; 103 | v = x0; 104 | } 105 | 106 | l = 0; 107 | sum = m; 108 | 109 | lcd_point_rgb(x0, y0, r, g, b); 110 | 111 | while (sum != 0) { 112 | sum = sum - 1; 113 | l = l + n; 114 | u = u + dm; 115 | if (l >= m) { 116 | v = v + dn; 117 | } 118 | l = l % m; 119 | 120 | if (k == 1) { 121 | lcd_point_rgb(u, v, r, g, b); 122 | } else { 123 | lcd_point_rgb(v, u, r, g, b); 124 | } 125 | } 126 | } 127 | 128 | static int set_color(int bpp, unsigned int color, int ystart, int yend) 129 | { 130 | int i; 131 | 132 | if (bpp == 16) { 133 | ushort *p = (ushort *) fbaddr+(ystart*xres); 134 | for (i = (ystart*xres); i < (yend * xres); i++) { 135 | *p++ = color; 136 | } 137 | } else if (bpp == 32) { 138 | unsigned int *p = (unsigned int *) fbaddr; 139 | for (i = (ystart*xres); i < (yend * xres); i++) { 140 | *p++ = color; 141 | } 142 | } else { 143 | printf("Unsupported BPP %d\n", bpp); 144 | } 145 | 146 | return 0; 147 | } 148 | 149 | 150 | int main(int argc, char *argv[]) 151 | { 152 | char *devname; 153 | struct fb_fix_screeninfo fix; 154 | struct fb_var_screeninfo var; 155 | int fb; 156 | int fbsize; 157 | int color; 158 | 159 | if ((devname = getenv("LCDSET_FB_DEV")) == NULL) { 160 | devname = "/dev/fb0"; 161 | } 162 | 163 | if ((fb = open(devname, O_RDWR)) < 0) { 164 | printf("Open FB '%s' failed: %d, %s\n", devname, errno, strerror(errno)); 165 | return -1; 166 | } 167 | 168 | memset(&var, 0, sizeof(struct fb_var_screeninfo)); 169 | if (ioctl(fb, FBIOGET_VSCREENINFO, &var)) { 170 | printf("Get var screeninfo failed, %d\n", errno); 171 | return -1; 172 | } 173 | 174 | /* initialize global vars */ 175 | xres = var.xres; 176 | yres = var.yres; 177 | bpp = var.bits_per_pixel; 178 | 179 | printf("Current setting(s):\n"); 180 | printf(" X-res, Y-res ........ %4d, %4d\n", var.xres, var.yres); 181 | printf(" bpp ................. %4d\n", bpp); 182 | printf(" pixclock ............ %4d\n", var.pixclock); 183 | printf(" left, right ........ %4d, %4d\n", var.left_margin, var.right_margin); 184 | printf(" upper, lower ........ %4d, %4d\n", var.upper_margin, var.lower_margin); 185 | printf(" hsync, vsync ........ %4d, %4d\n", var.hsync_len, var.vsync_len); 186 | printf("\n"); 187 | 188 | memset( &fix, 0, sizeof(struct fb_fix_screeninfo) ); 189 | if (ioctl(fb, FBIOGET_FSCREENINFO, &fix)) { 190 | printf("Get fix screeninfo failed, %d\n", errno); 191 | return -1; 192 | } 193 | 194 | fbsize = fix.smem_len; 195 | fbaddr = (unsigned char *)mmap(0, fbsize, PROT_READ | (1 * PROT_WRITE), MAP_SHARED, fb, 0); 196 | 197 | if (fbaddr == MAP_FAILED) { 198 | printf("Cannot map Frame Buffer, fbsize is %u, errno = %d\n", fbsize, errno); 199 | return -1; 200 | } 201 | 202 | if (argc == 6) { // draw board 203 | int x0, y0, x1, y1, color; 204 | int r, g, b; 205 | 206 | x0 = atoi(argv[1]); 207 | y0 = atoi(argv[2]); 208 | x1 = atoi(argv[3]); 209 | y1 = atoi(argv[4]); 210 | color = strtol(argv[5], NULL, 0); 211 | 212 | r = (color >> 16) & 0xff; 213 | g = (color >> 8) & 0xff; 214 | b = (color >> 0) & 0xff; 215 | lcd_drawline(x0, y0, x1, y1, r, g, b); 216 | printf("Line (%d, %d) --> (%d, %d), RGB(%02x, %02x, %02x)\n", x0, y0, x1, y1, r, g, b); 217 | } else if (argc == 2) { // draw color block 218 | color = strtoul(argv[1], 0, 0); 219 | set_color(bpp, color, 0, yres); 220 | } else { 221 | switch(bpp) { 222 | case 16: 223 | // only support fb-st7789s(BGR) 224 | // red, bit[0-4] 225 | set_color(bpp, 0x1f, 0, yres/3); 226 | printf("outputting red\n"); 227 | sleep(1); 228 | // green, bit[5-10] 229 | set_color(bpp, 0x7e0, yres/3, yres/3*2); 230 | printf("outputting green\n"); 231 | sleep(1); 232 | // blue, bit[11-15] 233 | set_color(bpp, 0x1f800, yres/3*2, yres); 234 | printf("outputting blue\n"); 235 | sleep(1); 236 | break; 237 | case 32: 238 | // blue, bit[0-7] 239 | set_color(bpp, 0xff, 0, yres/3); 240 | printf("outputting blue\n"); 241 | sleep(1); 242 | // green, bit[8-15] 243 | set_color(bpp, 0xff00, yres/3, yres/3*2); 244 | printf("outputting green\n"); 245 | sleep(1); 246 | // red, bit[16-23] 247 | set_color(bpp, 0xff0000, yres/3*2, yres); 248 | printf("outputting red\n"); 249 | sleep(1); 250 | break; 251 | break; 252 | default: 253 | printf("unsupported bpp\n"); 254 | break; 255 | } 256 | } 257 | 258 | close(fb); 259 | munmap(fbaddr, fbsize); 260 | 261 | return 0; 262 | } 263 | 264 | -------------------------------------------------------------------------------- /demo/matrix-pressure_and_temperature_sensor/.gitignore: -------------------------------------------------------------------------------- 1 | /matrix-pressure_temp 2 | -------------------------------------------------------------------------------- /demo/matrix-pressure_and_temperature_sensor/Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | 3 | TARGET = $(patsubst Matrix-%.c,matrix-%, $(wildcard *.c)) 4 | SRC = $(wildcard *.c) 5 | SRC += $(wildcard *.h) 6 | 7 | $(TARGET):$(SRC) 8 | $(CC) -o $@ -L ../../lib/ -I ../../lib/includes/ $^ -lfahw -lm -Wall 9 | 10 | .PHONY: clean 11 | clean: 12 | rm -f $(TARGET) 13 | -------------------------------------------------------------------------------- /demo/matrix-pressure_and_temperature_sensor/Matrix-pressure_temp.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "libfahw.h" 6 | 7 | #define DRIVER_MODULE "bmp085" 8 | 9 | int main(int argc, char ** argv) 10 | { 11 | int ret = -1; 12 | int bmpTemp=0, bmpPressure=0; 13 | int board; 14 | float altitude = 0; 15 | 16 | if ((board = boardInit()) < 0) { 17 | printf("Fail to init board\n"); 18 | return -1; 19 | } 20 | 21 | system("modprobe "DRIVER_MODULE); 22 | if ((ret = bmp180Read(BMP180_TEMP, &bmpTemp)) != -1) { 23 | printf("The temperature is %.1f C\n", (float)bmpTemp / 10); 24 | } else { 25 | printf("Faided to get humidity\n"); 26 | } 27 | if ((ret = bmp180Read(BMP180_PRESSURE, &bmpPressure)) != -1) { 28 | printf("The pressure is %.2f hPa\n", (float)bmpPressure / 100); 29 | } else { 30 | printf("Faided to get pressure\n"); 31 | } 32 | 33 | altitude = 44330 * ( 1 - pow( ((float)bmpPressure / 100 / 1013.25), (1/5.255) ) ); 34 | printf("The altitude is %.2f m\n", altitude); 35 | system("rmmod "DRIVER_MODULE); 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /demo/matrix-pwm/.gitignore: -------------------------------------------------------------------------------- 1 | /matrix-pwm 2 | -------------------------------------------------------------------------------- /demo/matrix-pwm/Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | 3 | TARGET = $(patsubst Matrix-%.c,matrix-%, $(wildcard *.c)) 4 | SRC = $(wildcard *.c) 5 | SRC += $(wildcard *.h) 6 | 7 | $(TARGET):$(SRC) 8 | $(CC) -o $@ -L ../../lib/ -I ../../lib/includes/ $^ -lfahw -lm -Wall 9 | 10 | .PHONY: clean 11 | clean: 12 | rm -f $(TARGET) 13 | -------------------------------------------------------------------------------- /demo/matrix-pwm/Matrix-pwm.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "libfahw.h" 6 | 7 | #define DRIVER_MODULE "matrix_pwm" 8 | static int pwm; 9 | 10 | void intHandler(int signNum) 11 | { 12 | if (signNum == SIGINT) { 13 | printf("Clean up\n"); 14 | PWMStop(pwm); 15 | system("rmmod "DRIVER_MODULE); 16 | } 17 | exit(0); 18 | } 19 | 20 | int main(int argc, char ** argv) 21 | { 22 | int Hz, duty, board; 23 | 24 | if ((board = boardInit()) < 0) { 25 | printf("Fail to init board\n"); 26 | return -1; 27 | } 28 | 29 | system("modprobe "DRIVER_MODULE); 30 | signal(SIGINT, intHandler); 31 | if (argc == 4) { 32 | // Usage:matrix-pwm channel freq duty[0~1000] 33 | pwm = atoi(argv[1]); 34 | Hz = atoi(argv[2]); 35 | duty = atoi(argv[3]); 36 | } else { 37 | Hz = 1000; 38 | duty = 500; 39 | printf("Using default config: channel=%d freq=%dHz duty=%d\n", pwm, Hz, duty); 40 | } 41 | if (PWMPlay(pwm, Hz, duty) == -1) { 42 | printf("Fail to output PWM\n"); 43 | } 44 | printf("Press enter to stop PWM\n"); 45 | getchar(); 46 | PWMStop(pwm); 47 | system("rmmod "DRIVER_MODULE); 48 | 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /demo/matrix-rgb_led/.gitignore: -------------------------------------------------------------------------------- 1 | /matrix-rgb_led 2 | -------------------------------------------------------------------------------- /demo/matrix-rgb_led/Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | 3 | TARGET = $(patsubst Matrix-%.c,matrix-%, $(wildcard *.c)) 4 | SRC = $(wildcard *.c) 5 | SRC += $(wildcard *.h) 6 | 7 | $(TARGET):$(SRC) 8 | $(CC) -o $@ -L ../../lib/ -I ../../lib/includes/ $^ -lfahw -lm -Wall 9 | 10 | .PHONY: clean 11 | clean: 12 | rm -f $(TARGET) 13 | -------------------------------------------------------------------------------- /demo/matrix-rgb_led/Matrix-rgb_led.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "libfahw.h" 6 | 7 | #define STATUS_CHANGE_TIMES (5) 8 | static int ledPin1 = GPIO_PIN(7); 9 | static int ledPin2 = GPIO_PIN(8); 10 | static int ledPin3 = GPIO_PIN(10); 11 | 12 | void intHandler(int signNum) 13 | { 14 | if (signNum == SIGINT) { 15 | printf("Clean up\n"); 16 | unexportGPIOPin(ledPin1); 17 | unexportGPIOPin(ledPin2); 18 | unexportGPIOPin(ledPin3); 19 | } 20 | exit(0); 21 | } 22 | 23 | void setRGBLED(int data) 24 | { 25 | if (data & 0x01) { 26 | setGPIOValue(ledPin1, GPIO_HIGH); 27 | } else { 28 | setGPIOValue(ledPin1, GPIO_LOW); 29 | } 30 | if (data & 0x02) { 31 | setGPIOValue(ledPin2, GPIO_HIGH); 32 | } else { 33 | setGPIOValue(ledPin2, GPIO_LOW); 34 | } 35 | if (data & 0x04) { 36 | setGPIOValue(ledPin3, GPIO_HIGH); 37 | } else { 38 | setGPIOValue(ledPin3 ,GPIO_LOW); 39 | } 40 | } 41 | 42 | int main(int argc, char ** argv) 43 | { 44 | int ret = -1; 45 | int val, board; 46 | 47 | if ((board = boardInit()) < 0) { 48 | printf("Fail to init board\n"); 49 | return -1; 50 | } 51 | if (board == BOARD_NANOPC_T2 || BOARD_NANOPC_T3) { 52 | ledPin1 = GPIO_PIN(15); 53 | ledPin2 = GPIO_PIN(16); 54 | ledPin3 = GPIO_PIN(17); 55 | } 56 | if ((ret = exportGPIOPin(ledPin1)) == -1) { 57 | printf("exportGPIOPin(%d) failed\n", ledPin1); 58 | } 59 | if ((ret = setGPIODirection(ledPin1, GPIO_OUT)) == -1) { 60 | printf("setGPIODirection(%d) failed\n", ledPin1); 61 | } 62 | if ((ret = exportGPIOPin(ledPin2)) == -1) { 63 | printf("exportGPIOPin(%d) failed\n", ledPin2); 64 | } 65 | if ((ret = setGPIODirection(ledPin2, GPIO_OUT)) == -1) { 66 | printf("setGPIODirection(%d) failed\n", ledPin2); 67 | } 68 | if ((ret = exportGPIOPin(ledPin3)) == -1) { 69 | printf("exportGPIOPin(%d) failed\n", ledPin3); 70 | } 71 | if ((ret = setGPIODirection(ledPin3, GPIO_OUT)) == -1) { 72 | printf("setGPIODirection(%d) failed\n", ledPin3); 73 | } 74 | signal(SIGINT, intHandler); 75 | for (val = 0; val < 8; val++) { 76 | printf("Set RGB LED: %x\n", val); 77 | setRGBLED(val); 78 | usleep(1000 * 1000); 79 | } 80 | unexportGPIOPin(ledPin1); 81 | unexportGPIOPin(ledPin2); 82 | unexportGPIOPin(ledPin3); 83 | return 0; 84 | } 85 | -------------------------------------------------------------------------------- /demo/matrix-rotary_encoder/.gitignore: -------------------------------------------------------------------------------- 1 | /matrix-rotary_encoder 2 | -------------------------------------------------------------------------------- /demo/matrix-rotary_encoder/Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | 3 | TARGET = $(patsubst Matrix-%.c,matrix-%, $(wildcard *.c)) 4 | SRC = $(wildcard *.c) 5 | SRC += $(wildcard *.h) 6 | 7 | $(TARGET):$(SRC) 8 | $(CC) -o $@ -L ../../lib/ -I ../../lib/includes/ $^ -lfahw -lm -Wall 9 | 10 | .PHONY: clean 11 | clean: 12 | rm -f $(TARGET) 13 | -------------------------------------------------------------------------------- /demo/matrix-rotary_encoder/Matrix-rotary_encoder.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include "libfahw.h" 8 | 9 | #define ENCODER_READ_TIMES (20) 10 | #define DRIVER_MODULE "matrix_rotary_encoder" 11 | 12 | void encoderHandler(int signNum) 13 | { 14 | if (signNum == SIGINT) { 15 | printf("Clean up\n"); 16 | rotaryEncoderDeInit(); 17 | system("rmmod "DRIVER_MODULE); 18 | } 19 | exit(0); 20 | } 21 | 22 | int main(int argc, char ** argv) 23 | { 24 | int i = 0; 25 | int encoderSw = 0; 26 | int encoderValue = 0; 27 | int board; 28 | int swPin = GPIO_PIN(7); 29 | int siaPin = GPIO_PIN(8); 30 | int sibPin = GPIO_PIN(10); 31 | 32 | if ((board = boardInit()) < 0) { 33 | printf("Fail to init board\n"); 34 | return -1; 35 | } 36 | if (board == BOARD_NANOPC_T2 || board == BOARD_NANOPC_T3) { 37 | swPin = GPIO_PIN(15); 38 | siaPin = GPIO_PIN(16); 39 | sibPin = GPIO_PIN(17); 40 | } 41 | system("modprobe "DRIVER_MODULE); 42 | if (rotaryEncoderInit(swPin, siaPin, sibPin)) { 43 | printf("Fail to init rotary encoder\n"); 44 | goto err; 45 | } 46 | signal(SIGINT, encoderHandler); 47 | for (i=0; i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include "libfahw.h" 11 | 12 | #define DRIVER_MODULE "rtc-ds1307" 13 | static const char default_rtc[] = "/dev/rtc-ds1307"; 14 | static const char default_date_time[] = "2015 9 15 1 1 1"; 15 | 16 | int main(int argc, char **argv) 17 | { 18 | int fd, retval, board; 19 | struct rtc_time rtc_tm; 20 | const char *rtc = default_rtc; 21 | const char *date_time = default_date_time; 22 | 23 | #if 0 24 | if ((board = boardInit()) < 0) { 25 | printf("Fail to init board\n"); 26 | return -1; 27 | } 28 | #endif 29 | 30 | switch (argc) { 31 | case 3: 32 | rtc = argv[1]; 33 | date_time = argv[2]; 34 | break; 35 | case 1: 36 | break; 37 | default: 38 | fprintf(stderr, "usage: rtctest [rtcdev] [year mon day hour min sec]\n"); 39 | return 1; 40 | } 41 | system("modprobe "DRIVER_MODULE); 42 | sleep(1); 43 | fd = open(rtc, O_RDONLY); 44 | if (fd == -1) { 45 | perror(rtc); 46 | goto err; 47 | } 48 | fprintf(stderr, "RTC Driver Test Example.\n"); 49 | 50 | sscanf(date_time, "%d %d %d %d %d %d", 51 | &rtc_tm.tm_year, 52 | &rtc_tm.tm_mon, 53 | &rtc_tm.tm_mday, 54 | &rtc_tm.tm_hour, 55 | &rtc_tm.tm_min, 56 | &rtc_tm.tm_sec); 57 | rtc_tm.tm_year -= 1900; 58 | rtc_tm.tm_mon -= 1; 59 | retval = ioctl(fd, RTC_SET_TIME, &rtc_tm); 60 | if (retval == -1) { 61 | perror("RTC_SET_TIME ioctl"); 62 | goto err; 63 | } 64 | 65 | fprintf(stderr, "Set RTC date/time is %d-%d-%d, %02d:%02d:%02d.\n", 66 | rtc_tm.tm_mon + 1, rtc_tm.tm_mday, rtc_tm.tm_year + 1900, 67 | rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec); 68 | 69 | /* Read the RTC time/date */ 70 | retval = ioctl(fd, RTC_RD_TIME, &rtc_tm); 71 | if (retval == -1) { 72 | perror("RTC_RD_TIME ioctl"); 73 | goto err; 74 | } 75 | 76 | fprintf(stderr, "Read RTC date/time is %d-%d-%d, %02d:%02d:%02d.\n", 77 | rtc_tm.tm_mon + 1, rtc_tm.tm_mday, rtc_tm.tm_year + 1900, 78 | rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec); 79 | 80 | fprintf(stderr, "Test complete\n"); 81 | close(fd); 82 | err: 83 | system("rmmod "DRIVER_MODULE); 84 | return 0; 85 | } 86 | -------------------------------------------------------------------------------- /demo/matrix-temperature_and_humidity_sensor/.gitignore: -------------------------------------------------------------------------------- 1 | /matrix-temp_humidity 2 | -------------------------------------------------------------------------------- /demo/matrix-temperature_and_humidity_sensor/Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | 3 | TARGET = $(patsubst Matrix-%.c,matrix-%, $(wildcard *.c)) 4 | SRC = $(wildcard *.c) 5 | SRC += $(wildcard *.h) 6 | 7 | $(TARGET):$(SRC) 8 | $(CC) -o $@ -L ../../lib/ -I ../../lib/includes/ $^ -lfahw -lm -Wall 9 | 10 | .PHONY: clean 11 | clean: 12 | rm -f $(TARGET) 13 | -------------------------------------------------------------------------------- /demo/matrix-temperature_and_humidity_sensor/Matrix-temp_humidity.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "libfahw.h" 4 | 5 | #define BUF_SIZE (64) 6 | #define DRIVER_MODULE "dht11" 7 | 8 | int main(int argc, char ** argv) 9 | { 10 | int ret = -1; 11 | int dhtTemp=0, dhtHdty=0, board; 12 | char modStr[BUF_SIZE]; 13 | int pin = GPIO_PIN(7); 14 | 15 | if ((board = boardInit()) < 0) { 16 | printf("Fail to init board\n"); 17 | return -1; 18 | } 19 | if (board == BOARD_NANOPC_T2 || board == BOARD_NANOPC_T3) 20 | pin = GPIO_PIN(15); 21 | 22 | sprintf(modStr, "modprobe %s gpio=%d", DRIVER_MODULE, pintoGPIO(pin)); 23 | system(modStr); 24 | if ((ret = dht11Read(DHT_HUMIDITY, &dhtHdty)) != -1) { 25 | printf("The humidity is %d\n", dhtHdty); 26 | } else { 27 | printf("Faided to get humidity\n"); 28 | } 29 | if ((ret = dht11Read(DHT_TEMP, &dhtTemp)) != -1) { 30 | printf("The temperature is %d\n", dhtTemp); 31 | } else { 32 | printf("Faided to get temperature\n"); 33 | } 34 | system("rmmod "DRIVER_MODULE); 35 | return ret; 36 | } 37 | -------------------------------------------------------------------------------- /demo/matrix-temperature_sensor/.gitignore: -------------------------------------------------------------------------------- 1 | /matrix-temp_sensor 2 | -------------------------------------------------------------------------------- /demo/matrix-temperature_sensor/Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | 3 | TARGET = $(patsubst Matrix-%.c,matrix-%, $(wildcard *.c)) 4 | SRC = $(wildcard *.c) 5 | SRC += $(wildcard *.h) 6 | 7 | $(TARGET):$(SRC) 8 | $(CC) -o $@ -L ../../lib/ -I ../../lib/includes/ $^ -lfahw -lm -Wall 9 | 10 | .PHONY: clean 11 | clean: 12 | rm -f $(TARGET) 13 | -------------------------------------------------------------------------------- /demo/matrix-temperature_sensor/Matrix-temp_sensor.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "libfahw.h" 5 | 6 | #define BUF_SIZE (64) 7 | #define TEMP_DRIVER_MODULE "w1-gpio" 8 | #define TEMP_GPIO_DRIVER_MODULE "w1-gpio-board" 9 | 10 | int main(int argc, char ** argv) 11 | { 12 | char temperature[BUF_SIZE], modStr[BUF_SIZE]; 13 | int board; 14 | int pin=GPIO_PIN(7); 15 | 16 | if ((board = boardInit()) < 0) { 17 | printf("Fail to init board\n"); 18 | return -1; 19 | } 20 | if (board == BOARD_NANOPC_T2 || board == BOARD_NANOPC_T3) 21 | pin = GPIO_PIN(15); 22 | 23 | if (argc == 2) 24 | pin = atoi(argv[1]); 25 | sprintf(modStr, "modprobe %s gpio=%d", TEMP_GPIO_DRIVER_MODULE, pintoGPIO(pin)); 26 | printf("%s\n", modStr); 27 | system(modStr); 28 | system("modprobe "TEMP_DRIVER_MODULE); 29 | sleep(1); 30 | memset(temperature, 0, BUF_SIZE); 31 | if (ds18b20Read(temperature) > 0) { 32 | printf("The temperature is %.3f C\n", atoi(temperature)/1000.0); 33 | } else { 34 | printf("Fail to get temperature\n"); 35 | } 36 | system("rmmod "TEMP_GPIO_DRIVER_MODULE); 37 | system("rmmod "TEMP_DRIVER_MODULE); 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /demo/matrix-ultrasonic_ranger/.gitignore: -------------------------------------------------------------------------------- 1 | /matrix-ultrasonic_ranger 2 | -------------------------------------------------------------------------------- /demo/matrix-ultrasonic_ranger/Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | 3 | TARGET = $(patsubst Matrix-%.c,matrix-%, $(wildcard *.c)) 4 | SRC = $(wildcard *.c) 5 | SRC += $(wildcard *.h) 6 | 7 | $(TARGET):$(SRC) 8 | $(CC) -o $@ -L ../../lib/ -I ../../lib/includes/ $^ -lfahw -lm -Wall 9 | 10 | .PHONY: clean 11 | clean: 12 | rm -f $(TARGET) 13 | -------------------------------------------------------------------------------- /demo/matrix-ultrasonic_ranger/Matrix-ultrasonic_ranger.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "libfahw.h" 4 | 5 | #define DRIVER_MODULE "matrix_hcsr04" 6 | 7 | int main(int argc, char ** argv) 8 | { 9 | int distance = -1; 10 | int pin = GPIO_PIN(7); 11 | int board; 12 | 13 | if ((board = boardInit()) < 0) { 14 | printf("Fail to init board\n"); 15 | return -1; 16 | } 17 | if (board == BOARD_NANOPC_T2 || board == BOARD_NANOPC_T3) 18 | pin = GPIO_PIN(15); 19 | 20 | system("modprobe "DRIVER_MODULE); 21 | if (Hcsr04Init(pin) == -1) { 22 | printf("Fail to init hcsr04\n"); 23 | goto err; 24 | } 25 | if (Hcsr04Read(&distance) != -1) { 26 | printf("The distance is %3d cm\n", distance); 27 | } else { 28 | printf("Faid to get distance\n"); 29 | } 30 | Hcsr04DeInit(); 31 | err: 32 | system("rmmod "DRIVER_MODULE); 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /demo/nanopi-status/.gitignore: -------------------------------------------------------------------------------- 1 | /nanopi-status 2 | -------------------------------------------------------------------------------- /demo/nanopi-status/bg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/friendlyarm/matrix/23d0d91ee66a20a1f51b824b2f773f12121750d8/demo/nanopi-status/bg.png -------------------------------------------------------------------------------- /demo/nanopi-status/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | export PATH=/usr/local/Trolltech/QtEmbedded-4.8.6-arm/bin/:$PATH 4 | 5 | [ -e /usr/local/Trolltech/QtEmbedded-4.8.6-arm ] || tar xvzf qte.tgz -C / 6 | qmake -project 7 | qmake -spec linux-g++ 8 | make 9 | -------------------------------------------------------------------------------- /demo/nanopi-status/display-debian.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | #set -x 3 | if [ $UID -ne 0 ] 4 | then 5 | echo "Please run as root" 6 | exit 1 7 | fi 8 | 9 | if [ $# -ne 1 ]; then 10 | echo "Usage:${0} /dev/fbx" 11 | exit 1 12 | fi 13 | FB_DEV=${1} 14 | TS_DEV="/dev/input/ts-ads7846" 15 | 16 | HARDWARE=`cat /proc/cpuinfo | grep Hardware` 17 | REVISION=`cat /proc/cpuinfo | grep Revision` 18 | HARDWARE=${HARDWARE#*: } 19 | REVISION=${REVISION#*: } 20 | echo "Hardware:${HARDWARE}" 21 | echo "Revision:${REVISION}" 22 | 23 | # clean up 24 | rm -rf ./Xorg.log 25 | if fuser ${TS_DEV} >/dev/null 2>&1; then 26 | # echo "clean up ts" 27 | killall -9 xinput_calibrator >/dev/null 2>&1 28 | sleep 1 29 | FUSER_RESULT=`fuser ${TS_DEV}` 30 | for word in ${FUSER_RESULT} 31 | do 32 | if [[ ${word} =~ [0-9]+ ]]; then 33 | kill ${word} 34 | fi 35 | done 36 | sleep 1 37 | fi 38 | if fuser ${FB_DEV} >/dev/null 2>&1; then 39 | # echo "clean up fb" 40 | FUSER_RESULT=`fuser ${FB_DEV}` 41 | for word in ${FUSER_RESULT} 42 | do 43 | if [[ ${word} =~ [0-9]+ ]]; then 44 | kill ${word} 45 | fi 46 | done 47 | killall -9 startx >/dev/null 2>&1 48 | sleep 1 49 | fi 50 | rmmod matrix_ads7846 fbtft_device >/dev/null 2>&1 51 | 52 | if [[ "x${FB_DEV}" = "x/dev/fb-st7735s" ]]; then 53 | if [[ "x${HARDWARE}" = "xsun8i" ]] && [[ "x${REVISION}" = "x0000" ]]; then # nanopi-m1/m1+/neo/air 54 | modprobe fbtft_device name=matrix-st7735s gpios=dc:17,reset:3,cs:201 55 | elif [[ "x${HARDWARE}" = "xNANOPI2" ]]; then # pi2/fire/m2 56 | modprobe fbtft_device name=matrix-st7735s gpios=dc:58,reset:63,cs:59 57 | elif [[ "x${HARDWARE}" = "xNANOPI3" ]]; then # m3 58 | modprobe fbtft_device name=matrix-st7735s gpios=dc:58,reset:63,cs:59 59 | else 60 | echo "Unsupported board" 61 | exit 1 62 | fi 63 | elif [[ "x${FB_DEV}" = "x/dev/fb-st7789s" ]]; then 64 | if [[ "x${HARDWARE}" = "xsun8i" ]] && [[ "x${REVISION}" = "x0000" ]]; then # nanopi-m1/m1+/neo/air 65 | modprobe fbtft_device name=matrix-st7789s gpios=dc:1,reset:203,cs:67 66 | elif [[ "x${HARDWARE}" = "xNANOPI2" ]] && [[ "x${REVISION}" = "x0000" ]]; then # nanopi 2 67 | modprobe fbtft_device name=matrix-st7789s gpios=dc:97,reset:60,cs:94 68 | elif [[ "x${HARDWARE}" = "xNANOPI2" ]] && [[ "x${REVISION}" = "x0002" ]]; then # nanopi s2 69 | modprobe fbtft_device name=matrix-st7789s gpios=dc:60,reset:104,cs:94 70 | elif [[ "x${HARDWARE}" = "xNANOPI2" ]] && [[ "x${REVISION}" = "x0004" ]]; then # nanopi fire 71 | modprobe fbtft_device name=matrix-st7789s gpios=dc:60,reset:104,cs:94 72 | elif [[ "x${HARDWARE}" = "xNANOPI2" ]] && [[ "x${REVISION}" = "x0005" ]]; then # nanopi m2 73 | modprobe fbtft_device name=matrix-st7789s gpios=dc:60,reset:104,cs:94 74 | elif [[ "x${HARDWARE}" = "xNANOPI3" ]] && [[ "x${REVISION}" = "x0005" ]]; then # nanopi m3 75 | modprobe fbtft_device name=matrix-st7789s gpios=dc:60,reset:104,cs:94 76 | else 77 | echo "Unsupported board" 78 | exit 1 79 | fi 80 | fi 81 | FRAMEBUFFER=${FB_DEV} startx >Xorg.log 2>&1 & 82 | 83 | if [[ "x${HARDWARE}" != "xsun8i" ]]; then # nanopi-m1 not support touchscreen yet 84 | let TIMEOUT=30 85 | while [ ${TIMEOUT} -gt 0 ]; do 86 | sleep 2 87 | let TIMEOUT-=2 88 | DISPLAY_ENV=`grep -oe "Xorg.[0-9]" ./Xorg.log` 89 | DISPLAY_ENV=${DISPLAY_ENV#*.} 90 | if [ ! -n "$DISPLAY_ENV" ] ;then 91 | printf "Waiting Desktop...%2d\r" ${TIMEOUT} 92 | else 93 | echo "" 94 | break 95 | fi 96 | done 97 | echo "DISPLAY_ENV=${DISPLAY_ENV}" 98 | 99 | modprobe matrix_ads7846 100 | DISPLAY=:${DISPLAY_ENV}.0 xinput_calibrator --device "ADS7846 Touchscreen" & 101 | fi 102 | -------------------------------------------------------------------------------- /demo/nanopi-status/main.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (C) Guangzhou FriendlyARM Computer Tech. Co., Ltd. 2 | // (http://www.friendlyarm.com) 3 | // 4 | // This program is free software; you can redistribute it and/or 5 | // modify it under the terms of the GNU General Public License 6 | // as published by the Free Software Foundation; either version 2 7 | // of the License, or (at your option) any later version. 8 | // 9 | // This program is distributed in the hope that it will be useful, 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | // GNU General Public License for more details. 13 | // 14 | // You should have received a copy of the GNU General Public License 15 | // along with this program; if not, you can access it online at 16 | // http://www.gnu.org/licenses/gpl-2.0.html. 17 | 18 | #include 19 | #include 20 | #include 21 | #include "mainwidget.h" 22 | int main(int argc, char **argv) { 23 | QApplication app (argc, argv); 24 | QWSServer::setCursorVisible(false); 25 | TMainWidget w; 26 | w.setWindowFlags(Qt::FramelessWindowHint); 27 | w.showFullScreen(); 28 | return app.exec(); 29 | } 30 | -------------------------------------------------------------------------------- /demo/nanopi-status/main.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | bg.png 4 | 5 | 6 | -------------------------------------------------------------------------------- /demo/nanopi-status/mainwidget.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (C) Guangzhou FriendlyARM Computer Tech. Co., Ltd. 2 | // (http://www.friendlyarm.com) 3 | // 4 | // This program is free software; you can redistribute it and/or 5 | // modify it under the terms of the GNU General Public License 6 | // as published by the Free Software Foundation; either version 2 7 | // of the License, or (at your option) any later version. 8 | // 9 | // This program is distributed in the hope that it will be useful, 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | // GNU General Public License for more details. 13 | // 14 | // You should have received a copy of the GNU General Public License 15 | // along with this program; if not, you can access it online at 16 | // http://www.gnu.org/licenses/gpl-2.0.html. 17 | 18 | #include "mainwidget.h" 19 | #include "util.h" 20 | #include "sys/sysinfo.h" 21 | 22 | TMainWidget::TMainWidget(QWidget *parent) : 23 | QWidget(parent),bg(QPixmap(":/bg.png")) 24 | { 25 | mpKeepAliveTimer = new QTimer(); 26 | mpKeepAliveTimer->setSingleShot(false); 27 | QObject::connect(mpKeepAliveTimer, SIGNAL(timeout()), this, SLOT(onKeepAlive())); 28 | mpKeepAliveTimer->start(1500); 29 | 30 | qtdemoButton = new QPushButton("Start Qt Demo >>", this); 31 | connect(qtdemoButton, SIGNAL(clicked()), this, SLOT(qtdemoButtonClicked())); 32 | } 33 | 34 | void TMainWidget::qtdemoButtonClicked() { 35 | system("/opt/QtE-Demo/run-fluidlauncher.sh&"); 36 | exit(0); 37 | } 38 | 39 | void TMainWidget::resizeEvent(QResizeEvent*) { 40 | const int buttonWidth = width()/4; 41 | const int buttonHeight = height()/12; 42 | qtdemoButton->setGeometry(width()-buttonWidth-10,height()-5-buttonHeight,buttonWidth, buttonHeight); 43 | 44 | if (width() < 800) { 45 | qtdemoButton->hide(); 46 | } 47 | } 48 | 49 | void TMainWidget::onKeepAlive() { 50 | static char ipStr[50]; 51 | memset(ipStr, 0, sizeof(ipStr)); 52 | int ret = Util::getIPAddress("eth0", ipStr, 49); 53 | if (ret == 0) { 54 | eth0IP = QString(ipStr); 55 | } else { 56 | eth0IP = "0.0.0.0"; 57 | } 58 | 59 | memset(ipStr, 0, sizeof(ipStr)); 60 | ret = Util::getIPAddress("wlan0", ipStr, 49); 61 | if (ret == 0) { 62 | wlan0IP = QString(ipStr); 63 | } else { 64 | wlan0IP = "0.0.0.0"; 65 | } 66 | 67 | struct sysinfo sys_info; 68 | if (sysinfo(&sys_info) == 0) { 69 | qint32 totalmem=(qint32)(sys_info.totalram/1048576); 70 | qint32 freemem=(qint32)(sys_info.freeram/1048576); // divide by 1024*1024 = 1048576 71 | // float f = ((sys_info.totalram-sys_info.freeram)*1.0/sys_info.totalram)*100; 72 | // memInfo = QString("%1%,F%2MB").arg(int(f)).arg(freemem); 73 | memInfo = QString("%1/%2 MB").arg(totalmem-freemem).arg(totalmem); 74 | } 75 | 76 | if (Util::GetBoardType().toUpper().startsWith("NANOPC-T") || Util::GetBoardType().toUpper() == "NANOPI-M3") { 77 | currentCPUTemp = Util::readFile("/sys/class/hwmon/hwmon0/device/temp_label").simplified(); 78 | maxCPUTemp = Util::readFile("/sys/class/hwmon/hwmon0/device/temp_max").simplified(); 79 | } else if (Util::GetBoardType().toUpper() == "NANOPI-M1") { 80 | currentCPUTemp = Util::readFile("/sys/class/thermal/thermal_zone0/temp").simplified(); 81 | maxCPUTemp = Util::readFile("/sys/class/thermal/thermal_zone0/temp").simplified(); 82 | } else if (Util::GetBoardType().toUpper().startsWith("RASPBERRYPI3")) { 83 | QString str; 84 | bool ok=false; 85 | float _currentCPUTemp = Util::readFile("/sys/class/thermal/thermal_zone0/temp").simplified().toInt(&ok)/1000.0; 86 | currentCPUTemp = str.sprintf("%.1f",_currentCPUTemp); 87 | maxCPUTemp = currentCPUTemp; 88 | } 89 | 90 | QString contents = Util::readFile("/proc/loadavg").simplified(); 91 | QStringList values = contents.split(" "); 92 | int vCount = 3; 93 | loadAvg = ""; 94 | foreach (const QString &v, values) { 95 | QString str = v.simplified(); 96 | if (!str.isEmpty()) { 97 | if (!loadAvg.isEmpty()) { 98 | loadAvg += "/"; 99 | } 100 | bool ok = false; 101 | float f = str.toFloat(&ok); 102 | if (ok) { 103 | loadAvg += QString("%1").arg(int(f)); 104 | } 105 | vCount--; 106 | if (vCount <= 0) { 107 | break; 108 | } 109 | } 110 | } 111 | 112 | 113 | QString fileName = "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq"; 114 | QString str = Util::readFile(fileName).simplified(); 115 | bool ok = false; 116 | int freq = str.toInt(&ok,10); 117 | freqStr = ""; 118 | if (ok) { 119 | QString str; 120 | if (freq > 1000000) { 121 | freqStr = str.sprintf("%.1fG",freq*1.0/1000000); 122 | } else if (freq > 1000) { 123 | freqStr = str.sprintf("%d",freq/1000); 124 | } else { 125 | freqStr = str.sprintf("%d",freq); 126 | } 127 | } 128 | update(); 129 | } 130 | 131 | void TMainWidget::paintEvent(QPaintEvent *) 132 | { 133 | QPainter p(this); 134 | 135 | int space = 3; 136 | int itemHeight = 20; 137 | p.fillRect(0,0,width(),height(),QBrush(QColor(0,0,0))); 138 | p.drawPixmap(0, 0, width(), height(), bg); 139 | 140 | QString ip = eth0IP; 141 | if (ip == "0.0.0.0") { 142 | ip = wlan0IP; 143 | } 144 | 145 | p.setPen(QPen(QColor(255,255,255))); 146 | if (Util::GetBoardType().toUpper() == "NANOPI-M1") 147 | p.drawText(space,itemHeight*0,width()-space*2,itemHeight,Qt::AlignLeft | Qt::AlignVCenter,QString("CPU: %1MHz %2C").arg(freqStr).arg(currentCPUTemp)); 148 | else 149 | p.drawText(space,itemHeight*0,width()-space*2,itemHeight,Qt::AlignLeft | Qt::AlignVCenter,QString("CPU: %1MHz").arg(freqStr)); 150 | p.drawText(space,itemHeight*1,width()-space*2,itemHeight,Qt::AlignLeft | Qt::AlignVCenter,QString("Mem: %1").arg(memInfo)); 151 | p.drawText(space,itemHeight*2,width()-space*2,itemHeight,Qt::AlignLeft | Qt::AlignVCenter,QString("LoadAvg: %1").arg(loadAvg)); 152 | p.drawText(space,itemHeight*3,width()-space*2,itemHeight,Qt::AlignLeft | Qt::AlignVCenter,QString("IP: %1").arg(ip)); 153 | 154 | if (width() >= 800) { 155 | p.setPen(QPen(QColor(192,192,192))); 156 | // const int buttonWidth = width()/4; 157 | const int buttonHeight = height()/12; 158 | p.drawText(10,height()-5-buttonHeight-5-buttonHeight, "This is an open source project, you can find the source code in /opt/QtE-Demo."); 159 | } 160 | } 161 | 162 | -------------------------------------------------------------------------------- /demo/nanopi-status/mainwidget.h: -------------------------------------------------------------------------------- 1 | // Copyright (C) Guangzhou FriendlyARM Computer Tech. Co., Ltd. 2 | // (http://www.friendlyarm.com) 3 | // 4 | // This program is free software; you can redistribute it and/or 5 | // modify it under the terms of the GNU General Public License 6 | // as published by the Free Software Foundation; either version 2 7 | // of the License, or (at your option) any later version. 8 | // 9 | // This program is distributed in the hope that it will be useful, 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | // GNU General Public License for more details. 13 | // 14 | // You should have received a copy of the GNU General Public License 15 | // along with this program; if not, you can access it online at 16 | // http://www.gnu.org/licenses/gpl-2.0.html. 17 | 18 | #ifndef WIDGET_H 19 | #define WIDGET_H 20 | 21 | #include 22 | class TMainWidget : public QWidget 23 | { 24 | Q_OBJECT 25 | 26 | public: 27 | explicit TMainWidget(QWidget *parent = 0); 28 | ~TMainWidget() {} 29 | private slots: 30 | void onKeepAlive(); 31 | void qtdemoButtonClicked(); 32 | private: 33 | void resizeEvent(QResizeEvent*); 34 | void paintEvent(QPaintEvent *); 35 | private: 36 | QTimer* mpKeepAliveTimer; 37 | QString loadAvg; 38 | QString currentCPUTemp; 39 | QString maxCPUTemp; 40 | QString freqStr; 41 | QString memInfo; 42 | QPixmap bg; 43 | QString eth0IP; 44 | QString wlan0IP; 45 | QPushButton* qtdemoButton; 46 | }; 47 | 48 | #endif // WIDGET_H 49 | -------------------------------------------------------------------------------- /demo/nanopi-status/qte.tgz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/friendlyarm/matrix/23d0d91ee66a20a1f51b824b2f773f12121750d8/demo/nanopi-status/qte.tgz -------------------------------------------------------------------------------- /demo/nanopi-status/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [ $UID -ne 0 ] 4 | then 5 | echo "Please run as root." 6 | exit 1 7 | fi 8 | 9 | if [ $# -ne 1 ]; then 10 | echo "Usage:${0} /dev/fbx" 11 | exit 1 12 | fi 13 | FB_DEV=${1} 14 | 15 | HARDWARE=`cat /proc/cpuinfo | grep Hardware` 16 | REVISION=`cat /proc/cpuinfo | grep Revision` 17 | HARDWARE=${HARDWARE#*: } 18 | REVISION=${REVISION#*: } 19 | echo "Hardware:${HARDWARE}" 20 | echo "Revision:${REVISION}" 21 | 22 | if [[ "x${FB_DEV}" = "x/dev/fb-st7735s" ]]; then 23 | if [[ "x${HARDWARE}" = "xsun8i" ]] && [[ "x${REVISION}" = "x0000" ]]; then # nanopi-m1/m1+/neo/air 24 | modprobe fbtft_device name=matrix-st7735s gpios=dc:17,reset:3,cs:201 25 | elif [[ "x${HARDWARE}" = "xNANOPI2" ]]; then # pi2/fire/m2 26 | modprobe fbtft_device name=matrix-st7735s gpios=dc:58,reset:63,cs:59 27 | elif [[ "x${HARDWARE}" = "xNANOPI3" ]]; then # m3 28 | modprobe fbtft_device name=matrix-st7735s gpios=dc:58,reset:63,cs:59 29 | else 30 | echo "Unsupported board" 31 | exit 1 32 | fi 33 | export QWS_DISPLAY=Transformed:Rot90:Linuxfb:${FB_DEV}:enable=1:mWidth90:mmHeight45:0 34 | elif [[ "x${FB_DEV}" = "x/dev/fb-st7789s" ]]; then 35 | if [[ "x${HARDWARE}" = "xsun8i" ]] && [[ "x${REVISION}" = "x0000" ]]; then # nanopi-m1/m1+/neo/air 36 | modprobe fbtft_device name=matrix-st7789s gpios=dc:1,reset:203,cs:67 37 | elif [[ "x${HARDWARE}" = "xNANOPI2" ]] && [[ "x${REVISION}" = "x0000" ]]; then # nanopi 2 38 | modprobe fbtft_device name=matrix-st7789s gpios=dc:97,reset:60,cs:94 39 | elif [[ "x${HARDWARE}" = "xNANOPI2" ]] && [[ "x${REVISION}" = "x0004" ]]; then # nanopi fire 40 | modprobe fbtft_device name=matrix-st7789s gpios=dc:60,reset:104,cs:94 41 | elif [[ "x${HARDWARE}" = "xNANOPI2" ]] && [[ "x${REVISION}" = "x0005" ]]; then # nanopi m2 42 | modprobe fbtft_device name=matrix-st7789s gpios=dc:60,reset:104,cs:94 43 | elif [[ "x${HARDWARE}" = "xNANOPI3" ]] && [[ "x${REVISION}" = "x0005" ]]; then # nanopi m3 44 | modprobe fbtft_device name=matrix-st7789s gpios=dc:60,reset:104,cs:94 45 | else 46 | echo "Unsupported board" 47 | exit 1 48 | fi 49 | export QWS_DISPLAY=Transformed:Rot90:Linuxfb:${FB_DEV}:enable=1:mWidth120:mmHeight92:0 50 | else 51 | # 16:9 52 | export QWS_DISPLAY=Transformed:Rot0:Linuxfb:${FB_DEV}:enable=1:mWidth400:mmHeight225:0 53 | fi 54 | 55 | export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH 56 | export PATH=/bin:/sbin:/usr/bin/:/usr/sbin:/usr/local/bin 57 | export QWS_MOUSE_PROTO="MouseMan:/dev/input/mice" 58 | export QWS_KEYBOARD=TTY:/dev/tty1 59 | 60 | killall nanopi-status > /dev/null 2>&1 61 | ./nanopi-status -qws & 62 | -------------------------------------------------------------------------------- /demo/nanopi-status/util.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (C) Guangzhou FriendlyARM Computer Tech. Co., Ltd. 2 | // (http://www.friendlyarm.com) 3 | // 4 | // This program is free software; you can redistribute it and/or 5 | // modify it under the terms of the GNU General Public License 6 | // as published by the Free Software Foundation; either version 2 7 | // of the License, or (at your option) any later version. 8 | // 9 | // This program is distributed in the hope that it will be useful, 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | // GNU General Public License for more details. 13 | // 14 | // You should have received a copy of the GNU General Public License 15 | // along with this program; if not, you can access it online at 16 | // http://www.gnu.org/licenses/gpl-2.0.html. 17 | 18 | #include "util.h" 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | #include 41 | 42 | int Util::getIPAddress(const char* ifaceName, char* ip, size_t maxLen) { 43 | int s; 44 | struct ifconf conf; 45 | struct ifreq *ifr; 46 | char buff[BUFSIZ]; 47 | int num; 48 | int i; 49 | 50 | s = socket(PF_INET, SOCK_DGRAM, 0); 51 | if (s < 0) { 52 | return -1; 53 | } 54 | 55 | conf.ifc_len = BUFSIZ; 56 | conf.ifc_buf = buff; 57 | 58 | ioctl(s, SIOCGIFCONF, &conf); 59 | num = conf.ifc_len / sizeof(struct ifreq); 60 | ifr = conf.ifc_req; 61 | 62 | for(i=0;i < num;i++) { 63 | struct sockaddr_in *sin = (struct sockaddr_in *)(&ifr->ifr_addr); 64 | 65 | if (ioctl(s, SIOCGIFFLAGS, ifr) < 0) { 66 | ::close(s); 67 | return -1; 68 | } 69 | 70 | if(((ifr->ifr_flags & IFF_LOOPBACK) == 0) && (ifr->ifr_flags & IFF_UP)) 71 | { 72 | if (strcmp(ifaceName, ifr->ifr_name) == 0) { 73 | strncpy(ip, inet_ntoa(sin->sin_addr), maxLen ); 74 | ::close(s); 75 | return 0; 76 | } 77 | } 78 | ifr++; 79 | } 80 | 81 | ::close(s); 82 | return -1; 83 | } 84 | 85 | QString Util::seconds_to_DHMS(unsigned long duration) 86 | { 87 | int seconds = (int) (duration % 60); 88 | duration /= 60; 89 | int minutes = (int) (duration % 60); 90 | duration /= 60; 91 | int hours = (int) (duration % 24); 92 | int days = (int) (duration / 24); 93 | if((hours == 0)&&(days == 0)) 94 | return QString::fromUtf8("%1分%2秒").arg(minutes).arg(seconds); 95 | if (days == 0) 96 | return QString::fromUtf8("%1时%2分%3秒").arg(hours).arg(minutes).arg(seconds); 97 | return QString::fromUtf8("%1天%2时%3分%4秒").arg(days).arg(hours).arg(minutes).arg(seconds); 98 | } 99 | 100 | QString Util::seconds_to_DHMS_US(unsigned long duration) 101 | { 102 | int seconds = (int) (duration % 60); 103 | duration /= 60; 104 | int minutes = (int) (duration % 60); 105 | duration /= 60; 106 | int hours = (int) (duration % 24); 107 | int days = (int) (duration / 24); 108 | if((hours == 0)&&(days == 0)) 109 | return QString::fromUtf8("%1m%2s").arg(minutes).arg(seconds); 110 | if (days == 0) 111 | return QString::fromUtf8("%1h%2m%3s").arg(hours).arg(minutes).arg(seconds); 112 | return QString::fromUtf8("%1d%2h%3m").arg(days).arg(hours).arg(minutes); 113 | } 114 | 115 | QString Util::GetBoardType() { 116 | QString contents = readFile("/proc/cpuinfo"); 117 | QStringList lines = contents.split("\n"); 118 | QString hardware; 119 | QString revision; 120 | 121 | foreach (const QString &line, lines) { 122 | QStringList keyValue = line.split(":"); 123 | if (keyValue.count() >= 2) { 124 | if (keyValue[0].simplified().toLower()=="hardware") { 125 | hardware =keyValue[1].simplified().toLower(); 126 | } 127 | if (keyValue[0].simplified().toLower()=="revision") { 128 | revision =keyValue[1].simplified().toLower(); 129 | } 130 | } 131 | } 132 | 133 | if (hardware.length()>0 && revision.length()>0) { 134 | bool bStatus = false; 135 | uint iRevision = revision.toUInt(&bStatus,16); 136 | if (bStatus) { 137 | if (hardware == "nanopi2") { 138 | if (iRevision == 0) { 139 | return "NanoPi2"; 140 | } else if (iRevision == 1) { 141 | return "NanoPC-T2"; 142 | } else if (iRevision == 3) { 143 | return "Smart4418"; 144 | } else if (iRevision == 4) { 145 | return "NanoPi2-Fire"; 146 | } else if (iRevision == 5) { 147 | return "NanoPi-M2"; 148 | } else if (iRevision == 6) { 149 | return "NanoPi-M2X"; 150 | } else { 151 | return "Unknow4418"; 152 | } 153 | } else if (hardware == "nanopi3") { 154 | if (iRevision == 1) { 155 | return "NanoPC-T3"; 156 | } else if (iRevision == 3) { 157 | return "Smart6818"; 158 | } else if (iRevision == 7) { 159 | return "NanoPi-M3"; 160 | } else { 161 | return "Unknow6818"; 162 | } 163 | 164 | } else if (hardware == "sun8i") { 165 | if (iRevision == 0) { 166 | return "NanoPi-M1"; 167 | } else { 168 | return "UnknowH3"; 169 | } 170 | 171 | } else if (hardware == "mini2451") { 172 | if (iRevision == 0) { 173 | return "NanoPi"; 174 | } else { 175 | return "Unknow2451"; 176 | } 177 | } else if (hardware=="bcm2709") { 178 | return "RaspberryPi3"; 179 | } else if (hardware=="bcm2835") { 180 | return "RaspberryPi/Zero/A/B/A+/B+"; 181 | } else if (hardware=="bcm2836") { 182 | return "RaspberryPi2"; 183 | } else { 184 | return hardware; 185 | } 186 | } 187 | } 188 | return "Unknow"; 189 | } 190 | 191 | QString Util::readFile (const QString& filename) { 192 | QFile file(filename); 193 | if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { 194 | QTextStream stream(&file); 195 | QString result = stream.readAll(); 196 | file.close(); 197 | return result; 198 | } else { 199 | qDebug() << "open file error: " << filename; 200 | } 201 | return ""; 202 | } 203 | 204 | int Util::GetMemInfoMB() { 205 | QProcess p; 206 | p.start("awk", QStringList() << "/MemTotal/ { print $2 }" << "/proc/meminfo"); 207 | p.waitForFinished(); 208 | QString memory = p.readAllStandardOutput(); 209 | int result = memory.toLong() / 1024; 210 | p.close(); 211 | return result; 212 | } 213 | -------------------------------------------------------------------------------- /demo/nanopi-status/util.h: -------------------------------------------------------------------------------- 1 | // Copyright (C) Guangzhou FriendlyARM Computer Tech. Co., Ltd. 2 | // (http://www.friendlyarm.com) 3 | // 4 | // This program is free software; you can redistribute it and/or 5 | // modify it under the terms of the GNU General Public License 6 | // as published by the Free Software Foundation; either version 2 7 | // of the License, or (at your option) any later version. 8 | // 9 | // This program is distributed in the hope that it will be useful, 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | // GNU General Public License for more details. 13 | // 14 | // You should have received a copy of the GNU General Public License 15 | // along with this program; if not, you can access it online at 16 | // http://www.gnu.org/licenses/gpl-2.0.html. 17 | 18 | #ifndef UTIL_H 19 | #define UTIL_H 20 | 21 | #include 22 | 23 | class Util 24 | { 25 | public: 26 | static QString GetBoardType(); 27 | static QString readFile (const QString& filename); 28 | static QString seconds_to_DHMS(unsigned long duration); 29 | static int GetMemInfoMB(); 30 | static QString seconds_to_DHMS_US(unsigned long duration); 31 | static int getIPAddress(const char* ifaceName, char* ip, size_t maxLen); 32 | }; 33 | #endif // UTIL_H 34 | -------------------------------------------------------------------------------- /lib/Makefile: -------------------------------------------------------------------------------- 1 | CROSS_COMPILE ?= 2 | 3 | AS = $(CROSS_COMPILE)as 4 | LD = $(CROSS_COMPILE)ld 5 | CC = $(CROSS_COMPILE)gcc 6 | AR = $(CROSS_COMPILE)ar 7 | NM = $(CROSS_COMPILE)nm 8 | STRIP = $(CROSS_COMPILE)strip 9 | OBJCOPY = $(CROSS_COMPILE)objcopy 10 | OBJDUMP = $(CROSS_COMPILE)objdump 11 | RANLIB = $(CROSS_COMPILE)ranlib 12 | 13 | CFLAGS = -Wall -Os 14 | LDFLAGS = -lpthread 15 | LIB = libfahw.so 16 | SLIB = libfahw.a 17 | CFLAGS += -fPIC 18 | 19 | OBJDIR = .obj 20 | DEPDIR = .deps 21 | LIBSRC = $(wildcard *.c) 22 | LIBOBJ = $(addprefix $(OBJDIR)/, $(addsuffix .o, $(basename $(LIBSRC)))) 23 | DEPS = $(patsubst %.c,$(DEPDIR)/%.Po,$(LIBSRC)) 24 | 25 | all: $(LIB) $(SLIB) 26 | lib: $(LIB) 27 | slib: $(SLIB) 28 | $(OBJDIR)/%.o: %.c 29 | @test -d $(DEPDIR) || mkdir -p $(DEPDIR) 30 | @test -d $(OBJDIR) || mkdir -p $(OBJDIR) 31 | $(CC) $(CFLAGS) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< -I ./includes 32 | @mv $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po 33 | 34 | -include $(DEPS) 35 | $(LIB): $(LIBOBJ) 36 | $(CC) $(LDFLAGS) -shared -o $@ $^ 37 | $(SLIB): $(LIBOBJ) 38 | $(AR) rcu $@ $^ 39 | $(RANLIB) $@ 40 | 41 | install: $(LIB) 42 | install ./includes/libfahw*.h /usr/local/include 43 | install --mode=644 libfahw.so /usr/local/lib 44 | install --mode=644 libfahw.a /usr/local/lib 45 | ldconfig 46 | clean: 47 | rm -rf $(LIB) $(LIBOBJ) $(SLIB) $(DEPS) $(OBJDIR) $(DEPDIR) 48 | -------------------------------------------------------------------------------- /lib/adxl34x.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "common.h" 6 | #include "libfahw-filectl.h" 7 | #include "libfahw-adxl34x.h" 8 | 9 | EXPORT int adxl34xRead(char *position) 10 | { 11 | clearLastError(); 12 | int ret; 13 | int maxLength = sizeof("(-255, -255, -255)"); 14 | DIR *d; 15 | struct dirent *de; 16 | 17 | if (!(d = opendir(ADXL34X_SYS_PATH))) { 18 | setLastError("Fail to opendir %s", ADXL34X_SYS_PATH); 19 | return 0; 20 | } 21 | char adxFile[FILE_PATH_LENGTH]; 22 | while ((de = readdir(d))) { 23 | if (de->d_name[0] == '.') 24 | continue; 25 | 26 | sprintf(adxFile, "%s%s/position", ADXL34X_SYS_PATH, de->d_name); 27 | 28 | if (access(adxFile, F_OK) != -1) { 29 | break; 30 | } 31 | } 32 | closedir(d); 33 | 34 | memset(position, 0, maxLength); 35 | if ((ret = readValueFromFile(adxFile, position, maxLength)) == -1) { 36 | setLastError("Fail to read adxl34x"); 37 | return -1; 38 | } 39 | ret = strlen(position) + 1; 40 | return ret; 41 | } 42 | -------------------------------------------------------------------------------- /lib/bmp180.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "common.h" 6 | #include "libfahw-bmp180.h" 7 | 8 | EXPORT int bmp180Read(int type, int *data) 9 | { 10 | clearLastError(); 11 | int ret = -1; 12 | DIR *d; 13 | struct dirent *de; 14 | 15 | if (!(d = opendir(BMP180_SYS_PATH))) { 16 | setLastError("Fail to opendir %s", BMP180_SYS_PATH); 17 | return 0; 18 | } 19 | char bmpFile[FILE_PATH_LENGTH]; 20 | while ((de = readdir(d))) { 21 | if (de->d_name[0] == '.') 22 | continue; 23 | 24 | if (type == BMP180_TEMP) 25 | sprintf(bmpFile, "%s%s/temp0_input", BMP180_SYS_PATH, de->d_name); 26 | else if(type == BMP180_PRESSURE) 27 | sprintf(bmpFile, "%s%s/pressure0_input", BMP180_SYS_PATH, de->d_name); 28 | 29 | if (access(bmpFile, F_OK) != -1) { 30 | break; 31 | } 32 | } 33 | closedir(d); 34 | 35 | switch(type) { 36 | case BMP180_TEMP: 37 | ret = readIntValueFromFile(bmpFile); 38 | break; 39 | case BMP180_PRESSURE: 40 | ret = readIntValueFromFile(bmpFile); 41 | break; 42 | default: 43 | setLastError("Unsupport bmp180 data type %d", type); 44 | break; 45 | } 46 | if (ret != -1) { 47 | *data = ret; 48 | ret = 0; 49 | } else { 50 | setLastError("Invalid bmp180 data"); 51 | } 52 | 53 | return ret; 54 | } 55 | -------------------------------------------------------------------------------- /lib/common.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | #include "common.h" 3 | #include "libfahw.h" 4 | 5 | int writeValueToFile(char* fileName, char* buff) 6 | { 7 | clearLastError(); 8 | int ret; 9 | FILE *fp = fopen(fileName, "w"); 10 | if (fp == NULL) { 11 | setLastError("Unable to open file %s", fileName); 12 | ret = -1; 13 | } else { 14 | ret = fwrite(buff, strlen(buff), 1, fp); 15 | fclose(fp); 16 | } 17 | return ret; 18 | } 19 | 20 | 21 | int writeIntValueToFile(char* fileName, int value) 22 | { 23 | clearLastError(); 24 | char buff[50]; 25 | sprintf(buff, "%d", value); 26 | return writeValueToFile(fileName, buff); 27 | } 28 | 29 | 30 | int readValueFromFile(char* fileName, char* buff, int len) 31 | { 32 | clearLastError(); 33 | int ret = -1; 34 | FILE *fp = fopen(fileName,"r"); 35 | if (fp == NULL) { 36 | setLastError("Unable to open file %s",fileName); 37 | return -1; 38 | } else { 39 | if (fread(buff, sizeof(char), len, fp)>0) { 40 | ret = 0; 41 | } 42 | } 43 | fclose(fp); 44 | return ret; 45 | } 46 | 47 | 48 | int readIntValueFromFile(char* fileName) 49 | { 50 | clearLastError(); 51 | char buff[255]; 52 | memset(buff, 0, sizeof(buff)); 53 | int ret = readValueFromFile(fileName, buff, sizeof(buff)-1); 54 | if (ret == 0) { 55 | return atoi(buff); 56 | } 57 | return ret; 58 | } 59 | 60 | static int getBoardInfo(int length, char info[][32]) 61 | { 62 | clearLastError(); 63 | int n,i,j; 64 | char lineUntrim[1024], line[1024],*p; 65 | FILE *f; 66 | int ret = 0; 67 | 68 | f = fopen("/sys/devices/platform/board/info", "r"); 69 | if (!f) 70 | f = fopen("/proc/cpuinfo", "r"); 71 | 72 | if (!f) { 73 | setLastError("Unable to get board info"); 74 | return ret; 75 | } 76 | 77 | while (!feof(f)) { 78 | if(!fgets(lineUntrim, sizeof(lineUntrim), f)) { 79 | return ret; 80 | } else { 81 | j=0; 82 | for(i=0; i0) { 90 | line[--n] = '\0'; 91 | if ( (p = strtok(line, ":")) ) { 92 | if (strncasecmp(p, "Hardware", strlen("Hardware")) == 0) { 93 | if ( (p = strtok(0, ":")) ) { 94 | printf("Hardware:%s\n", p); 95 | memset(info[0], 0, 32); 96 | strncpy(info[0], p, 32-1); 97 | ret++; 98 | } 99 | } else if (strncasecmp(p, "Revision", strlen("Revision")) == 0) { 100 | if ( (p = strtok(0, ":")) ) { 101 | printf("Revision:%s\n", p); 102 | memset(info[1], 0, 32); 103 | strncpy(info[1], p, 32-1); 104 | ret++; 105 | } 106 | } 107 | } 108 | } 109 | } 110 | } 111 | fclose(f); 112 | return ret; 113 | } 114 | 115 | static int getBoardType() 116 | { 117 | clearLastError(); 118 | int ret = -1; 119 | char info[2][32]; 120 | int boardType = getBoardInfo(2, info); 121 | if (boardType < 1) { 122 | return ret; 123 | } 124 | if (strncasecmp(info[0], "MINI6410", 8)==0) { 125 | ret = BOARD_MINI6410; 126 | } else if (strncasecmp(info[0], "MINI210", 7)==0) { 127 | ret = BOARD_MINI210; 128 | } else if (strncasecmp(info[0], "TINY4412", 8)==0) { 129 | ret = BOARD_TINY4412; 130 | } else if (strncasecmp(info[0], "sun8i", 5)==0) { 131 | ret = BOARD_NANOPI_M1; 132 | } else if (strncasecmp(info[0], "NANOPI2", 7)==0) { 133 | if (strncasecmp(info[1], "0000", 4)==0) { 134 | ret = BOARD_NANOPI_2; 135 | } else if (strncasecmp(info[1], "0004", 4)==0) { 136 | ret = BOARD_NANOPI_2_FIRE; 137 | } else if (strncasecmp(info[1], "0005", 4)==0) { 138 | ret = BOARD_NANOPI_M2; 139 | } else if (strncasecmp(info[1], "0001", 4)==0) { 140 | ret = BOARD_NANOPC_T2; 141 | } 142 | } else if(strncasecmp(info[0], "NANOPI3", 7)==0) { 143 | if (strncasecmp(info[1], "0007", 4)==0) { 144 | ret = BOARD_NANOPI_M3; 145 | } else if (strncasecmp(info[1], "0001", 4)==0) { 146 | ret = BOARD_NANOPC_T3; 147 | } 148 | } 149 | 150 | return ret; 151 | } 152 | 153 | EXPORT int boardInit() 154 | { 155 | clearLastError(); 156 | int board = getBoardType(); 157 | printf("BoardType:%d\n", board); 158 | if (initPinGPIO(board)) 159 | setLastError("Fail to initPinGPIO"); 160 | if (initPwmGPIO(board)) 161 | setLastError("Fail to initPwmGPIO"); 162 | return board; 163 | } 164 | 165 | static char FAHWLastError[255]; 166 | static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; 167 | EXPORT void clearLastError() 168 | { 169 | pthread_mutex_lock(&mutex); 170 | memset(FAHWLastError, 0, sizeof(FAHWLastError)); 171 | pthread_mutex_unlock(&mutex); 172 | } 173 | 174 | EXPORT void setLastError(const char *fmt, ...) 175 | { 176 | va_list vl; 177 | va_start(vl, fmt); 178 | char* errMsg; 179 | if (vasprintf(&errMsg, fmt, vl) > 0) { 180 | DEBUG("%s\n", errMsg); 181 | pthread_mutex_lock(&mutex); 182 | strncpy(FAHWLastError, errMsg, (sizeof(FAHWLastError)-1)); 183 | pthread_mutex_unlock(&mutex); 184 | free(errMsg); 185 | } 186 | va_end(vl); 187 | } 188 | 189 | EXPORT int getLastError(char* dest, int maxlen) 190 | { 191 | if (dest == NULL || maxlen<=0) { 192 | return -1; 193 | } 194 | pthread_mutex_lock(&mutex); 195 | int ret = strlen(FAHWLastError); 196 | strncpy(dest, FAHWLastError, maxlen); 197 | pthread_mutex_unlock(&mutex); 198 | 199 | return ret; 200 | } 201 | 202 | int Test() 203 | { 204 | setLastError("TestFunction"); 205 | return 999; 206 | } 207 | -------------------------------------------------------------------------------- /lib/common.h: -------------------------------------------------------------------------------- 1 | #ifndef _COMMON_H_ 2 | #define _COMMON_H_ 3 | 4 | #define __DEBUG 5 | 6 | #ifdef __DEBUG 7 | #define DEBUG(format, args...) \ 8 | printf("FAHW-Lib: " format, ## args) 9 | #else 10 | #define DEBUG(format, args...) 11 | #endif 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | extern int boardInit(); 27 | extern void clearLastError(); 28 | extern void setLastError(const char *fmt, ...); 29 | extern int getLastError(char* dest, int maxlen); 30 | 31 | #define EXPORT 32 | extern int writeValueToFile(char* fileName, char* buff); 33 | extern int writeIntValueToFile(char* fileName, int value); 34 | extern int readValueFromFile(char* fileName, char* buff, int len); 35 | extern int readIntValueFromFile(char* fileName); 36 | 37 | #define FILE_PATH_LENGTH (128) 38 | #define BOARD_MINI6410 (6410) 39 | #define BOARD_MINI210 (210) 40 | #define BOARD_TINY4412 (4412) 41 | #define BOARD_NANOPI_M1 (68330) //'H3' 42 | #define BOARD_NANOPI_2 (44180) 43 | #define BOARD_NANOPI_2_FIRE (44184) 44 | #define BOARD_NANOPI_M2 (44185) 45 | #define BOARD_NANOPC_T2 (44181) 46 | #define BOARD_NANOPI_M3 (68187) 47 | #define BOARD_NANOPC_T3 (68181) 48 | #endif 49 | -------------------------------------------------------------------------------- /lib/config.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/friendlyarm/matrix/23d0d91ee66a20a1f51b824b2f773f12121750d8/lib/config.c -------------------------------------------------------------------------------- /lib/config.h: -------------------------------------------------------------------------------- 1 | #ifndef _HEGANG_CONFIG_H_ 2 | #define _HEGANG_CONFIG_H_ 3 | 4 | 5 | /* 6 | * Set line length in configuration files 7 | */ 8 | #define LINE_LEN 128 9 | 10 | 11 | /* 12 | * Define return error code value 13 | */ 14 | #define ERR_NONE 0 /* read configuration file successfully */ 15 | #define ERR_NOFILE 2 /* not find or open configuration file */ 16 | #define ERR_READFILE 3 /* error occur in reading configuration file */ 17 | #define ERR_FORMAT 4 /* invalid format in configuration file */ 18 | #define ERR_NOTHING 5 /* not find section or key name in configuration file */ 19 | 20 | 21 | /* 22 | * Read the value of key name in string form 23 | */ 24 | int getconfigstr(const char* section, /* points to section name */ 25 | const char* keyname, /* points to key name */ 26 | char* keyvalue, /* points to destination buffer */ 27 | unsigned int len, /* size of destination buffer */ 28 | const char* filename);/* points to configuration filename */ 29 | 30 | 31 | /* 32 | * Read the value of key name in integer form 33 | */ 34 | int getconfigint(const char* section, /* points to section name */ 35 | const char* keyname, /* points to key name */ 36 | int* keyvalue, /* points to destination address */ 37 | const char* filename); /* points to configuration filename */ 38 | 39 | 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /lib/filectl.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | #include "common.h" 3 | #include 4 | #include 5 | #include 6 | 7 | EXPORT int openHW(const char *devName,int flags) { 8 | clearLastError(); 9 | int fd = -1; 10 | const char *strDevName = devName; 11 | fd = open(strDevName, flags); 12 | if (fd < 0) { 13 | setLastError("Open %s error: %s(%d)",devName, strerror(errno),errno); 14 | } 15 | 16 | return fd; 17 | } 18 | 19 | EXPORT int writeHW(int fd, const void *_data, size_t len) { 20 | clearLastError(); 21 | if (len <= 0) { 22 | return 0; 23 | } 24 | unsigned char *pData = (unsigned char *)_data; 25 | int ret = write(fd, pData, len); 26 | if (ret < 0) { 27 | setLastError("Write file error: %s(%d)",strerror(errno),errno); 28 | } 29 | return ret; 30 | } 31 | 32 | EXPORT int readHW(int fd, void *_data, size_t len) { 33 | clearLastError(); 34 | if (len <= 0) { 35 | return 0; 36 | } 37 | 38 | int retRead = 0; 39 | unsigned char* pRetData = _data; 40 | if (pRetData) { 41 | retRead = read(fd, pRetData, len); 42 | } 43 | if (retRead < 0) { 44 | setLastError("Read file error: %s(%d)",strerror(errno),errno); 45 | } 46 | return retRead; 47 | } 48 | 49 | EXPORT int selectHW(int fd, int sec, int usec) { 50 | clearLastError(); 51 | fd_set rfds; 52 | struct timeval timeout; 53 | int ret = -1; 54 | 55 | FD_ZERO(&rfds); 56 | FD_SET(fd, &rfds); 57 | 58 | if ( sec==0 && usec== 0 ) { 59 | ret = select(fd + 1, &rfds, NULL, NULL, NULL); 60 | } else { 61 | timeout.tv_sec = sec; 62 | timeout.tv_usec = usec; 63 | ret = select(fd + 1, &rfds, NULL, NULL, &timeout); 64 | } 65 | 66 | if (ret < 0) { 67 | setLastError("Fail to select"); 68 | return -1; 69 | } 70 | 71 | if (FD_ISSET(fd,&rfds)) { 72 | return 1; 73 | } 74 | 75 | return 0; 76 | } 77 | 78 | EXPORT void closeHW(int fd) { 79 | clearLastError(); 80 | close(fd); 81 | } 82 | 83 | EXPORT int ioctlWithIntValue(int fd, int cmd, int value) { 84 | clearLastError(); 85 | return ioctl(fd, cmd, &value); 86 | } 87 | -------------------------------------------------------------------------------- /lib/gpio.c: -------------------------------------------------------------------------------- 1 | #include "common.h" 2 | #include "libfahw-gpio.h" 3 | 4 | static int pinGPIO[41]; 5 | 6 | int initPinGPIO(int board) 7 | { 8 | clearLastError(); 9 | int ret = -1; 10 | 11 | switch(board) { 12 | case BOARD_NANOPI_M1: { 13 | int tempPinGPIO[41] = {-1, -1, -1, -1, -1, -1, -1, 203, 198, -1, 199, 14 | 0, 6, 2, -1, 3, 200, -1, 201, -1, -1, 15 | -1, 1, -1, -1, -1, -1, -1, -1, 20, -1, 16 | 21, 7, 8, -1, 16, 13, 9, 15, -1, 14, 17 | }; 18 | memcpy(pinGPIO, tempPinGPIO, sizeof(pinGPIO)); 19 | ret = 0; 20 | break; 21 | } 22 | case BOARD_NANOPI_2:{ 23 | int tempPinGPIO[41] = {-1, -1, -1, 99, -1, 98, -1, 60, 117, -1, 113, 24 | 61, 58, 62, -1, 63, 78, -1, 59, 95, -1, 25 | 96, 97, 93, 94, -1, 77, 103, 102, 72, -1, 26 | 73, 92, 74, -1, 76, 71, 75, 162, -1, 163, 27 | }; 28 | memcpy(pinGPIO, tempPinGPIO, sizeof(pinGPIO)); 29 | ret = 0; 30 | break; 31 | } 32 | case BOARD_NANOPI_2_FIRE: 33 | case BOARD_NANOPI_M2: { 34 | int tempPinGPIO[41] = {-1, -1, -1, -1, -1, -1, -1, 104, 117, -1, 113, 35 | 61, 97, 62, -1, 63, 78, -1, 59, -1, -1, 36 | -1, 60, -1, -1, -1, 58, -1, -1, 72, -1, 37 | 71, 92, 77, -1, 75, 74, 163, 76, -1, 73, 38 | }; 39 | memcpy(pinGPIO, tempPinGPIO, sizeof(pinGPIO)); 40 | ret = 0; 41 | break; 42 | } 43 | case BOARD_NANOPC_T2: { 44 | int tempPinGPIO[41] = {-1, -1, -1, 116, 112, -1, -1, -1, -1, -1, -1, 45 | 117, 113, 61, 60, 63, 62, 68, 71, 72, 88, 46 | 92, 58, 97, 104, 77, 163, 78, 165, -1, -1, 47 | }; 48 | memcpy(pinGPIO, tempPinGPIO, sizeof(pinGPIO)); 49 | ret = 0; 50 | break; 51 | } 52 | case BOARD_NANOPI_M3: { 53 | int tempPinGPIO[41] = {-1, -1, -1, -1, -1, -1, -1, 104, 117, -1, 113, 54 | 61, 97, 62, -1, 63, 78, -1, 59, -1, -1, 55 | -1, 60, -1, -1, -1, 58, -1, -1, 72, -1, 56 | 71, 92, 77, -1, 75, 74, 163, 76, -1, 73, 57 | }; 58 | memcpy(pinGPIO, tempPinGPIO, sizeof(pinGPIO)); 59 | ret = 0; 60 | break; 61 | } 62 | case BOARD_NANOPC_T3: { 63 | int tempPinGPIO[41] = {-1, -1, -1, 116, 112, -1, -1, -1, -1, -1, -1, 64 | 117, 113, 61, 60, 63, 62, 68, 71, 72, 88, 65 | 92, 58, 97, 104, 77, 163, 78, 165, -1, -1, 66 | }; 67 | memcpy(pinGPIO, tempPinGPIO, sizeof(pinGPIO)); 68 | ret = 0; 69 | break; 70 | } 71 | default: 72 | ret = -1; 73 | break; 74 | } 75 | 76 | return ret; 77 | } 78 | 79 | EXPORT int pintoGPIO(int pin) 80 | { 81 | clearLastError(); 82 | 83 | if (pin<1 || pin>40 || pinGPIO[pin]==-1) { 84 | setLastError("invalid pin %d, it may be 5V/3.3V/GND or occupied by kernel?", pin); 85 | return -1; 86 | } 87 | return pinGPIO[pin]; 88 | } 89 | 90 | EXPORT int exportGPIOPin(int pin) 91 | { 92 | clearLastError(); 93 | int gpio = pintoGPIO(pin); 94 | int ret = writeIntValueToFile("/sys/class/gpio/export", gpio); 95 | if (ret > 0) 96 | return 0; 97 | else 98 | return -1; 99 | } 100 | 101 | EXPORT int unexportGPIOPin(int pin) 102 | { 103 | clearLastError(); 104 | int gpio = pintoGPIO(pin); 105 | 106 | return writeIntValueToFile("/sys/class/gpio/unexport", gpio); 107 | } 108 | 109 | EXPORT int getGPIOValue(int pin) 110 | { 111 | clearLastError(); 112 | int gpio = pintoGPIO(pin); 113 | GPIO_FILENAME_DEFINE(gpio, "value") 114 | 115 | return readIntValueFromFile(fileName); 116 | } 117 | 118 | EXPORT int setGPIOValue(int pin, int value) 119 | { 120 | clearLastError(); 121 | int gpio = pintoGPIO(pin); 122 | GPIO_FILENAME_DEFINE(gpio, "value") 123 | 124 | return writeIntValueToFile(fileName, value); 125 | } 126 | 127 | EXPORT int setGPIODirection(int pin, int direction) 128 | { 129 | clearLastError(); 130 | int gpio = pintoGPIO(pin); 131 | char directionStr[10]; 132 | GPIO_FILENAME_DEFINE(gpio, "direction") 133 | 134 | if (direction == GPIO_IN) { 135 | strcpy(directionStr, "in"); 136 | } else if (direction == GPIO_OUT) { 137 | strcpy(directionStr, "out"); 138 | } else { 139 | setLastError("direction must be 1 or 2, 1->in, 2->out"); 140 | return -1; 141 | } 142 | return writeValueToFile(fileName, directionStr); 143 | } 144 | 145 | EXPORT int getGPIODirection(int pin) 146 | { 147 | clearLastError(); 148 | char buff[255] = {0}; 149 | int direction; 150 | int ret; 151 | int gpio = pintoGPIO(pin); 152 | GPIO_FILENAME_DEFINE(gpio, "direction") 153 | 154 | ret = readValueFromFile(fileName, buff, sizeof(buff)-1); 155 | if (ret >= 0) { 156 | if (strncasecmp(buff, "out", 3)==0) { 157 | direction = GPIO_OUT; 158 | } else if (strncasecmp(buff, "in", 2)==0) { 159 | direction = GPIO_IN; 160 | } else { 161 | setLastError("direction wrong, must be in or out, but %s", buff); 162 | return -1; 163 | } 164 | return direction; 165 | } 166 | return ret; 167 | } 168 | -------------------------------------------------------------------------------- /lib/gpio_sensor.c: -------------------------------------------------------------------------------- 1 | #include "common.h" 2 | #include "libfahw-gpio.h" 3 | #include "libfahw-GPIOSensor.h" 4 | 5 | static int sensor_num = 0; 6 | 7 | EXPORT int sensorInit(struct sensor *dev, int num) 8 | { 9 | clearLastError(); 10 | int devFD; 11 | int i; 12 | 13 | devFD = open(SENSOR_DEVICE, 0); 14 | if (devFD == -1) { 15 | setLastError("Fail to open %s", SENSOR_DEVICE); 16 | } 17 | 18 | for(i=0; i0; sensor_num--) { 56 | if(ioctl(devFD, DEL_SENSOR, sensor_num) == -1) { 57 | setLastError("Fail to delete sensor"); 58 | } 59 | } 60 | close(devFD); 61 | } 62 | -------------------------------------------------------------------------------- /lib/hmc5883.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "common.h" 3 | #include "libfahw-i2c.h" 4 | #include "libfahw-filectl.h" 5 | #include "libfahw-hmc5883.h" 6 | 7 | static int hmc5883HWInit(int devFD) 8 | { 9 | int ret = 0; 10 | if (I2CWriteByte(devFD, WriteAddress, 6) == -1) { 11 | ret = -1; 12 | } 13 | if (I2CWriteByteTo(devFD, 0x00, 0x70, 6) == -1) { 14 | ret = -1; 15 | } 16 | if (I2CWriteByte(devFD, WriteAddress, 6) == -1) { 17 | ret = -1; 18 | } 19 | // continued reading mode 20 | if (I2CWriteByteTo(devFD, 0x02, 0x00, 6) == -1) { 21 | ret = -1; 22 | } 23 | // read command to check the mode 24 | if (I2CWriteByte(devFD, ReadAddress, 6) == -1) { 25 | ret = -1; 26 | } 27 | // printf("A=%d\n",I2CReadByteFrom(devFD, 0x00, 6)); 28 | // printf("B=%d\n",I2CReadByteFrom(devFD, 0x02, 6)); 29 | usleep(1000*100); 30 | return ret; 31 | } 32 | 33 | EXPORT double hmc5883Read(int devFD) 34 | { 35 | int x,x1,x2,y,y1,y2,z,z1,z2; 36 | double angle; 37 | 38 | I2CWriteByte(devFD, ReadAddress, 6); // read command 39 | x1 = I2CReadByteFrom(devFD, 0x03, 6); 40 | x2 = I2CReadByteFrom(devFD, 0x04, 6); 41 | x = x1 << 8 | x2; // Combine MSB and LSB of X Data output register 42 | 43 | z1 = I2CReadByteFrom(devFD, 0x05, 6); 44 | z2 = I2CReadByteFrom(devFD, 0x06, 6); 45 | z = z1 << 8 | z2; // Combine MSB and LSB of Z Data output register 46 | 47 | y1 = I2CReadByteFrom(devFD, 0x07, 6); 48 | y2 = I2CReadByteFrom(devFD, 0x08, 6); 49 | y = y1 << 8 | y2; // Combine MSB and LSB of Y Data output register 50 | if (x > 32768) 51 | x = -(0xFFFF - x + 1); 52 | if (z > 32768) 53 | z = -(0xFFFF - z + 1); 54 | if (y>32768) 55 | y = -(0xFFFF - y + 1); 56 | 57 | // printf("x=%d,y=%d,z=%d\n",x,y,z); 58 | angle = atan2((double)y,(double)x) * (180 / PI) + 180; // angle in degrees 59 | if(angle >=0 && angle <=360) { 60 | return angle; 61 | } else { 62 | return -1; 63 | } 64 | 65 | } 66 | 67 | EXPORT int hmc5883Init(int i2cDev) 68 | { 69 | clearLastError(); 70 | int devFD; 71 | char buf[16]; 72 | memset(buf, 0, sizeof(buf)); 73 | sprintf(buf, "/dev/i2c-%d", i2cDev); 74 | if ((devFD = openHW(buf, O_RDWR)) < 0) { 75 | setLastError("Fail to open I2C hmc5883 device"); 76 | return -1; 77 | } else { 78 | if (setI2CSlave(devFD, HMC5883_ADDRESS) < 0) { 79 | setLastError("Fail to set hmc5883 I2C slave address"); 80 | closeHW(devFD); 81 | return -1; 82 | } 83 | } 84 | if (hmc5883HWInit(devFD) == -1) { 85 | closeHW(devFD); 86 | return -1; 87 | } 88 | return devFD; 89 | } 90 | 91 | EXPORT void hmc5883DeInit(int devFD) { 92 | closeHW(devFD); 93 | } 94 | -------------------------------------------------------------------------------- /lib/i2c.c: -------------------------------------------------------------------------------- 1 | #include "common.h" 2 | #include "includes/i2c-dev.h" 3 | 4 | EXPORT int setI2CSlave(int fd, int slave) { 5 | clearLastError(); 6 | if (ioctl(fd, I2C_SLAVE, slave) < 0) { 7 | setLastError("Fail ioctl I2C_SLAVE"); 8 | return -1; 9 | } 10 | 11 | return 0; 12 | } 13 | 14 | EXPORT int setI2CTimeout(int fd, int timeout) { 15 | clearLastError(); 16 | if (ioctl(fd, I2C_TIMEOUT, timeout) < 0) { 17 | setLastError("Fail ioctl I2C_TIMEOUT"); 18 | return -1; 19 | } 20 | 21 | return 0; 22 | } 23 | 24 | EXPORT int setI2CRetries(int fd, int retries) { 25 | clearLastError(); 26 | if (ioctl(fd, I2C_RETRIES, retries) < 0) { 27 | setLastError("Fail ioctl I2C_RETRIES"); 28 | return -1; 29 | } 30 | 31 | return 0; 32 | } 33 | 34 | EXPORT int I2CWriteByteTo(int fd, int pos, unsigned char byteData, int wait_ms) { 35 | clearLastError(); 36 | if (i2c_smbus_write_byte_data(fd, (unsigned char)pos, (unsigned char)byteData) < 0) { 37 | setLastError("Fail to I2CWriteByteTo"); 38 | return -1; 39 | } 40 | usleep(1000*wait_ms); 41 | 42 | return 0; 43 | } 44 | 45 | EXPORT int I2CReadByteFrom(int fd, int pos, int wait_ms) { 46 | clearLastError(); 47 | ioctl(fd, BLKFLSBUF); // clear kernel read buffer 48 | if (i2c_smbus_write_byte(fd, (unsigned char)pos) < 0) { 49 | setLastError("Fail to I2CReadByteFrom"); 50 | return -1; 51 | } 52 | usleep(1000*wait_ms); 53 | return i2c_smbus_read_byte(fd); 54 | } 55 | 56 | EXPORT int I2CWriteByte(int fd, unsigned char byteData, int wait_ms) { 57 | clearLastError(); 58 | if (i2c_smbus_write_byte(fd, (unsigned char)byteData) < 0) { 59 | setLastError("Fail to I2CWriteByte"); 60 | return -1; 61 | } 62 | usleep(1000*wait_ms); 63 | 64 | return 0; 65 | } 66 | 67 | EXPORT int I2CReadByte(int fd, int wait_ms) { 68 | clearLastError(); 69 | int ret; 70 | ioctl(fd, BLKFLSBUF); // clear kernel read buffer 71 | usleep(1000*wait_ms); 72 | if ((ret = i2c_smbus_read_byte(fd)) == -1) { 73 | setLastError("Fail to I2CReadByte"); 74 | } 75 | return ret; 76 | } 77 | -------------------------------------------------------------------------------- /lib/iio.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "common.h" 4 | #include "libfahw-iio.h" 5 | #include "libfahw-gpio.h" 6 | #include "libfahw-filectl.h" 7 | 8 | EXPORT int dht11Read(int type, int *data) 9 | { 10 | clearLastError(); 11 | int ret = -1; 12 | DIR *d; 13 | struct dirent *de; 14 | 15 | if (!(d = opendir(DHT11_SYS_PATH))) { 16 | setLastError("Fail to opendir %s", DHT11_SYS_PATH); 17 | return 0; 18 | } 19 | char dht11File[FILE_PATH_LENGTH]; 20 | while ((de = readdir(d))) { 21 | if (de->d_name[0] == '.') 22 | continue; 23 | 24 | if (type == DHT_HUMIDITY) 25 | sprintf(dht11File, "%s%s/in_humidityrelative_input", DHT11_SYS_PATH, de->d_name); 26 | else if(type == DHT_TEMP) 27 | sprintf(dht11File, "%s%s/in_temp_input", DHT11_SYS_PATH, de->d_name); 28 | 29 | if (access(dht11File, F_OK) != -1) { 30 | break; 31 | } 32 | } 33 | closedir(d); 34 | 35 | switch(type) { 36 | case DHT_TEMP: 37 | ret = readIntValueFromFile(dht11File); 38 | break; 39 | case DHT_HUMIDITY: 40 | ret = readIntValueFromFile(dht11File); 41 | break; 42 | default: 43 | setLastError("Unsupport dht11 data type %d", type); 44 | break; 45 | } 46 | if (ret != -1) { 47 | *data = ret; 48 | ret = 0; 49 | } else { 50 | setLastError("Invalid dht11 data"); 51 | } 52 | 53 | return ret; 54 | } 55 | 56 | static int Hcsr04Write(char* fileName, char* buff) 57 | { 58 | FILE *fp = fopen(fileName,"w"); 59 | if (fp == NULL) { 60 | setLastError("Unable to open file %s",fileName); 61 | return -1; 62 | } else { 63 | if (fwrite(buff, sizeof(struct HCSR04_resource), 1, fp) == -1) { 64 | setLastError("Unable to write file %s",fileName); 65 | } 66 | } 67 | fclose(fp); 68 | return 0; 69 | } 70 | 71 | EXPORT int Hcsr04Init(int Pin) 72 | { 73 | clearLastError(); 74 | struct HCSR04_resource res; 75 | int ret = HCSR04_MAX_DISTANCE+1; 76 | char *hcsr04Path = (char *) malloc(FILE_PATH_LENGTH); 77 | memset(hcsr04Path, 0, FILE_PATH_LENGTH); 78 | strcpy(hcsr04Path, HCSR04_PATH); 79 | 80 | res.gpio = pintoGPIO(Pin); 81 | char *resStr = (char *)&res; 82 | if (Hcsr04Write(hcsr04Path, resStr) == -1) { 83 | setLastError("Fail to write resource to hcsr04"); 84 | ret = -1; 85 | } 86 | free(hcsr04Path); 87 | return ret; 88 | } 89 | 90 | EXPORT void Hcsr04DeInit() 91 | { 92 | clearLastError(); 93 | struct HCSR04_resource res; 94 | res.gpio = -1; 95 | 96 | char *hcsr04Path = (char *) malloc(FILE_PATH_LENGTH); 97 | memset(hcsr04Path, 0, FILE_PATH_LENGTH); 98 | strcpy(hcsr04Path, HCSR04_PATH); 99 | 100 | char *resStr = (char *)&res; 101 | if (Hcsr04Write(hcsr04Path, resStr) == -1) { 102 | setLastError("Fail to write resource to hcsr04"); 103 | } 104 | free(hcsr04Path); 105 | } 106 | 107 | EXPORT int Hcsr04Read(int *distance) 108 | { 109 | clearLastError(); 110 | int ret = HCSR04_MAX_DISTANCE + 1; 111 | char *hcsr04Path = (char *) malloc(FILE_PATH_LENGTH); 112 | memset(hcsr04Path, 0, FILE_PATH_LENGTH); 113 | strcpy(hcsr04Path, HCSR04_PATH); 114 | ret = readIntValueFromFile(hcsr04Path); 115 | *distance = (int)(ret / HCSR04_PER_METRE); 116 | free(hcsr04Path); 117 | return ret; 118 | } 119 | -------------------------------------------------------------------------------- /lib/includes/i2c-dev.h: -------------------------------------------------------------------------------- 1 | /* 2 | i2c-dev.h - i2c-bus driver, char device interface 3 | 4 | Copyright (C) 1995-97 Simon G. Vogl 5 | Copyright (C) 1998-99 Frodo Looijaard 6 | 7 | This program is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation; either version 2 of the License, or 10 | (at your option) any later version. 11 | 12 | This program is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this program; if not, write to the Free Software 19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 20 | */ 21 | 22 | /* $Id: i2c-dev.h,v 1.1 2011/05/18 02:51:54 tzs Exp $ */ 23 | 24 | #ifndef LIB_I2CDEV_H 25 | #define LIB_I2CDEV_H 26 | 27 | #include 28 | #include 29 | 30 | 31 | /* -- i2c.h -- */ 32 | 33 | 34 | #ifdef __cplusplus 35 | extern "C" { 36 | #endif 37 | /* 38 | * I2C Message - used for pure i2c transaction, also from /dev interface 39 | */ 40 | struct i2c_msg { 41 | __u16 addr; /* slave address */ 42 | unsigned short flags; 43 | #define I2C_M_TEN 0x10 /* we have a ten bit chip address */ 44 | #define I2C_M_RD 0x01 45 | #define I2C_M_NOSTART 0x4000 46 | #define I2C_M_REV_DIR_ADDR 0x2000 47 | #define I2C_M_IGNORE_NAK 0x1000 48 | #define I2C_M_NO_RD_ACK 0x0800 49 | short len; /* msg length */ 50 | char *buf; /* pointer to msg data */ 51 | int err; 52 | short done; 53 | }; 54 | 55 | /* To determine what functionality is present */ 56 | 57 | #define I2C_FUNC_I2C 0x00000001 58 | #define I2C_FUNC_10BIT_ADDR 0x00000002 59 | #define I2C_FUNC_PROTOCOL_MANGLING 0x00000004 /* I2C_M_{REV_DIR_ADDR,NOSTART,..} */ 60 | #define I2C_FUNC_SMBUS_HWPEC_CALC 0x00000008 /* SMBus 2.0 */ 61 | #define I2C_FUNC_SMBUS_READ_WORD_DATA_PEC 0x00000800 /* SMBus 2.0 */ 62 | #define I2C_FUNC_SMBUS_WRITE_WORD_DATA_PEC 0x00001000 /* SMBus 2.0 */ 63 | #define I2C_FUNC_SMBUS_PROC_CALL_PEC 0x00002000 /* SMBus 2.0 */ 64 | #define I2C_FUNC_SMBUS_BLOCK_PROC_CALL_PEC 0x00004000 /* SMBus 2.0 */ 65 | #define I2C_FUNC_SMBUS_BLOCK_PROC_CALL 0x00008000 /* SMBus 2.0 */ 66 | #define I2C_FUNC_SMBUS_QUICK 0x00010000 67 | #define I2C_FUNC_SMBUS_READ_BYTE 0x00020000 68 | #define I2C_FUNC_SMBUS_WRITE_BYTE 0x00040000 69 | #define I2C_FUNC_SMBUS_READ_BYTE_DATA 0x00080000 70 | #define I2C_FUNC_SMBUS_WRITE_BYTE_DATA 0x00100000 71 | #define I2C_FUNC_SMBUS_READ_WORD_DATA 0x00200000 72 | #define I2C_FUNC_SMBUS_WRITE_WORD_DATA 0x00400000 73 | #define I2C_FUNC_SMBUS_PROC_CALL 0x00800000 74 | #define I2C_FUNC_SMBUS_READ_BLOCK_DATA 0x01000000 75 | #define I2C_FUNC_SMBUS_WRITE_BLOCK_DATA 0x02000000 76 | #define I2C_FUNC_SMBUS_READ_I2C_BLOCK 0x04000000 /* I2C-like block xfer */ 77 | #define I2C_FUNC_SMBUS_WRITE_I2C_BLOCK 0x08000000 /* w/ 1-byte reg. addr. */ 78 | #define I2C_FUNC_SMBUS_READ_I2C_BLOCK_2 0x10000000 /* I2C-like block xfer */ 79 | #define I2C_FUNC_SMBUS_WRITE_I2C_BLOCK_2 0x20000000 /* w/ 2-byte reg. addr. */ 80 | #define I2C_FUNC_SMBUS_READ_BLOCK_DATA_PEC 0x40000000 /* SMBus 2.0 */ 81 | #define I2C_FUNC_SMBUS_WRITE_BLOCK_DATA_PEC 0x80000000 /* SMBus 2.0 */ 82 | 83 | #define I2C_FUNC_SMBUS_BYTE I2C_FUNC_SMBUS_READ_BYTE | \ 84 | I2C_FUNC_SMBUS_WRITE_BYTE 85 | #define I2C_FUNC_SMBUS_BYTE_DATA I2C_FUNC_SMBUS_READ_BYTE_DATA | \ 86 | I2C_FUNC_SMBUS_WRITE_BYTE_DATA 87 | #define I2C_FUNC_SMBUS_WORD_DATA I2C_FUNC_SMBUS_READ_WORD_DATA | \ 88 | I2C_FUNC_SMBUS_WRITE_WORD_DATA 89 | #define I2C_FUNC_SMBUS_BLOCK_DATA I2C_FUNC_SMBUS_READ_BLOCK_DATA | \ 90 | I2C_FUNC_SMBUS_WRITE_BLOCK_DATA 91 | #define I2C_FUNC_SMBUS_I2C_BLOCK I2C_FUNC_SMBUS_READ_I2C_BLOCK | \ 92 | I2C_FUNC_SMBUS_WRITE_I2C_BLOCK 93 | #define I2C_FUNC_SMBUS_I2C_BLOCK_2 I2C_FUNC_SMBUS_READ_I2C_BLOCK_2 | \ 94 | I2C_FUNC_SMBUS_WRITE_I2C_BLOCK_2 95 | #define I2C_FUNC_SMBUS_BLOCK_DATA_PEC I2C_FUNC_SMBUS_READ_BLOCK_DATA_PEC | \ 96 | I2C_FUNC_SMBUS_WRITE_BLOCK_DATA_PEC 97 | #define I2C_FUNC_SMBUS_WORD_DATA_PEC I2C_FUNC_SMBUS_READ_WORD_DATA_PEC | \ 98 | I2C_FUNC_SMBUS_WRITE_WORD_DATA_PEC 99 | 100 | #define I2C_FUNC_SMBUS_READ_BYTE_PEC I2C_FUNC_SMBUS_READ_BYTE_DATA 101 | #define I2C_FUNC_SMBUS_WRITE_BYTE_PEC I2C_FUNC_SMBUS_WRITE_BYTE_DATA 102 | #define I2C_FUNC_SMBUS_READ_BYTE_DATA_PEC I2C_FUNC_SMBUS_READ_WORD_DATA 103 | #define I2C_FUNC_SMBUS_WRITE_BYTE_DATA_PEC I2C_FUNC_SMBUS_WRITE_WORD_DATA 104 | #define I2C_FUNC_SMBUS_BYTE_PEC I2C_FUNC_SMBUS_BYTE_DATA 105 | #define I2C_FUNC_SMBUS_BYTE_DATA_PEC I2C_FUNC_SMBUS_WORD_DATA 106 | 107 | #define I2C_FUNC_SMBUS_EMUL I2C_FUNC_SMBUS_QUICK | \ 108 | I2C_FUNC_SMBUS_BYTE | \ 109 | I2C_FUNC_SMBUS_BYTE_DATA | \ 110 | I2C_FUNC_SMBUS_WORD_DATA | \ 111 | I2C_FUNC_SMBUS_PROC_CALL | \ 112 | I2C_FUNC_SMBUS_WRITE_BLOCK_DATA | \ 113 | I2C_FUNC_SMBUS_WRITE_BLOCK_DATA_PEC | \ 114 | I2C_FUNC_SMBUS_I2C_BLOCK 115 | 116 | /* 117 | * Data for SMBus Messages 118 | */ 119 | #define I2C_SMBUS_BLOCK_MAX 32 /* As specified in SMBus standard */ 120 | #define I2C_SMBUS_I2C_BLOCK_MAX 32 /* Not specified but we use same structure */ 121 | union i2c_smbus_data { 122 | __u8 byte; 123 | __u16 word; 124 | __u8 block[I2C_SMBUS_BLOCK_MAX + 3]; /* block[0] is used for length */ 125 | /* one more for read length in block process call */ 126 | /* and one more for PEC */ 127 | }; 128 | 129 | /* smbus_access read or write markers */ 130 | #define I2C_SMBUS_READ 1 131 | #define I2C_SMBUS_WRITE 0 132 | 133 | /* SMBus transaction types (size parameter in the above functions) 134 | Note: these no longer correspond to the (arbitrary) PIIX4 internal codes! */ 135 | #define I2C_SMBUS_QUICK 0 136 | #define I2C_SMBUS_BYTE 1 137 | #define I2C_SMBUS_BYTE_DATA 2 138 | #define I2C_SMBUS_WORD_DATA 3 139 | #define I2C_SMBUS_PROC_CALL 4 140 | #define I2C_SMBUS_BLOCK_DATA 5 141 | #define I2C_SMBUS_I2C_BLOCK_DATA 6 142 | #define I2C_SMBUS_BLOCK_PROC_CALL 7 /* SMBus 2.0 */ 143 | #define I2C_SMBUS_BLOCK_DATA_PEC 8 /* SMBus 2.0 */ 144 | #define I2C_SMBUS_PROC_CALL_PEC 9 /* SMBus 2.0 */ 145 | #define I2C_SMBUS_BLOCK_PROC_CALL_PEC 10 /* SMBus 2.0 */ 146 | #define I2C_SMBUS_WORD_DATA_PEC 11 /* SMBus 2.0 */ 147 | 148 | 149 | /* ----- commands for the ioctl like i2c_command call: 150 | * note that additional calls are defined in the algorithm and hw 151 | * dependent layers - these can be listed here, or see the 152 | * corresponding header files. 153 | */ 154 | /* -> bit-adapter specific ioctls */ 155 | #define I2C_RETRIES 0x0701 /* number of times a device address */ 156 | /* should be polled when not */ 157 | /* acknowledging */ 158 | #define I2C_TIMEOUT 0x0702 /* set timeout - call with int */ 159 | 160 | 161 | /* this is for i2c-dev.c */ 162 | #define I2C_SLAVE 0x0703 /* Change slave address */ 163 | /* Attn.: Slave address is 7 or 10 bits */ 164 | #define I2C_SLAVE_FORCE 0x0706 /* Change slave address */ 165 | /* Attn.: Slave address is 7 or 10 bits */ 166 | /* This changes the address, even if it */ 167 | /* is already taken! */ 168 | #define I2C_TENBIT 0x0704 /* 0 for 7 bit addrs, != 0 for 10 bit */ 169 | 170 | #define I2C_FUNCS 0x0705 /* Get the adapter functionality */ 171 | #define I2C_RDWR 0x0707 /* Combined R/W transfer (one stop only)*/ 172 | #define I2C_PEC 0x0708 /* != 0 for SMBus PEC */ 173 | #if 0 174 | #define I2C_ACK_TEST 0x0710 /* See if a slave is at a specific address */ 175 | #endif 176 | 177 | #define I2C_SMBUS 0x0720 /* SMBus-level access */ 178 | 179 | /* -- i2c.h -- */ 180 | 181 | 182 | /* Note: 10-bit addresses are NOT supported! */ 183 | 184 | /* This is the structure as used in the I2C_SMBUS ioctl call */ 185 | struct i2c_smbus_ioctl_data { 186 | char read_write; 187 | __u8 command; 188 | int size; 189 | union i2c_smbus_data *data; 190 | }; 191 | 192 | /* This is the structure as used in the I2C_RDWR ioctl call */ 193 | struct i2c_rdwr_ioctl_data { 194 | struct i2c_msg *msgs; /* pointers to i2c_msgs */ 195 | int nmsgs; /* number of i2c_msgs */ 196 | }; 197 | 198 | #include "../common.h" 199 | 200 | static inline __s32 i2c_smbus_access(int file, char read_write, __u8 command, 201 | int size, union i2c_smbus_data *data) 202 | { 203 | struct i2c_smbus_ioctl_data args; 204 | 205 | args.read_write = read_write; 206 | args.command = command; 207 | args.size = size; 208 | args.data = data; 209 | 210 | int ret = ioctl(file,I2C_SMBUS,&args); 211 | return ret; 212 | } 213 | 214 | 215 | static inline __s32 i2c_smbus_write_quick(int file, __u8 value) 216 | { 217 | return i2c_smbus_access(file,value,0,I2C_SMBUS_QUICK,NULL); 218 | } 219 | 220 | static inline __s32 i2c_smbus_read_byte(int file) 221 | { 222 | union i2c_smbus_data data; 223 | if (i2c_smbus_access(file,I2C_SMBUS_READ,0,I2C_SMBUS_BYTE,&data)) 224 | return -1; 225 | else 226 | return 0x0FF & data.byte; 227 | } 228 | 229 | static inline __s32 i2c_smbus_write_byte(int file, __u8 value) 230 | { 231 | return i2c_smbus_access(file,I2C_SMBUS_WRITE,value, 232 | I2C_SMBUS_BYTE,NULL); 233 | } 234 | 235 | static inline __s32 i2c_smbus_read_byte_data(int file, __u8 command) 236 | { 237 | union i2c_smbus_data data; 238 | if (i2c_smbus_access(file,I2C_SMBUS_READ,command, 239 | I2C_SMBUS_BYTE_DATA,&data)) 240 | return -1; 241 | else 242 | return 0x0FF & data.byte; 243 | } 244 | 245 | static inline __s32 i2c_smbus_write_byte_data(int file, __u8 command, 246 | __u8 value) 247 | { 248 | union i2c_smbus_data data; 249 | data.byte = value; 250 | return i2c_smbus_access(file,I2C_SMBUS_WRITE,command, 251 | I2C_SMBUS_BYTE_DATA, &data); 252 | } 253 | 254 | static inline __s32 i2c_smbus_read_word_data(int file, __u8 command) 255 | { 256 | union i2c_smbus_data data; 257 | if (i2c_smbus_access(file,I2C_SMBUS_READ,command, 258 | I2C_SMBUS_WORD_DATA,&data)) 259 | return -1; 260 | else 261 | return 0x0FFFF & data.word; 262 | } 263 | 264 | static inline __s32 i2c_smbus_write_word_data(int file, __u8 command, 265 | __u16 value) 266 | { 267 | union i2c_smbus_data data; 268 | data.word = value; 269 | return i2c_smbus_access(file,I2C_SMBUS_WRITE,command, 270 | I2C_SMBUS_WORD_DATA, &data); 271 | } 272 | 273 | static inline __s32 i2c_smbus_process_call(int file, __u8 command, __u16 value) 274 | { 275 | union i2c_smbus_data data; 276 | data.word = value; 277 | if (i2c_smbus_access(file,I2C_SMBUS_WRITE,command, 278 | I2C_SMBUS_PROC_CALL,&data)) 279 | return -1; 280 | else 281 | return 0x0FFFF & data.word; 282 | } 283 | 284 | 285 | /* Returns the number of read bytes */ 286 | static inline __s32 i2c_smbus_read_block_data(int file, __u8 command, 287 | __u8 *values) 288 | { 289 | union i2c_smbus_data data; 290 | int i; 291 | if (i2c_smbus_access(file,I2C_SMBUS_READ,command, 292 | I2C_SMBUS_BLOCK_DATA,&data)) 293 | return -1; 294 | else { 295 | for (i = 1; i <= data.block[0]; i++) 296 | values[i-1] = data.block[i]; 297 | return data.block[0]; 298 | } 299 | } 300 | 301 | static inline __s32 i2c_smbus_write_block_data(int file, __u8 command, 302 | __u8 length, __u8 *values) 303 | { 304 | union i2c_smbus_data data; 305 | int i; 306 | if (length > 32) 307 | length = 32; 308 | for (i = 1; i <= length; i++) 309 | data.block[i] = values[i-1]; 310 | data.block[0] = length; 311 | return i2c_smbus_access(file,I2C_SMBUS_WRITE,command, 312 | I2C_SMBUS_BLOCK_DATA, &data); 313 | } 314 | 315 | /* Returns the number of read bytes */ 316 | static inline __s32 i2c_smbus_read_i2c_block_data(int file, __u8 command, 317 | __u8 *values) 318 | { 319 | union i2c_smbus_data data; 320 | int i; 321 | if (i2c_smbus_access(file,I2C_SMBUS_READ,command, 322 | I2C_SMBUS_I2C_BLOCK_DATA,&data)) 323 | return -1; 324 | else { 325 | for (i = 1; i <= data.block[0]; i++) 326 | values[i-1] = data.block[i]; 327 | return data.block[0]; 328 | } 329 | } 330 | 331 | static inline __s32 i2c_smbus_write_i2c_block_data(int file, __u8 command, 332 | __u8 length, __u8 *values) 333 | { 334 | union i2c_smbus_data data; 335 | int i; 336 | if (length > 32) 337 | length = 32; 338 | for (i = 1; i <= length; i++) 339 | data.block[i] = values[i-1]; 340 | data.block[0] = length; 341 | return i2c_smbus_access(file,I2C_SMBUS_WRITE,command, 342 | I2C_SMBUS_I2C_BLOCK_DATA, &data); 343 | } 344 | 345 | /* Returns the number of read bytes */ 346 | static inline __s32 i2c_smbus_block_process_call(int file, __u8 command, 347 | __u8 length, __u8 *values) 348 | { 349 | union i2c_smbus_data data; 350 | int i; 351 | if (length > 32) 352 | length = 32; 353 | for (i = 1; i <= length; i++) 354 | data.block[i] = values[i-1]; 355 | data.block[0] = length; 356 | if (i2c_smbus_access(file,I2C_SMBUS_WRITE,command, 357 | I2C_SMBUS_BLOCK_PROC_CALL,&data)) 358 | return -1; 359 | else { 360 | for (i = 1; i <= data.block[0]; i++) 361 | values[i-1] = data.block[i]; 362 | return data.block[0]; 363 | } 364 | } 365 | 366 | #ifdef __cplusplus 367 | } 368 | #endif 369 | 370 | #endif /* LIB_I2CDEV_H */ 371 | -------------------------------------------------------------------------------- /lib/includes/libfahw-GPIOSensor.h: -------------------------------------------------------------------------------- 1 | #ifndef __FRIENDLYARM_HARDWARE_GPIOSENSOR_H__ 2 | #define __FRIENDLYARM_HARDWARE_GPIOSENSOR_H__ 3 | 4 | #define ADD_SENSOR (0) 5 | #define DEL_SENSOR (1) 6 | #define START_ALL_SENSOR (4) 7 | #define STOP_ALL_SENSOR (8) 8 | #define IRQ_TYPE_EDGE_RISING (0x1) 9 | #define IRQ_TYPE_EDGE_FALLING (0x2) 10 | #define IRQ_TYPE_EDGE_BOTH (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING) 11 | 12 | struct sensor { 13 | int pin; 14 | int intType; 15 | }; 16 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) 17 | #define SENSOR_DEVICE "/dev/sensor" 18 | 19 | int sensorInit(struct sensor *dev, int num); 20 | int sensorRead(int devFD, char *buf, int len); 21 | void sensorDeinit(int devFD); 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /lib/includes/libfahw-adxl34x.h: -------------------------------------------------------------------------------- 1 | #ifndef __FRIENDLYARM_HARDWARE_ADXL34X_H__ 2 | #define __FRIENDLYARM_HARDWARE_ADXL34X_H__ 3 | 4 | #define ADXL34X_SYS_PATH "/sys/bus/i2c/drivers/adxl34x/" 5 | 6 | int adxl34xRead(char *position); 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /lib/includes/libfahw-bmp180.h: -------------------------------------------------------------------------------- 1 | #ifndef __FRIENDLYARM_HARDWARE_BMP180_H__ 2 | #define __FRIENDLYARM_HARDWARE_BMP180_H__ 3 | 4 | #define BMP180_SYS_PATH "/sys/bus/i2c/drivers/bmp085/" 5 | #define BMP180_TEMP (1) 6 | #define BMP180_PRESSURE (2) 7 | 8 | int bmp180Read(int type, int *data); 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /lib/includes/libfahw-encoder.h: -------------------------------------------------------------------------------- 1 | #ifndef __FRIENDLYARM_HARDWARE_ENCODER_H__ 2 | #define __FRIENDLYARM_HARDWARE_ENCODER_H__ 3 | 4 | #define ENCODER_SW (1) 5 | #define ENCODER_VALUE (2) 6 | #define ENCODER_PATH "/sys/devices/platform/rotary_encoder/" 7 | 8 | int rotaryEncoderInit(int swPin, int siaPin, int sibPin); 9 | int rotaryEncoderRead(int type, int *data); 10 | int rotaryEncoderDeInit(); 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /lib/includes/libfahw-filectl.h: -------------------------------------------------------------------------------- 1 | #ifndef __FRIENDLYARM_HARDWARE_FILECTRL_H__ 2 | #define __FRIENDLYARM_HARDWARE_FILECTRL_H__ 3 | 4 | int openHW(const char *devName, int flags); 5 | int writeHW(int fd, const void *_data, size_t len); 6 | int readHW(int fd, void *_data, size_t len); 7 | int selectHW(int fd, int sec, int usec); 8 | void closeHW(int fd); 9 | int ioctlWithIntValue(int fd, int cmd, int value); 10 | 11 | #endif 12 | -------------------------------------------------------------------------------- /lib/includes/libfahw-gpio.h: -------------------------------------------------------------------------------- 1 | #ifndef __FRIENDLYARM_HARDWARE_GPIO_H__ 2 | #define __FRIENDLYARM_HARDWARE_GPIO_H__ 3 | 4 | // Direction 5 | #define GPIO_IN (1) 6 | #define GPIO_OUT (2) 7 | 8 | // Value 9 | #define GPIO_LOW (0) 10 | #define GPIO_HIGH (1) 11 | 12 | //nanopi 13 | #define GPIO_PIN(x) (x) 14 | 15 | #define PWM0 (96 + 1) // GPIOD1 16 | #define PWM1 (64 + 13) // GPIOC13 17 | #define PWM2 (64 + 14) // GPIOC14 18 | 19 | #define GPIO_FILENAME_DEFINE(pin,field) char fileName[255] = {0}; \ 20 | sprintf(fileName, "/sys/class/gpio/gpio%d/%s", pin, field); 21 | 22 | int initPinGPIO(int board); 23 | int pintoGPIO(int pin); 24 | int exportGPIOPin(int pin); 25 | int unexportGPIOPin(int pin); 26 | 27 | // GPIO_LOW or GPIO_HIGH 28 | int setGPIOValue(int pin, int value); 29 | int getGPIOValue(int pin); 30 | 31 | // GPIO_IN or GPIO_OUT 32 | int setGPIODirection(int pin, int direction); 33 | int getGPIODirection(int pin); 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /lib/includes/libfahw-hmc5883.h: -------------------------------------------------------------------------------- 1 | #ifndef __FRIENDLYARM_HARDWARE_HMC5883_H__ 2 | #define __FRIENDLYARM_HARDWARE_HMC5883_H__ 3 | 4 | #define WriteAddress (0x3C) 5 | #define ReadAddress (0x3D) 6 | #define HMC5883_ADDRESS (0x1E) 7 | #define PI (3.14159265) 8 | 9 | double hmc5883Read(int devFD); 10 | int hmc5883Init(int i2cDev); 11 | void hmc5883DeInit(int devFD); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /lib/includes/libfahw-i2c.h: -------------------------------------------------------------------------------- 1 | #ifndef __FRIENDLYARM_HARDWARE_I2C_H__ 2 | #define __FRIENDLYARM_HARDWARE_I2C_H__ 3 | 4 | /* OldInterface: for EEPROM */ 5 | int openI2CDevice(); 6 | int setI2CSlave(int fd, int slave); 7 | int setI2CTimeout(int fd, int timeout); 8 | int setI2CRetries(int fd, int retries); 9 | int I2CWriteByteTo(int fd, int pos, unsigned char byteData, int wait_ms); 10 | int I2CReadByteFrom(int fd, int pos, int wait_ms); 11 | int I2CWriteByte(int fd, unsigned char byteData, int wait_ms); 12 | int I2CReadByte(int fd, int wait_ms); 13 | #endif 14 | -------------------------------------------------------------------------------- /lib/includes/libfahw-iio.h: -------------------------------------------------------------------------------- 1 | #ifndef __FRIENDLYARM_HARDWARE_IIO_H__ 2 | #define __FRIENDLYARM_HARDWARE_IIO_H__ 3 | 4 | // DHT11 5 | #define DHT_TEMP (1) 6 | #define DHT_HUMIDITY (2) 7 | #define DHT11_SYS_PATH "/sys/devices/platform/dht11/" 8 | #define SET_DHT11_PIN (0) 9 | #define UNSET_DHT11_PIN (1) 10 | #define GET_DHT11_PIN (4) 11 | int dht11Read(int type, int *data); 12 | 13 | // HCSR04 14 | #define HCSR04_PATH "/sys/class/hcsr04/value" 15 | #define HCSR04_MAX_DISTANCE (200000) 16 | #define HCSR04_PER_METRE (58) 17 | struct HCSR04_resource { 18 | int gpio; 19 | }; 20 | 21 | int Hcsr04Init(int echoPin); 22 | int Hcsr04Read(int *data); 23 | void Hcsr04DeInit(); 24 | #endif 25 | -------------------------------------------------------------------------------- /lib/includes/libfahw-mcp23017.h: -------------------------------------------------------------------------------- 1 | #ifndef __FRIENDLYARM_HARDWARE_MCP23017_H__ 2 | #define __FRIENDLYARM_HARDWARE_MCP23017_H__ 3 | 4 | #define MCP23017_ADDRESS (0x27) 5 | 6 | int mcpInit(int i2cDev); 7 | int mcpWriteData8(int devFD, unsigned char data); 8 | int mcpWriteData4(int devFD, unsigned char data); 9 | int mcpWriteCmd8(int devFD, unsigned char command); 10 | int mcpWriteCmd4(int devFD, unsigned char command); 11 | void mcpDeInit(int devFD); 12 | 13 | int LCD1602KeyInit(int i2cDev); 14 | int LCD1602KeyClear(int devFD); 15 | int LCD1602KeyDispChar(int devFD, unsigned char x, unsigned char y, unsigned char data); 16 | int LCD1602KeyDispStr(int devFD, unsigned char x, unsigned char y, char *str); 17 | int LCD1602KeyDispLines(int devFD, char* line1, char* line2); 18 | int LCD1602GetKey(int devFD); 19 | void LCD1602KeyDeInit(int devFD); 20 | 21 | #define REG_IODIRA (0x0) 22 | #define REG_IODIRB (0x1) 23 | #define REG_IOCON (0x0a) 24 | #define REG_GPIOA (0x12) 25 | #define REG_GPIOB (0x13) 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /lib/includes/libfahw-oled.h: -------------------------------------------------------------------------------- 1 | #ifndef __FRIENDLYARM_HARDWARE_OLED_H__ 2 | #define __FRIENDLYARM_HARDWARE_OLED_H__ 3 | 4 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) 5 | #define OLED_WIDTH (132) 6 | #define OLED_HEIGHT (64) 7 | 8 | int OLEDDisp8x16Str(int devFD, int x, int y, char ch[]); 9 | int OLEDDisp8x16Char(int devFD, int x, int y, char ch); 10 | int OLEDCleanScreen(int devFD); 11 | int OLEDInit(int resetPin, int cmdDatPin); 12 | void OLEDDeInit(int devFD); 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /lib/includes/libfahw-pcf8574.h: -------------------------------------------------------------------------------- 1 | #ifndef __FRIENDLYARM_HARDWARE_PCF8574_H__ 2 | #define __FRIENDLYARM_HARDWARE_PCF8574_H__ 3 | 4 | #define PCF8574_ADDRESS (0x27) 5 | 6 | int pcf8574Init(int i2cDev); 7 | int pcf8574WriteData8(int devFD, unsigned char data); 8 | int pcf8574WriteData4(int devFD, unsigned char data); 9 | int pcf8574WriteCmd8(int devFD, unsigned char command); 10 | int pcf8574WriteCmd4(int devFD, unsigned char command); 11 | void pcf8574DeInit(int devFD); 12 | 13 | int LCD1602Init(int i2cDev); 14 | int LCD1602Clear(int devFD); 15 | int LCD1602DispChar(int devFD, unsigned char x, unsigned char y, unsigned char data); 16 | int LCD1602DispStr(int devFD, unsigned char x, unsigned char y, char *str); 17 | int LCD1602DispLines(int devFD, char* line1, char* line2); 18 | void LCD1602DeInit(int devFD); 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /lib/includes/libfahw-pcf8591.h: -------------------------------------------------------------------------------- 1 | #ifndef __FRIENDLYARM_HARDWARE_PCF8591_H__ 2 | #define __FRIENDLYARM_HARDWARE_PCF8591_H__ 3 | 4 | #define PCF8591_SYS_PATH "/sys/bus/i2c/drivers/pcf8591/" 5 | 6 | int pcf8591Read(int channel, int *value); 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /lib/includes/libfahw-pwm.h: -------------------------------------------------------------------------------- 1 | #ifndef __FRIENDLYARM_HARDWARE_PWM_H__ 2 | #define __FRIENDLYARM_HARDWARE_PWM_H__ 3 | 4 | #define PWM_IOCTL_SET_FREQ (0x1) 5 | #define PWM_IOCTL_STOP (0x0) 6 | #define PWM_IOCTL_CONFIG (0x4) 7 | #define PWM_IOCTL_RELEASE (0x8) 8 | 9 | int initPwmGPIO(int board); 10 | int PWMPlay(int pin, int freq, int duty); 11 | int PWMStop(int pin); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /lib/includes/libfahw-spi.h: -------------------------------------------------------------------------------- 1 | #ifndef __FRIENDLYARM_HARDWARE_SPI_H__ 2 | #define __FRIENDLYARM_HARDWARE_SPI_H__ 3 | 4 | 5 | // SPIBitOrder 6 | #define LSBFIRST (0) ///< LSB First 7 | #define MSBFIRST (1) ///< MSB First 8 | 9 | // SPIMode 10 | #define SPI_MODE0 (0) ///< CPOL (0, CPHA (0 11 | #define SPI_MODE1 (1) ///< CPOL (0, CPHA (1 12 | #define SPI_MODE2 (2) ///< CPOL (1, CPHA (0 13 | #define SPI_MODE3 (3) ///< CPOL (1, CPHA (1 14 | 15 | 16 | #define SPI_CPHA (0x01) 17 | #define SPI_CPOL (0x02) 18 | #define SPI_CS_HIGH (0x04) 19 | #define SPI_LSB_FIRST (0x08) 20 | #define SPI_3WIRE (0x10) 21 | #define SPI_LOOP (0x20) 22 | #define SPI_NO_CS (0x40) 23 | #define SPI_READY (0x80) 24 | 25 | // SPIClockDivider 26 | #define SPI_CLOCK_DIV65536 (0) ///< 65536 (256us (4kHz 27 | #define SPI_CLOCK_DIV32768 (32768) ///< 32768 (126us (8kHz 28 | #define SPI_CLOCK_DIV16384 (16384) ///< 16384 (64us (15.625kHz 29 | #define SPI_CLOCK_DIV8192 (8192) ///< 8192 (32us (31.25kHz 30 | #define SPI_CLOCK_DIV4096 (4096) ///< 4096 (16us (62.5kHz 31 | #define SPI_CLOCK_DIV2048 (2048) ///< 2048 (8us (125kHz 32 | #define SPI_CLOCK_DIV1024 (1024) ///< 1024 (4us (250kHz 33 | #define SPI_CLOCK_DIV512 (512) ///< 512 (2us (500kHz 34 | #define SPI_CLOCK_DIV256 (256) ///< 256 (1us (1MHz 35 | #define SPI_CLOCK_DIV128 (128) ///< 128 (500ns (= 2MHz 36 | #define SPI_CLOCK_DIV64 (64) ///< 64 (250ns (4MHz 37 | #define SPI_CLOCK_DIV32 (32) ///< 32 (125ns (8MHz 38 | #define SPI_CLOCK_DIV16 (16) ///< 16 (50ns (20MHz 39 | #define SPI_CLOCK_DIV8 (8) ///< 8 (25ns (40MHz 40 | #define SPI_CLOCK_DIV4 (4) ///< 4 (12.5ns 80MHz 41 | #define SPI_CLOCK_DIV2 (2) ///< 2 (6.25ns (160MHz 42 | #define SPI_CLOCK_DIV1 (1) ///< 0 (256us (4kHz 43 | 44 | 45 | int setSPIWriteBitsPerWord( int spi_fd, int bits ); 46 | int setSPIReadBitsPerWord( int spi_fd, int bits ); 47 | int setSPIBitOrder( int spi_fd, int order); 48 | int setSPIMaxSpeed(int spi_fd, unsigned int spi_speed); 49 | int setSPIDataMode( int spi_fd, int mode); 50 | int SPItransferOneByte( int spi_fd, unsigned char byteData, int spi_delay, int spi_speed, int spi_bits); 51 | int SPItransferBytes( 52 | int spi_fd 53 | , unsigned char * writeData 54 | , int writeLen 55 | , unsigned char * readBuffer 56 | , int readLen 57 | , int spi_delay 58 | , int spi_speed 59 | , int spi_bits 60 | ); 61 | int writeBytesToSPI( 62 | int spi_fd 63 | , unsigned char * writeData 64 | , int writeLen 65 | , int spi_delay 66 | , int spi_speed 67 | , int spi_bits 68 | ); 69 | int readBytesFromSPI( 70 | int spi_fd 71 | , unsigned char * readBuffer 72 | , int readLen 73 | , int spi_delay 74 | , int spi_speed 75 | , int spi_bits 76 | ); 77 | 78 | 79 | #define SPI0_PATH "/dev/spidev0.0" 80 | 81 | #endif 82 | -------------------------------------------------------------------------------- /lib/includes/libfahw-w1.h: -------------------------------------------------------------------------------- 1 | #ifndef __FRIENDLYARM_HARDWARE_W1_H__ 2 | #define __FRIENDLYARM_HARDWARE_W1_H__ 3 | 4 | #define DS18B20_SYS_PATH "/sys/devices/w1_bus_master1/" 5 | int ds18b20Read(char * temperature); 6 | 7 | #endif 8 | -------------------------------------------------------------------------------- /lib/includes/libfahw.h: -------------------------------------------------------------------------------- 1 | #ifndef __FRIENDLYARM_HARDWARE_H__ 2 | #define __FRIENDLYARM_HARDWARE_H__ 3 | 4 | #include "../common.h" 5 | #include "libfahw-filectl.h" 6 | #include "libfahw-gpio.h" 7 | #include "libfahw-i2c.h" 8 | #include "libfahw-iio.h" 9 | #include "libfahw-pcf8591.h" 10 | #include "libfahw-pcf8574.h" 11 | #include "libfahw-adxl34x.h" 12 | #include "libfahw-GPIOSensor.h" 13 | #include "libfahw-hmc5883.h" 14 | #include "libfahw-w1.h" 15 | #include "libfahw-pwm.h" 16 | #include "libfahw-mcp23017.h" 17 | #include "libfahw-bmp180.h" 18 | #include "libfahw-encoder.h" 19 | #include "libfahw-spi.h" 20 | #include "libfahw-oled.h" 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /lib/includes/spi_enum.h: -------------------------------------------------------------------------------- 1 | #ifndef __SPI_ENUM__ 2 | #define __SPI_ENUM__ 3 | 4 | /// SPIBitOrder 5 | /// Specifies the SPI data bit ordering 6 | typedef enum 7 | { 8 | LSBFIRST = 0, ///< LSB First 9 | MSBFIRST = 1 ///< MSB First 10 | }SPIBitOrder; 11 | 12 | /// SPIMode 13 | /// Specify the SPI data mode 14 | typedef enum 15 | { 16 | SPI_MODE0 = 0, ///< CPOL = 0, CPHA = 0 17 | SPI_MODE1 = 1, ///< CPOL = 0, CPHA = 1 18 | SPI_MODE2 = 2, ///< CPOL = 1, CPHA = 0 19 | SPI_MODE3 = 3, ///< CPOL = 1, CPHA = 1 20 | }SPIMode; 21 | 22 | /// SPIClockDivider 23 | /// Specifies the divider used to generate the SPI clock from the system clock. 24 | /// Figures below give the divider, clock period and clock frequency. 25 | typedef enum 26 | { 27 | SPI_CLOCK_DIV65536 = 0, ///< 65536 = 256us = 4kHz 28 | SPI_CLOCK_DIV32768 = 32768, ///< 32768 = 126us = 8kHz 29 | SPI_CLOCK_DIV16384 = 16384, ///< 16384 = 64us = 15.625kHz 30 | SPI_CLOCK_DIV8192 = 8192, ///< 8192 = 32us = 31.25kHz 31 | SPI_CLOCK_DIV4096 = 4096, ///< 4096 = 16us = 62.5kHz 32 | SPI_CLOCK_DIV2048 = 2048, ///< 2048 = 8us = 125kHz 33 | SPI_CLOCK_DIV1024 = 1024, ///< 1024 = 4us = 250kHz 34 | SPI_CLOCK_DIV512 = 512, ///< 512 = 2us = 500kHz 35 | SPI_CLOCK_DIV256 = 256, ///< 256 = 1us = 1MHz 36 | SPI_CLOCK_DIV128 = 128, ///< 128 = 500ns = = 2MHz 37 | SPI_CLOCK_DIV64 = 64, ///< 64 = 250ns = 4MHz 38 | SPI_CLOCK_DIV32 = 32, ///< 32 = 125ns = 8MHz 39 | SPI_CLOCK_DIV16 = 16, ///< 16 = 50ns = 20MHz 40 | SPI_CLOCK_DIV8 = 8, ///< 8 = 25ns = 40MHz 41 | SPI_CLOCK_DIV4 = 4, ///< 4 = 12.5ns 80MHz 42 | SPI_CLOCK_DIV2 = 2, ///< 2 = 6.25ns = 160MHz 43 | SPI_CLOCK_DIV1 = 1, ///< 0 = 256us = 4kHz 44 | } SPIClockDivider; 45 | 46 | #endif -------------------------------------------------------------------------------- /lib/includes/spidev.h: -------------------------------------------------------------------------------- 1 | /* 2 | * include/linux/spi/spidev.h 3 | * 4 | * Copyright (C) 2006 SWAPP 5 | * Andrea Paterniani 6 | * 7 | * This program is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 20 | */ 21 | 22 | #ifndef SPIDEV_H 23 | #define SPIDEV_H 24 | 25 | #include 26 | 27 | /* User space versions of kernel symbols for SPI clocking modes, 28 | * matching 29 | */ 30 | 31 | #define SPI_CPHA 0x01 32 | #define SPI_CPOL 0x02 33 | 34 | #define SPI_MODE_0 (0|0) 35 | #define SPI_MODE_1 (0|SPI_CPHA) 36 | #define SPI_MODE_2 (SPI_CPOL|0) 37 | #define SPI_MODE_3 (SPI_CPOL|SPI_CPHA) 38 | 39 | #define SPI_CS_HIGH 0x04 40 | #define SPI_LSB_FIRST 0x08 41 | #define SPI_3WIRE 0x10 42 | #define SPI_LOOP 0x20 43 | #define SPI_NO_CS 0x40 44 | #define SPI_READY 0x80 45 | 46 | /*---------------------------------------------------------------------------*/ 47 | 48 | /* IOCTL commands */ 49 | 50 | #define SPI_IOC_MAGIC 'k' 51 | 52 | /** 53 | * struct spi_ioc_transfer - describes a single SPI transfer 54 | * @tx_buf: Holds pointer to userspace buffer with transmit data, or null. 55 | * If no data is provided, zeroes are shifted out. 56 | * @rx_buf: Holds pointer to userspace buffer for receive data, or null. 57 | * @len: Length of tx and rx buffers, in bytes. 58 | * @speed_hz: Temporary override of the device's bitrate. 59 | * @bits_per_word: Temporary override of the device's wordsize. 60 | * @delay_usecs: If nonzero, how long to delay after the last bit transfer 61 | * before optionally deselecting the device before the next transfer. 62 | * @cs_change: True to deselect device before starting the next transfer. 63 | * 64 | * This structure is mapped directly to the kernel spi_transfer structure; 65 | * the fields have the same meanings, except of course that the pointers 66 | * are in a different address space (and may be of different sizes in some 67 | * cases, such as 32-bit i386 userspace over a 64-bit x86_64 kernel). 68 | * Zero-initialize the structure, including currently unused fields, to 69 | * accomodate potential future updates. 70 | * 71 | * SPI_IOC_MESSAGE gives userspace the equivalent of kernel spi_sync(). 72 | * Pass it an array of related transfers, they'll execute together. 73 | * Each transfer may be half duplex (either direction) or full duplex. 74 | * 75 | * struct spi_ioc_transfer mesg[4]; 76 | * ... 77 | * status = ioctl(fd, SPI_IOC_MESSAGE(4), mesg); 78 | * 79 | * So for example one transfer might send a nine bit command (right aligned 80 | * in a 16-bit word), the next could read a block of 8-bit data before 81 | * terminating that command by temporarily deselecting the chip; the next 82 | * could send a different nine bit command (re-selecting the chip), and the 83 | * last transfer might write some register values. 84 | */ 85 | struct spi_ioc_transfer { 86 | __u64 tx_buf; 87 | __u64 rx_buf; 88 | 89 | __u32 len; 90 | __u32 speed_hz; 91 | 92 | __u16 delay_usecs; 93 | __u8 bits_per_word; 94 | __u8 cs_change; 95 | __u32 pad; 96 | 97 | /* If the contents of 'struct spi_ioc_transfer' ever change 98 | * incompatibly, then the ioctl number (currently 0) must change; 99 | * ioctls with constant size fields get a bit more in the way of 100 | * error checking than ones (like this) where that field varies. 101 | * 102 | * NOTE: struct layout is the same in 64bit and 32bit userspace. 103 | */ 104 | }; 105 | 106 | /* not all platforms use or _IOC_TYPECHECK() ... */ 107 | #define SPI_MSGSIZE(N) \ 108 | ((((N)*(sizeof (struct spi_ioc_transfer))) < (1 << _IOC_SIZEBITS)) \ 109 | ? ((N)*(sizeof (struct spi_ioc_transfer))) : 0) 110 | #define SPI_IOC_MESSAGE(N) _IOW(SPI_IOC_MAGIC, 0, char[SPI_MSGSIZE(N)]) 111 | 112 | 113 | /* Read / Write of SPI mode (SPI_MODE_0..SPI_MODE_3) */ 114 | #define SPI_IOC_RD_MODE _IOR(SPI_IOC_MAGIC, 1, __u8) 115 | #define SPI_IOC_WR_MODE _IOW(SPI_IOC_MAGIC, 1, __u8) 116 | 117 | /* Read / Write SPI bit justification */ 118 | #define SPI_IOC_RD_LSB_FIRST _IOR(SPI_IOC_MAGIC, 2, __u8) 119 | #define SPI_IOC_WR_LSB_FIRST _IOW(SPI_IOC_MAGIC, 2, __u8) 120 | 121 | /* Read / Write SPI device word length (1..N) */ 122 | #define SPI_IOC_RD_BITS_PER_WORD _IOR(SPI_IOC_MAGIC, 3, __u8) 123 | #define SPI_IOC_WR_BITS_PER_WORD _IOW(SPI_IOC_MAGIC, 3, __u8) 124 | 125 | /* Read / Write SPI device default max speed hz */ 126 | #define SPI_IOC_RD_MAX_SPEED_HZ _IOR(SPI_IOC_MAGIC, 4, __u32) 127 | #define SPI_IOC_WR_MAX_SPEED_HZ _IOW(SPI_IOC_MAGIC, 4, __u32) 128 | 129 | 130 | 131 | #endif /* SPIDEV_H */ 132 | -------------------------------------------------------------------------------- /lib/led.c: -------------------------------------------------------------------------------- 1 | #include "common.h" 2 | 3 | EXPORT int getLedState(int ledID) { 4 | clearLastError(); 5 | char ledPath[64]; 6 | int ledVal = -1; 7 | sprintf(ledPath, "/sys/class/leds/led%d/brightness", ledID); 8 | 9 | if (ledID>=0 && ledID<=4) { 10 | ledVal = readIntValueFromFile(ledPath); 11 | if (ledVal > 0) { 12 | ledVal = 1; 13 | } else { 14 | ledVal = 0; 15 | } 16 | } else { 17 | setLastError("Unsupport led %d", ledID); 18 | } 19 | 20 | return ledVal; 21 | } 22 | 23 | EXPORT int setLedState(int ledID, int ledState) { 24 | clearLastError(); 25 | int ret = -1; 26 | char ledPath[64]; 27 | sprintf(ledPath, "/sys/class/leds/led%d/brightness", ledID); 28 | 29 | if (ledID>=0 && ledID<=4) { 30 | ret = writeIntValueToFile(ledPath, ledState); 31 | } else { 32 | setLastError("Unsupport led %d", ledID); 33 | } 34 | return ret; 35 | } 36 | 37 | EXPORT int setLedStateForTiny4412(int ledID, int ledState) { 38 | clearLastError(); 39 | int fd = open("/dev/leds0", O_RDONLY); 40 | if (fd < 0) { 41 | fd = open("/dev/leds", O_RDONLY); 42 | } 43 | if (fd < 0) { 44 | setLastError("Can't open led device!"); 45 | return -1; 46 | } 47 | 48 | if ((ledState == 0 || ledState == 1) && (ledID >= 0 && ledID <= 3)) { 49 | if (ioctl(fd, ledState, ledID) == -1) { 50 | setLastError("set LED state error!"); 51 | goto ERROR; 52 | } 53 | } else { 54 | setLastError("Wrong args!"); 55 | goto ERROR; 56 | } 57 | 58 | close(fd); 59 | return 0; 60 | 61 | ERROR: 62 | close(fd); 63 | return -1; 64 | } 65 | -------------------------------------------------------------------------------- /lib/mcp23017.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "libfahw-mcp23017.h" 3 | #include "libfahw-filectl.h" 4 | #include "libfahw-i2c.h" 5 | #include "includes/i2c-dev.h" 6 | #include "common.h" 7 | 8 | static unsigned char RS = 0x01; 9 | //static unsigned char RW = 0x02; 10 | static unsigned char En = 0x04; 11 | static unsigned char BL = 0x08; 12 | 13 | static int pulseEnable(int devFD, unsigned char data) 14 | { 15 | clearLastError(); 16 | usleep(1000); 17 | if (I2CWriteByteTo(devFD, REG_GPIOA, (data | En), 0) == -1) { 18 | return -1; 19 | } 20 | usleep(1000); 21 | if (I2CWriteByteTo(devFD, REG_GPIOA, (data & ~En), 0) == -1) { 22 | return -1; 23 | } 24 | return 0; 25 | } 26 | 27 | // 写四位指令 28 | EXPORT int mcpWriteCmd4(int devFD, unsigned char command) 29 | { 30 | clearLastError(); 31 | 32 | if (I2CWriteByteTo(devFD, REG_GPIOA, command | BL, 0) == -1) { 33 | return -1; 34 | } 35 | if (pulseEnable(devFD, command | BL) == -1) { 36 | setLastError("Fail to pulseEnable"); 37 | return -1; 38 | } 39 | return 0; 40 | } 41 | 42 | // 写八位指令 43 | EXPORT int mcpWriteCmd8(int devFD, unsigned char command) 44 | { 45 | clearLastError(); 46 | unsigned char command_H = command & 0xf0; 47 | unsigned char command_L = (command << 4) & 0xf0; 48 | if (mcpWriteCmd4(devFD, command_H) == -1) { 49 | setLastError("Fail to write cmd high 4bit"); 50 | return -1; 51 | } 52 | 53 | if (mcpWriteCmd4(devFD, command_L) == -1) { 54 | setLastError("Fail to write cmd low 4bit"); 55 | return -1; 56 | } 57 | return 0; 58 | } 59 | 60 | // 写四位数据 61 | EXPORT int mcpWriteData4(int devFD, unsigned char data) 62 | { 63 | clearLastError(); 64 | if (I2CWriteByteTo(devFD, REG_GPIOA, (data | RS | BL), 0) == -1) { 65 | return -1; 66 | } 67 | if (pulseEnable(devFD, (data | RS | BL)) == -1) { 68 | return -1; 69 | } 70 | return 0; 71 | } 72 | 73 | // 写八位数据 74 | EXPORT int mcpWriteData8(int devFD, unsigned char data) 75 | { 76 | clearLastError(); 77 | unsigned char data_H = data & 0xf0; 78 | unsigned char data_L = (data << 4) & 0xf0; 79 | if (mcpWriteData4(devFD, data_H) == -1) { 80 | setLastError("Fail to write dat low 4bit"); 81 | return -1; 82 | } 83 | if (mcpWriteData4(devFD, data_L) == -1) { 84 | setLastError("Fail to write dat low 4bit"); 85 | return -1; 86 | } 87 | return 0; 88 | } 89 | 90 | EXPORT int mcpInit(int i2cDev) 91 | { 92 | clearLastError(); 93 | int devFD; 94 | char buf[16]; 95 | memset(buf, 0, sizeof(buf)); 96 | sprintf(buf, "/dev/i2c-%d", i2cDev); 97 | if ((devFD = openHW(buf, O_RDWR)) < 0) { 98 | setLastError("Fail to open I2C device mcp"); 99 | return -1; 100 | } else { 101 | if (setI2CSlave(devFD, MCP23017_ADDRESS) < 0) { 102 | setLastError("Fail to set mcp I2C slave address"); 103 | closeHW(devFD); 104 | return -1; 105 | } 106 | } 107 | I2CWriteByteTo(devFD, REG_IOCON, 0x20, 0); // set not auto 108 | I2CWriteByteTo(devFD, REG_IODIRA, 0x0, 0); // set GPA output 109 | I2CWriteByteTo(devFD, REG_IODIRB, 0xFF, 0); // set GPB input 110 | 111 | return devFD; 112 | } 113 | 114 | EXPORT void mcpDeInit(int devFD) 115 | { 116 | clearLastError(); 117 | closeHW(devFD); 118 | } 119 | 120 | // 在第y行第x个显示字符 121 | EXPORT int LCD1602KeyDispChar(int devFD, unsigned char x, unsigned char y, unsigned char data) 122 | { 123 | clearLastError(); 124 | unsigned char addr; 125 | if (y == 0) 126 | addr = 0x80 + x; 127 | else 128 | addr = 0xc0 + x; 129 | 130 | if (mcpWriteCmd8(devFD, addr) == -1) { 131 | setLastError("Fail to write cmd 8bit"); 132 | return -1; 133 | } 134 | if (mcpWriteData8(devFD, data) == -1) { 135 | setLastError("Fail to write data 8bit"); 136 | return -1; 137 | } 138 | 139 | return 0; 140 | } 141 | 142 | // 在第y行第x个开始写字符串 143 | EXPORT int LCD1602KeyDispStr(int devFD, unsigned char x, unsigned char y, char *str) 144 | { 145 | clearLastError(); 146 | unsigned char addr; 147 | addr = ((y + 2) * 0x40) + x; 148 | 149 | if (mcpWriteCmd8(devFD, addr) == -1) { 150 | setLastError("Fail to write cmd 8bit"); 151 | return -1; 152 | } 153 | while (*str != '\0') { 154 | if (mcpWriteData8(devFD, *str++) == -1) { 155 | setLastError("Fail to write data 8bit"); 156 | return -1; 157 | } 158 | } 159 | 160 | return 0; 161 | } 162 | 163 | EXPORT int LCD1602KeyDispLines(int devFD, char* line1, char* line2) { 164 | int ret = LCD1602KeyDispStr(devFD, 0, 0, line1); 165 | if (ret != -1) { 166 | ret = LCD1602KeyDispStr(devFD, 0, 1, line2); 167 | } 168 | return ret; 169 | } 170 | 171 | EXPORT int LCD1602KeyInit(int i2cDev) 172 | { 173 | clearLastError(); 174 | int devFD; 175 | if ((devFD = mcpInit(i2cDev)) == -1) { 176 | setLastError("Fail to init mcp23017"); 177 | return -1; 178 | } 179 | 180 | usleep(1000*15); 181 | if (mcpWriteCmd4(devFD, 0x03 << 4) == -1) { 182 | return -1; 183 | } 184 | usleep(100*41); 185 | if (mcpWriteCmd4(devFD, 0x03 << 4) == -1) { 186 | return -1; 187 | } 188 | usleep(100); 189 | if (mcpWriteCmd4(devFD, 0x03 << 4) == -1) { // 8位数据总线 190 | return -1; 191 | } 192 | if (mcpWriteCmd4(devFD, 0x02 << 4) == -1) { // 4位数据总线 193 | return -1; 194 | } 195 | if (mcpWriteCmd8(devFD, 0x28) == -1) { // 4位数据总线,显示2行,5x7 196 | return -1; 197 | } 198 | if (mcpWriteCmd8(devFD, 0x0c) == -1) { // 显示开,无光标 199 | return -1; 200 | } 201 | usleep(2000); 202 | if (mcpWriteCmd8(devFD, 0x06) == -1) { // 光标右移,显示屏不移动 203 | return -1; 204 | } 205 | if (mcpWriteCmd8(devFD, 0x02) == -1) { // 光标复位 206 | return -1; 207 | } 208 | usleep(2000); 209 | 210 | return devFD; 211 | } 212 | 213 | EXPORT int LCD1602KeyClear(int devFD) { 214 | clearLastError(); 215 | if (mcpWriteCmd8(devFD, 0x01) == -1) { 216 | setLastError("Fail to mcp write cmd 0x01"); 217 | return -1; 218 | } 219 | return 0; 220 | } 221 | 222 | EXPORT void LCD1602KeyDeInit(int devFD) 223 | { 224 | clearLastError(); 225 | closeHW(devFD); 226 | } 227 | 228 | EXPORT int LCD1602GetKey(int devFD) 229 | { 230 | clearLastError(); 231 | return I2CReadByteFrom(devFD, REG_GPIOB, 0); 232 | } 233 | -------------------------------------------------------------------------------- /lib/oled.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "libfahw-filectl.h" 4 | #include "libfahw-spi.h" 5 | #include "libfahw-oled.h" 6 | #include "libfahw-gpio.h" 7 | #include "common.h" 8 | 9 | static int gCmdDatPin = -1; 10 | static int gResetPin = -1; 11 | static unsigned char spiMode = 0; 12 | static unsigned char spiBits = 8; 13 | static unsigned int spiSpeed = 25000000; 14 | static short spiDelay; 15 | // 8 x 16 font 16 | static unsigned char OLEDCharMap[]= 17 | { 18 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// 0 19 | 0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x33,0x30,0x00,0x00,0x00,//! 1 20 | 0x00,0x10,0x0C,0x06,0x10,0x0C,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//" 2 21 | 0x40,0xC0,0x78,0x40,0xC0,0x78,0x40,0x00,0x04,0x3F,0x04,0x04,0x3F,0x04,0x04,0x00,//# 3 22 | 0x00,0x70,0x88,0xFC,0x08,0x30,0x00,0x00,0x00,0x18,0x20,0xFF,0x21,0x1E,0x00,0x00,//$ 4 23 | 0xF0,0x08,0xF0,0x00,0xE0,0x18,0x00,0x00,0x00,0x21,0x1C,0x03,0x1E,0x21,0x1E,0x00,//% 5 24 | 0x00,0xF0,0x08,0x88,0x70,0x00,0x00,0x00,0x1E,0x21,0x23,0x24,0x19,0x27,0x21,0x10,//& 6 25 | 0x10,0x16,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//' 7 26 | 0x00,0x00,0x00,0xE0,0x18,0x04,0x02,0x00,0x00,0x00,0x00,0x07,0x18,0x20,0x40,0x00,//( 8 27 | 0x00,0x02,0x04,0x18,0xE0,0x00,0x00,0x00,0x00,0x40,0x20,0x18,0x07,0x00,0x00,0x00,//) 9 28 | 0x40,0x40,0x80,0xF0,0x80,0x40,0x40,0x00,0x02,0x02,0x01,0x0F,0x01,0x02,0x02,0x00,//* 10 29 | 0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x1F,0x01,0x01,0x01,0x00,//+ 11 30 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xB0,0x70,0x00,0x00,0x00,0x00,0x00,//, 12 31 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,//- 13 32 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,//. 14 33 | 0x00,0x00,0x00,0x00,0x80,0x60,0x18,0x04,0x00,0x60,0x18,0x06,0x01,0x00,0x00,0x00,/// 15 34 | 0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,//0 16 35 | 0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//1 17 36 | 0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,//2 18 37 | 0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,//3 19 38 | 0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00,//4 20 39 | 0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,//5 21 40 | 0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00,//6 22 41 | 0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,//7 23 42 | 0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,//8 24 43 | 0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00,//9 25 44 | 0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,//: 26 45 | 0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x60,0x00,0x00,0x00,0x00,//; 27 46 | 0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x00,0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x00,//< 28 47 | 0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x00,//= 29 48 | 0x00,0x08,0x10,0x20,0x40,0x80,0x00,0x00,0x00,0x20,0x10,0x08,0x04,0x02,0x01,0x00,//> 30 49 | 0x00,0x70,0x48,0x08,0x08,0x08,0xF0,0x00,0x00,0x00,0x00,0x30,0x36,0x01,0x00,0x00,//? 31 50 | 0xC0,0x30,0xC8,0x28,0xE8,0x10,0xE0,0x00,0x07,0x18,0x27,0x24,0x23,0x14,0x0B,0x00,//@ 32 51 | 0x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00,0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20,//A 33 52 | 0x08,0xF8,0x88,0x88,0x88,0x70,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x11,0x0E,0x00,//B 34 53 | 0xC0,0x30,0x08,0x08,0x08,0x08,0x38,0x00,0x07,0x18,0x20,0x20,0x20,0x10,0x08,0x00,//C 35 54 | 0x08,0xF8,0x08,0x08,0x08,0x10,0xE0,0x00,0x20,0x3F,0x20,0x20,0x20,0x10,0x0F,0x00,//D 36 55 | 0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x20,0x23,0x20,0x18,0x00,//E 37 56 | 0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x00,0x03,0x00,0x00,0x00,//F 38 57 | 0xC0,0x30,0x08,0x08,0x08,0x38,0x00,0x00,0x07,0x18,0x20,0x20,0x22,0x1E,0x02,0x00,//G 39 58 | 0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x20,0x3F,0x21,0x01,0x01,0x21,0x3F,0x20,//H 40 59 | 0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//I 41 60 | 0x00,0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,0x00,//J 42 61 | 0x08,0xF8,0x88,0xC0,0x28,0x18,0x08,0x00,0x20,0x3F,0x20,0x01,0x26,0x38,0x20,0x00,//K 43 62 | 0x08,0xF8,0x08,0x00,0x00,0x00,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x20,0x30,0x00,//L 44 63 | 0x08,0xF8,0xF8,0x00,0xF8,0xF8,0x08,0x00,0x20,0x3F,0x00,0x3F,0x00,0x3F,0x20,0x00,//M 45 64 | 0x08,0xF8,0x30,0xC0,0x00,0x08,0xF8,0x08,0x20,0x3F,0x20,0x00,0x07,0x18,0x3F,0x00,//N 46 65 | 0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x10,0x20,0x20,0x20,0x10,0x0F,0x00,//O 47 66 | 0x08,0xF8,0x08,0x08,0x08,0x08,0xF0,0x00,0x20,0x3F,0x21,0x01,0x01,0x01,0x00,0x00,//P 48 67 | 0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x18,0x24,0x24,0x38,0x50,0x4F,0x00,//Q 49 68 | 0x08,0xF8,0x88,0x88,0x88,0x88,0x70,0x00,0x20,0x3F,0x20,0x00,0x03,0x0C,0x30,0x20,//R 50 69 | 0x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00,0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00,//S 51 70 | 0x18,0x08,0x08,0xF8,0x08,0x08,0x18,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//T 52 71 | 0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//U 53 72 | 0x08,0x78,0x88,0x00,0x00,0xC8,0x38,0x08,0x00,0x00,0x07,0x38,0x0E,0x01,0x00,0x00,//V 54 73 | 0xF8,0x08,0x00,0xF8,0x00,0x08,0xF8,0x00,0x03,0x3C,0x07,0x00,0x07,0x3C,0x03,0x00,//W 55 74 | 0x08,0x18,0x68,0x80,0x80,0x68,0x18,0x08,0x20,0x30,0x2C,0x03,0x03,0x2C,0x30,0x20,//X 56 75 | 0x08,0x38,0xC8,0x00,0xC8,0x38,0x08,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//Y 57 76 | 0x10,0x08,0x08,0x08,0xC8,0x38,0x08,0x00,0x20,0x38,0x26,0x21,0x20,0x20,0x18,0x00,//Z 58 77 | 0x00,0x00,0x00,0xFE,0x02,0x02,0x02,0x00,0x00,0x00,0x00,0x7F,0x40,0x40,0x40,0x00,//[ 59 78 | 0x00,0x0C,0x30,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x06,0x38,0xC0,0x00,//\ 60 79 | 0x00,0x02,0x02,0x02,0xFE,0x00,0x00,0x00,0x00,0x40,0x40,0x40,0x7F,0x00,0x00,0x00,//] 61 80 | 0x00,0x00,0x04,0x02,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//^ 62 81 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,//_ 63 82 | 0x00,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//` 64 83 | 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x19,0x24,0x22,0x22,0x22,0x3F,0x20,//a 65 84 | 0x08,0xF8,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x3F,0x11,0x20,0x20,0x11,0x0E,0x00,//b 66 85 | 0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x0E,0x11,0x20,0x20,0x20,0x11,0x00,//c 67 86 | 0x00,0x00,0x00,0x80,0x80,0x88,0xF8,0x00,0x00,0x0E,0x11,0x20,0x20,0x10,0x3F,0x20,//d 68 87 | 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00,//e 69 88 | 0x00,0x80,0x80,0xF0,0x88,0x88,0x88,0x18,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//f 70 89 | 0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x6B,0x94,0x94,0x94,0x93,0x60,0x00,//g 71 90 | 0x08,0xF8,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//h 72 91 | 0x00,0x80,0x98,0x98,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//i 73 92 | 0x00,0x00,0x00,0x80,0x98,0x98,0x00,0x00,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,//j 74 93 | 0x08,0xF8,0x00,0x00,0x80,0x80,0x80,0x00,0x20,0x3F,0x24,0x02,0x2D,0x30,0x20,0x00,//k 75 94 | 0x00,0x08,0x08,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//l 76 95 | 0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x20,0x3F,0x20,0x00,0x3F,0x20,0x00,0x3F,//m 77 96 | 0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//n 78 97 | 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//o 79 98 | 0x80,0x80,0x00,0x80,0x80,0x00,0x00,0x00,0x80,0xFF,0xA1,0x20,0x20,0x11,0x0E,0x00,//p 80 99 | 0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x0E,0x11,0x20,0x20,0xA0,0xFF,0x80,//q 81 100 | 0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x20,0x20,0x3F,0x21,0x20,0x00,0x01,0x00,//r 82 101 | 0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x33,0x24,0x24,0x24,0x24,0x19,0x00,//s 83 102 | 0x00,0x80,0x80,0xE0,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x1F,0x20,0x20,0x00,0x00,//t 84 103 | 0x80,0x80,0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x1F,0x20,0x20,0x20,0x10,0x3F,0x20,//u 85 104 | 0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x00,0x01,0x0E,0x30,0x08,0x06,0x01,0x00,//v 86 105 | 0x80,0x80,0x00,0x80,0x00,0x80,0x80,0x80,0x0F,0x30,0x0C,0x03,0x0C,0x30,0x0F,0x00,//w 87 106 | 0x00,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x31,0x2E,0x0E,0x31,0x20,0x00,//x 88 107 | 0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x80,0x81,0x8E,0x70,0x18,0x06,0x01,0x00,//y 89 108 | 0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x21,0x30,0x2C,0x22,0x21,0x30,0x00,//z 90 109 | 0x00,0x00,0x00,0x00,0x80,0x7C,0x02,0x02,0x00,0x00,0x00,0x00,0x00,0x3F,0x40,0x40,//{ 91 110 | 0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,//| 92 111 | 0x00,0x02,0x02,0x7C,0x80,0x00,0x00,0x00,0x00,0x40,0x40,0x3F,0x00,0x00,0x00,0x00,//} 93 112 | 0x00,0x06,0x01,0x01,0x02,0x02,0x04,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//~ 94 113 | }; 114 | 115 | // write 8 * 8 bitmap 116 | static int OLEDWriteData(int devFD, unsigned char dat) 117 | { 118 | int ret = 0; 119 | unsigned char tx[] = {dat}; 120 | int writeLen = ARRAY_SIZE(tx); 121 | int pin = gCmdDatPin; 122 | 123 | if (setGPIODirection(pin, GPIO_OUT) == -1) { 124 | ret = -1; 125 | } 126 | if (setGPIOValue(pin, GPIO_HIGH) == -1) { 127 | ret = -1; 128 | } 129 | if (writeBytesToSPI(devFD, tx, writeLen, spiDelay, spiSpeed, spiBits) == -1) { 130 | ret = -1; 131 | } 132 | return ret; 133 | } 134 | 135 | static int OLEDWriteCmd(int devFD, unsigned char cmd) 136 | { 137 | int ret = 0; 138 | unsigned char tx[] = {cmd}; 139 | int writeLen = ARRAY_SIZE(tx); 140 | int pin = gCmdDatPin; 141 | 142 | if (setGPIODirection(pin, GPIO_OUT) == -1) { 143 | ret = -1; 144 | } 145 | if (setGPIOValue(pin, GPIO_LOW) == -1) { 146 | ret = -1; 147 | } 148 | if ((writeBytesToSPI(devFD, tx, writeLen, spiDelay, spiSpeed, spiBits)) == -1) { 149 | ret = -1; 150 | } 151 | return ret; 152 | } 153 | 154 | static int OLEDSetPos(int devFD, int x, int y) 155 | { 156 | int ret = 0; 157 | 158 | y = y/8 + y%8; 159 | if (OLEDWriteCmd(devFD, 0xb0+y) == -1) { 160 | ret = -1; 161 | } 162 | if (OLEDWriteCmd(devFD, ((x&0xf0)>>4)|0x10) == -1) { 163 | ret = -1; 164 | } 165 | if (OLEDWriteCmd(devFD, (x&0x0f)|0x01) == -1) { 166 | ret = -1; 167 | } 168 | return ret; 169 | } 170 | 171 | static int OLEDReset(int devFD, int pin) 172 | { 173 | clearLastError(); 174 | int ret = -1; 175 | 176 | if ((ret = setGPIODirection(pin, GPIO_OUT)) == -1) { 177 | return -1; 178 | } 179 | if ((ret = setGPIOValue(pin, GPIO_LOW)) == -1) { 180 | return -1; 181 | } 182 | usleep(100); 183 | if ((ret = setGPIOValue(pin, GPIO_HIGH)) == -1) { 184 | return -1; 185 | } 186 | return ret; 187 | } 188 | 189 | static int OLEDSPIInit(int devFD) 190 | { 191 | clearLastError(); 192 | if (setSPIDataMode(devFD, spiMode) == -1 ) { 193 | return -1; 194 | } 195 | if (setSPIWriteBitsPerWord(devFD, spiBits) == -1 ) { 196 | return -1; 197 | } 198 | if (setSPIMaxSpeed(devFD, spiSpeed) == -1 ) { 199 | return -1; 200 | } 201 | return 0; 202 | } 203 | 204 | EXPORT int OLEDInit(int cmdDatPin, int resetPin) 205 | { 206 | clearLastError(); 207 | int ret = 0; 208 | int devFD; 209 | 210 | gResetPin = resetPin; 211 | gCmdDatPin = cmdDatPin; 212 | if ((devFD = openHW(SPI0_PATH, O_RDWR)) < 0) { 213 | setLastError("Fail to open SPI device MAX7219"); 214 | return -1; 215 | } 216 | if ((ret = OLEDSPIInit(devFD)) == -1) { 217 | setLastError("Fail to init SPI device OLED"); 218 | } 219 | if (exportGPIOPin(gResetPin) || exportGPIOPin(gCmdDatPin)) { 220 | setLastError("Fail to request resetPin or DCPin"); 221 | } 222 | if ((ret = OLEDReset(devFD, gResetPin)) == -1) { 223 | setLastError("Fail to reset OLED"); 224 | } 225 | 226 | OLEDWriteCmd(devFD, 0xae);//--turn off oled panel 227 | OLEDWriteCmd(devFD, 0x00);//---set low column address 228 | OLEDWriteCmd(devFD, 0x10);//---set high column address 229 | OLEDWriteCmd(devFD, 0x40);//--set start line address Set Mapping RAM Display Start Line (0x00~0x3F) 230 | OLEDWriteCmd(devFD, 0x81);//--set contrast control register 231 | OLEDWriteCmd(devFD, 0xcf); // Set SEG Output Current Brightness 232 | OLEDWriteCmd(devFD, 0xa1);//--Set SEG/Column Mapping 0xa0左右反置 0xa1正常 233 | OLEDWriteCmd(devFD, 0xc8);//Set COM/Row Scan Direction 0xc0上下反置 0xc8正常 234 | OLEDWriteCmd(devFD, 0xa6);//--set normal display 235 | OLEDWriteCmd(devFD, 0xa8);//--set multiplex ratio(1 to 64) 236 | OLEDWriteCmd(devFD, 0x3f);//--1/64 duty 237 | OLEDWriteCmd(devFD, 0xd3);//-set display offset Shift Mapping RAM Counter (0x00~0x3F) 238 | OLEDWriteCmd(devFD, 0x00);//-not offset 239 | OLEDWriteCmd(devFD, 0xd5);//--set display clock divide ratio/oscillator frequency 240 | OLEDWriteCmd(devFD, 0x80);//--set divide ratio, Set Clock as 100 Frames/Sec 241 | OLEDWriteCmd(devFD, 0xd9);//--set pre-charge period 242 | OLEDWriteCmd(devFD, 0xf1);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock 243 | OLEDWriteCmd(devFD, 0xda);//--set com pins hardware configuration 244 | OLEDWriteCmd(devFD, 0x12); 245 | OLEDWriteCmd(devFD, 0xdb);//--set vcomh 246 | OLEDWriteCmd(devFD, 0x40);//Set VCOM Deselect Level 247 | OLEDWriteCmd(devFD, 0x20);//-Set Page Addressing Mode (0x00/0x01/0x02) 248 | OLEDWriteCmd(devFD, 0x02);// 249 | OLEDWriteCmd(devFD, 0x8d);//--set Charge Pump enable/disable 250 | OLEDWriteCmd(devFD, 0x14);//--set(0x10) disable 251 | OLEDWriteCmd(devFD, 0xa4);// Disable Entire Display On (0xa4/0xa5) 252 | OLEDWriteCmd(devFD, 0xa6);// Disable Inverse Display On (0xa6/a7) 253 | OLEDWriteCmd(devFD, 0xaf);//--turn on oled panel 254 | 255 | /* if (OLEDCleanScreen(devFD) == -1) { 256 | ret = -1; 257 | } 258 | */ 259 | if (OLEDSetPos(devFD, 0, 0) == -1) { 260 | ret = -1; 261 | } 262 | if (ret == 0) { 263 | return devFD; 264 | } else { 265 | unexportGPIOPin(resetPin); 266 | unexportGPIOPin(cmdDatPin); 267 | return ret; 268 | } 269 | } 270 | 271 | EXPORT void OLEDDeInit(int devFD) 272 | { 273 | clearLastError(); 274 | unexportGPIOPin(gCmdDatPin); 275 | unexportGPIOPin(gResetPin); 276 | gCmdDatPin = -1; 277 | gResetPin = -1; 278 | closeHW(devFD); 279 | } 280 | 281 | EXPORT int OLEDDisp8x16Char(int devFD, int x, int y, char ch) 282 | { 283 | clearLastError(); 284 | int i = 0; 285 | int index = ch-32; 286 | 287 | if (x<0 || x>(OLED_WIDTH-8) || y<0 || y>(OLED_HEIGHT-16)) { 288 | setLastError("Unsupported OLED coordinate(%d %d) x:0~%d y:0~%d", x, y, OLED_WIDTH, OLED_HEIGHT); 289 | return -1; 290 | } 291 | 292 | // printf("x=%3d, y=%3d\n", x, y); 293 | OLEDSetPos(devFD, x, y); 294 | for (i=0; i<8; i++) 295 | OLEDWriteData(devFD,OLEDCharMap[index*16+i]); 296 | // printf("x=%3d, y=%3d\n", x, y+8); 297 | OLEDSetPos(devFD, x, y+8); 298 | for (i=0; i<8; i++) 299 | OLEDWriteData(devFD,OLEDCharMap[index*16+i+8]); 300 | 301 | return 0; 302 | } 303 | 304 | EXPORT int OLEDDisp8x16Str(int devFD, int x, int y, char ch[]) 305 | { 306 | clearLastError(); 307 | int i = 0; 308 | while (ch[i] != '\0') 309 | { 310 | if (OLEDDisp8x16Char(devFD, x, y, ch[i]) == -1) { 311 | setLastError("Fail to write 8x16 Char to OLED"); 312 | return -1; 313 | } 314 | x+=8; 315 | i++; 316 | } 317 | return 0; 318 | } 319 | 320 | EXPORT int OLEDCleanScreen(int devFD) 321 | { 322 | int x,y; 323 | unsigned char data = 0x00; 324 | int ret = 0; 325 | for (y=0; y<8; y++) 326 | { 327 | // set pos (y, 0) 328 | OLEDWriteCmd(devFD, 0xb0+y); 329 | OLEDWriteCmd(devFD, 0x00); 330 | OLEDWriteCmd(devFD, 0x10); 331 | for (x=0; x 2 | #include "libfahw-pcf8574.h" 3 | #include "libfahw-filectl.h" 4 | #include "libfahw-i2c.h" 5 | #include "includes/i2c-dev.h" 6 | #include "common.h" 7 | 8 | static unsigned char RS = 0x01; 9 | //static unsigned char RW = 0x02; 10 | static unsigned char En = 0x04; 11 | static unsigned char BL = 0x08; 12 | 13 | static int pulseEnable(int devFD, unsigned char data) 14 | { 15 | clearLastError(); 16 | if (I2CWriteByte(devFD, (data | En), 0) == -1) { 17 | return -1; 18 | } 19 | usleep(1); 20 | if (I2CWriteByte(devFD, (data & ~En), 0) == -1) { 21 | return -1; 22 | } 23 | return 0; 24 | } 25 | 26 | // 写四位指令 27 | EXPORT int pcf8574WriteCmd4(int devFD, unsigned char command) 28 | { 29 | clearLastError(); 30 | if (I2CWriteByte(devFD, command | BL, 0) == -1) { 31 | return -1; 32 | } 33 | if (pulseEnable(devFD, command | BL) == -1) { 34 | setLastError("Fail to pulseEnable"); 35 | return -1; 36 | } 37 | return 0; 38 | } 39 | 40 | // 写八位指令 41 | EXPORT int pcf8574WriteCmd8(int devFD, unsigned char command) 42 | { 43 | clearLastError(); 44 | unsigned char command_H = command & 0xf0; 45 | unsigned char command_L = (command << 4) & 0xf0; 46 | if (pcf8574WriteCmd4(devFD, command_H) == -1) { 47 | setLastError("Fail to write cmd high 4bit"); 48 | return -1; 49 | } 50 | 51 | if (pcf8574WriteCmd4(devFD, command_L) == -1) { 52 | setLastError("Fail to write cmd low 4bit"); 53 | return -1; 54 | } 55 | return 0; 56 | } 57 | 58 | // 写四位数据 59 | EXPORT int pcf8574WriteData4(int devFD, unsigned char data) 60 | { 61 | clearLastError(); 62 | if (I2CWriteByte(devFD, (data | RS | BL), 0) == -1) { 63 | return -1; 64 | } 65 | if (pulseEnable(devFD, (data | RS | BL)) == -1) { 66 | return -1; 67 | } 68 | return 0; 69 | } 70 | 71 | // 写八位数据 72 | EXPORT int pcf8574WriteData8(int devFD, unsigned char data) 73 | { 74 | clearLastError(); 75 | unsigned char data_H = data & 0xf0; 76 | unsigned char data_L = (data << 4) & 0xf0; 77 | if (pcf8574WriteData4(devFD, data_H) == -1) { 78 | setLastError("Fail to write dat low 4bit"); 79 | return -1; 80 | } 81 | if (pcf8574WriteData4(devFD, data_L) == -1) { 82 | setLastError("Fail to write dat low 4bit"); 83 | return -1; 84 | } 85 | return 0; 86 | } 87 | 88 | EXPORT int pcf8574Init(int i2cDev) 89 | { 90 | clearLastError(); 91 | int devFD; 92 | char buf[16]; 93 | memset(buf, 0, sizeof(buf)); 94 | sprintf(buf, "/dev/i2c-%d", i2cDev); 95 | if ((devFD = openHW(buf, O_RDWR)) < 0) { 96 | setLastError("Fail to open I2C device pcf8574"); 97 | return -1; 98 | } else { 99 | if (setI2CSlave(devFD, PCF8574_ADDRESS) < 0) { 100 | setLastError("Fail to set pcf8574 I2C slave address"); 101 | closeHW(devFD); 102 | return -1; 103 | } 104 | } 105 | return devFD; 106 | } 107 | 108 | EXPORT void pcf8574DeInit(int devFD) 109 | { 110 | clearLastError(); 111 | closeHW(devFD); 112 | } 113 | 114 | // 在第y行第x个显示字符 115 | EXPORT int LCD1602DispChar(int devFD, unsigned char x, unsigned char y, unsigned char data) 116 | { 117 | clearLastError(); 118 | unsigned char addr; 119 | if (y == 0) 120 | addr = 0x80 + x; 121 | else 122 | addr = 0xc0 + x; 123 | 124 | if (pcf8574WriteCmd8(devFD, addr) == -1) { 125 | setLastError("Fail to write cmd 8bit"); 126 | return -1; 127 | } 128 | if (pcf8574WriteData8(devFD, data) == -1) { 129 | setLastError("Fail to write data 8bit"); 130 | return -1; 131 | } 132 | 133 | return 0; 134 | } 135 | 136 | // 在第y行第x个开始写字符串 137 | EXPORT int LCD1602DispStr(int devFD, unsigned char x, unsigned char y, char *str) 138 | { 139 | clearLastError(); 140 | unsigned char addr; 141 | addr = ((y + 2) * 0x40) + x; 142 | 143 | if (pcf8574WriteCmd8(devFD, addr) == -1) { 144 | setLastError("Fail to write cmd 8bit"); 145 | return -1; 146 | } 147 | while (*str != '\0') { 148 | if (pcf8574WriteData8(devFD, *str++) == -1) { 149 | setLastError("Fail to write data 8bit"); 150 | return -1; 151 | } 152 | } 153 | 154 | return 0; 155 | } 156 | 157 | EXPORT int LCD1602DispLines(int devFD, char* line1, char* line2) { 158 | int ret = LCD1602DispStr(devFD, 0, 0, line1); 159 | if (ret != -1) { 160 | ret = LCD1602DispStr(devFD, 0, 1, line2); 161 | } 162 | return ret; 163 | } 164 | 165 | EXPORT int LCD1602Init(int i2cDev) 166 | { 167 | clearLastError(); 168 | int devFD; 169 | if ((devFD = pcf8574Init(i2cDev)) == -1) { 170 | setLastError("Fail to init pcf8574"); 171 | return -1; 172 | } 173 | 174 | usleep(1000*15); 175 | if (pcf8574WriteCmd4(devFD, 0x03 << 4) == -1) { 176 | return -1; 177 | } 178 | usleep(100*41); 179 | if (pcf8574WriteCmd4(devFD, 0x03 << 4) == -1) { 180 | return -1; 181 | } 182 | usleep(100); 183 | if (pcf8574WriteCmd4(devFD, 0x03 << 4) == -1) { // 8位数据总线 184 | return -1; 185 | } 186 | if (pcf8574WriteCmd4(devFD, 0x02 << 4) == -1) { // 4位数据总线 187 | return -1; 188 | } 189 | if (pcf8574WriteCmd8(devFD, 0x28) == -1) { // 4位数据总线,显示2行,5x7 190 | return -1; 191 | } 192 | if (pcf8574WriteCmd8(devFD, 0x0c) == -1) { // 显示开,无光标 193 | return -1; 194 | } 195 | usleep(2000); 196 | if (pcf8574WriteCmd8(devFD, 0x06) == -1) { // 光标右移,显示屏不移动 197 | return -1; 198 | } 199 | if (pcf8574WriteCmd8(devFD, 0x02) == -1) { // 光标复位 200 | return -1; 201 | } 202 | usleep(2000); 203 | 204 | return devFD; 205 | } 206 | 207 | EXPORT int LCD1602Clear(int devFD) { 208 | clearLastError(); 209 | if (pcf8574WriteCmd8(devFD, 0x01) == -1) { 210 | setLastError("Fail to pcf8574write cmd 0x01"); 211 | return -1; 212 | } 213 | return 0; 214 | } 215 | 216 | EXPORT void LCD1602DeInit(int devFD) 217 | { 218 | clearLastError(); 219 | closeHW(devFD); 220 | } 221 | 222 | -------------------------------------------------------------------------------- /lib/pcf8591.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "common.h" 4 | #include "libfahw-filectl.h" 5 | #include "libfahw-pcf8591.h" 6 | 7 | EXPORT int pcf8591Read(int channel, int *value) 8 | { 9 | clearLastError(); 10 | int existFlag = 0; 11 | DIR *d; 12 | struct dirent *de; 13 | char pcfFile[FILE_PATH_LENGTH]; 14 | 15 | if (!(d = opendir(PCF8591_SYS_PATH))) { 16 | setLastError("Fail to opendir %s", PCF8591_SYS_PATH); 17 | return -1; 18 | } 19 | while ((de = readdir(d))) { 20 | if (de->d_name[0] == '.') 21 | continue; 22 | sprintf(pcfFile, "%s%s/in%d_input", PCF8591_SYS_PATH, de->d_name, channel); 23 | if (access(pcfFile, F_OK) != -1) { 24 | existFlag = 1; 25 | break; 26 | } 27 | } 28 | closedir(d); 29 | 30 | if (existFlag == 0) { 31 | setLastError("Fail to access pcf8591 sys file"); 32 | return -1; 33 | } 34 | if ((*value = readIntValueFromFile(pcfFile)) != -1) { 35 | return 0; 36 | } else { 37 | setLastError("Invalid dht11 data"); 38 | return -1; 39 | } 40 | } 41 | 42 | -------------------------------------------------------------------------------- /lib/pwm.c: -------------------------------------------------------------------------------- 1 | #include "common.h" 2 | #include "libfahw-filectl.h" 3 | #include "libfahw-pwm.h" 4 | 5 | static int pwmGPIO[3]; 6 | static int pwmNum = -1; 7 | int initPwmGPIO(int board) 8 | { 9 | clearLastError(); 10 | int ret = -1; 11 | 12 | memset(pwmGPIO, -1, sizeof(pwmGPIO)); 13 | switch(board) { 14 | case BOARD_NANOPI_M1: { 15 | int buf[2] = {5, 6}; 16 | memcpy(pwmGPIO, buf, sizeof(buf)); 17 | pwmNum = 2; 18 | ret = 0; 19 | break; 20 | } 21 | case BOARD_NANOPI_2: { 22 | int buf[3] = {97, 77, 78}; 23 | memcpy(pwmGPIO, buf, sizeof(buf)); 24 | pwmNum = 3; 25 | ret = 0; 26 | break; 27 | } 28 | case BOARD_NANOPI_2_FIRE: 29 | case BOARD_NANOPI_M2: { 30 | int buf[3] = {97, 77, 78}; 31 | memcpy(pwmGPIO, buf, sizeof(buf)); 32 | pwmNum = 3; 33 | ret = 0; 34 | break; 35 | } 36 | case BOARD_NANOPC_T2: { 37 | int buf[3] = {97, 77, 78}; 38 | memcpy(pwmGPIO, buf, sizeof(buf)); 39 | pwmNum = 3; 40 | ret = 0; 41 | break; 42 | } 43 | case BOARD_NANOPI_M3: { 44 | int buf[3] = {97, 77, 78}; 45 | memcpy(pwmGPIO, buf, sizeof(buf)); 46 | pwmNum = 3; 47 | ret = 0; 48 | break; 49 | } 50 | case BOARD_NANOPC_T3: { 51 | int buf[3] = {97, 77, 78}; 52 | memcpy(pwmGPIO, buf, sizeof(buf)); 53 | pwmNum = 3; 54 | ret = 0; 55 | break; 56 | } 57 | default: 58 | ret = -1; 59 | break; 60 | } 61 | return ret; 62 | } 63 | 64 | EXPORT int pwmtoGPIO(int pwm) 65 | { 66 | clearLastError(); 67 | 68 | if (pwm<0 || pwm>=pwmNum || pwmGPIO[pwm]==-1) { 69 | setLastError("invalid pwm %d", pwm); 70 | return -1; 71 | } 72 | return pwmGPIO[pwm]; 73 | } 74 | 75 | EXPORT int PWMPlay(int pwm, int freq, int duty) 76 | { 77 | clearLastError(); 78 | int arg[3]; 79 | int devFD = -1; 80 | int gpio = pwmtoGPIO(pwm); 81 | arg[0] = gpio; 82 | arg[1] = freq; 83 | arg[2] = duty; 84 | 85 | if (duty < 0 || duty > 1000) { 86 | setLastError("Invalid duty\n"); 87 | return -1; 88 | } 89 | 90 | if ((devFD = openHW("/dev/pwm", O_RDONLY)) == -1) { 91 | setLastError("Fail to open pwm device"); 92 | return -1; 93 | } 94 | 95 | if (ioctl(devFD, PWM_IOCTL_SET_FREQ, arg) == -1) { 96 | setLastError("Fail to set pwm"); 97 | closeHW(devFD); 98 | return -1; 99 | } 100 | closeHW(devFD); 101 | return 0; 102 | } 103 | 104 | EXPORT int PWMStop(int pwm) 105 | { 106 | clearLastError(); 107 | int devFD = -1; 108 | int gpio = pwmtoGPIO(pwm); 109 | 110 | if ((devFD = openHW("/dev/pwm", O_RDONLY)) == -1) { 111 | setLastError("Fail to open pwm device"); 112 | return -1; 113 | } 114 | 115 | if (ioctl(devFD, PWM_IOCTL_STOP, &gpio) == -1) { 116 | setLastError("Fail to stop pwm %d", pwm); 117 | closeHW(devFD); 118 | return -1; 119 | } 120 | close(devFD); 121 | return 0; 122 | } 123 | -------------------------------------------------------------------------------- /lib/rotary_encoder.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "common.h" 4 | #include "libfahw-gpio.h" 5 | #include "libfahw-encoder.h" 6 | 7 | EXPORT int rotaryEncoderInit(int swPin, int siaPin, int sibPin) 8 | { 9 | clearLastError(); 10 | char buf[32]; 11 | 12 | sprintf(buf,"%d,%d,%d\n", pintoGPIO(swPin), pintoGPIO(siaPin), pintoGPIO(sibPin)); 13 | if (writeValueToFile(ENCODER_PATH"gpio", buf) <= 0) { 14 | setLastError("Fail to set pin for rotary encoder"); 15 | return -1; 16 | } 17 | return 0; 18 | } 19 | 20 | EXPORT int rotaryEncoderRead(int type, int *data) 21 | { 22 | clearLastError(); 23 | int ret = -1; 24 | char *encoderPath = (char *) malloc(FILE_PATH_LENGTH); 25 | memset(encoderPath, 0, FILE_PATH_LENGTH); 26 | strcpy(encoderPath, ENCODER_PATH); 27 | 28 | switch(type) { 29 | case ENCODER_SW: 30 | strcat(encoderPath, "sw"); 31 | ret = readIntValueFromFile(encoderPath); 32 | break; 33 | case ENCODER_VALUE: 34 | strcat(encoderPath, "value"); 35 | ret = readIntValueFromFile(encoderPath); 36 | break; 37 | default: 38 | setLastError("Unsupport encoder data type %d", type); 39 | break; 40 | } 41 | *data = ret; 42 | free(encoderPath); 43 | return ret; 44 | } 45 | 46 | EXPORT int rotaryEncoderDeInit() 47 | { 48 | clearLastError(); 49 | char buf[32]; 50 | 51 | sprintf(buf,"%d,%d,%d\n", -1, -1, -1); 52 | if (writeValueToFile(ENCODER_PATH"gpio", buf) <= 0) { 53 | setLastError("Fail to unset pin for rotary encoder"); 54 | return -1; 55 | } 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /lib/spi.c: -------------------------------------------------------------------------------- 1 | #include "common.h" 2 | #include "includes/spidev.h" 3 | #include "includes/spi_enum.h" 4 | 5 | #define SPI_MAX_SPEED 25000000 6 | 7 | EXPORT int setSPIWriteBitsPerWord(int spi_fd, int bits) 8 | { 9 | clearLastError(); 10 | int ret = ioctl(spi_fd, SPI_IOC_WR_BITS_PER_WORD, &bits); 11 | if (ret < 0) { 12 | setLastError("Can't ioctl SPI_IOC_WR_BITS_PER_WORD"); 13 | } 14 | return ret; 15 | } 16 | 17 | EXPORT int setSPIReadBitsPerWord(int spi_fd, int bits) 18 | { 19 | int ret = ioctl(spi_fd, SPI_IOC_RD_BITS_PER_WORD, &bits); 20 | clearLastError(); 21 | if (ret < 0) { 22 | setLastError("Can't ioctl SPI_IOC_RD_BITS_PER_WORD"); 23 | } 24 | return ret; 25 | } 26 | 27 | EXPORT int setSPIBitOrder(int spi_fd, int order) 28 | { 29 | int ret; 30 | int spi_mode = 0; 31 | clearLastError(); 32 | if(order == LSBFIRST) { 33 | spi_mode |= SPI_LSB_FIRST; 34 | } else { 35 | spi_mode &= ~SPI_LSB_FIRST; 36 | } 37 | ret = ioctl(spi_fd, SPI_IOC_WR_MODE, &spi_mode); 38 | if (ret < 0) { 39 | setLastError("Can't ioctl SPI_IOC_WR_MODE"); 40 | return ret; 41 | } 42 | return ret; 43 | } 44 | 45 | EXPORT int setSPIMaxSpeed(int spi_fd, unsigned int spi_speed) 46 | { 47 | int ret; 48 | unsigned int realSpeed; 49 | clearLastError(); 50 | if (spi_speed<0 || spi_speed>SPI_MAX_SPEED) { 51 | setLastError("invalid spi speed %d", spi_speed); 52 | } 53 | ret = ioctl(spi_fd, SPI_IOC_WR_MAX_SPEED_HZ, &spi_speed); 54 | if (ret < 0) { 55 | setLastError("Can't ioctl SPI_IOC_WR_MAX_SPEED_HZ"); 56 | return ret; 57 | } 58 | ret = ioctl(spi_fd, SPI_IOC_RD_MAX_SPEED_HZ, &realSpeed); 59 | if (ret < 0) { 60 | setLastError("Can't ioctl SPI_IOC_RD_MAX_SPEED_HZ"); 61 | return ret; 62 | } 63 | return ret; 64 | } 65 | 66 | EXPORT int setSPIDataMode(int spi_fd, int mode) 67 | { 68 | int ret; 69 | int spi_mode = 0; 70 | clearLastError(); 71 | switch(mode) { 72 | case SPI_MODE0: 73 | spi_mode &= ~(SPI_CPHA|SPI_CPOL); 74 | break; 75 | case SPI_MODE1: 76 | spi_mode &= ~(SPI_CPOL); 77 | spi_mode |= (SPI_CPHA); 78 | break; 79 | case SPI_MODE2: 80 | spi_mode |= (SPI_CPOL); 81 | spi_mode &= ~(SPI_CPHA); 82 | break; 83 | case SPI_MODE3: 84 | spi_mode |= (SPI_CPHA|SPI_CPOL); 85 | break; 86 | default: 87 | setLastError("error SPIDataMode"); 88 | return -1; 89 | } 90 | 91 | ret = ioctl(spi_fd, SPI_IOC_WR_MODE, &mode); 92 | if (ret < 0) { 93 | setLastError("Can't ioctl SPI_IOC_WR_MODE"); 94 | return ret; 95 | } 96 | 97 | ret = ioctl(spi_fd, SPI_IOC_RD_MODE, &mode); 98 | if (ret < 0) { 99 | setLastError("Can't ioctl SPI_IOC_RD_MODE"); 100 | return ret; 101 | } 102 | 103 | return ret; 104 | } 105 | 106 | /* 107 | struct spi_ioc_transfer { 108 | __u64 tx_buf; // 写数据缓冲 109 | __u64 rx_buf; // 读数据缓冲 110 | __u32 len; // 缓冲的长度 111 | __u32 speed_hz; // 通信的时钟频率 112 | __u16 delay_usecs; // 两个spi_ioc_transfer之间的延时 113 | __u8 bits_per_word; // 字长(比特数) 114 | __u8 cs_change; // 是否改变片选 115 | __u32 pad; 116 | }; 117 | */ 118 | 119 | EXPORT int SPItransferOneByte( 120 | int spi_fd 121 | , unsigned char byteData 122 | , int spi_delay 123 | , int spi_speed 124 | , int spi_bits 125 | ) 126 | { 127 | int ret; 128 | unsigned char tx[1] = {0}; 129 | unsigned char rx[1] = {0}; 130 | tx[0] = byteData; 131 | 132 | struct spi_ioc_transfer tr; 133 | tr.tx_buf = (unsigned long)tx; 134 | tr.rx_buf = (unsigned long)rx; 135 | tr.len = 1; 136 | tr.delay_usecs = spi_delay; 137 | tr.speed_hz = spi_speed; 138 | tr.bits_per_word = spi_bits; 139 | 140 | clearLastError(); 141 | ret = ioctl(spi_fd, SPI_IOC_MESSAGE(1), &tr); 142 | if (ret < 0) { 143 | setLastError("Can't ioctl SPI_IOC_MESSAGE"); 144 | return ret; 145 | } 146 | return rx[0]; 147 | } 148 | 149 | EXPORT int SPItransferBytes( 150 | int spi_fd 151 | , unsigned char * writeData 152 | , int writeLen 153 | , unsigned char * readBuffer 154 | , int readLen 155 | , int spi_delay 156 | , int spi_speed 157 | , int spi_bits 158 | ) 159 | { 160 | unsigned int len = writeLen; 161 | if (len > readLen) { 162 | len = readLen; 163 | } 164 | 165 | unsigned char * pWriteData = writeData; 166 | unsigned char * pReadBuffer = readBuffer; 167 | 168 | struct spi_ioc_transfer tr; 169 | tr.tx_buf = (unsigned long)pWriteData; 170 | tr.rx_buf = (unsigned long)pReadBuffer; 171 | tr.len = len; 172 | tr.delay_usecs = spi_delay; 173 | tr.speed_hz = spi_speed; 174 | tr.bits_per_word = spi_bits; 175 | 176 | int ret = ioctl(spi_fd, SPI_IOC_MESSAGE(1), &tr); 177 | 178 | clearLastError(); 179 | if (ret < 0) { 180 | setLastError("Can't ioctl SPI_IOC_MESSAGE"); 181 | } 182 | return ret; 183 | } 184 | 185 | EXPORT int writeBytesToSPI( 186 | int spi_fd 187 | , unsigned char * writeData 188 | , int writeLen 189 | , int spi_delay 190 | , int spi_speed 191 | , int spi_bits 192 | ) 193 | { 194 | unsigned int len = writeLen; 195 | 196 | unsigned char * pWriteData = writeData; 197 | 198 | struct spi_ioc_transfer tr; 199 | tr.tx_buf = (unsigned long)pWriteData; 200 | tr.rx_buf = (unsigned long)0; 201 | tr.len = len; 202 | tr.delay_usecs = spi_delay; 203 | tr.speed_hz = spi_speed; 204 | tr.bits_per_word = spi_bits; 205 | 206 | int ret = ioctl(spi_fd, SPI_IOC_MESSAGE(1), &tr); 207 | clearLastError(); 208 | if (ret < 0) { 209 | setLastError("Can't ioctl SPI_IOC_MESSAGE"); 210 | } 211 | return ret; 212 | } 213 | 214 | EXPORT int readBytesFromSPI( 215 | int spi_fd 216 | , unsigned char * readBuffer 217 | , int readLen 218 | , int spi_delay 219 | , int spi_speed 220 | , int spi_bits 221 | ) 222 | { 223 | unsigned int len = readLen; 224 | 225 | unsigned char * pReadBuffer = readBuffer; 226 | 227 | struct spi_ioc_transfer tr; 228 | tr.tx_buf = (unsigned long)0; 229 | tr.rx_buf = (unsigned long)pReadBuffer; 230 | tr.len = len; 231 | tr.delay_usecs = spi_delay; 232 | tr.speed_hz = spi_speed; 233 | tr.bits_per_word = spi_bits; 234 | 235 | int ret = ioctl(spi_fd, SPI_IOC_MESSAGE(1), &tr); 236 | clearLastError(); 237 | if (ret < 0) { 238 | setLastError("Can't ioctl SPI_IOC_MESSAGE"); 239 | } 240 | return ret; 241 | } 242 | -------------------------------------------------------------------------------- /lib/w1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include "common.h" 38 | #include "libfahw-filectl.h" 39 | #include "libfahw-w1.h" 40 | 41 | static void rTrim(char *sSource) 42 | { 43 | char *sPtr; 44 | if(sSource==NULL) 45 | return; 46 | sPtr = sSource + strlen(sSource) - 1; 47 | 48 | while(sPtr>=sSource && (isspace(*sPtr))) 49 | sPtr--; 50 | *(sPtr+1)=0; 51 | } 52 | 53 | /* 54 | f6 01 4b 46 7f ff 0a 10 eb : crc=eb YES 55 | f6 01 4b 46 7f ff 0a 10 eb t=31375 56 | */ 57 | static int readTemp(const char* filename, int* ok) 58 | { 59 | FILE* f = fopen(filename, "r"); 60 | 61 | char line[512]; 62 | int chIndex; 63 | char ch; 64 | int len; 65 | int result = 0; 66 | 67 | *ok = 0; 68 | if (f != NULL) { 69 | chIndex = 0; 70 | line[0] = 0; 71 | int lineNum = 1; 72 | int yes = 0; 73 | 74 | while(1) { 75 | if (fread(&ch, 1, 1, f) == 1) { 76 | if (ch == '\n') { 77 | line[chIndex] = 0; 78 | chIndex=0; 79 | rTrim(line); 80 | len = strlen(line); 81 | 82 | if (len > 0) { 83 | // printf("line[%d] %s\n", lineNum, line); 84 | if (lineNum == 1) { 85 | if (line[strlen(line)-1] == 'S' 86 | && line[strlen(line)-2] == 'E' 87 | && line[strlen(line)-3] == 'Y' 88 | ) { 89 | yes = 1; 90 | } 91 | } else if (yes == 1 && lineNum == 2) { 92 | char* pLine = strstr(line, "t="); 93 | if (pLine != 0) { 94 | char* pTemp = pLine + 2; 95 | result = atoi(pTemp); 96 | // printf("pTemp: %s\n", pTemp); 97 | *ok = 1; 98 | } 99 | } 100 | 101 | lineNum ++; 102 | } 103 | 104 | } else { 105 | line[chIndex++] = ch; 106 | } 107 | } else { 108 | break; 109 | } 110 | } 111 | 112 | line[chIndex] = 0; 113 | chIndex=0; 114 | rTrim(line); 115 | len = strlen(line); 116 | if (len > 0) { 117 | if (yes == 1 && lineNum == 2) { 118 | char* pLine = strstr(line, "t="); 119 | if (pLine != 0) { 120 | char* pTemp = pLine + 2; 121 | result = atoi(pTemp); 122 | printf("pTemp: %s\n", pTemp); 123 | *ok = 1; 124 | } 125 | } 126 | } 127 | 128 | fclose(f); 129 | } 130 | return result; 131 | } 132 | 133 | EXPORT int ds18b20Read(char * temperature) 134 | { 135 | DIR *d; 136 | struct dirent *de; 137 | clearLastError(); 138 | if (!(d = opendir(DS18B20_SYS_PATH))) { 139 | setLastError("Fail to opendir %s", DS18B20_SYS_PATH); 140 | return 0; 141 | } 142 | 143 | int ok = 0; 144 | int temp = 0; 145 | char fname[255]; 146 | while ((de = readdir(d))) { 147 | if (de->d_name[0] == '.') 148 | continue; 149 | 150 | sprintf( fname, "%s%s/w1_slave", DS18B20_SYS_PATH, de->d_name); 151 | if( access( fname, F_OK ) != -1 ) { 152 | temp = readTemp(fname, &ok); 153 | break; 154 | } 155 | } 156 | closedir(d); 157 | 158 | if (ok == 0) { 159 | setLastError("Fail to read %s", fname); 160 | return 0; 161 | } 162 | 163 | char* pRetData = temperature; 164 | if (pRetData) { 165 | sprintf(pRetData, "%d", temp); 166 | } 167 | int retSize = strlen(pRetData); 168 | return retSize; 169 | } 170 | --------------------------------------------------------------------------------