查看原文
其他

STM32硬核DIY机械键盘|蓝牙USB双模|灯控

项目起因

项目简介

为什么叫创世一号(WorldEdit)呢?因为想着以后用这把键盘,闯荡天下,制造更多东西,就像一个创世神一样,所以就叫创世一号WorldEdit了

创世一号采用STM32F103RCT6主控,整合有锂电池充放电管理,稳压,蓝牙串口HID,USB-HID,LED灯阵列驱动,按键检测电路。可实现蓝牙连接模式(支持IOS、Android等平台),和USB连接模式。蓝牙连接时响应时间约15ms,USB连接模式主控响应时间约为0.3Ms。

靓照如下:

一、准备材料阶段

1.键盘配列

首先做机械键盘,需要了解键盘的配列。制作配列有很多工具可以用,我们在这一步需要把定位板的CAD图生成,用到以下两个网站:

配列编辑网站: http://www.keyboard-layout-editor.com/

定位板CAD生成网站:

http://builder.swillkb.com/

如果要做的配列和我的一样,可以直接使用我的配列CAD文件,我要做的配列如下:(这个网站可以很方便的修改配列,网上教程已经很多了,我就不详细介绍了)

然后将你的配列的Raw data复制到CAD生成网站。生成CAD定位板文件 (该文件在画PCB和键盘外壳都需要用到)

然后底下的这些是和轴体有关的,一般来说默认就行了。

点击

生成文件,然后点击右上DXF并将文件下载保存。

到这里,键盘配列就完成了。然后去网上冲浪 淘一淘 , 买键帽、蓝牙HID模块、主控啊、一些电容电阻啊、锂电池啊、等等需要用到的东西。

2. 元器件、模块、电池、轴体、键帽等采购

我的整个键盘需要的物品清单如下:

器件购买完毕后,就可以开始画电路了(不会的可以用我的PCB去加工,我100元做了5张PCB)由于整个项目都已开源,这里就不介绍具体电路了。

然后是设计外壳。由于没有加工条件,只能网上订制,所以就简单的设计了一个不包底的钢板做框架。最后成品也还可以。

二、组装调试

以上都准备完成后,就是焊电路,装轴,上键帽了。具体步骤,可以看我的制作视频。

三、优化程序代码

代码部分的思路就是检查键盘消息是否更新,更新了就生成按键数据包,然后串口传输给蓝牙模块。如果是USB模式的话就直接USB发送给电脑主机。按键物理上如何快速与程序代码链接起来呢,这里有一个思路,如果有更好的方法或建议请留言或私信,谢谢啦。这里将按键的位置与数组的顺序对应了起来,在后面处理按键消息的时候用起来很方便。

#define xxCK myKeyBoard_ControlKey 
#define xxSK myKeyBoard_SpecialKey 

const char * myKeyBoard_KeyMap_Name[6][16] = 
{
 "Esc","F1","F2","F3","F4","F5","F6","F7","F8","F9","F10","F11","F12","PrtSc","ScrollLock","PauseBreak",
 "~","!","@","#","$","%","^","&","*","(",")","-","+","BackSpace","Insert","Home",
 "Tab","Q","W","E","R","T","Y","U","I","O","P","{","}","|","Delete","End",
 "CapsLock","A","S","D","F","G","H","J","K","L",";","'","Enter","","","PageUp",
 "LShift","Z","X","C","V","B","N","M",",",".","/","RShift","Up","","","PageDown",
 "LCtrl","Win","LAlt","Space","RAlt","FN","Menu","RCtrl","Left","Down","Right","","","","",""
};


const unsigned char myKeyBoard_KeyMap_ATValue[6][16] = 
{
 0x29,0x3A,0x3B,0x3C,0x3D,0x3E,0x3F,0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,
 0x35,0x1E,0x1F,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x2D,0x2E,0x2A,0x49,0x4A,
 0x2B,0x14,0x1A,0x08,0x15,0x17,0x1C,0x18,0x0C,0x12,0x13,0x2F,0x30,0x31,0x4C,0x4D,
 0x39,0x04,0x16,0x07,0x09,0x0A,0x0B,0x0D,0x0E,0x0F,0x33,0x34,0x28,0x00,0x00,0x4B,
 xxCK,0x1D,0x1B,0x06,0x19,0x05,0x11,0x10,0x36,0x37,0x38,xxCK,0x52,0x00,0x00,0x4E,
 xxCK,xxCK,xxCK,0x2C,xxCK,xxSK,xxSK,xxCK,0x50,0x51,0x4F,0x00,0x00,0x00,0x00,0x00,
};

