攀藤 G1 PM2.5传感器

本文有更新,请参考 拆了攀登 G1 PM 2.5 传感器 《拆了攀登 G1 PM 2.5 传感器》

最近帮朋友做东西,他要求测试当前环境PM2.5。我首先拿出了珍藏许久的神荣模块。记得当时是90多入手的,当初Taobao上也没几个卖这个模块的,90多是最低价格了。写这篇文章的时候顺手查了一下目前的价格,已经降到了60多,卖家也不少了。很可能是目前出货量大,整体价格都降低下来了。我发现除了线材或者连接器,几乎所有的电子模块都有这样的问题。比方说:MPU6050模块,刚出来的时候居然要五六十,现在只要五六元。

先说一下神荣模块,问题多多,首先是根据他的SPEC,要求连续采集30秒,采集的时候基本上什么也不能做,否则会严重影响精度;另外,他每次计算的数值偏差很大,委婉一点的说法是:能够精确反应当前空气颗粒趋势……对于采集偏差,有资料说可能是因为他是采用发热电阻加热空气来实现流动的,而这样的方式不能确保空气的流通速度,所以结果不是很准确。个人观点:如果你的传感器上没有风扇换气,都是靠不住的。

后来,看到论坛上有人推荐攀藤模块,我在上网考察了一下,感觉上微创联合【参考1】这家很专业,但是不知道为什么他邮费报价很高(25?);最后我在树莓派一号店【参考2】这家买的,155+5元邮费,型号是G1。

简单说一下型号,有 G1 G2 G3,这几个主要差别在于外形尺寸。另外,G1可以输出最近一段测试到的单位个数。

外壳上有一层蓝膜,撕下之后能露出金属外壳(有朋友看照片说这个模块很老,那是因为我照相技术的原因,这个模块应该是2014年10月之后才有的):

image002

可以看到,上面带有一个风扇进行空气交换的。

image004

引脚介绍【参考3】。特别注意,似乎他们他们家的模块G1 G3引脚顺序上有差别?根据你手上的实物进行选择

image006

入手之后就开始实验,连接上最小只需要三根线即可让他工作,分别是VCC (Pin1) GND(Pin2)和TXD(Pin5)。我直接将这个模块和蓝牙透传模块连接起来(9600),PC串口即可获得数据非常方便。

image007

因为手头没有能够进行对照测试的仪器,只是简单测试了一下:我用手堵住进气口,过一段PM2.5值会变为0. 可以看一下工作的视频:

http://www.tudou.com/programs/view/xexkePsAd_c/?resourceId=0_06_02_99

上述代码下载

PM2.5Fix

编译器是 Delphi 2010 + CPort VCL

上面说的都是优点,下面说点缺点,貌似我更喜欢负结果?
1. 可能是因为上面有电机的缘故,所以对于电流要求比较高。产品手册说最大电流为 120ma,官网说最大是200ma,实际启动时非常有可能比这个更大,我试图使用万用表测量,无法让其工作未果。因此,如果给Arduino使用,一定不能用Arduino取电,否则有不可预料的后果;我的解决方法是:USB充电宝先是接到一个 USB母头上取电,然后下来的电给G1,蓝牙供电,再直接进入Arduino Pro Micro的VCC;

2. 接口设计上有问题,我买到的那个接口不是很牢固,经常出现串口取不到数据反查一路才发现接口松动,如果能选用更粗大的排插效果会更好;

3. 整体上没有指示灯,无法判断当前工作状态。对于我来说出现问题时首先要把手放在模块上看看是否有震动来判断是否工作;、
4. 只在两个角落预留螺丝孔,如果整体多几个孔位,使用上会更方便;孔径应该是 M2,我手上的M2螺丝太短,没有办法插进去。

5. 手册提供的数据格式似乎有问题,刚开始我看的是卖家介绍网页,以为是卖家搞错了,后来翻了一下手册,手册也是这样写的:

image010

