Advertisement
computermuseo

ef canon lens focus/iris control arduino nano

Jul 16th, 2022
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.05 KB | None | 0 0
  1. #define DEBUG
  2. #include <SPI.h>
  3. #include <EEPROM.h>
  4.  
  5. #include <Adafruit_GFX.h>
  6. #include <Adafruit_SSD1306.h>
  7. #include <Wire.h>
  8. #define SCREEN_WIDTH 128
  9. #define SCREEN_HEIGHT 64
  10. #define OLED_RESET -1
  11. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
  12.  
  13. #define LED_SW 7
  14. #define SS 10 // SPI SS for ATmega168/328
  15. #define F1 9 // Function SW
  16. #define ENC_A 2
  17. #define ENC_B 3
  18. #define LED_Red 8
  19. #define LED_Green 9
  20.  
  21. volatile byte pos;
  22. volatile int enc_count;
  23. boolean sw = false;
  24. boolean rec_mode = false; // move LENS without pushing SW
  25.  
  26. int mode = 0; // 0:Focus control, 1:Aperture control
  27. int mode_counter[2] , focuserPosition, offset;
  28. int apAddr, apValue, fpAddr, fpValue, stepAddr, stepValue;
  29.  
  30. void InitLens()
  31. {
  32. SPI.transfer(0x0A);
  33. delay(30);
  34. SPI.transfer(0x00);
  35. delay(30);
  36. SPI.transfer(0x0A);
  37. delay(30);
  38. SPI.transfer(0x00);
  39. delay(30);
  40. }
  41.  
  42. int ENC_COUNT(int incoming) {
  43. static int enc_old = enc_count;
  44. int val_change = enc_count - enc_old;
  45.  
  46. if (val_change != 0)
  47. {
  48. incoming += val_change;
  49. enc_old = enc_count;
  50. }
  51. return incoming;
  52. }
  53.  
  54. void ENC_READ() {
  55. byte cur = (!digitalRead(ENC_B) << 1) + !digitalRead(ENC_A);
  56. byte old = pos & B00000011;
  57. byte dir = (pos & B00110000) >> 4;
  58.  
  59. if (cur == 3) cur = 2;
  60. else if (cur == 2) cur = 3;
  61.  
  62. if (cur != old)
  63. {
  64. if (dir == 0)
  65. {
  66. if (cur == 1 || cur == 3) dir = cur;
  67. } else {
  68. if (cur == 0)
  69. {
  70. if (dir == 1 && old == 3) enc_count++;
  71. else if (dir == 3 && old == 1) enc_count--;
  72. dir = 0;
  73. }
  74. }
  75. pos = (dir << 4) + (old << 2) + cur;
  76. }
  77. }
  78.  
  79. void setup() {
  80. digitalWrite(13, LOW); // SPI Clock PIN
  81. pinMode(ENC_A, INPUT_PULLUP);
  82. pinMode(ENC_B, INPUT_PULLUP);
  83. pinMode(LED_Red, OUTPUT);
  84. pinMode(LED_Green, OUTPUT);
  85. pinMode(LED_SW, INPUT_PULLUP);
  86. pinMode(SS, OUTPUT);
  87. pinMode(F1, INPUT_PULLUP);
  88.  
  89. attachInterrupt(0, ENC_READ, CHANGE);
  90. attachInterrupt(1, ENC_READ, CHANGE);
  91.  
  92. display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  93. display.clearDisplay();
  94.  
  95. // Font size
  96. display.setTextSize(3);
  97. // Font color
  98. display.setTextColor(WHITE);
  99. display.setCursor(0, 10);
  100. display.println("EF-LensFocus");
  101. display.display();
  102. delay(1000);
  103. mode = 0; // focus control mode
  104. apAddr = 0; // 1 byte memory for apurture value
  105. fpAddr = 1; // 2 byte memory for focus position
  106. stepAddr = 3; // 1 bye memory for focusing steps
  107. focuserPosition = 5000;
  108. SPI.begin();
  109. SPI.setBitOrder(MSBFIRST);
  110. SPI.setClockDivider(SPI_CLOCK_DIV128);
  111. SPI.setDataMode(SPI_MODE3);
  112. digitalWrite(12, HIGH);
  113. digitalWrite(LED_Red, HIGH);
  114. digitalWrite(LED_Green, LOW);
  115. InitLens();
  116. SPI.transfer(0x05); // Focus Max
  117. delay(1000);
  118. apValue = EEPROM.read(apAddr);
  119. fpValue = EEPROM.read(fpAddr) * 256 + EEPROM.read(fpAddr + 1); // focus position
  120. stepValue = EEPROM.read(stepAddr);
  121.  
  122. offset = fpValue - focuserPosition;
  123. proc_lens(); // Move focus tot last position
  124. focuserPosition = fpValue;
  125. #ifdef DEBUG
  126. Serial.begin(9600);
  127. Serial.print("Focus:");
  128. Serial.println(fpValue);
  129. Serial.print("OffSet:");
  130. Serial.println(offset);
  131. Serial.print("statp:");
  132. Serial.println(stepValue);
  133. Serial.print("Apertura:");
  134. Serial.println(apValue);
  135. #endif
  136. /*
  137. * Initialize starting values when needed.
  138. * Press SW at startup.
  139. */
  140. if (digitalRead(LED_SW) == LOW){
  141. fpValue=5000;
  142. focuserPosition=5000;
  143. offset=0;
  144. stepValue=10;
  145. apValue=0;
  146. }
  147. display.clearDisplay();
  148. display.setCursor(0, 10);
  149. display.print("F:");
  150. display.println(fpValue);
  151. display.print("A:");
  152. display.println(apValue);
  153. display.display();
  154. delay(1000);
  155. }
  156.  
  157. void loop() {
  158. int sw_count;
  159. int enc_diff;
  160. short counter_now;
  161. digitalWrite(SS, HIGH);
  162. sw_count = 0;
  163. /* encorder counts */
  164. counter_now = ENC_COUNT(mode_counter[mode]);
  165. enc_diff = counter_now - mode_counter[mode]; // encoder counter state
  166. /* SW pressed ? */
  167. while (digitalRead(LED_SW) == LOW) {
  168. sw_count++;
  169. if (!rec_mode) {
  170. if (mode == 1) { // forcus control mode RED
  171. SetLED(HIGH, LOW);
  172. } else { // aperture control mode GREEN
  173. SetLED(LOW, HIGH);
  174. }
  175. }
  176. if (sw_count > 50) { // Change Step Size mode
  177. SetLED(HIGH, HIGH);
  178. }
  179. delay(10);
  180. }
  181. delay(100);
  182. if (sw_count > 0 && sw_count < 50) { // Toggle Focus/Aperture
  183. mode == 0 ? mode = 1 : mode = 0;
  184. disp_update(stepValue, focuserPosition);
  185. }
  186. if (mode == 1) {
  187. SetLED(LOW, HIGH);
  188. } else {
  189. SetLED(HIGH, LOW);
  190. }
  191.  
  192. if (sw_count > 50 || (rec_mode && (sw_count != 0)) ) { // Toggle step counts setting mode
  193. rec_mode = ! rec_mode;
  194. if (!rec_mode) mode = 0;
  195. }
  196. if (rec_mode) {
  197. SetLED(HIGH, HIGH);
  198. mode = 0;
  199. if (sw_count != 0 || enc_diff != 0) {
  200. stepValue += enc_diff;
  201. if (stepValue < 1) stepValue = 1; // minimum step size should be 1
  202. EEPROM.write(stepAddr, stepValue);
  203. }
  204. disp_update(stepValue, focuserPosition);
  205. } else {
  206. if (mode_counter[mode] != counter_now) {
  207. enc_diff > 0 ? 1 : -1;
  208. switch (mode) {
  209. case 0:
  210. offset = enc_diff * stepValue; // Focus current position
  211. disp_update( offset , focuserPosition);
  212. break;
  213. case 1:
  214. apValue += enc_diff;
  215. disp_update( apValue , focuserPosition);
  216. break;
  217. }
  218. proc_lens();
  219. }
  220. }
  221. }
  222.  
  223. void SetLED(int red, int green) {
  224. digitalWrite(LED_Red, red);
  225. digitalWrite(LED_Green, green);
  226. }
  227.  
  228. /*
  229. * Status display on OLED
  230. */
  231. void disp_update(int tmp, int focuserPosition) {
  232. char sep;
  233. display.clearDisplay();
  234. display.setCursor(0, 10);
  235. sep = ':';
  236. if (rec_mode) { // Update when encoder rotated
  237. display.print("S");
  238. display.print(sep);
  239. display.println(tmp );
  240. display.print("F");
  241. display.print(sep);
  242. display.println( posizione fuoco);
  243. } else { // Update when switch pushed
  244. sep = ':';
  245. switch (mode) {
  246. case 0:
  247. display.print("S");
  248. display.print(sep);
  249. display.println( offset );
  250. display.print("F");
  251. display.print(sep);
  252. display.println( posizione fuoco);
  253. break;
  254. case 1:
  255. display.print("A");
  256. display.print(sep);
  257. display.println(apValue);
  258. display.print("F");
  259. display.print(sep);
  260. display.println( posizione fuoco );
  261. break;
  262. }
  263. }
  264. display.display();
  265. }
  266. /*
  267. * 1:Send control commands to LENS
  268. * 2:Update EEPROM data
  269. *
  270. * mode 0:focus control mode
  271. * param1 focuserPosition... focus value
  272. * param2 offset ... focus move value
  273. * mode 1:aperture control mode
  274. * param apValue ... aperture value
  275. */
  276. void proc_lens() {
  277. int ap, x, y;
  278. digitalWrite(SS, LOW);
  279. if (mode == 0 ) { // Focus control
  280. //offset = stepValue ;
  281. x = highByte(focuserPosition);
  282. y = lowByte(focuserPosition);
  283. EEPROM.write(fpAddr, x); // write to EEPROM last focus position
  284. EEPROM.write(fpAddr + 1, y);
  285. x = highByte(offset);
  286. y = lowByte(offset);
  287. InitLens();
  288. SPI.transfer(0x44); delay(30);
  289. SPI.transfer(x); delay(30);
  290. SPI.transfer(y); delay(30);
  291. SPI.transfer(0); delay(100);
  292. focuserPosition += offset;
  293. #ifdef DEBUG
  294. Serial.print("FP:");
  295. Serial.print(offset);
  296. Serial.print(", ");
  297. Serial.println(posizione fuoco);
  298. #endif
  299. } else { // aperture control
  300. ap = (apValue - EEPROM.read(apAddr)) * 3;
  301. if (apValue != EEPROM.read(apAddr))
  302. {
  303. InitLens();
  304. SPI.transfer(0x07); delay(10);
  305. SPI.transfer(0x13); delay(10);
  306. SPI.transfer((apValue - EEPROM.read(apAddr)) * 3);
  307. delay(100);
  308. SPI.transfer(0x08); delay(10);
  309. SPI.transfer(0x00); delay(10);
  310. EEPROM.write(apAddr, apValue);
  311. }
  312. }
  313. #ifdef DEBUG
  314. Serial.print("apertura:");
  315. Serial.print(apValue);
  316. Serial.print(",SET ap:");
  317. Serial.println(ap);
  318. #endif
  319. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement