car_hw: fix usb stack array size, translator: add serialization test

This commit is contained in:
e5l
2016-08-17 13:06:05 +03:00
parent 44089473ed
commit c083aa92e5
7 changed files with 190 additions and 23 deletions
+7
View File
@@ -0,0 +1,7 @@
fun blinkProgramm() {
while (true) {
blink()
wait(PROGRAM_DURATION)
}
}
+19 -15
View File
@@ -1,21 +1,24 @@
fun echoUsb() { fun echoUsb() {
led_on()
clear_buffer() clear_buffer()
while (true) { while (true) {
val command = receive_int() val command = receive_int()
send_int(command) send_int(command)
blink()
wait(PROGRAM_DURATION)
} }
} }
fun echoProto() { fun echoProto() {
led_on()
clear_buffer() clear_buffer()
while (true) { while (true) {
val route = readRoute() val route = readRoute()
go(route) go(route)
blink()
wait(PROGRAM_DURATION)
} }
} }
@@ -28,11 +31,23 @@ fun readRoute(): RouteRequest {
return result return result
} }
fun writeRoute(route: RouteRequest) {
val size = route.getSizeNoTag()
val buffer = ByteArray(size)
val stream = CodedOutputStream(buffer)
route.writeTo(stream)
sendByteArray(buffer)
}
fun go(request: RouteRequest) { fun go(request: RouteRequest) {
val times = request.distances val times = request.distances
var j = 0 var j = 0
while (j < times.size) { while (j < times.size) {
val time = times[j] val time = times[j]
engine_forward() engine_forward()
wait(time) wait(time)
engine_stop() engine_stop()
@@ -40,14 +55,3 @@ fun go(request: RouteRequest) {
} }
} }
//fun writeProto() {
// val msg = DirectionResponse.BuilderDirectionResponse(4242).build()
// val size = msg.getSizeNoTag()
// val buffer = ByteArray(size)
// val outputStream = CodedOutputStream(buffer)
//
// msg.writeTo(outputStream)
// sendByteArray(buffer)
// send_int(0xDDEEFF)
//}
+1 -1
View File
@@ -4,5 +4,5 @@ val PROGRAM_DURATION: Int = 1000
fun main() { fun main() {
init() init()
echoProto() echoUsb()
} }
+13 -1
View File
@@ -1,7 +1,19 @@
fun simpleRoute() { fun simpleRoute() {
while (true) { while (true) {
engine_forward()
wait(PROGRAM_DURATION)
blink()
engine_turn_right()
wait(PROGRAM_DURATION)
blink()
engine_backward()
wait(PROGRAM_DURATION)
blink()
engine_turn_left()
wait(PROGRAM_DURATION) wait(PROGRAM_DURATION)
blink() blink()
} }
+1 -1
View File
@@ -28,7 +28,7 @@
*/ */
#define USBD_CFG_MAX_NUM 1 #define USBD_CFG_MAX_NUM 1
#define USBD_ITF_MAX_NUM 1 #define USBD_ITF_MAX_NUM 1
#define USB_MAX_STR_DESC_SIZ 50 #define USB_MAX_STR_DESC_SIZ 70
/** @defgroup USB_VCP_Class_Layer_Parameter /** @defgroup USB_VCP_Class_Layer_Parameter
* @{ * @{
@@ -1 +1,2 @@
serialization_test1_Int(1442) == 1442
serialization_test2_Int(5) serialization_test2_Int(5)
@@ -1,4 +1,140 @@
class DirectionResponse private constructor(var code: Int) {
//========== Properties ===========
//int32 code = 1
var errorCode: Int = 0
//========== Serialization methods ===========
fun writeTo(output: CodedOutputStream) {
//int32 code = 1
if (code != 0) {
output.writeInt32(1, code)
}
}
fun mergeWith(other: DirectionResponse) {
code = other.code
this.errorCode = other.errorCode
}
fun mergeFromWithSize(input: CodedInputStream, expectedSize: Int) {
val builder = DirectionResponse.BuilderDirectionResponse(0)
mergeWith(builder.parseFromWithSize(input, expectedSize).build())
}
fun mergeFrom(input: CodedInputStream) {
val builder = DirectionResponse.BuilderDirectionResponse(0)
mergeWith(builder.parseFrom(input).build())
}
//========== Size-related methods ===========
fun getSize(fieldNumber: Int): Int {
var size = 0
if (code != 0) {
size += WireFormat.getInt32Size(1, code)
}
size += WireFormat.getVarint32Size(size) + WireFormat.getTagSize(fieldNumber, WireType.LENGTH_DELIMITED)
return size
}
fun getSizeNoTag(): Int {
var size = 0
if (code != 0) {
size += WireFormat.getInt32Size(1, code)
}
return size
}
//========== Builder ===========
class BuilderDirectionResponse constructor(var code: Int) {
//========== Properties ===========
//int32 code = 1
fun setCode(value: Int): DirectionResponse.BuilderDirectionResponse {
code = value
return this
}
var errorCode: Int = 0
//========== Serialization methods ===========
fun writeTo(output: CodedOutputStream) {
//int32 code = 1
if (code != 0) {
output.writeInt32(1, code)
}
}
//========== Mutating methods ===========
fun build(): DirectionResponse {
val res = DirectionResponse(code)
res.errorCode = errorCode
return res
}
fun parseFieldFrom(input: CodedInputStream): Boolean {
if (input.isAtEnd()) {
return false
}
val tag = input.readInt32NoTag()
if (tag == 0) {
return false
}
val fieldNumber = WireFormat.getTagFieldNumber(tag)
val wireType = WireFormat.getTagWireType(tag)
when (fieldNumber) {
1 -> {
if (wireType.id != WireType.VARINT.id) {
errorCode = 1
return false
}
code = input.readInt32NoTag()
}
else -> errorCode = 4
}
return true
}
fun parseFromWithSize(input: CodedInputStream, expectedSize: Int): DirectionResponse.BuilderDirectionResponse {
while (getSizeNoTag() < expectedSize) {
parseFieldFrom(input)
}
if (getSizeNoTag() > expectedSize) {
errorCode = 2
}
return this
}
fun parseFrom(input: CodedInputStream): DirectionResponse.BuilderDirectionResponse {
while (parseFieldFrom(input)) {
}
return this
}
//========== Size-related methods ===========
fun getSize(fieldNumber: Int): Int {
var size = 0
if (code != 0) {
size += WireFormat.getInt32Size(1, code)
}
size += WireFormat.getVarint32Size(size) + WireFormat.getTagSize(fieldNumber, WireType.LENGTH_DELIMITED)
return size
}
fun getSizeNoTag(): Int {
var size = 0
if (code != 0) {
size += WireFormat.getInt32Size(1, code)
}
return size
}
}
}
class RouteRequest private constructor(var distances: IntArray, var angles: IntArray) { class RouteRequest private constructor(var distances: IntArray, var angles: IntArray) {
//========== Properties =========== //========== Properties ===========
//repeated int32 distances = 1 //repeated int32 distances = 1
@@ -261,10 +397,7 @@ class RouteRequest private constructor(var distances: IntArray, var angles: IntA
while (readSize < expectedByteSize) { while (readSize < expectedByteSize) {
var tmp = IntArray(1) var tmp = IntArray(1)
tmp[0] = input.readInt32NoTag() tmp[0] = input.readInt32NoTag()
println(tmp)
println(newArray)
newArray = newArray.plus(tmp) newArray = newArray.plus(tmp)
println(newArray)
readSize += WireFormat.getInt32SizeNoTag(tmp[0]) readSize += WireFormat.getInt32SizeNoTag(tmp[0])
} }
distances = newArray distances = newArray
@@ -378,6 +511,18 @@ class RouteRequest private constructor(var distances: IntArray, var angles: IntA
} }
fun serialization_test1(i: Int): Int {
val msg = DirectionResponse.BuilderDirectionResponse(i).build()
val buffer = ByteArray(msg.getSizeNoTag())
val output = CodedOutputStream(buffer)
msg.writeTo(output)
val input = CodedInputStream(buffer)
val msg2 = DirectionResponse.BuilderDirectionResponse(1).parseFrom(input)
return msg2.code
}
fun serialization_test2(size: Int) { fun serialization_test2(size: Int) {
val arr = IntArray(size) val arr = IntArray(size)
@@ -405,8 +550,6 @@ fun serialization_test2(size: Int) {
i = 0 i = 0
while (i < size) { while (i < size) {
println(message.distances[i])
println(receivedMessage.distances[i])
assert(message.distances[i] == receivedMessage.distances[i]) assert(message.distances[i] == receivedMessage.distances[i])
i++ i++
} }