Initial Release for PMT9123 for nRF51 on mbed

Dependencies:   Pixart_OTS_PMT9123 mbed

Fork of PMT9123_OTS_nRF51 by PixArt Imaging

Committer:
pixus_mbed
Date:
Mon May 08 22:55:31 2017 +0000
Revision:
1:db74b79ffb5e
Parent:
0:f5c80a9777f3
Initial Release for PMT9123 for nRF51 on mbed

Who changed what in which revision?

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