我实际获得的数据是这样的,每一笔串口数据以“BM” 开头,后面是数据,我实验发现 PM1.0 = Data[4]+(Data[5] shl 8) 才是正确值。如果用 (Data[4] shl 8) + (Data[5]) 计算结果明显不正确。【这个问题是Delphi数组下标从1开始导致的,如果你使用VC即可follow spec】刚开始实验我根据手册写成后面的这种形式,结果7000多,疑似身在帝都了;后来琢磨一下写成前面这种,结果为 32 (室内),感觉才正确;

6. 如同文章开头所说,和其他相比价格还是偏贵;

7. 资料偏少,没有官方资料【参考4】缺少寿命方面的详细数据,让人很难信服。特别是带有机械部分的电子设备,寿命通常会远低于电子部分,缺少数据让人很不放心;万一选用的是国产电机,你更无法判断什么时候会悲剧。

最后的总结:如果你是为了简单的研发目的,或者毕业设计,相比其他PM2.5传感器,这款使用简单,结果看起来很准确,非常值得推荐。但是如果你是为了稳定长期的工作,我认为有待时间检验。

参考:
1. http://shop115958317.taobao.com/index.htm?spm=a1z10.1-c.w5002-9767628871.2.jTEb6C 微创联合
2. http://shop110224467.taobao.com/index.htm?spm=2013.1.w5002-6755541327.2.ZK3XMi 树莓派一号店
3. 攀藤 G1 说明书
4. http://www.plantower.com/ 攀藤科技官网

2015年9月5日 放上来说明书 PantengGx

Step to UEFI (46) —– EFILOADEDIMAGEPROTOCOL的使用

上次介绍了如何在一个程序中直接调用另外的程序,那么在调用过程中是否有机会获得一些加载起来的EFI的信息呢?经过一番搜索,发现EFI_LOADED_IMAGE_PROTOCOL【参考1】。这个protocol的作用就是 “Can be used on any image handle to obtain information about the loaded image.”

elip

从定义上看,我们能够得到加载的Image的一些基本信息。在上次程序的基础上,添加一些代码来实验。

#include  <Uefi.h>
#include  <Library/UefiLib.h>
#include  <Library/ShellCEntryLib.h>

#include  <stdio.h>
#include  <stdlib.h>
#include  <wchar.h>

#include <Protocol/EfiShell.h>
#include <Library/ShellLib.h>

extern EFI_BOOT_SERVICES           	 *gBS;
extern EFI_SYSTEM_TABLE				 *gST;
extern EFI_RUNTIME_SERVICES 		 *gRT;

extern EFI_SHELL_PROTOCOL            *gEfiShellProtocol;
extern EFI_SHELL_ENVIRONMENT2 		 *mEfiShellEnvironment2;

extern EFI_HANDLE					 gImageHandle;
/**
  GET  DEVICEPATH
**/
EFI_DEVICE_PATH_PROTOCOL *
EFIAPI
ShellGetDevicePath (
  IN CHAR16                     * CONST DeviceName OPTIONAL
  )
{
  //
  // Check for UEFI Shell 2.0 protocols
  //
  if (gEfiShellProtocol != NULL) {
    return (gEfiShellProtocol->GetDevicePathFromFilePath(DeviceName));
  }

  //
  // Check for EFI shell
  //
  if (mEfiShellEnvironment2 != NULL) {
    return (mEfiShellEnvironment2->NameToPath(DeviceName));
  }

  return (NULL);
}

