aa
Revision 0:db8d4af513c0, committed 2020-01-20
- Comitter:
- M_souta
- Date:
- Mon Jan 20 08:46:24 2020 +0000
- Child:
- 1:5b0303768126
- Commit message:
- jjjjjj
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/MCP/MCP.cpp Mon Jan 20 08:46:24 2020 +0000
@@ -0,0 +1,14 @@
+#include "MCP.h"
+#include "mbed.h"
+
+MCP::MCP(PinName sda, PinName scl, uint8_t device_address)
+ :i2c(sda, scl)
+{
+ _write_opcode = device_address & 0xFE; // low order bit = 0 for write
+ _read_opcode = device_address | 0x01; // low order bit = 1 for read;
+}
+
+void MCP::PinMode(uint8_t pin, uint8_t mode)
+{
+ //_pull_data
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/MCP/MCP.h Mon Jan 20 08:46:24 2020 +0000
@@ -0,0 +1,79 @@
+#ifndef MCP_H_
+#define MCP_H_
+
+#include "mbed.h"
+
+#define SDA PB_7
+#define SCL PB_6
+
+#define MCP_ADDRESS 0x40
+
+// MCP register address
+#define IODIRA 0x00
+#define IODIRB 0x01
+#define IPOLA 0x02
+#define IPOLB 0x03
+#define GPINTENA 0x04
+#define GPINTENB 0x05
+#define DEFVALA 0x06
+#define DEFVALB 0x07
+#define INTCONA 0x08
+#define INTCONB 0x09
+#define IOCONA 0x0A
+#define IOCONB 0x0B
+#define GPPUA 0x0C
+#define GPPUB 0x0D
+#define INTFA 0x0E
+#define INTFB 0x0F
+#define INTCAPA 0x10
+#define INTCAPB 0x11
+#define GPIOA 0x12
+#define GPIOB 0x13
+#define OLATA 0x14
+#define OLATB 0x15
+
+typedef enum {
+ OUTPUT,
+ INPUT,
+ INPUT_PULLUP,
+}pin_mode;
+
+typedef union {
+ uint8_t port_A, port_B;
+ uint16_t all;
+}mcp_register;
+
+class MCP {
+ public:
+ /* MCP class constracter
+ / deffult Input
+ / */
+ MCP(PinName sda, PinName scl, uint8_t device_address);
+ // MCP pin define * pin number is 0 ~ 15
+ void PinMode(uint8_t pin, uint8_t mode);
+ // MCP DigitalWrite * pin number is 0 ~ 15
+ void Write(uint8_t pin, bool signal);
+ // MCP DigitalRead * pin number is 0 ~ 15
+ bool Read(uint8_t pin, bool signal);
+ // MCP initialize
+ void Initialize(void);
+ // MCP register update and read new data
+ void Update(void);
+ // i2c error check
+ bool ErrorOccurred(void);
+
+ private:
+ I2C i2c;
+ char _read_opcode;
+ char _write_opcode;
+
+ uint16_t _pull_data;
+ uint16_t _read_data;
+ uint16_t _write_data;
+
+ void _Write(uint8_t address, uint8_t data);
+ void _Read(uint8_t address, uint8_t data);
+
+};
+
+#endif //I2C_H_
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MCP/MCP23017.lib Mon Jan 20 08:46:24 2020 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/wim/code/MCP23017/#5696b886a895
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Motor/Motor.cpp Mon Jan 20 08:46:24 2020 +0000
@@ -0,0 +1,29 @@
+#include "Motor.h"
+
+#include "mbed.h"
+
+MOTOR::MOTOR(PinName D1, PinName D2, PinName pwm)
+ :D1_(D1), D2_(D2), pwm_(pwm)
+{
+ D1_.write(0);
+ D2_.write(0);
+ pwm_.write(0);
+}
+
+void MOTOR::Dir(dire mode, uint8_t pwm)
+{
+ D1_.write((char)mode & 0x01);
+ D1_.write((char)mode & 0x02);
+ pwm_.write((float)(pwm / 100.0));
+}
+
+void MOTOR::Dir(dire mode)
+{
+ D1_.write((char)mode & 0x01);
+ D1_.write((char)mode & 0x02);
+}
+
+void MOTOR::PWM(uint8_t pwm)
+{
+ pwm_.write((float)(pwm / 100.0));
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Motor/Motor.h Mon Jan 20 08:46:24 2020 +0000
@@ -0,0 +1,40 @@
+#ifndef MOTOR_H_
+#define MOTOR_H_
+
+#include "mbed.h"
+
+typedef enum dire {
+ FREE = 0,
+ FOR = 1,
+ BACK = 2,
+ BRAKE = 3,
+}dire;
+
+class MOTOR {
+ public:
+ //
+ MOTOR(PinName D1, PinName D2, PinName pwm);
+ /* direction
+ FREE
+ FOR
+ BACK
+ BRAKE
+ */
+ /* pwm
+ pwm is 0 ~ 100(%)
+ */
+ // durection and pwm set
+ void Dir(dire mode, uint8_t pwm);
+ // direction set
+ void Dir(dire mode);
+ // pwm set
+ void PWM(uint8_t pwm);
+
+ private:
+ DigitalOut D1_;
+ DigitalOut D2_;
+ PwmOut pwm_;
+};
+
+#endif //MOTOR_H_
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/XBee/XBee.cpp Mon Jan 20 08:46:24 2020 +0000
@@ -0,0 +1,112 @@
+#include "XBee.h"
+
+#include <stdint.h>
+#include "mbed.h"
+
+namespace XBEE
+{
+ Ticker xbee_timer;
+ Serial xbee_uart(XBEE_TX, XBEE_RX);
+ DigitalOut XBee_LED(LED1);
+
+ void uartUpdate(void);
+ void lostCheck(void);
+
+ namespace
+ {
+ ControllerData ctrData;
+ ControllerData keepCtrData;
+ const uint8_t defaultData[4] = CTR_DEFAULT_DATA;
+ const char check[] = "DT=";
+ volatile char packet[24];
+
+ bool controllerLost = false;
+ uint8_t timerCount = 0;
+ }
+
+ void Controller::Initialize(void) {
+ xbee_timer.attach(lostCheck, 0.025);
+ xbee_uart.baud(4800);
+ xbee_uart.attach(uartUpdate, Serial::RxIrq);
+ DataReset();
+ }
+
+ ControllerData* Controller::GetData(void) {
+ __disable_irq();
+ for(uint8_t i = 0; i < CTR_DATA_LENGTH; i++) keepCtrData.buf[i] = ctrData.buf[i];
+ __enable_irq();
+ return &keepCtrData;
+ }
+
+ void Controller::DataReset(void) {
+ __disable_irq();
+ for(uint8_t i = 0; i < CTR_DATA_LENGTH; i++) ctrData.buf[i] = defaultData[i];
+ __enable_irq();
+ }
+
+ bool Controller::CheckControllerLost(void) {
+ return controllerLost;
+ }
+
+ void uartUpdate(void) {
+ static bool phase = false;
+ static uint8_t count = 0;
+
+ char data = xbee_uart.getc();
+
+ if(phase)
+ {
+ packet[count] = data;
+ if(count < 2)
+ {
+ if(data != check[count])
+ {
+ phase = false;
+ controllerLost = true;
+ XBee_LED = LED_OFF;
+ }
+ }
+ else if(count == 8)
+ {
+ if(data != '\r')
+ {
+ phase = false;
+ controllerLost = true;
+ XBee_LED = LED_OFF;
+ }
+ else
+ {
+ ctrData.buf[0] = packet[4];
+ ctrData.buf[1] = packet[5];
+ ctrData.buf[2] = packet[6];
+ ctrData.buf[3] = packet[7];
+ phase = false;
+ timerCount = 0;
+ controllerLost = false;
+ XBee_LED = LED_ON;
+ }
+ }
+ count++;
+ }
+ else
+ {
+ if(data == '@')
+ {
+ count = 0;
+ phase = true;
+ }
+ }
+ }
+
+ void lostCheck(void) {
+ timerCount++;
+ if(timerCount == 2) XBee_LED = LED_OFF;
+ if(timerCount >= 20) {
+ controllerLost = true;
+ Controller::DataReset();
+ timerCount = 0;
+ XBee_LED = LED_OFF;
+ }
+ }
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/XBee/XBee.h Mon Jan 20 08:46:24 2020 +0000
@@ -0,0 +1,61 @@
+#ifndef XBEE_H_
+#define XBEE_H_
+
+#include <stdint.h>
+
+namespace XBEE
+{
+ #define CTR_DATA_LENGTH 4
+ #define CTR_DEFAULT_DATA {0x00, 0x00, 0x77, 0x77}
+
+ #define XBEE_TX D1
+ #define XBEE_RX D0
+
+ #define LED_OFF 0
+ #define LED_ON 1
+
+ typedef union
+ {
+ struct {
+ struct {
+ unsigned int X:1;
+ unsigned int A:1;
+ unsigned int B:1;
+ unsigned int Y:1;
+ unsigned int UP:1;
+ unsigned int RIGHT:1;
+ unsigned int DOWN:1;
+ unsigned int LEFT:1;
+ unsigned int SELECT:1;
+ unsigned int HOME:1;
+ unsigned int START:1;
+ unsigned int ZL:1;
+ unsigned int ZR:1;
+ unsigned int L:1;
+ unsigned int R:1;
+ unsigned int :1;
+ } __attribute__ ((packed)) Button;
+ struct {
+ unsigned int Y:4;
+ unsigned int X:4;
+ } __attribute__ ((packed)) AnalogL;
+ struct {
+ unsigned int Y:4;
+ unsigned int X:4;
+ } __attribute__ ((packed)) AnalogR;
+ } __attribute__ ((packed)) ;
+ uint8_t buf[CTR_DATA_LENGTH];
+ }ControllerData;
+
+ class Controller
+ {
+ public:
+ static void Initialize(void);
+ static ControllerData* GetData(void);
+ static void DataReset(void);
+ static bool CheckControllerLost(void);
+ };
+}
+
+#endif
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Mon Jan 20 08:46:24 2020 +0000
@@ -0,0 +1,23 @@
+#include "mbed.h"
+#include "Motor.h"
+#include "MCP.h"
+#include "XBee.h"
+
+#define SDA PB_7
+#define SCL PB_6
+#define MCP_ADDRESS 0x40
+
+MCP MCP(SDA, SCL, MCP_ADDRESS);
+
+XBEE::ControllerData *controller;
+
+int main() {
+
+ while(1) {
+ controller = XBEE::Controller::GetData();
+
+ /* write ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ */
+
+
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Mon Jan 20 08:46:24 2020 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400 \ No newline at end of file