PC虚拟单片机的LCD (假的VNC)

本文已经在STM中文论坛发过,在这里记录一下

 

 

起因:手里有块nucleo-f767的板子,某天心血来潮,想玩一下GUI,下面开始正题。

 

思路:1.单片机+STemwin部分没什么说的,主要问题在于如何把屏幕数据传到PC上显示。

 

2.板子上有USB和网口,USB方便连接,于是选定USB口

 

3.USB协议选择:在自定义、hid、USB-CDC之间选择了USB-CDC,其实自定义性能最好了,不过上位机下位机的驱动可不是随随便便就搞得定的。选择CDC下位机通过CUBEMAX生成然后修改一下即可,上位机直接QT serialport即可,方便

 

4.STemWim底层驱动实现

 

 a.实现void _SetPixelIndex(GUI_DEVICE * pDevice, int x, int y, int PixelIndex)函数;

 

   定义协议 msg(u8)+x(u16)+y(u16)+PixelIndex(u16)+checkSum(u8);

 

   转字符串添加头 "@@"尾"##"

    代码

uint8_t str[128];
static void _SetPixelIndex(GUI_DEVICE * pDevice, int x, int y, int PixelIndex) {
    //
    // Convert logical into physical coordinates (Dep. on LCDConf.h)
    //
    #if (LCD_MIRROR_X == 1) || (LCD_MIRROR_Y == 1) || (LCD_SWAP_XY == 1)
      int xPhys, yPhys;

      xPhys = LOG2PHYS_X(x, y);
      yPhys = LOG2PHYS_Y(x, y);
    #else
      #define xPhys x
      #define yPhys y
    #endif
    GUI_USE_PARA(pDevice);
    GUI_USE_PARA(x);
    GUI_USE_PARA(y);
    GUI_USE_PARA(PixelIndex);
    {
      //
      // Write into hardware ... Adapt to your system
      //
      // TBD by customer...
      //
			uint8_t sum=0,i,a,num=0,n=0;
			uint8_t buf[8];
			buf[0] = 1;
			buf[1] = x&0xff;
			buf[2] = (x>>8)&0xff;
			buf[3] = y&0xff;
			buf[4] = (y>>8)&0xff;
			buf[5] = PixelIndex&0xff;
			buf[6] = (PixelIndex>>8)&0xff;
			for(i=0;i<7;i++)
			{
				sum += buf[i];
			}
			buf[7] = sum;
			
			str[n++] = '@';
			str[n++] = '@';
			num=8;
			for(int i=0;i<num;i++)
			{
				a = (buf[i]&0xf0)>>4;
				if(a<10)a += 0x30;
				else a = a - 10 + 0X41;
				str[n++]=a;
				a = buf[i]&0x0f;
				if(a<10)a += 0x30;
				else a = a - 10 + 0X41;
				str[n++]=a;;
			}		
			str[n++] = '#';
			str[n++] = '#';
//			str[n++] = '\r';
//			str[n++] = '\n';
			//while(Transmit_FS_Stord(str,n)<0);
			while(USBD_OK!=CDC_Transmit_FS(str,n));
    }
    #if (LCD_MIRROR_X == 0) && (LCD_MIRROR_Y == 0) && (LCD_SWAP_XY == 0)
      #undef xPhys
      #undef yPhys
    #endif
}

b.实现static void _FillRect(GUI_DEVICE * pDevice, int x0, int y0, int x1, int y1)

 

直接贴代码

static void _FillRect(GUI_DEVICE * pDevice, int x0, int y0, int x1, int y1) {
  LCD_PIXELINDEX PixelIndex;
//  int x;
	uint8_t sum=0,i,a,num=0,n=0;
	uint8_t buf[12];
	
  PixelIndex = LCD__GetColorIndex();
	

			buf[0] = 2;
			buf[1] = x0&0xff;
			buf[2] = (x0>>8)&0xff;
			buf[3] = y0&0xff;
			buf[4] = (y0>>8)&0xff;
			buf[5] = x1&0xff;
			buf[6] = (x1>>8)&0xff;
			buf[7] = y1&0xff;
			buf[8] = ((y1)>>8)&0xff;
			buf[9] = PixelIndex&0xff;
			buf[10] = (PixelIndex>>8)&0xff;
			for(i=0;i<11;i++)
			{
				sum += buf[i];
			}
			buf[11] = sum;
			
			str[n++] = '@';
			str[n++] = '@';
			num=12;
			for(int i=0;i<num;i++)
			{
				a = (buf[i]&0xf0)>>4;
				if(a<10)a += 0x30;
				else a = a - 10 + 0X41;
				str[n++]=a;
				a = buf[i]&0x0f;
				if(a<10)a += 0x30;
				else a = a - 10 + 0X41;
				str[n++]=a;;
			}		
			str[n++] = '#';
			str[n++] = '#';
			
		//	CDC_Transmit_FS(str,n);
	//	while(Transmit_FS_Stord(str,n)<0);
	while(USBD_OK!=CDC_Transmit_FS(str,n));
//  if (GUI_pContext->DrawMode & LCD_DRAWMODE_XOR) {
//    for (; y0 <= y1; y0++) {
//      for (x = x0; x <= x1; x++) {
//        _XorPixel(pDevice, x, y0);
//      }
//    }
//  } else {
//    for (; y0 <= y1; y0++) {
//      for (x = x0; x <= x1; x++) {
//        _SetPixelIndex(pDevice, x, y0, PixelIndex);
//      }
//    }
//  }
}

其他部分略过

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值