V5Hardware.h
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2011, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage, Inc. nor the names of its
18  * contributors may be used to endorse or promote prducts derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #ifndef _ROSSERIAL_VEX_V5_V5_HARDWARE_H_
36 #define _ROSSERIAL_VEX_V5_V5_HARDWARE_H_
37 
38 #include "main.h"
40 
41 // for the mutex.
42 #include "pros/apix.h"
43 
44 #define SERIAL_CLASS int
45 #define ROSVEX_BUFFER_INPUT_SIZE 32
46 
48 
49 // load the serial reading into a buffer.
50 inline void vexRosBufferInput(void* arg) {
51 
52  void** arglist = (void**) arg;
53  RB* inputBuffer = (RB*) arglist[0];
54  __FILE* streamOut = (__FILE*) arglist[1];
55 
56  int readcount = 0;
57  while(1) {
58  char c = fgetc(streamOut);
59  inputBuffer->add(c);
60  }
61 }
62 
63 class V5Hardware {
64 
65  public:
67  }
68 
69  // any initialization code necessary to use the serial port
70  // note: the serial port initialization for rosserial for VEX Cortex must be implemented in `src/init.cpp`
71  // see that file for more information.
72  void init() {
73  serctl(SERCTL_DISABLE_COBS, NULL);
74  rosFile = fopen("/ser/sout", "r+");
75 
76  // not typesafe, be careful!
77  void** taskArgs = (void**) malloc( sizeof(void*) * 2);
78  taskArgs[0] = &inputBuffer;
79  taskArgs[1] = rosFile;
80 
81  pros::Task reader(vexRosBufferInput, taskArgs);
82  }
83 
84  // read a byte from the serial port. -1 = failure
85  int read() {
86  char c;
87  // pull serial reading out of the buffer.
88  if(inputBuffer.pull(&c)) {
89  char sucmsg[16];
90  return c;
91  }
92 
93  return -1;
94  }
95 
96  // write data to the connection to ROS
97  void write(uint8_t* data, int length) {
98  for(int i = 0; i < length; i++) {
99  vexroswritechar(data[i]);
100  }
101  }
102  // returns milliseconds since start of program
103  unsigned long time() {
104  return pros::c::millis();
105  }
106  private:
109  pros::Mutex rosvexMutex;
110  __FILE * rosFile;
112 
113  // writing helper.
114  void vexroswritechar(uint8_t data) {
115  fputc(data, rosFile);
116  fflush(rosFile);
117  }
118 
119  // reading helper.
120  char vexrosreadchar() {
121  return fgetc(rosFile);
122  }
123  };
124 
125 #endif
void init()
Definition: V5Hardware.h:72
void write(uint8_t *data, int length)
Definition: V5Hardware.h:97
#define NULL
pros::Mutex rosvexMutex
Definition: V5Hardware.h:109
void vexRosBufferInput(void *arg)
Definition: V5Hardware.h:50
int read()
Definition: V5Hardware.h:85
char vexrosreadchar()
Definition: V5Hardware.h:120
bool pull(Type *dest)
Definition: RingBuf.h:74
__FILE * rosFile
Definition: V5Hardware.h:110
int failCount
Definition: V5Hardware.h:107
int successCount
Definition: V5Hardware.h:108
RB inputBuffer
Definition: V5Hardware.h:111
bool add(Type &obj)
Definition: RingBuf.h:51
unsigned long time()
Definition: V5Hardware.h:103
void vexroswritechar(uint8_t data)
Definition: V5Hardware.h:114


rosserial_vex_v5
Author(s): Cannon
autogenerated on Fri Jun 7 2019 22:03:08