int
EFIAPI
main (
  IN int Argc,
  IN char **Argv
  )
{
  EFI_DEVICE_PATH_PROTOCOL 	*DevicePath;
  EFI_HANDLE				NewHandle;
  EFI_STATUS				Status;
  UINTN			ExitDataSizePtr;  
  CHAR16 					*R=L"HelloWorld.efi";
  EFI_LOADED_IMAGE_PROTOCOL	*ImageInfo = NULL;
  
  Print(L"File [%s]\n",R);

  DevicePath=ShellGetDevicePath(R);

  //
  // Load the image with:
  // FALSE - not from boot manager and NULL, 0 being not already in memory
  //
  Status = gBS->LoadImage(
    FALSE,
    gImageHandle,
    DevicePath,
    NULL,
    0,
    &NewHandle);  

  if (EFI_ERROR(Status)) {
    if (NewHandle != NULL) {
      gBS->UnloadImage(NewHandle);
    }
	Print(L"Error during LoadImage [%X]\n",Status);
    return (Status);
  }

  Status = gBS -> HandleProtocol (
						NewHandle,
						&gEfiLoadedImageProtocolGuid,
						&ImageInfo
						);
  Print(L"ImageBase [%lX]\n",ImageInfo->ImageBase);
  Print(L"ImageSize [%lX]\n",ImageInfo->ImageSize);

  //
  // now start the image, passing up exit data if the caller requested it
  //
  Status = gBS->StartImage(
                     NewHandle,
                     &ExitDataSizePtr,
                     NULL
              );
  if (EFI_ERROR(Status)) {
    if (NewHandle != NULL) {
      gBS->UnloadImage(NewHandle);
    }
	Print(L"Error during StartImage [%X]\n",Status);
    return (Status);
  }
  
  gBS->UnloadImage (NewHandle);  
  return EFI_SUCCESS;
}

 

特别注意,我们代码中需要使用这个Protocol的GUID,在INF中添加下面的引用即可。

[Protocols]
  gEfiLoadedImageProtocolGuid    

 

运行结果

elip2

可以看到显示的ImageSize就是 HelloWorld.efi的大小。

elip3

实验调用的代码比较特殊,如果直接调用CLIB编写的程序会导致错误。至于具体的原因,后续再进行研究。

实验的 HelloWorld.EFI 的代码在下面

#include <Uefi.h>
#include <Library/PcdLib.h>
#include <Library/UefiLib.h>
#include <Library/UefiApplicationEntryPoint.h>


/**
  The user Entry Point for Application. The user code starts with this function
  as the real entry point for the application.

  @param[in] ImageHandle    The firmware allocated handle for the EFI image.  
  @param[in] SystemTable    A pointer to the EFI System Table.
  
  @retval EFI_SUCCESS       The entry point is executed successfully.
  @retval other             Some error occurs when executing this entry point.

**/
EFI_STATUS
EFIAPI
UefiMain (
  IN EFI_HANDLE        ImageHandle,
  IN EFI_SYSTEM_TABLE  *SystemTable
  )
{
  Print(L"Hello,World! \r\n");
  Print(L"www.lab-z.com \r\n");
  
  return EFI_SUCCESS;
}

 

对应的INF

[Defines]
  INF_VERSION                    = 0x00010005
  BASE_NAME                      = HelloWorld
  FILE_GUID                      = 6987936E-ED34-44db-AE97-1FA5E4ED2116
  MODULE_TYPE                    = UEFI_APPLICATION
  VERSION_STRING                 = 1.0
  ENTRY_POINT                    = UefiMain

#
# The following information is for reference only and not required by the build tools.
#
#  VALID_ARCHITECTURES           = IA32 X64 IPF EBC
#

[Sources]
  HelloWorld.c

[Packages]
  MdePkg/MdePkg.dec
  MdeModulePkg/MdeModulePkg.dec

[LibraryClasses]
  UefiApplicationEntryPoint
  UefiLib
  PcdLib

[FeaturePcd]

[Pcd]

 

实验完整代码下载

exec2
参考:

1.UEFI Spec 2.4 P265

Arduino 驱动74HC595 LCD模块

Arduino 驱动8位数码管模块

之前我们使用数码管都是直接使用【参考1】,缺点是电路复杂,占用引脚太多,稍微有点问题就会出现缺笔画的问题(测试的时候务必完整测试,否则很可能遇到你想显示8,结果出现6的状况)。最近入手了下面这个模块,上面通过74HC595芯片驱动LCD,接线很简单。我做了简单的实验,感觉挺方便的。