main 函数如下:(具体代码已开源至GitHub)

int main()
{
 sysInit();
 //BTK05_Wake();//唤醒BTK05
 

 while(1)
 { 
  if(g_USBModeFlag)//USB模式下
  {
   if(sleepTime1SCounter <= 1000)
   {
    myKeyBoard_ScanKeyAndUpdataATBuffer();//键盘事件更新
    if(g_myKeyBoard_DataWaitForUploadFlag == 1)
    {
     g_myKeyBoard_DataWaitForUploadFlag = 0;
     if(bDeviceState == CONFIGURED)//如果连接上了 USB
      Keyboard_Send(BTK05_ATKeyDataPack+4);
     GPIO_SetBits(LED_LOGO_GPIOPort,LED_LOGO_GPIOPin);
     
    }
   }
   else if(system_Status == 0)
   {
    myKeyBoard_ScanKeyAndUpdataATBuffer();//键盘事件更新
    if(g_myKeyBoard_DataWaitForUploadFlag == 1)
    {
     g_myKeyBoard_DataWaitForUploadFlag = 0;
     if(bDeviceState == CONFIGURED)//如果连接上了 USB
      Keyboard_Send(BTK05_ATKeyDataPack+4);
     GPIO_SetBits(LED_LOGO_GPIOPort,LED_LOGO_GPIOPin);
     system_Status = 1;
     TIM_Cmd(TIM2, ENABLE);  //使能TIMx 
     sleepTime1SCounter = 0;
     if(LED_Status) LED_WakeUp(); 
    }
   }
   else if(sleepTime1SCounter > 1000)
   {
    TIM_Cmd(TIM2, DISABLE);  //失能TIMx  
    system_Status = 0;
    LED_GoToSleep();
   }
  }
  else//蓝牙模块模式
  {
   //如果键盘休眠计时器小于400并且键盘处于醒着的状态
   if(sleepTime1SCounter <= 400 && BTK05_Status == BTK_WAKE)
   {
    myKeyBoard_ScanKeyAndUpdataATBuffer();//键盘事件更新
    if(g_myKeyBoard_DataWaitForUploadFlag == 1)
    {
     g_myKeyBoard_DataWaitForUploadFlag = 0;
     //GPIO_ResetBits(LED_LOGO_GPIOPort,LED_LOGO_GPIOPin);
     BTK05_UART_SendKeyData(BTK05_ATKeyDataPack,12);
     sleepTime1SCounter = 0;

     if(ATKeyControlByte5 != 0)
     {
      GPIO_SetBits(LED_LOGO_GPIOPort,LED_LOGO_GPIOPin);
     }
     else
     {
      GPIO_ResetBits(LED_LOGO_GPIOPort,LED_LOGO_GPIOPin);
     }
    }
   }
   //如果蓝牙处于休眠状态
   else if(BTK05_Status == BTK_SLEEP)
   {
    myKeyBoard_ScanKeyAndUpdataATBuffer();
    if(g_myKeyBoard_DataWaitForUploadFlag == 1)
    {
     system_Status = 1;
     BTK05_Wake();//唤醒BTK05
     TIM_Cmd(TIM2, ENABLE);  //使能TIMx 
     sleepTime1SCounter = 0;
     g_myKeyBoard_DataWaitForUploadFlag = 0;
     if(LED_Status) LED_WakeUp();
     BTK05_UART_SendKeyData(BTK05_ATKeyDataPack,12);
    }
   }
   //如果 键盘休眠计时器大于600   即600S没有按下任何按键则然键盘进入休眠状态
   else if(sleepTime1SCounter > 400)
   {
    //进入休眠模式...
    BTK05_Sleep();//休眠BTK05
    TIM_Cmd(TIM2, DISABLE);  //失能TIMx  
    system_Status = 0;
    LED_GoToSleep(); 
   }
  }
  
  if(LED_BreathEfectFlag && LED_breath_Flag)
  {
   LED_BreathProcess();
   LED_breath_Flag = 0;
 }

}

Github:

https://github.com/firestaradmin/WorldEdit-No.1

项目制作全过程视频BiliBili:

https://www.bilibili.com/video/av81005730#reply2225849370

往期精彩

MCU串口命令解析器的实现

深圳云之手科技开源炫酷RGB灯板DIY项目

一个超火超给力的STM32开源疫情监控项目

云之手红外式测温计产品设计分享(基于合泰BH67F2752方案)

觉得本次分享的文章对您有帮助,随手点[在看]并转发分享,也是对我的支持。

    您可能也对以下帖子感兴趣

    文章有问题?点此查看未经处理的缓存