一、源代码:
/***********************mini2440_leds.c***********************
*
*
*******************************************************************/
#include <linux/miscdevice.h>
#include <linux/delay.h>
#include <asm/irq.h>
#include <mach/regs-gpio.h>
#include <mach/hardware.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/delay.h>
#include <linux/moduleparam.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/ioctl.h>
#include <linux/cdev.h>
#include <linux/string.h>
#include <linux/list.h>
#include <linux/pci.h>
#include <asm/uaccess.h>
#include <asm/atomic.h>
#include <asm/unistd.h>
#include <linux/gpio.h> //2.6.32.2内核版本要求
#define DEVICE_NAME "leds"
//定义GPIO管脚
static unsigned long led_table [] = {
S3C2410_GPB(5), //不能是S3C2410_GPB5; 因为没有这样定义,可以通过#define S3C2410_GPB5 S3C2410_GPB(5)
S3C2410_GPB(6),
S3C2410_GPB(7),
S3C2410_GPB(8),
};
//设置管脚模式
static unsigned int led_cfg_table [] = {
S3C2410_GPIO_OUTPUT, //随内核版本中定义类型的变化,
S3C2410_GPIO_OUTPUT,
S3C2410_GPIO_OUTPUT,
S3C2410_GPIO_OUTPUT,
};
//ioctl函数实现
static int sbc2440_leds_ioctl(
struct inode *inode,
struct file *file,
unsigned int cmd,
unsigned long arg)
{
switch(cmd) {
case 0:
case 1:
if (arg > 4) {
return -EINVAL;
}
s3c2410_gpio_setpin(led_table[arg], !cmd);
return 0;
default:
return -EINVAL;
}
}
//dev_fops操作指令集
static struct file_operations dev_fops = {
.owner =THIS_MODULE,
.ioctl =sbc2440_leds_ioctl,
};
//第三步:混杂设备定义
static struct miscdevice misc = {
.minor = MISC_DYNAMIC_MINOR,
.name = DEVICE_NAME,
.fops = &dev_fops,
};
//第二步:gpio模式选择,设定管脚
// 注册混杂设备
static int __init dev_init(void)
{
int ret;
int i;
for (i = 0; i < 4; i++) {
s3c2410_gpio_cfgpin(led_table[i], led_cfg_table[i]);
s3c2410_gpio_setpin(led_table[i], 0);
}
ret = misc_register(&misc);
printk (DEVICE_NAME"\tinitialized\n");
return ret;
}
static void __exit dev_exit(void)
{
misc_deregister(&misc);
}
//第一步:module_init(dev_init);
// module_exit(dev_exit);
module_init(dev_init);
module_exit(dev_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("FriendlyARM Inc.");
/**************************Makefile文件************************
*
*
*****************************************************************/
ifneq ($(KERNELRELEASE),)
obj-m := mini2440_leds.o
else
KDIR:=/home/kernel/linux-2.6.32.2
all:
make -C $(KDIR) M=$(PWD) modules ARCH=arm CROSS_COMPILE=arm-linux-
clean:
rm -f *.ko *.o *.mod.o *.mod.c *.symvers modul*
endif
二、测试LED驱动的APP程序
/*******************************app_leds.c***************************************
*
*
**********************************************************************************/
ifneq ($(KERNELRELEASE),)
obj-m := mini2440_leds.o
else
KDIR:=/home/kernel/linux-2.6.32.2
all:
make -C $(KDIR) M=$(PWD) modules ARCH=arm CROSS_COMPILE=arm-linux-
clean:
rm -f *.ko *.o *.mod.o *.mod.c *.symvers modul*
endif
三、遇到的问题
1、
[root@localhost LED]# make
make -C /home/kernel/linux-2.6.32.2 M=/home/nfsshare/root_qtopia/Drive/LED modules ARCH=arm CROSS_COMPILE=arm-linux-
make[1]: Entering directory `/home/kernel/linux-2.6.32.2'
CC [M] /home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.o
/home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.c:29: error: 'S3C2410_GPB5' undeclared here (not in a function)
/home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.c:30: error: 'S3C2410_GPB6' undeclared here (not in a function)
/home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.c:31: error: 'S3C2410_GPB7' undeclared here (not in a function)
/home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.c:32: error: 'S3C2410_GPB8' undeclared here (not in a function)
/home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.c:36: error: 'S3C2410_GPB5_OUTP' undeclared here (not in a function)
/home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.c:36: error: initializer element is not constant
/home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.c:36: error: (near initialization for 'led_cfg_table[0]')
/home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.c:37: error: 'S3C2410_GPB6_OUTP' undeclared here (not in a function)
/home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.c:37: error: initializer element is not constant
/home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.c:37: error: (near initialization for 'led_cfg_table[1]')
/home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.c:38: error: 'S3C2410_GPB7_OUTP' undeclared here (not in a function)
/home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.c:38: error: initializer element is not constant
/home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.c:38: error: (near initialization for 'led_cfg_table[2]')
/home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.c:39: error: 'S3C2410_GPB8_OUTP' undeclared here (not in a function)
/home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.c:39: error: initializer element is not constant
/home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.c:39: error: (near initialization for 'led_cfg_table[3]')
/home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.c: In function 'sbc2440_leds_ioctl':
/home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.c:54: error: implicit declaration of function 's3c2410_gpio_setpin'
/home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.c: In function 'dev_init':
/home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.c:80: error: implicit declaration of function 's3c2410_gpio_cfgpin'
make[2]: *** [/home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.o] 错误 1
make[1]: *** [_module_/home/nfsshare/root_qtopia/Drive/LED] 错误 2
make[1]: Leaving directory `/home/kernel/linux-2.6.32.2'
make: *** [all] 错误 2
错误原因:内核版本不对,没有S3C2410_GPB5的类型定义 需要修改为S3C2410_GPB(5)
内核版本定义如下:
/* arch/arm/mach-s3c2410/include/mach/gpio-nrs.h
*
* Copyright (c) 2008 Simtec Electronics
* http://armlinux.simtec.co.uk/
* Ben Dooks <ben@simtec.co.uk>
*
* S3C2410 - GPIO bank numbering
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#ifndef __MACH_GPIONRS_H
#define __MACH_GPIONRS_H
#define S3C2410_GPIONO(bank,offset) ((bank) + (offset))
#define S3C2410_GPIO_BANKA (32*0)
#define S3C2410_GPIO_BANKB (32*1)
#define S3C2410_GPIO_BANKC (32*2)
#define S3C2410_GPIO_BANKD (32*3)
#define S3C2410_GPIO_BANKE (32*4)
#define S3C2410_GPIO_BANKF (32*5)
#define S3C2410_GPIO_BANKG (32*6)
#define S3C2410_GPIO_BANKH (32*7)
/* GPIO bank sizes */A
#define S3C2410_GPIO_A_NR (32)
#define S3C2410_GPIO_B_NR (32)
#define S3C2410_GPIO_C_NR (32)
#define S3C2410_GPIO_D_NR (32)
#define S3C2410_GPIO_E_NR (32)
#define S3C2410_GPIO_F_NR (32)
#define S3C2410_GPIO_G_NR (32)
#define S3C2410_GPIO_H_NR (32)
#if CONFIG_S3C_GPIO_SPACE != 0
#error CONFIG_S3C_GPIO_SPACE cannot be zero at the moment
#endif
#define S3C2410_GPIO_NEXT(__gpio) \
((__gpio##_START) + (__gpio##_NR) + CONFIG_S3C_GPIO_SPACE + 0)
#ifndef __ASSEMBLY__
enum s3c_gpio_number {
S3C2410_GPIO_A_START = 0,
S3C2410_GPIO_B_START = S3C2410_GPIO_NEXT(S3C2410_GPIO_A),
S3C2410_GPIO_C_START = S3C2410_GPIO_NEXT(S3C2410_GPIO_B),
S3C2410_GPIO_D_START = S3C2410_GPIO_NEXT(S3C2410_GPIO_C),
S3C2410_GPIO_E_START = S3C2410_GPIO_NEXT(S3C2410_GPIO_D),
S3C2410_GPIO_F_START = S3C2410_GPIO_NEXT(S3C2410_GPIO_E),
S3C2410_GPIO_G_START = S3C2410_GPIO_NEXT(S3C2410_GPIO_F),
S3C2410_GPIO_H_START = S3C2410_GPIO_NEXT(S3C2410_GPIO_G),
};
#endif /* __ASSEMBLY__ */
/* S3C2410 GPIO number definitions. */
#define S3C2410_GPA(_nr) (S3C2410_GPIO_A_START + (_nr))
#define S3C2410_GPB(_nr) (S3C2410_GPIO_B_START + (_nr))
#define S3C2410_GPC(_nr) (S3C2410_GPIO_C_START + (_nr))
#define S3C2410_GPD(_nr) (S3C2410_GPIO_D_START + (_nr))
#define S3C2410_GPE(_nr) (S3C2410_GPIO_E_START + (_nr))
#define S3C2410_
用一个鸭子模拟游戏为例子,开始我们的设计模式学习之旅。
首先,我们需要设计各种鸭子的父类,包含有鸭子的共同特征和行为:
Duck
quack()
swim()
display()
.... //鸭子的其他行为特征
后来,PM(产品经理)需要鸭子具有飞行的行为,于是我们觉得很自然得让父类Duck加入了fly()方法, 子类通过继承父类,就都具有了飞行的行为。
问题来了....
PM发现, 游戏中的玩具橡皮鸭也飞了起来..... 这可真是雷人啊。
于是,可怜的我们就必须要重新设计。我们想到了在父类中fly()方法不做任何实现, 由子类去各自实现自己的fly行为, 比如橡皮鸭的fly()就什么都不做。
但是,如果仔细想想,以后不断新增鸭子的种类, 我们要不断去实现和覆盖fly方法, 这样真是太悲催了....
该怎么办?
我们受到启发,想到必须要把这些fly特殊行为从Duck类中分离出来,建立一组新类来实现具体的行为。 这一组新类由fly行为类型的接口来实现。
OK, 我们设计了飞行行为的接口:
<interface>
FlyBehavior
我们可以针对这个接口实现不同的飞行类, 比如:FlyWithWings(真实的翅膀飞行), FlyNoWay(不会飞,比如橡皮鸭)
这样,我们的鸭子父类代码可以如下:
public abstract class Duck{ FlyBehavior flyBehavior; public Duck(){ } public abstract void display(); public void performFly(){ flyBehavior.fly(); } }
FlyBehavior接口的实现代码:
public interface FlyBehavior{ public void fly(); } public class FlyWithWings implements FylBehavior{ public void fly() { ........ } } public class FlyNoWay implements FlyBehavior //不会飞 { public void fly() { ...... } }
现在的设计已经有了很大进步了, 但是我们还需要做到能够动态绑定具体的行为接口。于是,我们在Duck父类中,加入了新方法
public void setFlyBehavior(FlyBehavior fb) { flyBehavior = fb; }
这样,我们可以动态的切换鸭子具体行为了,太好了 哈哈
上述实现过程中, 我们把多个类组合起来使用,灵活性更强,耦合性也相对弱了很多。
做完上述设计,我们已经学会了第一个设计模式:策略模式(Strategy Pattern)
策略模式的定义: 它定义了算法族,分别封装起来,让他们之间可以相互替换,此模式算法的变化独立于使用算法的客户。
在软件开发上有个不变的真理: change
设计原则:
1. 找出应用中可能需要变化之处, 把他们独立出来, 别和不需要变化的代码混在一起。
2. 针对接口编程, 而不是针对实现编程。
3.多用组合,少用继承。
转换一个存在的接口到一个新的接口,以完成不相关的类的兼容性和重用性,这种模式叫做适配器模式。这个模式也被称为封装(wrapper)模式,适配器模式是属于结构设计模式之一。
适用场合和优势:
接口MediaPlayer, AdvanceAudioPlayer的定义。
public interface MediaPlayer { public void play(String audioType, String fileName) ; }
public interface AdvancedMediaPlayer { public void playVlc(String fileName); public void playMp4(String fileName); }
AudioPlayer的具体实现。
public class AudioPlayer implements MediaPlayer { MediaAdapter mediaAdapter; @Override public void play(String audioType, String fileName) { //inbuilt support to play mp3 music files if(audioType.equalsIgnoreCase("mp3")){ System.out.println("Playing mp3 file. Name: "+ fileName); } //mediaAdapter is providing support to play other file formats else if(audioType.equalsIgnoreCase("vlc") || audioType.equalsIgnoreCase("mp4")){ mediaAdapter = new MediaAdapter(audioType); mediaAdapter.play(audioType, fileName); } else{ System.out.println("Invalid media. "+ audioType + " format not supported"); } } }
高级音频播放器的具体实现。
public class Mp4Player implements AdvancedMediaPlayer{ @Override public void playVlc(String fileName) { //do nothing } @Override public void playMp4(String fileName) { System.out.println("Playing mp4 file. Name: "+ fileName); } }
public class VlcPlayer implements AdvancedMediaPlayer { @Override public void playVlc(String fileName) { System.out.println("Playing vlc file. Name: "+ fileName); } @Override public void playMp4(String fileName) { //do nothing } }定义是一个适配器,适配高级音频播放器功能。
public class MediaAdapter implements MediaPlayer { AdvancedMediaPlayer advancedMusicPlayer; public MediaAdapter(String audioType){ if(audioType.equalsIgnoreCase("vlc") ){ advancedMusicPlayer = new VlcPlayer(); } else if (audioType.equalsIgnoreCase("mp4")){ advancedMusicPlayer = new Mp4Player(); } } @Override public void play(String audioType, String fileName) { if(audioType.equalsIgnoreCase("vlc")){ advancedMusicPlayer.playVlc(fileName); }else if(audioType.equalsIgnoreCase("mp4")){ advancedMusicPlayer.playMp4(fileName); } } }
正如AudioPlayer代码展示的,只需要调用AudioPlayer就能够播放高级音频格式的视频文件。具体测试代码如下:
public class AdapterPatternDemo { public static void main(String[] args) { AudioPlayer audioPlayer = new AudioPlayer(); audioPlayer.play("mp3", "beyond the horizon.mp3"); audioPlayer.play("mp4", "alone.mp4"); audioPlayer.play("vlc", "far far away.vlc"); audioPlayer.play("avi", "mind me.avi"); } }
运行结果:
Playing mp3 file. Name: beyond the horizon.mp3 Playing mp4 file. Name: alone.mp4 Playing vlc file. Name: far far away.vlc Invalid media. avi format not supported