示例代码

    unsigned char LED_0F[] = 
    {// 0	 1	  2	   3	4	 5	  6	   7	8	 9	  A	   b	C    d	  E    F    -
    	0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x8C,0xBF,0xC6,0xA1,0x86,0xFF,0xbf
    };
    unsigned char LED[8];	//用于LED的8位显示缓存
    int SCLK = 10;
    int RCLK = 9;
    int DIO = 8; //这里定义了那三个脚
    
    void setup ()
    {
      pinMode(SCLK,OUTPUT);
      pinMode(RCLK,OUTPUT);
      pinMode(DIO,OUTPUT); //让三个脚都是输出状态
    }
    void loop()
    {
        LED[0]=1;
    	LED[1]=2;
    	LED[2]=3;
    	LED[3]=4;
        LED[4]=5;
    	LED[5]=6;
    	LED[6]=7;
    	LED[7]=8;
    	while(1)
    	{
    		LED8_Display ();
    	} 
      
    }
    
    void LED8_Display (void)
    {
    	unsigned char *led_table;          // 查表指针
    	unsigned char i;
    	//显示第1位
    	led_table = LED_0F + LED[0];
    	i = *led_table;
    	LED_OUT(i);			
    	LED_OUT(0x01);		
        digitalWrite(RCLK,LOW);
        digitalWrite(RCLK,HIGH);
    	//显示第2位
    	led_table = LED_0F + LED[1];
    	i = *led_table;
    	LED_OUT(i);		
    	LED_OUT(0x02);		
        digitalWrite(RCLK,LOW);
        digitalWrite(RCLK,HIGH);
    	//显示第3位
    	led_table = LED_0F + LED[2];
    	i = *led_table;
    	LED_OUT(i);			
    	LED_OUT(0x04);	
        digitalWrite(RCLK,LOW);
        digitalWrite(RCLK,HIGH);
    	//显示第4位
    	led_table = LED_0F + LED[3];
    	i = *led_table;
    	LED_OUT(i);			
    	LED_OUT(0x08);		
        digitalWrite(RCLK,LOW);
        digitalWrite(RCLK,HIGH);

    	//显示第5位
    	led_table = LED_0F + LED[4];
    	i = *led_table;
    	LED_OUT(i);			
    	LED_OUT(0x10);		
        digitalWrite(RCLK,LOW);
        digitalWrite(RCLK,HIGH);
    	//显示第6位
    	led_table = LED_0F + LED[5];
    	i = *led_table;
    	LED_OUT(i);			
    	LED_OUT(0x20);		
        digitalWrite(RCLK,LOW);
        digitalWrite(RCLK,HIGH);
    	//显示第7位
    	led_table = LED_0F + LED[6];
    	i = *led_table;
    	LED_OUT(i);			
    	LED_OUT(0x40);		
        digitalWrite(RCLK,LOW);
        digitalWrite(RCLK,HIGH);        
    	//显示第8位
    	led_table = LED_0F + LED[7];
    	i = *led_table;
    	LED_OUT(i);			
    	LED_OUT(0x80);		
        digitalWrite(RCLK,LOW);
        digitalWrite(RCLK,HIGH);        
    }
    
    void LED_OUT(unsigned char X)
    {
    	unsigned char i;
    	for(i=8;i>=1;i--)
    	{
    		if (X&0x80) 
                {
                  digitalWrite(DIO,HIGH);
                 }  
                else 
                {
                  digitalWrite(DIO,LOW);
                }
    		X<<=1;
                digitalWrite(SCLK,LOW);
                digitalWrite(SCLK,HIGH);
    	}
    }

 

卖家没有提供相应的代码,上述代码是我根据从其他地方找到的4位LED的代码基础上修改出来的。

工作照片:

595LED

附件是卖家提供的8 LED的资料和其他地方找到的 4 LED 的资料。

4LED

8LEDDS

参考:

1. http://www.lab-z.com/4digitial/ Arduino 四位数码管实验
2. http://www.lab-z.com/usb7seg/ USB 控制七段数码管
3. http://www.lab-z.com/usb-%E6%8E%A7%E5%88%B6%E4%B8%83%E6%AE%B5%E6%95%B0%E7%A0%81%E7%AE%A1ii/ USB 控制七段数码管(II)

Step to UEFI (45) —– 在程序中执行另外的程序

某些情况下,我们有在自己的程序中调用另外一个 EFI 程序的需求。

关于这个问题【参考1】建议参考Shell的源程序。如果有时间,建议阅读这一段代码,相信对于具体的实现很有帮助。

“RunCommandOrFile()
==> case Efi_Application:
InternalShellExecuteDevicePath()
==> Status = gBS->LoadImage( … ) ”

