@@ -313,8 +313,21 @@ typedef struct {
313
313
#define ACCEL_POLL_INTERVAL_MAX 5000 // in msec - DEFAULT_ACCEL_POLL_INTERVAL_MAX+TIMER_MAX must be <65535
314
314
#define BTN_LOAD_TIMEOUT 1500 // in msec - how long does the button have to be pressed for before we restart
315
315
#define TIMER_MAX 60000 // 60 sec - enough to fit in uint16_t without overflow if we add ACCEL_POLL_INTERVAL
316
+
317
+ #ifdef SMAQ3
318
+ JshI2CInfo i2cAccel ;
319
+ JshI2CInfo i2cMag ;
320
+ // TODO: pressure, hrm...
321
+ #define ACCEL_I2C &i2cAccel
322
+ #define MAG_I2C &i2cMag
323
+ #else
316
324
/// Internal I2C used for Accelerometer/Pressure
317
325
JshI2CInfo i2cInternal ;
326
+ #define ACCEL_I2C &i2cInternal
327
+ #define MAG_I2C &i2cInternal
328
+ #endif
329
+
330
+
318
331
/// Is I2C busy? if so we'll skip one reading in our interrupt so we don't overlap
319
332
bool i2cBusy ;
320
333
/// How often should be poll for accelerometer/compass data?
@@ -537,8 +550,8 @@ void peripheralPollHandler() {
537
550
// check the magnetometer if we had it on
538
551
if (compassPowerOn ) {
539
552
buf [0 ]= 0x10 ;
540
- jsi2cWrite (& i2cInternal , MAG_ADDR , 1 , buf , true);
541
- jsi2cRead (& i2cInternal , MAG_ADDR , 7 , buf , true);
553
+ jsi2cWrite (MAG_I2C , MAG_ADDR , 1 , buf , true);
554
+ jsi2cRead (MAG_I2C , MAG_ADDR , 7 , buf , true);
542
555
if (buf [0 ]& 1 ) { // then we have data (hopefully? No datasheet)
543
556
mag .y = buf [1 ] | (buf [2 ]<<8 );
544
557
mag .x = buf [3 ] | (buf [4 ]<<8 );
@@ -555,8 +568,8 @@ void peripheralPollHandler() {
555
568
// poll KX023 accelerometer (no other way as IRQ line seems disconnected!)
556
569
// read interrupt source data
557
570
buf [0 ]= 0x12 ; // INS1
558
- jsi2cWrite (& i2cInternal , ACCEL_ADDR , 1 , buf , true);
559
- jsi2cRead (& i2cInternal , ACCEL_ADDR , 2 , buf , true);
571
+ jsi2cWrite (ACCEL_I2C , ACCEL_ADDR , 1 , buf , true);
572
+ jsi2cRead (ACCEL_I2C , ACCEL_ADDR , 2 , buf , true);
560
573
// 0 -> 0x12 INS1 - tap event
561
574
// 1 -> 0x13 INS2 - what kind of event
562
575
bool hasAccelData = (buf [1 ]& 16 )!= 0 ; // DRDY
@@ -567,13 +580,13 @@ void peripheralPollHandler() {
567
580
bangleTasks |= JSBT_ACCEL_TAPPED ;
568
581
// clear the IRQ flags
569
582
buf [0 ]= 0x17 ;
570
- jsi2cWrite (& i2cInternal , ACCEL_ADDR , 1 , buf , true);
571
- jsi2cRead (& i2cInternal , ACCEL_ADDR , 1 , buf , true);
583
+ jsi2cWrite (ACCEL_I2C , ACCEL_ADDR , 1 , buf , true);
584
+ jsi2cRead (ACCEL_I2C , ACCEL_ADDR , 1 , buf , true);
572
585
}
573
586
if (hasAccelData ) {
574
587
buf [0 ]= 6 ;
575
- jsi2cWrite (& i2cInternal , ACCEL_ADDR , 1 , buf , true);
576
- jsi2cRead (& i2cInternal , ACCEL_ADDR , 6 , buf , true);
588
+ jsi2cWrite (ACCEL_I2C , ACCEL_ADDR , 1 , buf , true);
589
+ jsi2cRead (ACCEL_I2C , ACCEL_ADDR , 6 , buf , true);
577
590
// work out current reading in 16 bit
578
591
short newx = (buf [1 ]<<8 )|buf [0 ];
579
592
short newy = (buf [3 ]<<8 )|buf [2 ];
@@ -1435,14 +1448,24 @@ void jswrap_banglejs_init() {
1435
1448
1436
1449
// Set up I2C
1437
1450
i2cBusy = true;
1451
+ #ifdef SMAQ3
1452
+ jshI2CInitInfo (& i2cAccel );
1453
+ i2cAccel .bitrate = 0x7FFFFFFF ; // make it as fast as we can go
1454
+ i2cAccel .pinSDA = ACCEL_PIN_SDA ;
1455
+ i2cAccel .pinSCL = ACCEL_PIN_SCL ;
1456
+ jsi2cSetup (& i2cMag );
1457
+ jshI2CInitInfo (& i2cMag );
1458
+ i2cMag .bitrate = 0x7FFFFFFF ; // make it as fast as we can go
1459
+ i2cMag .pinSDA = MAG_PIN_SDA ;
1460
+ i2cMag .pinSCL = MAG_PIN_SCL ;
1461
+ jsi2cSetup (& i2cMag );
1462
+ #else
1438
1463
jshI2CInitInfo (& i2cInternal );
1439
1464
i2cInternal .bitrate = 0x7FFFFFFF ; // make it as fast as we can go
1440
1465
i2cInternal .pinSDA = ACCEL_PIN_SDA ;
1441
1466
i2cInternal .pinSCL = ACCEL_PIN_SCL ;
1442
- jshPinSetValue (i2cInternal .pinSCL , 1 );
1443
- jshPinSetState (i2cInternal .pinSCL , JSHPINSTATE_GPIO_OUT_OPENDRAIN_PULLUP );
1444
- jshPinSetValue (i2cInternal .pinSDA , 1 );
1445
- jshPinSetState (i2cInternal .pinSDA , JSHPINSTATE_GPIO_OUT_OPENDRAIN_PULLUP );
1467
+ jsi2cSetup (& i2cInternal );
1468
+ #endif
1446
1469
#ifndef SMAQ3
1447
1470
// LCD pin init
1448
1471
jshPinOutput (LCD_PIN_CS , 1 );
@@ -2102,7 +2125,7 @@ void jswrap_banglejs_accelWr(JsVarInt reg, JsVarInt data) {
2102
2125
buf [0 ] = (unsigned char )reg ;
2103
2126
buf [1 ] = (unsigned char )data ;
2104
2127
i2cBusy = true;
2105
- jsi2cWrite (& i2cInternal , ACCEL_ADDR , 2 , buf , true);
2128
+ jsi2cWrite (ACCEL_I2C , ACCEL_ADDR , 2 , buf , true);
2106
2129
i2cBusy = false;
2107
2130
#endif
2108
2131
}
@@ -2125,8 +2148,8 @@ int jswrap_banglejs_accelRd(JsVarInt reg) {
2125
2148
unsigned char buf [1 ];
2126
2149
buf [0 ] = (unsigned char )reg ;
2127
2150
i2cBusy = true;
2128
- jsi2cWrite (& i2cInternal , ACCEL_ADDR , 1 , buf , true);
2129
- jsi2cRead (& i2cInternal , ACCEL_ADDR , 1 , buf , true);
2151
+ jsi2cWrite (ACCEL_I2C , ACCEL_ADDR , 1 , buf , true);
2152
+ jsi2cRead (ACCEL_I2C , ACCEL_ADDR , 1 , buf , true);
2130
2153
i2cBusy = false;
2131
2154
return buf [0 ];
2132
2155
#else
@@ -2154,7 +2177,7 @@ void jswrap_banglejs_compassWr(JsVarInt reg, JsVarInt data) {
2154
2177
buf [0 ] = (unsigned char )reg ;
2155
2178
buf [1 ] = (unsigned char )data ;
2156
2179
i2cBusy = true;
2157
- jsi2cWrite (& i2cInternal , MAG_ADDR , 2 , buf , true);
2180
+ jsi2cWrite (MAG_I2C , MAG_ADDR , 2 , buf , true);
2158
2181
i2cBusy = false;
2159
2182
#endif
2160
2183
}
@@ -2173,6 +2196,7 @@ void jswrap_banglejs_compassWr(JsVarInt reg, JsVarInt data) {
2173
2196
Changes a pin state on the IO expander
2174
2197
*/
2175
2198
void jswrap_banglejs_ioWr (JsVarInt mask , bool on ) {
2199
+ #ifndef SMAQ3
2176
2200
#ifndef EMSCRIPTEN
2177
2201
static unsigned char state ;
2178
2202
if (on ) state |= mask ;
@@ -2181,6 +2205,7 @@ void jswrap_banglejs_ioWr(JsVarInt mask, bool on) {
2181
2205
jsi2cWrite (& i2cInternal , 0x20 , 1 , & state , true);
2182
2206
i2cBusy = false;
2183
2207
#endif
2208
+ #endif
2184
2209
}
2185
2210
2186
2211
0 commit comments