Joystick Controlled Servos

Dependencies:   Servo Map

Committer:
Galib_Rahman
Date:
Thu Dec 13 02:26:26 2018 +0000
Revision:
1:6dc9dbf60e67
Parent:
0:7e748374bdff
Modified Analog.read()

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Galib_Rahman 0:7e748374bdff 1 /* mbed Microcontroller Library
Galib_Rahman 0:7e748374bdff 2 * Copyright (c) 2018 ARM Limited
Galib_Rahman 0:7e748374bdff 3 * SPDX-License-Identifier: Apache-2.0
Galib_Rahman 0:7e748374bdff 4 */
Galib_Rahman 0:7e748374bdff 5
Galib_Rahman 0:7e748374bdff 6 #ifndef STATS_REPORT_H
Galib_Rahman 0:7e748374bdff 7 #define STATS_REPORT
Galib_Rahman 0:7e748374bdff 8
Galib_Rahman 0:7e748374bdff 9 #include "mbed.h"
Galib_Rahman 0:7e748374bdff 10
Galib_Rahman 0:7e748374bdff 11 /**
Galib_Rahman 0:7e748374bdff 12 * System Reporting library. Provides runtime information on device:
Galib_Rahman 0:7e748374bdff 13 * - CPU sleep, idle, and wake times
Galib_Rahman 0:7e748374bdff 14 * - Heap and stack usage
Galib_Rahman 0:7e748374bdff 15 * - Thread information
Galib_Rahman 0:7e748374bdff 16 * - Static system information
Galib_Rahman 0:7e748374bdff 17 */
Galib_Rahman 0:7e748374bdff 18 class SystemReport {
Galib_Rahman 0:7e748374bdff 19 mbed_stats_heap_t heap_stats;
Galib_Rahman 0:7e748374bdff 20 mbed_stats_cpu_t cpu_stats;
Galib_Rahman 0:7e748374bdff 21 mbed_stats_sys_t sys_stats;
Galib_Rahman 0:7e748374bdff 22
Galib_Rahman 0:7e748374bdff 23 mbed_stats_thread_t *thread_stats;
Galib_Rahman 0:7e748374bdff 24 uint8_t thread_count;
Galib_Rahman 0:7e748374bdff 25 uint8_t max_thread_count;
Galib_Rahman 0:7e748374bdff 26 uint32_t sample_time_ms;
Galib_Rahman 0:7e748374bdff 27
Galib_Rahman 0:7e748374bdff 28 public:
Galib_Rahman 0:7e748374bdff 29 /**
Galib_Rahman 0:7e748374bdff 30 * SystemReport - Sample rate in ms is required to handle the CPU percent awake logic
Galib_Rahman 0:7e748374bdff 31 */
Galib_Rahman 0:7e748374bdff 32 SystemReport(uint32_t sample_rate) : max_thread_count(8), sample_time_ms(sample_rate)
Galib_Rahman 0:7e748374bdff 33 {
Galib_Rahman 0:7e748374bdff 34 thread_stats = new mbed_stats_thread_t[max_thread_count];
Galib_Rahman 0:7e748374bdff 35
Galib_Rahman 0:7e748374bdff 36 // Collect the static system information
Galib_Rahman 0:7e748374bdff 37 mbed_stats_sys_get(&sys_stats);
Galib_Rahman 0:7e748374bdff 38
Galib_Rahman 0:7e748374bdff 39 printf("=============================== SYSTEM INFO ================================\r\n");
Galib_Rahman 0:7e748374bdff 40 printf("Mbed OS Version: %ld \r\n", sys_stats.os_version);
Galib_Rahman 0:7e748374bdff 41 printf("CPU ID: 0x%lx \r\n", sys_stats.cpu_id);
Galib_Rahman 0:7e748374bdff 42 printf("Compiler ID: %d \r\n", sys_stats.compiler_id);
Galib_Rahman 0:7e748374bdff 43 printf("Compiler Version: %ld \r\n", sys_stats.compiler_version);
Galib_Rahman 0:7e748374bdff 44 }
Galib_Rahman 0:7e748374bdff 45
Galib_Rahman 0:7e748374bdff 46 ~SystemReport(void)
Galib_Rahman 0:7e748374bdff 47 {
Galib_Rahman 0:7e748374bdff 48 free(thread_stats);
Galib_Rahman 0:7e748374bdff 49 }
Galib_Rahman 0:7e748374bdff 50
Galib_Rahman 0:7e748374bdff 51 /**
Galib_Rahman 0:7e748374bdff 52 * Report on each Mbed OS Platform stats API
Galib_Rahman 0:7e748374bdff 53 */
Galib_Rahman 0:7e748374bdff 54 void report_state(void)
Galib_Rahman 0:7e748374bdff 55 {
Galib_Rahman 0:7e748374bdff 56 report_cpu_stats();
Galib_Rahman 0:7e748374bdff 57 report_heap_stats();
Galib_Rahman 0:7e748374bdff 58 report_thread_stats();
Galib_Rahman 0:7e748374bdff 59
Galib_Rahman 0:7e748374bdff 60 // Clear next line to separate subsequent report logs
Galib_Rahman 0:7e748374bdff 61 printf("\r\n");
Galib_Rahman 0:7e748374bdff 62 }
Galib_Rahman 0:7e748374bdff 63
Galib_Rahman 0:7e748374bdff 64 /**
Galib_Rahman 0:7e748374bdff 65 * Report CPU idle and awake time in terms of percentage
Galib_Rahman 0:7e748374bdff 66 */
Galib_Rahman 0:7e748374bdff 67 void report_cpu_stats(void)
Galib_Rahman 0:7e748374bdff 68 {
Galib_Rahman 0:7e748374bdff 69 static uint64_t prev_idle_time = 0;
Galib_Rahman 0:7e748374bdff 70
Galib_Rahman 0:7e748374bdff 71 printf("================= CPU STATS =================\r\n");
Galib_Rahman 0:7e748374bdff 72
Galib_Rahman 0:7e748374bdff 73 // Collect and print cpu stats
Galib_Rahman 0:7e748374bdff 74 mbed_stats_cpu_get(&cpu_stats);
Galib_Rahman 0:7e748374bdff 75
Galib_Rahman 0:7e748374bdff 76 uint64_t diff = (cpu_stats.idle_time - prev_idle_time);
Galib_Rahman 0:7e748374bdff 77 uint8_t idle = (diff * 100) / (sample_time_ms * 1000); // usec;
Galib_Rahman 0:7e748374bdff 78 uint8_t usage = 100 - ((diff * 100) / (sample_time_ms * 1000)); // usec;;
Galib_Rahman 0:7e748374bdff 79 prev_idle_time = cpu_stats.idle_time;
Galib_Rahman 0:7e748374bdff 80
Galib_Rahman 0:7e748374bdff 81 printf("Idle: %d%% Usage: %d%% \r\n", idle, usage);
Galib_Rahman 0:7e748374bdff 82 }
Galib_Rahman 0:7e748374bdff 83
Galib_Rahman 0:7e748374bdff 84 /**
Galib_Rahman 0:7e748374bdff 85 * Report current heap stats. Current heap refers to the current amount of
Galib_Rahman 0:7e748374bdff 86 * allocated heap. Max heap refers to the highest amount of heap allocated
Galib_Rahman 0:7e748374bdff 87 * since reset.
Galib_Rahman 0:7e748374bdff 88 */
Galib_Rahman 0:7e748374bdff 89 void report_heap_stats(void)
Galib_Rahman 0:7e748374bdff 90 {
Galib_Rahman 0:7e748374bdff 91 printf("================ HEAP STATS =================\r\n");
Galib_Rahman 0:7e748374bdff 92
Galib_Rahman 0:7e748374bdff 93 // Collect and print heap stats
Galib_Rahman 0:7e748374bdff 94 mbed_stats_heap_get(&heap_stats);
Galib_Rahman 0:7e748374bdff 95
Galib_Rahman 0:7e748374bdff 96 printf("Current heap: %lu\r\n", heap_stats.current_size);
Galib_Rahman 0:7e748374bdff 97 printf("Max heap size: %lu\r\n", heap_stats.max_size);
Galib_Rahman 0:7e748374bdff 98 }
Galib_Rahman 0:7e748374bdff 99
Galib_Rahman 0:7e748374bdff 100 /**
Galib_Rahman 0:7e748374bdff 101 * Report active thread stats
Galib_Rahman 0:7e748374bdff 102 */
Galib_Rahman 0:7e748374bdff 103 void report_thread_stats(void)
Galib_Rahman 0:7e748374bdff 104 {
Galib_Rahman 0:7e748374bdff 105 printf("================ THREAD STATS ===============\r\n");
Galib_Rahman 0:7e748374bdff 106
Galib_Rahman 0:7e748374bdff 107 // Collect and print running thread stats
Galib_Rahman 0:7e748374bdff 108 int count = mbed_stats_thread_get_each(thread_stats, max_thread_count);
Galib_Rahman 0:7e748374bdff 109
Galib_Rahman 0:7e748374bdff 110 for (int i = 0; i < count; i++) {
Galib_Rahman 0:7e748374bdff 111 printf("ID: 0x%lx \r\n", thread_stats[i].id);
Galib_Rahman 0:7e748374bdff 112 printf("Name: %s \r\n", thread_stats[i].name);
Galib_Rahman 0:7e748374bdff 113 printf("State: %ld \r\n", thread_stats[i].state);
Galib_Rahman 0:7e748374bdff 114 printf("Priority: %ld \r\n", thread_stats[i].priority);
Galib_Rahman 0:7e748374bdff 115 printf("Stack Size: %ld \r\n", thread_stats[i].stack_size);
Galib_Rahman 0:7e748374bdff 116 printf("Stack Space: %ld \r\n", thread_stats[i].stack_space);
Galib_Rahman 0:7e748374bdff 117 }
Galib_Rahman 0:7e748374bdff 118 }
Galib_Rahman 0:7e748374bdff 119 };
Galib_Rahman 0:7e748374bdff 120
Galib_Rahman 0:7e748374bdff 121 #endif // STATS_REPORT_H