另外,【参考2】介绍了一下调用的流程:

1. BS->LoadImage 加载你要调用的 EFI 到内存
2. BS->StartImage 执行你加载的EFI程序
3. BS->UnLoadImage 执行完成之后释放EFI

了解了基本流程原理,下面就要认真阅读函数的原型。

LoadImage的原型如下,来自【参考3】

loadimage

BootPolicy 告诉加载的EFI是否为可启动的选项
ParentImageHandle 是调用者的Handle
DevicePath 告诉要调用的EFI文件的位置
SourceBuffer 可选如果不为NULL的话,是指向内存中的要加载的EFI的指针
SourceSize 如果上面这个指针存在的话,给出指向内存的大小
EFI_HANDLE 加载之后Image的Handle

StartImage 原型

startimage

ImageHandle  前面LoadImage给出来的EFI Image Handle
ExitDataSize 下面ExitData的大小
ExitData 看起来在一个 EFI 结束的时候,可以返回一些内容

UnLoadImage 原型

unimage

给出要释放的EFI的Handle即可

根据上面的介绍,再结合Shell.c中的具体实现,编写程序如下。为了方便验证和保持整个程序的简洁,程序固定调用“HellowWorld.efi”。这个程序的作用是输出一段String。

#include  <Uefi.h>
#include  <Library/UefiLib.h>
#include  <Library/ShellCEntryLib.h>

#include  <stdio.h>
#include  <stdlib.h>
#include  <wchar.h>

#include <Protocol/EfiShell.h>
#include <Library/ShellLib.h>

extern EFI_BOOT_SERVICES           	 *gBS;
extern EFI_SYSTEM_TABLE				 *gST;
extern EFI_RUNTIME_SERVICES 		 *gRT;

extern EFI_SHELL_PROTOCOL            *gEfiShellProtocol;
extern EFI_SHELL_ENVIRONMENT2 		 *mEfiShellEnvironment2;

extern EFI_HANDLE					 gImageHandle;
/**
  GET  DEVICEPATH
**/
EFI_DEVICE_PATH_PROTOCOL *
EFIAPI
ShellGetDevicePath (
  IN CHAR16                     * CONST DeviceName OPTIONAL
  )
{
  //
  // Check for UEFI Shell 2.0 protocols
  //
  if (gEfiShellProtocol != NULL) {
    return (gEfiShellProtocol->GetDevicePathFromFilePath(DeviceName));
  }

  //
  // Check for EFI shell
  //
  if (mEfiShellEnvironment2 != NULL) {
    return (mEfiShellEnvironment2->NameToPath(DeviceName));
  }

  return (NULL);
}

int
EFIAPI
main (
  IN int Argc,
  IN char **Argv
  )
{
  EFI_DEVICE_PATH_PROTOCOL *DevicePath;
  EFI_HANDLE	NewHandle;
  EFI_STATUS	Status;
  UINTN			ExitDataSizePtr;
  CHAR16 *R=L"HelloWorld.efi";
  
  Print(L"File [%s]\n",R);

  DevicePath=ShellGetDevicePath(R);

  //
  // Load the image with:
  // FALSE - not from boot manager and NULL, 0 being not already in memory
  //
  Status = gBS->LoadImage(
    FALSE,
    gImageHandle,
    DevicePath,
    NULL,
    0,
    &NewHandle);  

  if (EFI_ERROR(Status)) {
    if (NewHandle != NULL) {
      gBS->UnloadImage(NewHandle);
    }
	Print(L"Error during LoadImage [%X]\n",Status);
    return (Status);
  }

  //
  // now start the image, passing up exit data if the caller requested it
  //
  Status = gBS->StartImage(
                     NewHandle,
                     &ExitDataSizePtr,
                     NULL
              );
  if (EFI_ERROR(Status)) {
    if (NewHandle != NULL) {
      gBS->UnloadImage(NewHandle);
    }
	Print(L"Error during StartImage [%X]\n",Status);
    return (Status);
  }
  

  gBS->UnloadImage (NewHandle);  
  return EFI_SUCCESS;
}

 

