查看: 3870|回复: 15

[分享] FRDM-KL25Z 2、USB鼠标写字

[复制链接]
  • TA的每日心情
    奋斗
    2017-5-3 11:19
  • 签到天数: 10 天

    连续签到: 1 天

    [LV.3]偶尔看看II

    50

    主题

    1万

    帖子

    0

    金牌会员

    Rank: 6Rank: 6

    积分
    14090
    最后登录
    2024-4-19
    发表于 2015-4-25 13:09:21 | 显示全部楼层 |阅读模式
    昨天发了用KL25Z USB鼠标画圆,今天我们让KL25ZUSB鼠标写出“飞思卡尔”四个汉字。
    对昨天的项目进行改造,引入库不变,还是mbed和USBDevice这两个库,新添加了三个函数:画点,画线,画文本。
    让USB鼠标自己写“飞思卡尔”四个汉字,用到画文本函数,画文本函数又用到画点函数。画线的函数没有使用。以后再用,废话不多说看代码吧。
    1. #include <math.h>
    2. #include <stdlib.h>

    3. #include "mbed.h"
    4. #include "USBMouse.h"


    5. #define SCALE 120

    6. USBMouse mouse(ABS_MOUSE);

    7. //定义点
    8. struct Point
    9. {
    10.     int16_t x;
    11.     int16_t y;   
    12. };
    13. //画圆
    14. void DrawCircle(Point basePoint,int32_t radius);
    15. //画线
    16. void DrawLine(Point startPoint,Point endPoint);
    17. //画点
    18. void DrawDot(Point targetPoint);
    19. //画点阵
    20. void DrawText();
    21. //定义全局变量
    22. Point origin;   //圆心坐标
    23. //字体点阵
    24. uint8_t HZ_1 [4][32]= {{0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0xFE,0x40,0xA0,0x10,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x0C,0x10,0x21,0x42,0xF0,0x00},
    25. {0x00,0x00,0xFE,0x92,0x92,0x92,0x92,0xFE,0x92,0x92,0x92,0x92,0xFE,0x00,0x00,0x00,0x40,0x38,0x01,0x00,0x3C,0x40,0x40,0x42,0x4C,0x40,0x40,0x70,0x05,0x08,0x30,0x00},
    26. {0x40,0x40,0x40,0x40,0x40,0x40,0xFF,0x44,0x44,0x44,0x44,0x44,0x44,0x40,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x02,0x04,0x08,0x10,0x00,0x00,0x00},
    27. {0x80,0x40,0x20,0x18,0x0F,0x08,0x08,0xE8,0x08,0x08,0x08,0x08,0x28,0x18,0x00,0x00,0x20,0x10,0x08,0x06,0x00,0x40,0x80,0x7F,0x00,0x00,0x00,0x02,0x04,0x08,0x30,0x00}};

    28. int main()
    29. {
    30.     //中心坐标
    31.     origin.x = (X_MAX_ABS - X_MIN_ABS)/2;
    32.     origin.y = (Y_MAX_ABS - Y_MIN_ABS)/2;
    33.    
    34.     int32_t radius = origin.y / 2;
    35.     //画圆
    36.     DrawCircle(origin,radius);
    37.     //变换坐标
    38.     origin.x -= 3500;
    39.     origin.y -= 2500;
    40.     //输出文字
    41.     DrawText();
    42.    
    43.     while(1);
    44. }


    45. void DrawCircle(Point basePoint,int32_t radius)
    46. {
    47.     int16_t x =0,y=0;
    48.     int angle = 0;
    49.    
    50.     mouse.press(MOUSE_LEFT);
    51.     wait(0.01);
    52.     for(angle =0;angle<=360;angle++)
    53.     {
    54.         x = basePoint.x + radius * cos((double)angle * 3.14 / 180);
    55.         y = basePoint.y + radius * sin((double)angle * 3.14 / 180);
    56.         mouse.move(x,y);
    57.         
    58.         wait(0.01);
    59.     }
    60.     mouse.release(MOUSE_LEFT);
    61.     wait(0.01);
    62. }

    63. void DrawLine(Point startPoint,Point endPoint)
    64. {
    65.     wait(0.01);
    66.     mouse.move(startPoint.x,startPoint.y);
    67.     wait(0.01);
    68.     mouse.press(MOUSE_LEFT);
    69.     wait(0.01);
    70.     mouse.move(endPoint.x,endPoint.y);
    71.     wait(0.01);
    72.     mouse.release(MOUSE_LEFT);
    73.     wait(0.01);
    74. }

    75. void DrawDot(Point targetPoint)
    76. {   
    77.     mouse.move(targetPoint.x,targetPoint.y);
    78.     wait(0.01);
    79.     mouse.press(MOUSE_LEFT);
    80.     wait(0.01);
    81.     mouse.release(MOUSE_LEFT);
    82.     wait(0.01);
    83. }

    84. void DrawText()
    85. {
    86.     mouse.move(origin.x,origin.y);
    87.     wait(0.01);
    88.     for(int i=0;i<4;i++)
    89.     {
    90.         Point targetPoint;
    91.         targetPoint.x = origin.x + i * 16 * SCALE;
    92.         
    93.         for(int j=0;j<32;j++)
    94.         {
    95.             targetPoint.x += SCALE;
    96.             if(j == 15)
    97.                 targetPoint.x = origin.x + i * 16 * SCALE;
    98.             if(j <= 15)
    99.             {
    100.                 targetPoint.y = origin.y;
    101.             }
    102.             else
    103.             {
    104.                 targetPoint.y = origin.y + 8 * (SCALE + 80);
    105.             }
    106.             
    107.             for(int k=0;k<8;k++)
    108.             {
    109.                 targetPoint.y += SCALE + 80;
    110.                 uint8_t dot = (HZ_1[i][j] >> k) & 0x01;
    111.                 if(dot)
    112.                 {
    113.                     DrawDot(targetPoint);
    114.                 }
    115.             }
    116.         }
    117.     }
    118. }
    复制代码
    今天日了怪了,附件上传不上去,效果图看不到。
    补个视频给大家看:




    该会员没有填写今日想说内容.
    回复

    使用道具 举报

    该用户从未签到

    0

    主题

    10

    帖子

    0

    新手上路

    Rank: 1

    积分
    46
    最后登录
    1970-1-1
    发表于 2015-5-6 10:03:52 | 显示全部楼层
    是自己写的上位机吧,你用什么语言写的呀
    回复 支持 1 反对 0

    使用道具 举报

  • TA的每日心情
    奋斗
    2017-5-3 11:19
  • 签到天数: 10 天

    连续签到: 1 天

    [LV.3]偶尔看看II

    50

    主题

    1万

    帖子

    0

    金牌会员

    Rank: 6Rank: 6

    积分
    14090
    最后登录
    2024-4-19
     楼主| 发表于 2015-4-25 13:18:32 | 显示全部楼层
    效果图补一下:
    3.png
    nothing.png nothing.png nothing.png nothing.png nothing.png nothing.png nothing.png nothing.png nothing.png
    该会员没有填写今日想说内容.
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    奋斗
    2023-9-17 19:57
  • 签到天数: 2310 天

    连续签到: 1 天

    [LV.Master]伴坛终老

    107

    主题

    4270

    帖子

    1

    金牌会员

    Rank: 6Rank: 6

    积分
    10481
    最后登录
    2023-9-17
    发表于 2015-4-25 14:49:41 | 显示全部楼层
    很有创意,顶一个。
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    11

    主题

    251

    帖子

    0

    高级会员

    Rank: 4

    积分
    690
    最后登录
    2016-11-3
    发表于 2015-4-25 17:34:16 | 显示全部楼层
    感谢楼主分享,学习中
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2017-1-24 09:50
  • 签到天数: 2 天

    连续签到: 1 天

    [LV.1]初来乍到

    654

    主题

    3262

    帖子

    0

    金牌会员

    Rank: 6Rank: 6

    积分
    13133
    最后登录
    2019-1-27
    发表于 2015-4-25 18:55:34 | 显示全部楼层
    这个有点意思

    回复 支持 反对

    使用道具 举报

    该用户从未签到

    5

    主题

    42

    帖子

    0

    中级会员

    Rank: 3Rank: 3

    积分
    250
    最后登录
    2015-12-10
    发表于 2015-4-25 19:47:08 | 显示全部楼层
    效果很好看啊
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    27

    主题

    157

    帖子

    0

    中级会员

    Rank: 3Rank: 3

    积分
    465
    最后登录
    2015-6-20
    发表于 2015-4-25 20:12:21 | 显示全部楼层
    有创意哦
    回复

    使用道具 举报

  • TA的每日心情
    开心
    2021-12-10 16:14
  • 签到天数: 1442 天

    连续签到: 1 天

    [LV.10]以坛为家III

    17

    主题

    3862

    帖子

    0

    金牌会员

    Rank: 6Rank: 6

    积分
    8291
    最后登录
    2021-12-10
    发表于 2015-4-25 21:54:35 | 显示全部楼层
    很不错的东西,感谢分享
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2019-10-18 11:35
  • 签到天数: 9 天

    连续签到: 1 天

    [LV.3]偶尔看看II

    21

    主题

    945

    帖子

    0

    金牌会员

    Rank: 6Rank: 6

    积分
    2001
    最后登录
    2020-6-8
    发表于 2015-4-25 22:00:07 | 显示全部楼层
    真的很不错,跟楼主学习。
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    奋斗
    2025-5-7 09:07
  • 签到天数: 353 天

    连续签到: 1 天

    [LV.8]以坛为家I

    141

    主题

    8056

    帖子

    0

    金牌会员

    Rank: 6Rank: 6

    积分
    5785
    最后登录
    2025-5-7
    发表于 2015-4-26 08:52:26 | 显示全部楼层
    你可以画个表了
    该会员没有填写今日想说内容.
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /3 下一条

    Archiver|手机版|小黑屋|恩智浦技术社区

    GMT+8, 2025-7-24 08:14 , Processed in 0.110684 second(s), 31 queries , MemCache On.

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.

    快速回复 返回顶部 返回列表