Mouse code for the MacroRat

Dependencies:   ITG3200 QEI

Committer:
sahilmgandhi
Date:
Sat Jun 03 00:22:44 2017 +0000
Revision:
46:b156ef445742
Parent:
18:6a4db94011d3
Final code for internal battlebot competition.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sahilmgandhi 18:6a4db94011d3 1 /* mbed Microcontroller Library
sahilmgandhi 18:6a4db94011d3 2 * Copyright (c) 2006-2013 ARM Limited
sahilmgandhi 18:6a4db94011d3 3 *
sahilmgandhi 18:6a4db94011d3 4 * Licensed under the Apache License, Version 2.0 (the "License");
sahilmgandhi 18:6a4db94011d3 5 * you may not use this file except in compliance with the License.
sahilmgandhi 18:6a4db94011d3 6 * You may obtain a copy of the License at
sahilmgandhi 18:6a4db94011d3 7 *
sahilmgandhi 18:6a4db94011d3 8 * http://www.apache.org/licenses/LICENSE-2.0
sahilmgandhi 18:6a4db94011d3 9 *
sahilmgandhi 18:6a4db94011d3 10 * Unless required by applicable law or agreed to in writing, software
sahilmgandhi 18:6a4db94011d3 11 * distributed under the License is distributed on an "AS IS" BASIS,
sahilmgandhi 18:6a4db94011d3 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
sahilmgandhi 18:6a4db94011d3 13 * See the License for the specific language governing permissions and
sahilmgandhi 18:6a4db94011d3 14 * limitations under the License.
sahilmgandhi 18:6a4db94011d3 15 */
sahilmgandhi 18:6a4db94011d3 16 #include <stdio.h>
sahilmgandhi 18:6a4db94011d3 17 #include "hal/gpio_api.h"
sahilmgandhi 18:6a4db94011d3 18 #include "platform/mbed_wait_api.h"
sahilmgandhi 18:6a4db94011d3 19 #include "platform/mbed_toolchain.h"
sahilmgandhi 18:6a4db94011d3 20 #include "platform/mbed_interface.h"
sahilmgandhi 18:6a4db94011d3 21 #include "platform/mbed_critical.h"
sahilmgandhi 18:6a4db94011d3 22 #include "hal/serial_api.h"
sahilmgandhi 18:6a4db94011d3 23
sahilmgandhi 18:6a4db94011d3 24 #if DEVICE_SERIAL
sahilmgandhi 18:6a4db94011d3 25 extern int stdio_uart_inited;
sahilmgandhi 18:6a4db94011d3 26 extern serial_t stdio_uart;
sahilmgandhi 18:6a4db94011d3 27 #endif
sahilmgandhi 18:6a4db94011d3 28
sahilmgandhi 18:6a4db94011d3 29 WEAK void mbed_die(void) {
sahilmgandhi 18:6a4db94011d3 30 #if !defined (NRF51_H) && !defined(TARGET_EFM32)
sahilmgandhi 18:6a4db94011d3 31 core_util_critical_section_enter();
sahilmgandhi 18:6a4db94011d3 32 #endif
sahilmgandhi 18:6a4db94011d3 33 #if (DEVICE_ERROR_RED == 1)
sahilmgandhi 18:6a4db94011d3 34 gpio_t led_red; gpio_init_out(&led_red, LED_RED);
sahilmgandhi 18:6a4db94011d3 35 #elif (DEVICE_ERROR_PATTERN == 1)
sahilmgandhi 18:6a4db94011d3 36 gpio_t led_1; gpio_init_out(&led_1, LED1);
sahilmgandhi 18:6a4db94011d3 37 gpio_t led_2; gpio_init_out(&led_2, LED2);
sahilmgandhi 18:6a4db94011d3 38 gpio_t led_3; gpio_init_out(&led_3, LED3);
sahilmgandhi 18:6a4db94011d3 39 gpio_t led_4; gpio_init_out(&led_4, LED4);
sahilmgandhi 18:6a4db94011d3 40 #endif
sahilmgandhi 18:6a4db94011d3 41
sahilmgandhi 18:6a4db94011d3 42 while (1) {
sahilmgandhi 18:6a4db94011d3 43 #if (DEVICE_ERROR_RED == 1)
sahilmgandhi 18:6a4db94011d3 44 gpio_write(&led_red, 1);
sahilmgandhi 18:6a4db94011d3 45
sahilmgandhi 18:6a4db94011d3 46 #elif (DEVICE_ERROR_PATTERN == 1)
sahilmgandhi 18:6a4db94011d3 47 gpio_write(&led_1, 1);
sahilmgandhi 18:6a4db94011d3 48 gpio_write(&led_2, 0);
sahilmgandhi 18:6a4db94011d3 49 gpio_write(&led_3, 0);
sahilmgandhi 18:6a4db94011d3 50 gpio_write(&led_4, 1);
sahilmgandhi 18:6a4db94011d3 51 #endif
sahilmgandhi 18:6a4db94011d3 52
sahilmgandhi 18:6a4db94011d3 53 wait_ms(150);
sahilmgandhi 18:6a4db94011d3 54
sahilmgandhi 18:6a4db94011d3 55 #if (DEVICE_ERROR_RED == 1)
sahilmgandhi 18:6a4db94011d3 56 gpio_write(&led_red, 0);
sahilmgandhi 18:6a4db94011d3 57
sahilmgandhi 18:6a4db94011d3 58 #elif (DEVICE_ERROR_PATTERN == 1)
sahilmgandhi 18:6a4db94011d3 59 gpio_write(&led_1, 0);
sahilmgandhi 18:6a4db94011d3 60 gpio_write(&led_2, 1);
sahilmgandhi 18:6a4db94011d3 61 gpio_write(&led_3, 1);
sahilmgandhi 18:6a4db94011d3 62 gpio_write(&led_4, 0);
sahilmgandhi 18:6a4db94011d3 63 #endif
sahilmgandhi 18:6a4db94011d3 64
sahilmgandhi 18:6a4db94011d3 65 wait_ms(150);
sahilmgandhi 18:6a4db94011d3 66 }
sahilmgandhi 18:6a4db94011d3 67 }
sahilmgandhi 18:6a4db94011d3 68
sahilmgandhi 18:6a4db94011d3 69 void mbed_error_printf(const char* format, ...) {
sahilmgandhi 18:6a4db94011d3 70 va_list arg;
sahilmgandhi 18:6a4db94011d3 71 va_start(arg, format);
sahilmgandhi 18:6a4db94011d3 72 mbed_error_vfprintf(format, arg);
sahilmgandhi 18:6a4db94011d3 73 va_end(arg);
sahilmgandhi 18:6a4db94011d3 74 }
sahilmgandhi 18:6a4db94011d3 75
sahilmgandhi 18:6a4db94011d3 76 void mbed_error_vfprintf(const char * format, va_list arg) {
sahilmgandhi 18:6a4db94011d3 77 #if DEVICE_SERIAL
sahilmgandhi 18:6a4db94011d3 78 #define ERROR_BUF_SIZE (128)
sahilmgandhi 18:6a4db94011d3 79 core_util_critical_section_enter();
sahilmgandhi 18:6a4db94011d3 80 char buffer[ERROR_BUF_SIZE];
sahilmgandhi 18:6a4db94011d3 81 int size = vsnprintf(buffer, ERROR_BUF_SIZE, format, arg);
sahilmgandhi 18:6a4db94011d3 82 if (size > 0) {
sahilmgandhi 18:6a4db94011d3 83 if (!stdio_uart_inited) {
sahilmgandhi 18:6a4db94011d3 84 serial_init(&stdio_uart, STDIO_UART_TX, STDIO_UART_RX);
sahilmgandhi 18:6a4db94011d3 85 }
sahilmgandhi 18:6a4db94011d3 86 #if MBED_CONF_PLATFORM_STDIO_CONVERT_NEWLINES
sahilmgandhi 18:6a4db94011d3 87 char stdio_out_prev = '\0';
sahilmgandhi 18:6a4db94011d3 88 for (int i = 0; i < size; i++) {
sahilmgandhi 18:6a4db94011d3 89 if (buffer[i] == '\n' && stdio_out_prev != '\r') {
sahilmgandhi 18:6a4db94011d3 90 serial_putc(&stdio_uart, '\r');
sahilmgandhi 18:6a4db94011d3 91 }
sahilmgandhi 18:6a4db94011d3 92 serial_putc(&stdio_uart, buffer[i]);
sahilmgandhi 18:6a4db94011d3 93 stdio_out_prev = buffer[i];
sahilmgandhi 18:6a4db94011d3 94 }
sahilmgandhi 18:6a4db94011d3 95 #else
sahilmgandhi 18:6a4db94011d3 96 for (int i = 0; i < size; i++) {
sahilmgandhi 18:6a4db94011d3 97 serial_putc(&stdio_uart, buffer[i]);
sahilmgandhi 18:6a4db94011d3 98 }
sahilmgandhi 18:6a4db94011d3 99 #endif
sahilmgandhi 18:6a4db94011d3 100 }
sahilmgandhi 18:6a4db94011d3 101 core_util_critical_section_exit();
sahilmgandhi 18:6a4db94011d3 102 #endif
sahilmgandhi 18:6a4db94011d3 103 }