运行结果:

execres

工作的视频:

http://www.tudou.com/programs/view/92MTmguSCZk/?resourceId=414535982_06_02_99

代码下载

exec

最后,如果你只是想简单的执行一个程序,可以考虑直接使用 EFI_SHELL_PROTOCOL 的 EfiShellExecute 或者 EFI_SHELL_ENVIRONMENT2的 Execute ,这样会简单许多。

参考:
1. http://biosren.com/viewthread.php?tid=7440&highlight=%BC%D3%D4%D8%2B%B3%CC%D0%F2 请问:在shell下,应用程序的.efi文件被加载到内存的基地址为多少?
2. http://blog.csdn.net/kaven128708/article/details/6042307 EFI Load Image
3. UEFI Spec 2.4 P196

推荐绘制电路图的软件 TinyCAD

之前我尝试用 Fritzing 绘制短路图一段时间。这个软件有直观方便的特点。问题是大多数我用到的原件他的库中没有,如果自己绘制的话非常复杂,修改元件管脚也异常麻烦。

http://sourceforge.net/projects/tinycad/

tinycada

用这个很容易就绘制一个 Arduino Pro Micro的电路

tinycadb

对应文件可以在这里下载
Arduino-Pro-Micro

小水泵套件

最近从淘宝上买了一个小水泵和水管给Arduino 用,效果还不错

螃蟹王国 隔膜泵 抽水机 水泵 茶具功夫茶配件 烧水茶具 385水泵 9.9元
http://detail.tmall.com/item.htm?spm=a1z10.3-b.w4011-2934116009.41.OZz7kv&id=27069408842&;rn=19abc97d3a3f333b0532d77acf876c95&abbucket=10

poop
螃蟹王国 硅胶管 耐高温 食品级 水管 多规格 6元

http://detail.tmall.com/item.htm?spm=a220o.1000855.0.0.aUdENI&id=16273533392&;rn=5ae68cf7c122dcfc1738eafece3afc9c&abbucket=0

同样他家选购了一个6V的适配器。

螃蟹王国 电源适配器6V2A 高性能 路由器电源 适配器 充电器 9.9元
http://detail.tmall.com/item.htm?spm=a1z10.3-b.w4011-2934116009.58.24xZpI&id=20618420536&;rn=6257e27c013737241e82d5c120284aaa&abbucket=10

螃蟹王国 DC电源插座 5.5*2.1MM 直流电源接口 DC-005 铜脚 3.95元
http://detail.tmall.com/item.htm?spm=a1z10.3-b.w4011-2934116009.27.pZwIWV&id=20623496826&;rn=1bc29be0dbbabd7042db076efd6ce381&abbucket=10

需要注意的是,这种插座有2个参数,5.5是直径,2.1是长度。也有2.5mm长度的,如果座和插头不匹配的话,插入之后会有部分露在外面。

下面是它工作视频以便参考

http://www.tudou.com/programs/view/7diay5i7Z-c/?resourceId=0_06_02_99

Step to UEFI (44) —– 获得按键

在Shell下面编写工具程序,我们经常需要和用户进行交互,需要取得客户按键的信息。

对于这个问题,可以使用 EFI_SIMPLE_TEXT_INPUT_PROTOCOL 的 ReadKeyStroke 来解决【参考1】

readkeystroke

写一个小程序来验证一下

#include  <Uefi.h>
#include  <Library/UefiLib.h>
#include  <Library/ShellCEntryLib.h>

#include  <stdio.h>
#include  <stdlib.h>
#include  <wchar.h>
#include  <time.h>
#include <Protocol/EfiShell.h>
#include <Library/ShellLib.h>

#include <Protocol/SimpleFileSystem.h>
#include <Protocol/BlockIo.h>
#include <Library/DevicePathLib.h>
#include <Library/HandleParsingLib.h>
#include <Library/SortLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/BaseMemoryLib.h>

extern EFI_BOOT_SERVICES         *gBS;
extern EFI_SYSTEM_TABLE			 *gST;
extern EFI_RUNTIME_SERVICES 	 *gRT;

extern EFI_SHELL_ENVIRONMENT2    *mEfiShellEnvironment2;
extern EFI_HANDLE				 gImageHandle;


