rikirobot——STM32hardware.h结合串口和ros_lib

roslib是所有ROS客户机库和工具的基本依赖项

#pragma once//由编译器提供保证:同一个文件不会被包含多次,保证头文件只被编译一次。注意这里所说的“同一个文件”是指物理上的一个文件,而不是指内容相同的两个文件。
#include "hardwareserial.h"
HardwareSerial Serial(SERIAL1);
#define SERIAL_CLASS  HardwareSerial

class STM32Hardware {
  public:
  	STM32Hardware(SERIAL_CLASS* io , long baud= 57600){
      iostream = io;//输入输出流
      baud_ = baud;
    }
    STM32Hardware()
    {
      iostream = &Serial;
      baud_ = 57600;
    }
    STM32Hardware(STM32Hardware& h){
	  this->iostream = iostream;
      this->baud_ = h.baud_;
    }

    void setBaud(long baud){
      this->baud_= baud;
    }

    int getBaud(){return baud_;}

    void init(){
      iostream->begin(baud_);
    }

    int read(){
      if(iostream->available()){
	  	return iostream->read();
      }else{
	    return -1;
      }
    };

    void write(uint8_t* data, int length){
      for(int i=0; i<length; i++){
		  iostream->write(data[i]);
      }
    }

    unsigned long time(){return millis();}

  protected:
    SERIAL_CLASS* iostream;
    long baud_;
};

struct与class:
struct中的默认访问控制权限是public,而class的默认访问控制权限是private
为了隐藏结构体内的成员,添加访问控制标识:

struct RecTangle{
private:
int width; int height; 
int pos_x; int pos_y;
public:
int Right(); // get right
int Bottom(); // get bottom
int Left(); // get left
int Top(); // get top
};

如果用class来代替struct,则需要添加访问控制标识.
比如用class来定义类C结构体

class RecTangle{
public:
int width; int height; 
int pos_x; int pos_y;
};

iostream.h与iostream
iostream.h是包含输入/输出流处理的头文件,iostream就什么都不是了
但用iostream要加名词空间namespace

#include<iostream.h> 

或者是

#include<iostream> 
 using namespace std;

二者都行。

C++中为了避免名字定义冲突,特别引入了“名字空间的定义”,即namespace。当代码中用时,输出可直接引用cout继承C语言的标准库文件,未引入名字空间定义,所以可直接使用。

当代码中引入时,输出需要引用std::cout<时,引入std::有以下方法:

1.
using namespace std;
cout<

2.
using std::cout;
cout<

3.最基本的
std::cout<

通常用#include 时,要用using namespace std;。
如果不用这个,就要在使用cout时,用后两种方法了。其他头文件也是同样的道理。

java中public、private、protected区别
类中的数据成员和成员函数据具有的访问权限包括:public、private、protect、friendly(包访问权限)
1、public:public表明该数据成员、成员函数是对所有用户开放的,所有用户都可以直接进行调用
2、private:private表示私有,私有的意思就是除了class自己之外,任何人都不可以直接使用,私有财产神圣不可侵犯嘛,即便是子女,朋友,都不可以使用。
3、protected:protected对于子女、朋友来说,就是public的,可以自由使用,没有任何限制,而对于其他的外部class,protected就变成private。

priavte 本类可见

public 所有类可见

protected 本包和所有子类都可见(本包中的子类非子类均可访问,不同包中的子类可以访问,不是子类不能访问)

friendly 本包可见(即默认的形式)(本包中的子类非子类均可访问,不同包中的类及子类均不能访问)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
由于您的要求涉及到硬件操作和中断控制,需要在具体的嵌入式系统上进行编程。下面是一个基于 STM32F4xx 的 C 代码示例,可以实现您所要求的功能: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include "stm32f4xx.h" #include "stm32f4xx_gpio.h" #include "stm32f4xx_usart.h" #define BUFFER_SIZE 10 GPIO_InitTypeDef GPIO_InitStruct; USART_InitTypeDef USART_InitStruct; NVIC_InitTypeDef NVIC_InitStruct; volatile char CharBuff[BUFFER_SIZE]; volatile int head = 0; volatile int tail = 0; volatile int buffer_full = 0; volatile int uart_rx_enabled = 1; void init_gpio(void) { RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &GPIO_InitStruct); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_DOWN; GPIO_Init(GPIOA, &GPIO_InitStruct); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA, &GPIO_InitStruct); GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2); } void init_uart(void) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); USART_InitStruct.USART_BaudRate = 9600; USART_InitStruct.USART_WordLength = USART_WordLength_8b; USART_InitStruct.USART_StopBits = USART_StopBits_1; USART_InitStruct.USART_Parity = USART_Parity_No; USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStruct.USART_Mode = USART_Mode_Rx; USART_Init(USART2, &USART_InitStruct); NVIC_InitStruct.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0; NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStruct); USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); USART_Cmd(USART2, ENABLE); } void USART2_IRQHandler(void) { if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) { char data = USART_ReceiveData(USART2); if (data >= 'A' && data <= 'Z' && !buffer_full && uart_rx_enabled) { CharBuff[tail] = data; tail = (tail + 1) % BUFFER_SIZE; if (tail == head) { buffer_full = 1; USART_ITConfig(USART2, USART_IT_RXNE, DISABLE); } } USART_ClearITPendingBit(USART2, USART_IT_RXNE); } } void blink_led(char data) { int i, j; int dot_time = 200; int dash_time = 500; switch (data) { case 'A': GPIO_SetBits(GPIOA, GPIO_Pin_5); delay_ms(dot_time); GPIO_ResetBits(GPIOA, GPIO_Pin_5); delay_ms(dash_time); GPIO_SetBits(GPIOA, GPIO_Pin_5); delay_ms(dot_time); GPIO_ResetBits(GPIOA, GPIO_Pin_5); break; case 'B': GPIO_SetBits(GPIOA, GPIO_Pin_5); delay_ms(dash_time); GPIO_ResetBits(GPIOA, GPIO_Pin_5); delay_ms(dot_time); GPIO_SetBits(GPIOA, GPIO_Pin_5); delay_ms(dot_time); GPIO_ResetBits(GPIOA, GPIO_Pin_5); delay_ms(dot_time); GPIO_SetBits(GPIOA, GPIO_Pin_5); delay_ms(dot_time); GPIO_ResetBits(GPIOA, GPIO_Pin_5); break; // Add more cases for the rest of the alphabet... } } int main(void) { init_gpio(); init_uart(); while (1) { if (head != tail) { blink_led(CharBuff[head]); head = (head + 1) % BUFFER_SIZE; if (buffer_full) { buffer_full = 0; USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); } } if (!UART_RX_ENABLED && GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0)) { UART_RX_ENABLED = 1; USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); } } } ``` 该程序使用 STM32F4xx 的 GPIO 和 USART 模块来实现所需的功能。其中,`init_gpio()` 函数用于初始化 GPIO 端口,`init_uart()` 函数用于初始化 USART 端口。在中断服务函数 `USART2_IRQHandler()` 中,程序会检测是否有数据通过 USART 接收到,如果是大写字母且 CharBuff 没有满,那么将其添加到 CharBuff 中。如果 CharBuff 已满,则禁用 UART RX 中断。在主循环中,程序会周期性地检查 CharBuff 是否有数据,如果有则将其转换为 Morse 代码并通过 LED 闪烁输出。当 CharBuff 已满时,程序会禁用 UART RX 中断,直到 CharBuff 又有空间可用。同时,程序会检测 P_B1 按键是否按下,如果按下且 UART RX 已禁用,则启用 UART RX 中断。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值