PMT9123 OTS on Nucleo (Initial Release)
Dependencies: Pixart_9123_Nucleo_Library mbed
Fork of PMT9123_OTS_NUCLEO_Program by
main.cpp@1:66afdef90518, 2017-05-11 (annotated)
- Committer:
- pixus_mbed
- Date:
- Thu May 11 17:33:05 2017 +0000
- Revision:
- 1:66afdef90518
- Parent:
- 0:384ce9819ea6
PMT9123 OTS on Nucleo (Initial Release)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pixus_mbed | 0:384ce9819ea6 | 1 | /* mbed Microcontroller Library |
pixus_mbed | 0:384ce9819ea6 | 2 | * Copyright (c) 2006-2013 ARM Limited |
pixus_mbed | 0:384ce9819ea6 | 3 | * |
pixus_mbed | 0:384ce9819ea6 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
pixus_mbed | 0:384ce9819ea6 | 5 | * you may not use this file except in compliance with the License. |
pixus_mbed | 0:384ce9819ea6 | 6 | * You may obtain a copy of the License at |
pixus_mbed | 0:384ce9819ea6 | 7 | * |
pixus_mbed | 0:384ce9819ea6 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
pixus_mbed | 0:384ce9819ea6 | 9 | * |
pixus_mbed | 0:384ce9819ea6 | 10 | * Unless required by applicable law or agreed to in writing, software |
pixus_mbed | 0:384ce9819ea6 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
pixus_mbed | 0:384ce9819ea6 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
pixus_mbed | 0:384ce9819ea6 | 13 | * See the License for the specific language governing permissions and |
pixus_mbed | 0:384ce9819ea6 | 14 | * limitations under the License. |
pixus_mbed | 0:384ce9819ea6 | 15 | */ |
pixus_mbed | 0:384ce9819ea6 | 16 | |
pixus_mbed | 0:384ce9819ea6 | 17 | #include "mbed.h" |
pixus_mbed | 0:384ce9819ea6 | 18 | #include "OTC.h" |
pixus_mbed | 0:384ce9819ea6 | 19 | #include <string> |
pixus_mbed | 0:384ce9819ea6 | 20 | #include <ctype.h> |
pixus_mbed | 0:384ce9819ea6 | 21 | using namespace std; |
pixus_mbed | 0:384ce9819ea6 | 22 | |
pixus_mbed | 0:384ce9819ea6 | 23 | // Function prototypes |
pixus_mbed | 0:384ce9819ea6 | 24 | void serialEvent(); |
pixus_mbed | 0:384ce9819ea6 | 25 | void serialProcess(string input); |
pixus_mbed | 0:384ce9819ea6 | 26 | bool IsParamValid(char *param, bool hex = true); |
pixus_mbed | 0:384ce9819ea6 | 27 | bool IsDigit(char *p, bool hex); |
pixus_mbed | 0:384ce9819ea6 | 28 | void Hex8(uint8_t data); |
pixus_mbed | 0:384ce9819ea6 | 29 | void PrintHex8(uint8_t data); |
pixus_mbed | 0:384ce9819ea6 | 30 | |
pixus_mbed | 0:384ce9819ea6 | 31 | Serial pc(USBTX,USBRX);//tx, rx |
pixus_mbed | 0:384ce9819ea6 | 32 | I2C i2c(I2C_SDA, I2C_SCL); |
pixus_mbed | 0:384ce9819ea6 | 33 | |
pixus_mbed | 0:384ce9819ea6 | 34 | DigitalOut led1(LED1); |
pixus_mbed | 0:384ce9819ea6 | 35 | DigitalOut pwm(D9); |
pixus_mbed | 0:384ce9819ea6 | 36 | PwmOut mypwm(D9); |
pixus_mbed | 0:384ce9819ea6 | 37 | |
pixus_mbed | 0:384ce9819ea6 | 38 | // OTS object declaration |
pixus_mbed | 0:384ce9819ea6 | 39 | Pixart_OTS *m_OTS = NULL; |
pixus_mbed | 0:384ce9819ea6 | 40 | |
pixus_mbed | 0:384ce9819ea6 | 41 | // Serial string |
pixus_mbed | 0:384ce9819ea6 | 42 | string strRead = ""; |
pixus_mbed | 0:384ce9819ea6 | 43 | |
pixus_mbed | 0:384ce9819ea6 | 44 | // LED PWM setting |
pixus_mbed | 0:384ce9819ea6 | 45 | #define LED_PERIOD_US 3000 // 3ms |
pixus_mbed | 0:384ce9819ea6 | 46 | #define LED_MAX_DC 100 |
pixus_mbed | 0:384ce9819ea6 | 47 | // LED brightness control value in us |
pixus_mbed | 0:384ce9819ea6 | 48 | int16_t LED_on_time_us = 0; |
pixus_mbed | 0:384ce9819ea6 | 49 | int16_t Counts; |
pixus_mbed | 0:384ce9819ea6 | 50 | int16_t max_on_period; |
pixus_mbed | 0:384ce9819ea6 | 51 | |
pixus_mbed | 0:384ce9819ea6 | 52 | // LED control |
pixus_mbed | 0:384ce9819ea6 | 53 | void LED_control(int16_t value) |
pixus_mbed | 0:384ce9819ea6 | 54 | { |
pixus_mbed | 0:384ce9819ea6 | 55 | int16_t led_on_time; |
pixus_mbed | 0:384ce9819ea6 | 56 | |
pixus_mbed | 0:384ce9819ea6 | 57 | LED_on_time_us += value; |
pixus_mbed | 0:384ce9819ea6 | 58 | led_on_time = LED_on_time_us; |
pixus_mbed | 0:384ce9819ea6 | 59 | |
pixus_mbed | 0:384ce9819ea6 | 60 | if (LED_on_time_us < 5) // hysterisis so that the LED stay off |
pixus_mbed | 0:384ce9819ea6 | 61 | led_on_time = 0; |
pixus_mbed | 0:384ce9819ea6 | 62 | else if (LED_on_time_us > max_on_period) |
pixus_mbed | 0:384ce9819ea6 | 63 | led_on_time = LED_on_time_us = max_on_period; |
pixus_mbed | 0:384ce9819ea6 | 64 | |
pixus_mbed | 0:384ce9819ea6 | 65 | mypwm.pulsewidth_us(led_on_time); |
pixus_mbed | 0:384ce9819ea6 | 66 | } |
pixus_mbed | 0:384ce9819ea6 | 67 | |
pixus_mbed | 0:384ce9819ea6 | 68 | // Reset count and LED |
pixus_mbed | 0:384ce9819ea6 | 69 | void ClearCount() |
pixus_mbed | 0:384ce9819ea6 | 70 | { |
pixus_mbed | 0:384ce9819ea6 | 71 | Counts = 0; |
pixus_mbed | 0:384ce9819ea6 | 72 | pc.printf("Count = 0\n"); |
pixus_mbed | 0:384ce9819ea6 | 73 | LED_on_time_us = 0; |
pixus_mbed | 0:384ce9819ea6 | 74 | LED_control(0); |
pixus_mbed | 0:384ce9819ea6 | 75 | } |
pixus_mbed | 0:384ce9819ea6 | 76 | |
pixus_mbed | 0:384ce9819ea6 | 77 | void GetData(int16_t value) |
pixus_mbed | 0:384ce9819ea6 | 78 | { |
pixus_mbed | 0:384ce9819ea6 | 79 | Counts += value; |
pixus_mbed | 0:384ce9819ea6 | 80 | pc.printf("Count = %d\n", Counts); |
pixus_mbed | 0:384ce9819ea6 | 81 | |
pixus_mbed | 0:384ce9819ea6 | 82 | // Change brightness |
pixus_mbed | 0:384ce9819ea6 | 83 | LED_control(value); |
pixus_mbed | 0:384ce9819ea6 | 84 | } |
pixus_mbed | 0:384ce9819ea6 | 85 | |
pixus_mbed | 0:384ce9819ea6 | 86 | // main() runs in its own thread in the OS |
pixus_mbed | 0:384ce9819ea6 | 87 | // (note the calls to Thread::wait below for delays) |
pixus_mbed | 0:384ce9819ea6 | 88 | int main() { |
pixus_mbed | 0:384ce9819ea6 | 89 | |
pixus_mbed | 0:384ce9819ea6 | 90 | pc.format(8, Serial::None, 1); |
pixus_mbed | 0:384ce9819ea6 | 91 | pc.baud(115200); |
pixus_mbed | 0:384ce9819ea6 | 92 | pc.attach(&serialEvent); |
pixus_mbed | 0:384ce9819ea6 | 93 | i2c.frequency(400000); |
pixus_mbed | 0:384ce9819ea6 | 94 | |
pixus_mbed | 0:384ce9819ea6 | 95 | // LED control |
pixus_mbed | 0:384ce9819ea6 | 96 | max_on_period = LED_MAX_DC*LED_PERIOD_US/100; |
pixus_mbed | 0:384ce9819ea6 | 97 | mypwm.period_us(LED_PERIOD_US); |
pixus_mbed | 0:384ce9819ea6 | 98 | LED_on_time_us = 0; // start with 0, which is off |
pixus_mbed | 0:384ce9819ea6 | 99 | LED_control(0); |
pixus_mbed | 0:384ce9819ea6 | 100 | |
pixus_mbed | 0:384ce9819ea6 | 101 | Counts = 0; // restart count to zero |
pixus_mbed | 0:384ce9819ea6 | 102 | |
pixus_mbed | 0:384ce9819ea6 | 103 | bool Result = false; |
pixus_mbed | 0:384ce9819ea6 | 104 | // Set to polling at 4ms |
pixus_mbed | 0:384ce9819ea6 | 105 | m_OTS = new Pixart_OTS(&i2c,4,GetData,Result); |
pixus_mbed | 0:384ce9819ea6 | 106 | |
pixus_mbed | 0:384ce9819ea6 | 107 | if(Result == true) |
pixus_mbed | 0:384ce9819ea6 | 108 | { |
pixus_mbed | 0:384ce9819ea6 | 109 | pc.printf("Initial Pixart OTS successful\n\r"); |
pixus_mbed | 0:384ce9819ea6 | 110 | } |
pixus_mbed | 0:384ce9819ea6 | 111 | else |
pixus_mbed | 0:384ce9819ea6 | 112 | { |
pixus_mbed | 0:384ce9819ea6 | 113 | pc.printf("Initial Pixart OTS fail\n\r"); |
pixus_mbed | 0:384ce9819ea6 | 114 | } |
pixus_mbed | 0:384ce9819ea6 | 115 | |
pixus_mbed | 0:384ce9819ea6 | 116 | |
pixus_mbed | 0:384ce9819ea6 | 117 | while (true) |
pixus_mbed | 0:384ce9819ea6 | 118 | { |
pixus_mbed | 0:384ce9819ea6 | 119 | ; |
pixus_mbed | 0:384ce9819ea6 | 120 | } |
pixus_mbed | 0:384ce9819ea6 | 121 | } |
pixus_mbed | 0:384ce9819ea6 | 122 | |
pixus_mbed | 0:384ce9819ea6 | 123 | void serialEvent() { |
pixus_mbed | 0:384ce9819ea6 | 124 | char inChar; |
pixus_mbed | 0:384ce9819ea6 | 125 | |
pixus_mbed | 0:384ce9819ea6 | 126 | while (pc.readable()) |
pixus_mbed | 0:384ce9819ea6 | 127 | { |
pixus_mbed | 0:384ce9819ea6 | 128 | // get the new byte: |
pixus_mbed | 0:384ce9819ea6 | 129 | inChar = (char)pc.getc(); |
pixus_mbed | 0:384ce9819ea6 | 130 | //pc.putc(inChar); |
pixus_mbed | 0:384ce9819ea6 | 131 | // if the incoming character is a newline, set a flag |
pixus_mbed | 0:384ce9819ea6 | 132 | // so the main loop can do something about it: |
pixus_mbed | 0:384ce9819ea6 | 133 | if ((inChar == '\n') || (inChar == '\r')) |
pixus_mbed | 0:384ce9819ea6 | 134 | { |
pixus_mbed | 0:384ce9819ea6 | 135 | //strRead.trim(); |
pixus_mbed | 0:384ce9819ea6 | 136 | strRead += " "; // for tokenizing |
pixus_mbed | 0:384ce9819ea6 | 137 | serialProcess(strRead); |
pixus_mbed | 0:384ce9819ea6 | 138 | strRead = ""; |
pixus_mbed | 0:384ce9819ea6 | 139 | } |
pixus_mbed | 0:384ce9819ea6 | 140 | else if (inChar != 0xf0) |
pixus_mbed | 0:384ce9819ea6 | 141 | // add it to the strRead |
pixus_mbed | 0:384ce9819ea6 | 142 | strRead += inChar; |
pixus_mbed | 0:384ce9819ea6 | 143 | } |
pixus_mbed | 0:384ce9819ea6 | 144 | } |
pixus_mbed | 0:384ce9819ea6 | 145 | |
pixus_mbed | 0:384ce9819ea6 | 146 | // Macro define for easily reading |
pixus_mbed | 0:384ce9819ea6 | 147 | #define IsEqual(a,b) (!strcmp((a),(b))) |
pixus_mbed | 0:384ce9819ea6 | 148 | |
pixus_mbed | 0:384ce9819ea6 | 149 | void serialProcess(string input) |
pixus_mbed | 0:384ce9819ea6 | 150 | { |
pixus_mbed | 0:384ce9819ea6 | 151 | int val; |
pixus_mbed | 0:384ce9819ea6 | 152 | unsigned char Address,Value; |
pixus_mbed | 0:384ce9819ea6 | 153 | |
pixus_mbed | 0:384ce9819ea6 | 154 | //pc.printf("cmd = %s\n", input); |
pixus_mbed | 0:384ce9819ea6 | 155 | // ==================================================== |
pixus_mbed | 0:384ce9819ea6 | 156 | // Single command goes here |
pixus_mbed | 0:384ce9819ea6 | 157 | // NOTE: All command compare must have " " at the back |
pixus_mbed | 0:384ce9819ea6 | 158 | // ==================================================== |
pixus_mbed | 0:384ce9819ea6 | 159 | if (input == "c ") |
pixus_mbed | 0:384ce9819ea6 | 160 | { |
pixus_mbed | 0:384ce9819ea6 | 161 | ClearCount(); |
pixus_mbed | 0:384ce9819ea6 | 162 | } |
pixus_mbed | 0:384ce9819ea6 | 163 | // ==================================================== |
pixus_mbed | 0:384ce9819ea6 | 164 | // Command with parameters goes here, like r 00, w 48 00 |
pixus_mbed | 0:384ce9819ea6 | 165 | // ==================================================== |
pixus_mbed | 0:384ce9819ea6 | 166 | else if (input != " ") // not empty string, then proceed to decode the command |
pixus_mbed | 0:384ce9819ea6 | 167 | { |
pixus_mbed | 0:384ce9819ea6 | 168 | char buff[40]; |
pixus_mbed | 0:384ce9819ea6 | 169 | size_t l = input.copy(buff, input.length(), 0); |
pixus_mbed | 0:384ce9819ea6 | 170 | buff[l] = '\0'; |
pixus_mbed | 0:384ce9819ea6 | 171 | |
pixus_mbed | 0:384ce9819ea6 | 172 | // Get the command |
pixus_mbed | 0:384ce9819ea6 | 173 | char *tok = strtok(buff, " "); |
pixus_mbed | 0:384ce9819ea6 | 174 | if (!tok) |
pixus_mbed | 0:384ce9819ea6 | 175 | { |
pixus_mbed | 0:384ce9819ea6 | 176 | pc.printf("empty command\n"); |
pixus_mbed | 0:384ce9819ea6 | 177 | return; |
pixus_mbed | 0:384ce9819ea6 | 178 | } |
pixus_mbed | 0:384ce9819ea6 | 179 | |
pixus_mbed | 0:384ce9819ea6 | 180 | // Get parameters, up to 4 parameters, if more than that, individual command will need to extract it on their own |
pixus_mbed | 0:384ce9819ea6 | 181 | char *param[4]; |
pixus_mbed | 0:384ce9819ea6 | 182 | for (val = 0; val < 4; val++) |
pixus_mbed | 0:384ce9819ea6 | 183 | { |
pixus_mbed | 0:384ce9819ea6 | 184 | param[val] = strtok(NULL, " "); |
pixus_mbed | 0:384ce9819ea6 | 185 | if (!param[val]) // break if we reach the end |
pixus_mbed | 0:384ce9819ea6 | 186 | break; |
pixus_mbed | 0:384ce9819ea6 | 187 | } |
pixus_mbed | 0:384ce9819ea6 | 188 | |
pixus_mbed | 0:384ce9819ea6 | 189 | // convert the first parameter into decimal first, as it is most commonly used |
pixus_mbed | 0:384ce9819ea6 | 190 | val = strtol(param[0], NULL, 10); |
pixus_mbed | 0:384ce9819ea6 | 191 | |
pixus_mbed | 0:384ce9819ea6 | 192 | // switch for command |
pixus_mbed | 0:384ce9819ea6 | 193 | if ((IsEqual(tok,"r")) || (IsEqual(tok,"w"))) |
pixus_mbed | 0:384ce9819ea6 | 194 | { |
pixus_mbed | 0:384ce9819ea6 | 195 | // Address is the first byte |
pixus_mbed | 0:384ce9819ea6 | 196 | if (!IsParamValid(param[0])) |
pixus_mbed | 0:384ce9819ea6 | 197 | { |
pixus_mbed | 0:384ce9819ea6 | 198 | return; |
pixus_mbed | 0:384ce9819ea6 | 199 | } |
pixus_mbed | 0:384ce9819ea6 | 200 | // convert to integer |
pixus_mbed | 0:384ce9819ea6 | 201 | Address = strtol(param[0], NULL, 16); |
pixus_mbed | 0:384ce9819ea6 | 202 | if (Address > 0x7F) |
pixus_mbed | 0:384ce9819ea6 | 203 | { |
pixus_mbed | 0:384ce9819ea6 | 204 | pc.printf("Error (r/w): address > 0x7F\n"); |
pixus_mbed | 0:384ce9819ea6 | 205 | return; |
pixus_mbed | 0:384ce9819ea6 | 206 | } |
pixus_mbed | 0:384ce9819ea6 | 207 | |
pixus_mbed | 0:384ce9819ea6 | 208 | if ((IsEqual(tok,"w"))) |
pixus_mbed | 0:384ce9819ea6 | 209 | { |
pixus_mbed | 0:384ce9819ea6 | 210 | // Value is second byte |
pixus_mbed | 0:384ce9819ea6 | 211 | if (!IsParamValid(param[1])) |
pixus_mbed | 0:384ce9819ea6 | 212 | { |
pixus_mbed | 0:384ce9819ea6 | 213 | return; |
pixus_mbed | 0:384ce9819ea6 | 214 | } |
pixus_mbed | 0:384ce9819ea6 | 215 | // convert to integer |
pixus_mbed | 0:384ce9819ea6 | 216 | Value = strtol(param[1], NULL, 16); |
pixus_mbed | 0:384ce9819ea6 | 217 | if (Value > 0xFF) |
pixus_mbed | 0:384ce9819ea6 | 218 | { |
pixus_mbed | 0:384ce9819ea6 | 219 | pc.printf("Error (w): value > 0xFF\n"); |
pixus_mbed | 0:384ce9819ea6 | 220 | return; |
pixus_mbed | 0:384ce9819ea6 | 221 | } |
pixus_mbed | 0:384ce9819ea6 | 222 | |
pixus_mbed | 0:384ce9819ea6 | 223 | val = m_OTS->Register_Write(Address, Value); |
pixus_mbed | 0:384ce9819ea6 | 224 | } |
pixus_mbed | 0:384ce9819ea6 | 225 | else |
pixus_mbed | 0:384ce9819ea6 | 226 | { |
pixus_mbed | 0:384ce9819ea6 | 227 | val = m_OTS->Register_Read(Address); |
pixus_mbed | 0:384ce9819ea6 | 228 | } |
pixus_mbed | 0:384ce9819ea6 | 229 | |
pixus_mbed | 0:384ce9819ea6 | 230 | pc.printf("ADDR %02X %02X\n", Address, val); |
pixus_mbed | 0:384ce9819ea6 | 231 | } |
pixus_mbed | 0:384ce9819ea6 | 232 | else |
pixus_mbed | 0:384ce9819ea6 | 233 | { |
pixus_mbed | 0:384ce9819ea6 | 234 | pc.printf("Bad command : "); |
pixus_mbed | 0:384ce9819ea6 | 235 | pc.printf("%s\n", tok); |
pixus_mbed | 0:384ce9819ea6 | 236 | } |
pixus_mbed | 0:384ce9819ea6 | 237 | } |
pixus_mbed | 0:384ce9819ea6 | 238 | } |
pixus_mbed | 0:384ce9819ea6 | 239 | |
pixus_mbed | 0:384ce9819ea6 | 240 | // Check if param is not given or not hexa, print the error as well |
pixus_mbed | 0:384ce9819ea6 | 241 | bool IsParamValid(char *param, bool hex) |
pixus_mbed | 0:384ce9819ea6 | 242 | { |
pixus_mbed | 0:384ce9819ea6 | 243 | if (!param) |
pixus_mbed | 0:384ce9819ea6 | 244 | { |
pixus_mbed | 0:384ce9819ea6 | 245 | return false; |
pixus_mbed | 0:384ce9819ea6 | 246 | } |
pixus_mbed | 0:384ce9819ea6 | 247 | if (!IsDigit(param, hex)) |
pixus_mbed | 0:384ce9819ea6 | 248 | { |
pixus_mbed | 0:384ce9819ea6 | 249 | pc.printf("Error : arg invalid\n"); |
pixus_mbed | 0:384ce9819ea6 | 250 | return false; |
pixus_mbed | 0:384ce9819ea6 | 251 | } |
pixus_mbed | 0:384ce9819ea6 | 252 | return true; |
pixus_mbed | 0:384ce9819ea6 | 253 | } |
pixus_mbed | 0:384ce9819ea6 | 254 | |
pixus_mbed | 0:384ce9819ea6 | 255 | // Check whether is hexa for given char |
pixus_mbed | 0:384ce9819ea6 | 256 | bool IsDigit(char *p, bool hex) |
pixus_mbed | 0:384ce9819ea6 | 257 | { |
pixus_mbed | 0:384ce9819ea6 | 258 | for (int i; i < strlen(p); i++) |
pixus_mbed | 0:384ce9819ea6 | 259 | { |
pixus_mbed | 0:384ce9819ea6 | 260 | if (hex) |
pixus_mbed | 0:384ce9819ea6 | 261 | { |
pixus_mbed | 0:384ce9819ea6 | 262 | if (!isxdigit(p[i])) |
pixus_mbed | 0:384ce9819ea6 | 263 | return false; |
pixus_mbed | 0:384ce9819ea6 | 264 | } |
pixus_mbed | 0:384ce9819ea6 | 265 | else if (!isdigit(p[i])) |
pixus_mbed | 0:384ce9819ea6 | 266 | return false; |
pixus_mbed | 0:384ce9819ea6 | 267 | } |
pixus_mbed | 0:384ce9819ea6 | 268 | return true; |
pixus_mbed | 0:384ce9819ea6 | 269 | } |
pixus_mbed | 0:384ce9819ea6 | 270 |