int
EFIAPI
main (                                         
  IN int Argc,
  IN char **Argv
  )
{
  EFI_INPUT_KEY	Key;
  EFI_STATUS Status;
  
  while (1)
	{
		Status= gST -> ConIn -> ReadKeyStroke(gST->ConIn,&Key);	
		if (Status == EFI_SUCCESS)	{
			break;
		}
		gST -> ConOut -> OutputString(gST->ConOut,L"Test......");
		gBS->Stall(500);
	}  
  Print(L"\n\r Scancode [%d], UnicodeChar [%c] \n\r",Key.ScanCode,Key.UnicodeChar);
  return EFI_SUCCESS;
}

 

按下 F1 检测结果:

F1key

按下 q键 检测结果:

qkey

代码下载

KeyTest1

参考:

1.UEFI 2.4 P445

Arduino Pro Micro 打造PPT遥控器

之前,我们介绍过如何用Arduino Uno打造一个PPT遥控器【参考1】,缺点是制作过程复杂,用到的器件较多导致整体稳定性不好。这里介绍使用 Arduino Pro Micro来做一个同样的PPT遥控器。

原理上和之前的并没有多少差别,都是通过模拟USB键盘的方式来进行控制。差别在于Pro Micro是缩小版的 Leonardo ,内部集成了USB Slave控制器,我们不需要再花费精力模拟自己为USB Keyboard.
硬件连接示意图:

ppt1

代码是直接修改自示例“KeyboardMessage”

ppt2

/* 
 Keyboard Button test
 
 For the Arduino Leonardo and Micro.
 
 Sends a text string when a button is pressed.
 
 The circuit:
 * pushbutton attached from pin 2 to +5V
 * 10-kilohm resistor attached from pin 4 to ground
 
 created 24 Oct 2011
 modified 27 Mar 2012
 by Tom Igoe
 
 This example code is in the public domain.
 
 http://www.arduino.cc/en/Tutorial/KeyboardButton
 */

const int buttonPinA = A1;          // input pin for A
const int buttonPinB = A2;          // input pin for B
int previousButtonStateA = HIGH;   // for checking the state of a pushButton
int previousButtonStateB = HIGH;   // for checking the state of a pushButton

void setup() {
  // make the pushButtonA pin an input:
  pinMode(buttonPinA, INPUT);
  // make the pushButtonB pin an input:
  pinMode(buttonPinB, INPUT);
  // initialize control over the keyboard:
  Keyboard.begin();
  delay(10000);
}

void loop() {
  // read the pushbutton:
  int buttonStateA = digitalRead(buttonPinA);
  int buttonStateB = digitalRead(buttonPinB);  
  // if the button state has changed, 
  if ((buttonStateA != previousButtonStateA) 
    // and it's currently pressed:
  && (buttonStateA == HIGH)) {
    // type out a message
    //Keyboard.print("A");
    Keyboard.press(KEY_LEFT_ARROW);
    Keyboard.releaseAll();
  }
  // if the button state has changed, 
  if ((buttonStateB != previousButtonStateB) 
    // and it's currently pressed:
  && (buttonStateB == HIGH)) {
    // type out a message
    //Keyboard.print("B");
    Keyboard.press(KEY_RIGHT_ARROW);
    Keyboard.releaseAll();
  }  
  // save the current button state for comparison next time:
  previousButtonStateA = buttonStateA; 
  previousButtonStateB = buttonStateB;   
}

 

实物图

ppt3

工作的视频

http://www.tudou.com/listplay/mAjgE5Ac_Sg/Fz0piTvQ9Cs.html?resourceId=414535982_06_02_99

参考:

1. http://www.lab-z.com/%E7%94%A8-arduino-%E6%89%93%E9%80%A0ppt%E9%81%A5%E6%8E%A7%E5%99%A8/ 用 Arduino 打造PPT遥控器
2. http://arduino.cc/en/Reference/KeyboardModifiers 键值定义

Step to UEFI (43) —– 取得当前目录的 EFI_DEVICE_PATH_PROTOCOL

