reform-tiny-fw.ino 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * MNT Reform 0.5.0 ATtiny 841 controller firmware
  3. * Copyright 2018 MNT Media and Technology UG, Berlin
  4. * SPDX-License-Identifier: GPL-3.0-or-later
  5. * Use ATTinyCore for building with Arduino IDE: https://github.com/SpenceKonde/ATTinyCore
  6. *
  7. * GOTCHAS:
  8. * - select 1MHz clock
  9. * - "burn bootloader" once
  10. * - select "counterclockwise" pin mapping
  11. * - flash with an "Arduino as ISP"
  12. *
  13. */
  14. #define SCL_PIN 0
  15. #define SCL_PORT PORTA
  16. #define SDA_PIN 1
  17. #define SDA_PORT PORTA
  18. #define HALL_SENSOR_PIN A5
  19. #define HALL_SENSOR_SUPPLY_PIN 4
  20. #define INA_ADDR 0x4e
  21. #define I2C_WRITE 0
  22. #define I2C_READ 1
  23. #include "SoftI2CMaster.h"
  24. #include <SoftwareSerial.h>
  25. SoftwareSerial softSerial(8, 3);
  26. int16_t ina_read16(unsigned char reg) {
  27. uint16_t val = 0;
  28. if (i2c_start((INA_ADDR << 1) | I2C_WRITE)) {
  29. i2c_write(reg);
  30. i2c_rep_start((INA_ADDR << 1) | I2C_READ);
  31. val = ((uint16_t)i2c_read(false)) << 8;
  32. val |= i2c_read(true);
  33. i2c_stop();
  34. }
  35. return val;
  36. }
  37. #define ST_EXPECT_DIGIT_0 0
  38. #define ST_EXPECT_DIGIT_1 1
  39. #define ST_EXPECT_DIGIT_2 2
  40. #define ST_EXPECT_DIGIT_3 3
  41. #define ST_EXPECT_CMD 4
  42. #define ST_SYNTAX_ERROR 5
  43. #define ST_EXPECT_RETURN 6
  44. #define LID_CLOSED 1
  45. #define LID_OPEN 0
  46. float ampSecs = 5*3600.0;
  47. unsigned char hallState = LID_OPEN;
  48. int thresh = 580;
  49. int window = 30;
  50. int hallSense = 0;
  51. unsigned char state = ST_EXPECT_DIGIT_0;
  52. unsigned int inputNumber = 0;
  53. unsigned long lastTime = 0;
  54. float volts = 0;
  55. float current = 0;
  56. char cmd = 'a';
  57. unsigned char echo = 1;
  58. char hallSenseDir = 0;
  59. char hallSenseEvents = 0;
  60. // TODO if there is no battery power, ignore lid sensor (values >900)
  61. void handleLidSensor() {
  62. hallSense = analogRead(HALL_SENSOR_PIN);
  63. if (hallState==LID_OPEN && (hallSenseDir == 0 && hallSense>(thresh+window) || hallSenseDir == 1 && hallSense<(thresh-window))) {
  64. hallState = LID_CLOSED;
  65. }
  66. if (hallState==LID_CLOSED && (hallSenseDir == 0 && hallSense<(thresh-window) || hallSenseDir == 1 && hallSense>(thresh+window))) {
  67. hallState = LID_OPEN;
  68. if (hallSenseEvents) softSerial.println("event:wake");
  69. }
  70. }
  71. void handleCommands() {
  72. char chr = softSerial.read();
  73. if (echo) softSerial.print(chr);
  74. // states:
  75. // 0-3 digits of optional command argument
  76. // 4 command letter expected
  77. // 5 syntax error (unexpected character)
  78. // 6 command letter entered
  79. if (state>=ST_EXPECT_DIGIT_0 && state<=ST_EXPECT_DIGIT_3) {
  80. // read number or command
  81. if (chr >= '0' && chr <= '9') {
  82. inputNumber*=10;
  83. inputNumber+=(chr-'0');
  84. state++;
  85. } else if (chr >= 'a' && chr <= 'z') {
  86. // command entered instead of digit
  87. cmd = chr;
  88. state = ST_EXPECT_RETURN;
  89. } else if (chr == '\n' || chr == ' ') {
  90. // ignore newlines or spaces
  91. } else if (chr == '\r') {
  92. softSerial.println("error:syntax");
  93. state = ST_EXPECT_DIGIT_0;
  94. inputNumber = 0;
  95. } else {
  96. // syntax error
  97. state = ST_SYNTAX_ERROR;
  98. }
  99. }
  100. else if (state == ST_EXPECT_CMD) {
  101. // read command
  102. if (chr >= 'a' && chr <= 'z') {
  103. cmd = chr;
  104. state = ST_EXPECT_RETURN;
  105. } else {
  106. state = ST_SYNTAX_ERROR;
  107. }
  108. }
  109. else if (state == ST_SYNTAX_ERROR) {
  110. // syntax error
  111. if (chr == '\r') {
  112. softSerial.println("error:syntax");
  113. state = ST_EXPECT_DIGIT_0;
  114. inputNumber = 0;
  115. }
  116. }
  117. else if (state == ST_EXPECT_RETURN) {
  118. if (chr == '\n' or chr == ' ') {
  119. // ignore newlines or spaces
  120. }
  121. else if (chr == '\r') {
  122. // execute
  123. if (cmd == 'a') {
  124. // get current (mA)
  125. softSerial.println((int)(current*1000.0));
  126. }
  127. else if (cmd == 'v') {
  128. // get voltage
  129. softSerial.println((int)(volts*1000.0));
  130. }
  131. else if (cmd == 'c') {
  132. // set/get battery capacity (mAh)
  133. if (inputNumber>0) {
  134. ampSecs = ((float)inputNumber)*3.6;
  135. }
  136. softSerial.println((int)(ampSecs/3.6));
  137. }
  138. else if (cmd == 's') {
  139. // print sensor analog reading 0-1023
  140. softSerial.println(hallSense);
  141. }
  142. else if (cmd == 'l') {
  143. // print lid open/close state
  144. softSerial.println(hallState);
  145. }
  146. else if (cmd == 't') {
  147. // set open/closed threshold
  148. if (inputNumber>0) {
  149. thresh = inputNumber;
  150. }
  151. softSerial.println(thresh);
  152. }
  153. else if (cmd == 'w') {
  154. // set open/closed threshold hysteresis window
  155. if (inputNumber>0) {
  156. window = inputNumber;
  157. }
  158. softSerial.println(window);
  159. }
  160. else if (cmd == 'u') {
  161. // uptime of attiny in seconds
  162. softSerial.println(millis()/1000);
  163. }
  164. else if (cmd == 'e') {
  165. // toggle serial echo
  166. echo = inputNumber?1:0;
  167. //softSerial.print("echo:");
  168. //softSerial.println(echo);
  169. }
  170. else if (cmd == 'o') {
  171. // toggle lid sensor magnet orientation
  172. hallSenseDir = inputNumber?1:0;
  173. //softSerial.print("orientation:");
  174. softSerial.println(hallSenseDir);
  175. }
  176. else if (cmd == 'k') {
  177. // toggle lid sensor waKe events
  178. hallSenseEvents = inputNumber?1:0;
  179. //softSerial.print("events:");
  180. //softSerial.println(hallSenseEvents);
  181. }
  182. else {
  183. softSerial.println("error:command");
  184. }
  185. state = ST_EXPECT_DIGIT_0;
  186. inputNumber = 0;
  187. } else {
  188. state = ST_SYNTAX_ERROR;
  189. }
  190. }
  191. }
  192. void handleBattery() {
  193. float raw_volts = (float)ina_read16(0x2);
  194. float raw_current = (float)ina_read16(0x1);
  195. volts = raw_volts * 0.00125;
  196. current = raw_current * 0.001;
  197. if (current>-0.02 && current<0.02) current = 0; // clamp to zero
  198. unsigned long thisTime = millis();
  199. if (lastTime>0 && thisTime>lastTime) {
  200. unsigned long millisPassed = thisTime - lastTime;
  201. if (millisPassed >= 1000) {
  202. lastTime = thisTime;
  203. // decrease estimated battery capacity
  204. ampSecs -= current*(millisPassed/1000);
  205. }
  206. } else {
  207. // timer uninitialized or timer wrap
  208. lastTime = thisTime;
  209. }
  210. }
  211. void loop() {
  212. handleBattery();
  213. handleLidSensor();
  214. if (softSerial.available() > 0) {
  215. handleCommands();
  216. }
  217. }
  218. void setup() {
  219. softSerial.begin(2400);
  220. softSerial.println("reform:attiny:0.5.0:boot");
  221. analogReference(DEFAULT);
  222. // if you don't want to use an analog sensor, you can
  223. // disable the ADC to save a bit of power
  224. //
  225. //ADCSRA&=(~(1<<ADEN));
  226. if (!i2c_init()) {
  227. softSerial.println("error:i2c");
  228. }
  229. // physical pin 8 (J34 pin 3) for hall effect sensor
  230. pinMode(HALL_SENSOR_PIN, INPUT);
  231. // physical pin 7 (J34 pin 5) for hall effect sensor supply voltage
  232. pinMode(HALL_SENSOR_SUPPLY_PIN, OUTPUT);
  233. digitalWrite(HALL_SENSOR_SUPPLY_PIN, HIGH);
  234. // PWRON output (TODO: ULVO)
  235. pinMode(7, OUTPUT);
  236. digitalWrite(7, HIGH);
  237. }