在 EFI_SHELL_PROTOCOL 中提供了 EfiShellGetDevicePathFromFilePath 函数

具体可以在 \ShellPkg\Application\Shell\ShellProtocol.h 看到原型

/**
  Converts a file system style name to a device path.

  This function converts a file system style name to a device path, by replacing any
  mapping references to the associated device path.

  @param[in] Path               The pointer to the path.

  @return                       The pointer of the file path. The file path is callee
                                allocated and should be freed by the caller.
  @retval NULL                  The path could not be found.
  @retval NULL                  There was not enough available memory.
**/
EFI_DEVICE_PATH_PROTOCOL *
EFIAPI
EfiShellGetDevicePathFromFilePath(
  IN CONST CHAR16 *Path
  )

 

在【参考1】中,可以看到下面的介绍

getcurdp

经过试验,这个函数结合之前我们获得当前目录的函数完全可以达到目标。但是,如同【参考2】提到的,某些情况下,因为 Shell 不提供 EFI_SHELL_PROTOCOL,这个函数无法使用。

再查查 EFI_SHELL_ENVIRONMENT2 发现其中有功能类似的 NameToPath 函数

Convert a file system style name to a device path.

This function will convert a shell path name to a Device Path Protocol path. This function will allocate any required memory for this operation and it is the responsibility of the caller to free that memory when no longer required.

If anything prevents the complete conversion free any allocated memory and return NULL.

Parameters
[in]	Path	The path to convert.
Return values
!NULL	A pointer to the callee allocated Device Path.
NULL	The operation could not be completed.

 

最后,决定仿照 ShellLib.h 中的 ShellGetCurrentDir,写一个混合上述两种函数的函数。最后的代码就是这样

#include  <Uefi.h>
#include  <Library/UefiLib.h>
#include  <Library/ShellCEntryLib.h>

#include  <stdio.h>
#include  <stdlib.h>
#include  <wchar.h>

#include <Protocol/EfiShell.h>
#include <Library/ShellLib.h>

extern EFI_BOOT_SERVICES           	 *gBS;
extern EFI_SYSTEM_TABLE				 *gST;
extern EFI_RUNTIME_SERVICES 		 *gRT;

extern EFI_SHELL_PROTOCOL            *gEfiShellProtocol;
extern EFI_SHELL_ENVIRONMENT2 		 *mEfiShellEnvironment2;
/**
  GET  DEVICEPATH
**/
EFI_DEVICE_PATH_PROTOCOL *
EFIAPI
ShellGetDevicePath (
  IN CHAR16                     * CONST DeviceName OPTIONAL
  )
{
  //
  // Check for UEFI Shell 2.0 protocols
  //
  if (gEfiShellProtocol != NULL) {
    return (gEfiShellProtocol->GetDevicePathFromFilePath(DeviceName));
  }

  //
  // Check for EFI shell
  //
  if (mEfiShellEnvironment2 != NULL) {
    return (mEfiShellEnvironment2->NameToPath(DeviceName));
  }

  return (NULL);
}

int
EFIAPI
main (
  IN int Argc,
  IN char **Argv
  )
{
  EFI_DEVICE_PATH_PROTOCOL *DevicePath;
  CHAR16 *R;
  
  R=(CHAR16 *)ShellGetCurrentDir(NULL);
  
  Print(L"Current dir [%s]\n",ShellGetCurrentDir(0));
  
  DevicePath=ShellGetDevicePath(R);
  
  Print(L"Type        [%d]\nSub-Type    [%d]\nLength      [%d]\n",
				DevicePath->Type,
				DevicePath->SubType,
				DevicePath->Length[0] + DevicePath->Length[1]*256);
  
  return EFI_SUCCESS;
}

 

运行结果

getCurDP

代码下载

GetCurDP

参考:

1.UEFI_Shell_Spec_2_1_July02release P28
2.http://www.lab-z.com/getcurdir2/ Shell GetCurDir 补遗
3.http://www.bluestop.org/edk2/docs/UDK2010.SR1/struct_e_f_i___s_h_e_l_l___e_n_v_i_r_o_n_m_e_n_t2.html EFI_SHELL_ENVIRONMENT2 Struct Reference